Skip to main content

tuwunel_service/integrity/
userroomid_highlightcount.rs

1//! The keys of `userroomid_highlightcount`.
2//!
3//! Conduit opened `roomuserid_lastnotificationread` on this column's tree, so a
4//! database it wrote holds both stores here. A highlight count's key leads with
5//! the user and a last-read marker's with the room.
6
7use std::fmt;
8
9use futures::Stream;
10use tuwunel_core::{Result, utils::TryReadyExt};
11use tuwunel_database::{KeyBuf, keyval::Buffer};
12
13use super::{Check, Report, ScanExt, tally};
14use crate::Services;
15
16type Value = Buffer<8>;
17
18const ROOM_SIGIL: &[u8] = b"!";
19
20pub(super) static CHECK: Check = Check {
21	name: "highlight-keys",
22	about: "No userroomid_highlightcount key leads with a room id",
23	quiescent: false,
24	walk: |services, limit| tally(scan(services), limit),
25};
26
27/// A last-notification-read marker Conduit stored in the highlight-count column.
28///
29/// Its key leads with the room, as the keys of `roomuserid_lastnotificationread`
30/// do, so no reader of either column finds it until the row moves there.
31pub struct Finding {
32	/// The stored key.
33	pub key: KeyBuf,
34
35	/// The stored marker.
36	pub value: Value,
37}
38
39/// Streams each `userroomid_highlightcount` row whose key leads with a room id.
40///
41/// Keys sort as bytes, so such rows sit together near the front of the column:
42/// any key sorting below them is skipped, as this check does not cover it, and
43/// the walk ends at the first key past them, so a native column costs one row.
44/// The defect is visible in the key alone, so a repair of one row leaves the rest.
45#[tracing::instrument(level = "trace", skip_all)]
46pub fn scan(services: &Services) -> impl Stream<Item = Result<Finding>> + Send + '_ {
47	services.db["userroomid_highlightcount"]
48		.raw_scan()
49		.scanned(&services.server)
50		.ready_try_skip_while(|(key, _)| Ok(*key < ROOM_SIGIL))
51		.ready_try_take_while(|(key, _)| Ok(key.starts_with(ROOM_SIGIL)))
52		.ready_and_then(|(key, value)| {
53			Ok(Finding {
54				key: KeyBuf::from_slice(key),
55				value: Value::from_slice(value),
56			})
57		})
58}
59
60impl Report for Finding {
61	fn kind(&self) -> &'static str { "room-keyed-row" }
62}
63
64impl fmt::Display for Finding {
65	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66		write!(f, "last-read marker in the highlight-count column: {}", self.key.escape_ascii())
67	}
68}