Skip to main content

coven_database/
staged_damage.rs

1//! Telling damage to a snapshot's staged records on this device from the
2//! snapshot's own (§19.1): the digest of what a staging holds, and, for
3//! tests, damage to it.
4
5#[cfg(any(test, feature = "test-utils"))]
6use super::table;
7use super::{Phase, SnapshotStaging, StagedTables};
8use crate::snapshot_error::SnapshotError;
9use crate::sqlite::StoreSql;
10use crate::DbError;
11
12/// A digest of one staging's records as its tables hold them. Staging is a
13/// function of the snapshot's bytes, so two stagings of the same snapshot
14/// hold the same records, and differ only where this device's disk changed
15/// one (§19.1).
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct StagedDigest([u8; 32]);
18
19/// Read back every record `tables` hold and digest them, each table's rows
20/// in the order of their encodings, so the digest is of their values alone.
21pub(super) fn digest(db: &StoreSql, tables: &StagedTables) -> Result<StagedDigest, DbError> {
22    let mut hasher = coven_crypto::ContentHasher::new();
23    let mut table = |name: &str, select: &str, params: &[&dyn sqlive::rusqlite::ToSql]| {
24        let mut rows = Vec::new();
25        db.for_each(select, params, |row| {
26            rows.push(encoded(row)?);
27            Ok::<_, DbError>(())
28        })?;
29        rows.sort_unstable();
30        hasher.update(&(name.len() as u64).to_be_bytes());
31        hasher.update(name.as_bytes());
32        hasher.update(&(rows.len() as u64).to_be_bytes());
33        for row in rows {
34            hasher.update(&row);
35        }
36        Ok::<_, DbError>(())
37    };
38    // A table is named for its staging; the digest names it without.
39    let staging = format!("_coven_staged_{}_", tables.id);
40    for name in &tables.tables {
41        table(&name.replacen(&staging, "", 1), &format!("SELECT * FROM {name}"), &[])?;
42    }
43    table(
44        "_coven_snapshot_writes",
45        "SELECT device,number,timestamp,read_through,slot FROM temp._coven_snapshot_writes WHERE source=?1",
46        &[&tables.id],
47    )?;
48    Ok(StagedDigest(*hasher.finish().as_bytes()))
49}
50
51/// Fail with `DamagedDatabase` unless the records the disk holds are the
52/// ones `staging` ended with (§16.2). A staging whose stream hasn't ended
53/// has no digest yet, and the load refuses it.
54pub(super) fn unchanged(db: &StoreSql, staging: &SnapshotStaging) -> Result<(), DbError> {
55    let (Phase::Staged { digest, .. }, Some(tables)) = (&staging.phase, &staging.tables) else {
56        return Ok(());
57    };
58    if self::digest(db, tables)? != *digest {
59        tracing::warn!("a snapshot's staged records changed on this device since staging");
60        return Err(DbError::DamagedDatabase);
61    }
62    Ok(())
63}
64
65/// One row's values, each tagged with its type and text or blob lengths
66/// written out, so no two rows share an encoding.
67fn encoded(row: &sqlive::rusqlite::Row<'_>) -> Result<Vec<u8>, DbError> {
68    use sqlive::rusqlite::types::ValueRef;
69    let mut bytes = Vec::new();
70    for i in 0..row.as_ref().column_count() {
71        match row.get_ref(i)? {
72            ValueRef::Null => bytes.push(0),
73            ValueRef::Integer(value) => {
74                bytes.push(1);
75                bytes.extend_from_slice(&value.to_be_bytes());
76            }
77            ValueRef::Real(value) => {
78                bytes.push(2);
79                bytes.extend_from_slice(&value.to_bits().to_be_bytes());
80            }
81            ValueRef::Text(value) | ValueRef::Blob(value) => {
82                bytes.push(3);
83                bytes.extend_from_slice(&(value.len() as u64).to_be_bytes());
84                bytes.extend_from_slice(value);
85            }
86        }
87    }
88    Ok(bytes)
89}
90
91/// A failure of a staged snapshot's body carries the digest of its staging
92/// as the failed read left it, for its caller to tell damage on this device
93/// from the snapshot's own (§19.1). When the staging can't be read back,
94/// that failure is the one reported: the input database failed.
95pub(super) fn with_staged_digest(
96    db: &StoreSql,
97    stagings: &[SnapshotStaging],
98    error: DbError,
99) -> DbError {
100    let DbError::Snapshot(SnapshotError::Body { snapshot, error, staged: None }) = error else {
101        return error;
102    };
103    let tables = stagings
104        .iter()
105        .find(|staging| staging.expected == snapshot)
106        .and_then(|staging| staging.tables.as_ref());
107    match tables.map(|tables| digest(db, tables)) {
108        Some(Ok(staged)) => SnapshotError::Body { snapshot, error, staged: Some(staged) }.into(),
109        Some(Err(failure)) => failure,
110        None => SnapshotError::Body { snapshot, error, staged: None }.into(),
111    }
112}
113
114/// Damage a test makes to a staging, as damage on this device's disk would
115/// (§19.1).
116#[cfg(any(test, feature = "test-utils"))]
117#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub enum StagedDamage {
119    /// Its first staged record is cut to one byte, which a load or file
120    /// check refuses.
121    Record,
122    /// Its staged writes claim to have read through their own slots, which
123    /// finishing the staging refuses.
124    Writes,
125    /// Its first staged row is gone: the records that remain are valid, and
126    /// a load or file check finds nothing wrong with them.
127    Row,
128}
129
130/// Make `damage` to the staging `staging`.
131#[cfg(any(test, feature = "test-utils"))]
132pub(super) fn damage(db: &StoreSql, staging: i64, damage: StagedDamage) -> Result<(), DbError> {
133    let changed = match damage {
134        StagedDamage::Record => {
135            let records = table(staging, "records");
136            db.execute(
137                &format!(
138                    "UPDATE {records} SET record=substr(record,1,1) WHERE rowid=(SELECT min(rowid) FROM {records})"
139                ),
140                [],
141            )?
142        }
143        StagedDamage::Writes => db.execute(
144            "UPDATE temp._coven_snapshot_writes SET read_through=slot WHERE source=?1",
145            [staging],
146        )?,
147        StagedDamage::Row => {
148            let others: Vec<String> =
149                ["generations", "columns", "parts", "authored", "creations", "records"]
150                    .iter()
151                    .map(|name| table(staging, name).trim_start_matches("temp.").to_owned())
152                    .collect();
153            let rows = db.query(
154                "SELECT name FROM temp.sqlite_schema WHERE type='table' AND name GLOB ?1
155                 ORDER BY rootpage",
156                [format!("_coven_staged_{staging}_*")],
157                |row| row.get::<_, String>(0),
158            )?;
159            let mut changed = 0;
160            for name in rows.into_iter().filter(|name| !others.contains(name)) {
161                let name = format!("temp.{}", sqlive::sql::identifier(&name));
162                changed = db.execute(
163                    &format!("DELETE FROM {name} WHERE rowid=(SELECT min(rowid) FROM {name})"),
164                    [],
165                )?;
166                if changed > 0 {
167                    break;
168                }
169            }
170            changed
171        }
172    };
173    match changed {
174        0 => {
175            Err(DbError::Invariant { what: "a test damages a staging that holds what it changes" })
176        }
177        _ => Ok(()),
178    }
179}
180
181/// The disk error a test's request fails a staging removal with.
182#[cfg(any(test, feature = "test-utils"))]
183pub(super) fn removal_failure() -> DbError {
184    DbError::Sqlite(sqlive::rusqlite::Error::SqliteFailure(
185        sqlive::rusqlite::ffi::Error::new(sqlive::rusqlite::ffi::SQLITE_IOERR),
186        Some("injected staging removal failure".into()),
187    ))
188}
189
190impl crate::StoreDatabase {
191    /// Fail the next removal of the stagings a failed load or file check
192    /// ends, as a disk error would; the stagings keep their records until
193    /// they are dropped (§16.2).
194    #[cfg(any(test, feature = "test-utils"))]
195    pub async fn fail_next_staging_removal(&self) -> Result<(), DbError> {
196        self.call(|inner| {
197            inner.inputs.fail_removal.store(true, std::sync::atomic::Ordering::SeqCst);
198            Ok::<_, DbError>(())
199        })
200        .await
201    }
202}
203
204impl SnapshotStaging {
205    /// The digest of the records it holds now (§19.1).
206    pub async fn digest(&self) -> Result<StagedDigest, DbError> {
207        self.id()?;
208        let tables = self.tables.as_ref().map(|tables| (tables.id, tables.tables.clone()));
209        let (id, tables) = tables.expect("a staging with an id holds its tables");
210        self.database
211            .call(move |inner| inner.inputs.write(|db, _| digest(db, &StagedTables { id, tables })))
212            .await
213    }
214}