Skip to main content

coven_database/
download.rs

1//! Authenticated, decoded writes at the sync/database boundary.
2use crate::sqlite::StoreSql;
3use crate::store_call::StoreCall;
4use crate::DbError;
5use coven_format::write::WriteHeader;
6#[cfg(test)]
7use coven_format::write::{WritePart, WriteRecord};
8use coven_merge::Audience;
9
10/// A write whose signature and opened parts sync has already authenticated.
11#[derive(Clone, Debug)]
12#[cfg(test)]
13pub(crate) struct DownloadedWrite {
14    /// The authenticated write header.
15    pub header: WriteHeader,
16    /// Every part, including ones the receiving device could not decrypt.
17    pub parts: Vec<DownloadedPart>,
18}
19
20/// Readable and unreadable parts belong to the same atomic slot.
21#[derive(Clone, Debug)]
22#[cfg(test)]
23pub(crate) enum DownloadedPart {
24    /// Authenticated plaintext changes for an audience.
25    Opened(WritePart),
26    /// The device does not have this audience's key.
27    Skipped(Audience),
28}
29
30#[cfg(test)]
31impl From<WriteRecord> for DownloadedWrite {
32    fn from(record: WriteRecord) -> Self {
33        Self {
34            header: record.header,
35            parts: record.parts.into_iter().map(DownloadedPart::Opened).collect(),
36        }
37    }
38}
39
40/// Applying a previously applied write never runs it again.
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub enum ApplyOutcome {
43    /// All opened parts and all merge metadata committed together.
44    Applied,
45    /// This slot had already been consumed, including skipped or lost parts.
46    AlreadyApplied,
47    /// Nothing applied; sync retains the write until this prerequisite changes.
48    Waiting(WriteWait),
49}
50
51/// A prerequisite for applying a downloaded write.
52#[derive(Clone, Debug, PartialEq, Eq)]
53pub enum WriteWait {
54    /// These original row changes are unavailable even if their slots were consumed.
55    Sources(Vec<coven_format::write_sources::ChangeId>),
56    /// The app must support this schema version.
57    SchemaVersion(u32),
58    /// The selected boundary has not committed its replacement data.
59    Reload(Audience),
60}
61
62pub(crate) fn source_wait(result: Result<ApplyOutcome, DbError>) -> Result<ApplyOutcome, DbError> {
63    match result {
64        Err(DbError::WriteWaiting(wait)) => Ok(ApplyOutcome::Waiting(wait)),
65        Err(DbError::MissingWriteSources(sources)) => {
66            Ok(ApplyOutcome::Waiting(WriteWait::Sources(sources)))
67        }
68        other => other,
69    }
70}
71
72/// What the write `header` waits for before it applies. Its prefix needs no check: its slot holds its
73/// `read_through` to the slot's `after`, which the caller has applied.
74pub(crate) fn prerequisite(
75    database: &StoreSql,
76    call: &StoreCall<'_>,
77    header: &WriteHeader,
78) -> Result<Option<WriteWait>, DbError> {
79    if header.schema_version > crate::transaction_constants::schema_version(database, call)? {
80        return Ok(Some(WriteWait::SchemaVersion(header.schema_version)));
81    }
82    Ok(None)
83}
84
85#[cfg(test)]
86#[path = "download_tests.rs"]
87mod tests;