Skip to main content

tuwunel_service/migrations/
rebuild_roomid_tscount_pducount.rs

1use tuwunel_core::{Result, info, utils::TryReadyExt, warn};
2use tuwunel_database::Map;
3
4use super::tally::{Outcome, Tally};
5use crate::{
6	Services,
7	integrity::roomid_tscount_pducount::{Expected, expected},
8};
9
10/// Writes the timestamp index row of every timeline event.
11///
12/// The rows are the ones the timestamp index check expects, written over the
13/// index without clearing it. An event whose key is not a PDU id is skipped.
14pub(super) async fn rebuild_roomid_tscount_pducount(services: &Services) -> Result {
15	let db = &services.db;
16	let cork = db.cork_and_sync();
17	let index = &db["roomid_tscount_pducount"];
18
19	warn!("Rebuilding roomid_tscount_pducount index for same-timestamp event ordering");
20
21	let tally = expected(services)
22		.ready_try_fold(Tally::default(), |tally, expected| {
23			Ok(tally.record(write(index, &expected)))
24		})
25		.await?;
26
27	drop(cork);
28	info!(
29		count = %tally.repaired,
30		skipped = %tally.skipped,
31		"Rebuilt roomid_tscount_pducount index"
32	);
33
34	index.sort()
35}
36
37fn write(index: &Map, expected: &Expected) -> Outcome {
38	match expected {
39		| Expected::Unparsable(_) => Outcome::Skipped,
40		| Expected::Row(row) => {
41			index.put_raw(row.key(), row.pdu_id.count());
42
43			Outcome::Repaired
44		},
45	}
46}