Skip to main content

coven_database/
prerequisite_rows.rs

1//! The rows whose commits can change what a waiting operation needs (§6.7,
2//! §21.2; IO `WaitingEvidence`). A commit changing one of a wait's rows
3//! is evidence for that wait; no other commit is. The rows are exactly what
4//! the waiting work reads, including advancement of the shared prefix.
5
6use crate::WriteId;
7use coven_foundation::id_source::DeviceId;
8use coven_merge::Audience;
9use sqlive::{RowScope, SqlValue, TableRead};
10
11/// Committed state a waiting operation names.
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub enum PrerequisiteRows {
14    /// The applied store prefix.
15    Slot,
16    /// This installation's active claim and settlement.
17    Claim,
18    /// An original write's queue and counted prefix.
19    Write(WriteId),
20    /// An audience's pending reload.
21    Reload(Audience),
22    /// A provider account's access record.
23    ProviderAccess(String),
24    /// Another operation's journal row: it advances or finishes.
25    Operation(crate::OperationId),
26}
27
28fn blob(value: impl Into<Vec<u8>>) -> SqlValue {
29    SqlValue::Blob(value.into())
30}
31
32fn text(value: &str) -> SqlValue {
33    SqlValue::Text(value.as_bytes().to_vec())
34}
35
36fn device(device: DeviceId) -> SqlValue {
37    blob(device.0.to_be_bytes())
38}
39
40fn number(number: u64) -> SqlValue {
41    blob(number.to_be_bytes())
42}
43
44/// Rows of `table` whose primary key holds `key`, through every listed
45/// column: its non-key columns, so an update to any of them counts.
46fn rows(table: &str, key: &[(&str, SqlValue)], columns: &[&str]) -> TableRead {
47    let scope = key
48        .iter()
49        .map(|(column, value)| RowScope::equal((*column).into(), vec![value.clone()]))
50        .fold(RowScope::All, RowScope::and);
51    TableRead {
52        table: table.into(),
53        columns: columns.iter().map(|column| (*column).into()).collect(),
54        scope,
55        inserted_only: false,
56    }
57}
58
59const UPLOADS: &[&str] = &["record"];
60const UPLOAD_KEYS: &[&str] = &["sealing_keys"];
61const UPLOAD_CLAIMS: &[&str] = &["claim"];
62const DATA_RELOADS: &[&str] = &["subject", "marker"];
63const PROVIDER_ACCESS: &[&str] = &["intended", "granted", "invitation", "operation", "target"];
64const OPERATIONS: &[&str] = &["kind", "last_step", "data", "started_by"];
65
66impl PrerequisiteRows {
67    pub(crate) fn reads(&self) -> Vec<TableRead> {
68        match self {
69            Self::Slot => vec![rows("_coven_sequence", &[], &["applied"])],
70            Self::Claim => vec![rows(
71                "_coven_claim_journal",
72                &[],
73                &["through", "operation", "settlement", "singleton"],
74            )],
75            Self::Write(write) => {
76                let at = [("device", device(write.device)), ("number", number(write.number))];
77                vec![
78                    rows("_coven_sequence", &[], &["applied"]),
79                    rows("_coven_uploads", &at, UPLOADS),
80                    rows("_coven_upload_keys", &at, UPLOAD_KEYS),
81                    rows("_coven_upload_claims", &at, UPLOAD_CLAIMS),
82                ]
83            }
84            Self::Reload(audience) => vec![rows(
85                "_coven_data_reloads",
86                &[("audience", text(&audience.to_string()))],
87                DATA_RELOADS,
88            )],
89            Self::ProviderAccess(account) => {
90                vec![rows("_coven_provider_access", &[("account", text(account))], PROVIDER_ACCESS)]
91            }
92            Self::Operation(id) => {
93                vec![rows("_coven_operations", &[("id", SqlValue::Integer(id.0))], OPERATIONS)]
94            }
95        }
96    }
97}
98
99#[cfg(test)]
100#[path = "prerequisite_rows_tests.rs"]
101mod tests;