Skip to content

Commit

Permalink
future (and present) proofing db selects
Browse files Browse the repository at this point in the history
  • Loading branch information
officiallyaninja committed Jul 4, 2023
1 parent f516f6e commit be2c69a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
4 changes: 2 additions & 2 deletions allocator/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sqlite as sq;
use std::collections::hash_map::HashMap;

pub fn read_halls_table(conn: &Connection) -> Vec<Hall> {
let query = "SELECT * FROM halls ORDER BY capacity DESC";
let query = "SELECT name, capacity FROM halls ORDER BY capacity DESC";
let mut halls: Vec<Hall> = vec![];
conn.iterate(query, |pair| {
//pair is an array slice of the columns and the values in the colums
Expand All @@ -30,7 +30,7 @@ pub fn read_halls_table(conn: &Connection) -> Vec<Hall> {
halls
}
pub fn read_students_table(conn: &Connection) -> HashMap<String, Vec<Student>> {
let query = "SELECT * FROM students";
let query = "SELECT id, subject FROM students";
let mut students: HashMap<String, Vec<Student>> = HashMap::new();
conn.iterate(query, |pair| {
//pair is an array slice of the columns and the values in the colums
Expand Down
5 changes: 0 additions & 5 deletions allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ fn main() {
)
.into_iter()
.collect();
// sorting is in place (and only defined on vectors, not iterators)
// so i need to collect it before I can sort
grouped_halls.sort_by_key(|(group, _)| *group);

let mut grouped_halls: Vec<Vec<Hall>> = grouped_halls
.into_iter()
.rev()
.map(|(_, vec)| vec)
.collect();
//shuffling is only defined on vectors not iterators, so i need to collect before shuffling
for group in &mut grouped_halls {
group.shuffle(&mut rng)
}
Expand Down Expand Up @@ -82,7 +78,6 @@ fn main() {
let mut extra_seats = {
let total_seats: usize = halls.iter().map(|h| h.seats_left()).sum();
let total_students: usize = students.values().map(|s| s.len()).sum();

if total_students > total_seats {
panic!("ERROR: more students than seats")
}
Expand Down

0 comments on commit be2c69a

Please sign in to comment.