coven_database/
download.rs1use 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#[derive(Clone, Debug)]
12#[cfg(test)]
13pub(crate) struct DownloadedWrite {
14 pub header: WriteHeader,
16 pub parts: Vec<DownloadedPart>,
18}
19
20#[derive(Clone, Debug)]
22#[cfg(test)]
23pub(crate) enum DownloadedPart {
24 Opened(WritePart),
26 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#[derive(Clone, Debug, PartialEq, Eq)]
42pub enum ApplyOutcome {
43 Applied,
45 AlreadyApplied,
47 Waiting(WriteWait),
49}
50
51#[derive(Clone, Debug, PartialEq, Eq)]
53pub enum WriteWait {
54 Sources(Vec<coven_format::write_sources::ChangeId>),
56 SchemaVersion(u32),
58 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
72pub(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;