From cdb734f58c4435ff389a50e1ad345c6f54954573 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 12 Aug 2019 00:06:30 +0100 Subject: [PATCH] Fix infinite loop when looking for missing state groups The code which handles state groups which appear in state_group_edges but not state_groups_state would get confused if such state_groups did not themselves have a prev_state_group. --- src/database.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/database.rs b/src/database.rs index b595087..f2ced91 100644 --- a/src/database.rs +++ b/src/database.rs @@ -43,7 +43,7 @@ pub fn get_data_from_db( // Since the returned groups may themselves reference groups we don't have, // we need to do this recursively until we don't find any more missing. loop { - let missing_sgs: Vec<_> = state_group_map + let mut missing_sgs: Vec<_> = state_group_map .iter() .filter_map(|(_sg, entry)| { if let Some(prev_sg) = entry.prev_state_group { @@ -59,9 +59,13 @@ pub fn get_data_from_db( .collect(); if missing_sgs.is_empty() { + println!("No missing state groups"); break; } + missing_sgs.sort_unstable(); + missing_sgs.dedup(); + println!("Missing {} state groups", missing_sgs.len()); let map = get_missing_from_db(&conn, &missing_sgs); @@ -154,13 +158,16 @@ fn get_missing_from_db(conn: &Connection, missing_sgs: &[i64]) -> BTreeMap = BTreeMap::new(); + // initialise the map with empty entries (the missing group may not + // have a prev_state_group either) + let mut state_group_map: BTreeMap = + missing_sgs.iter() + .map(|sg| (*sg, StateGroupEntry::default())) + .collect(); while let Some(row) = rows.next().unwrap() { let state_group = row.get(0); - - let entry = state_group_map.entry(state_group).or_default(); - + let entry = state_group_map.get_mut(&state_group).unwrap(); entry.prev_state_group = row.get(1); }