Skip to content

Commit 2c364b1

Browse files
authored
Merge pull request #11 from matrix-org/rav/missing_state_groups
Fix infinite loop when looking for missing state groups
2 parents c1e4229 + cdb734f commit 2c364b1

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/database.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn get_data_from_db(
4343
// Since the returned groups may themselves reference groups we don't have,
4444
// we need to do this recursively until we don't find any more missing.
4545
loop {
46-
let missing_sgs: Vec<_> = state_group_map
46+
let mut missing_sgs: Vec<_> = state_group_map
4747
.iter()
4848
.filter_map(|(_sg, entry)| {
4949
if let Some(prev_sg) = entry.prev_state_group {
@@ -59,9 +59,13 @@ pub fn get_data_from_db(
5959
.collect();
6060

6161
if missing_sgs.is_empty() {
62+
println!("No missing state groups");
6263
break;
6364
}
6465

66+
missing_sgs.sort_unstable();
67+
missing_sgs.dedup();
68+
6569
println!("Missing {} state groups", missing_sgs.len());
6670

6771
let map = get_missing_from_db(&conn, &missing_sgs);
@@ -154,13 +158,16 @@ fn get_missing_from_db(conn: &Connection, missing_sgs: &[i64]) -> BTreeMap<i64,
154158

155159
let mut rows = stmt.lazy_query(&trans, &[&missing_sgs], 100).unwrap();
156160

157-
let mut state_group_map: BTreeMap<i64, StateGroupEntry> = BTreeMap::new();
161+
// initialise the map with empty entries (the missing group may not
162+
// have a prev_state_group either)
163+
let mut state_group_map: BTreeMap<i64, StateGroupEntry> =
164+
missing_sgs.iter()
165+
.map(|sg| (*sg, StateGroupEntry::default()))
166+
.collect();
158167

159168
while let Some(row) = rows.next().unwrap() {
160169
let state_group = row.get(0);
161-
162-
let entry = state_group_map.entry(state_group).or_default();
163-
170+
let entry = state_group_map.get_mut(&state_group).unwrap();
164171
entry.prev_state_group = row.get(1);
165172
}
166173

0 commit comments

Comments
 (0)