Skip to main content

coven_database/
database_changes.rs

1//! The store's change watches: what wakes a worker waiting on the rows it
2//! names (§4, §6.7).
3
4use super::StoreDatabase;
5use crate::DbError;
6use std::collections::BTreeSet;
7
8impl StoreDatabase {
9    /// Observe new local writes: rows inserted into the outgoing write queue.
10    /// Upload confirmation removing a row is this device's own bookkeeping
11    /// and wakes nothing (§6.7). Register before the first sync to
12    /// include writes committed while a pass is running.
13    pub fn sync_changes(&self) -> crate::DatabaseChanges<DbError> {
14        self.table_changes(&["_coven_uploads"], true, true)
15    }
16
17    /// Observe operations as they start: rows inserted into the journal,
18    /// such as an invite, whose expiry the handle wakes to write (§13.2).
19    pub fn started_operations(&self) -> crate::DatabaseChanges<DbError> {
20        self.table_changes(&["_coven_operations"], true, true)
21    }
22
23    /// `first` reports the initial state as a change; `inserted_only`
24    /// ignores updates and deletions.
25    fn table_changes(
26        &self,
27        tables: &[&str],
28        first: bool,
29        inserted_only: bool,
30    ) -> crate::DatabaseChanges<DbError> {
31        let reads: Vec<_> = tables
32            .iter()
33            .map(|table| sqlive::TableRead {
34                table: (*table).into(),
35                columns: BTreeSet::new(),
36                scope: sqlive::RowScope::All,
37                inserted_only,
38            })
39            .collect();
40        self.observer.changes(reads, first, DbError::from)
41    }
42
43    /// Observe, from now, every commit made outside `attempt`: an operation
44    /// attempt's own commits are no evidence against the wait it ends in,
45    /// and the attempt learns which rows matter only when it ends (§6.7).
46    pub fn attempt_commits(&self, attempt: crate::CommitScope) -> crate::AttemptCommits {
47        self.observer.attempt(attempt)
48    }
49
50    /// Observe `rows` from here on, after `attempt`. The result first reports
51    /// whether a commit since the attempt began changed one of them: such a
52    /// commit is evidence against the wait the attempt ends in (IO
53    /// `racing_commit_wakes`).
54    pub fn watch_attempt(
55        &self,
56        attempt: crate::AttemptCommits,
57        rows: &[crate::PrerequisiteRows],
58    ) -> crate::DatabaseChanges<DbError> {
59        let reads = rows.iter().flat_map(crate::PrerequisiteRows::reads).collect();
60        self.observer.watch_attempt(attempt, reads, DbError::from)
61    }
62}
63
64#[cfg(test)]
65#[path = "database_changes_tests.rs"]
66mod tests;