Skip to main content

coven_database/
object_removals.rs

1//! Objects this device put in storage for a while, then deletes: an ended
2//! invite's marker, which turns away a request sent after the invite ended
3//! (ยง13.2). Nothing references such an object; its row is only the deletion
4//! still owed.
5
6use crate::sqlite::StoreSql;
7use crate::DbError;
8
9/// An object to delete from storage once `after` has passed.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct ObjectRemoval {
12    /// The object's path in storage.
13    pub path: String,
14    /// Seconds since the Unix epoch after which it goes.
15    pub after: u64,
16}
17
18pub(crate) fn record(db: &StoreSql, removal: &ObjectRemoval) -> Result<(), DbError> {
19    let after = i64::try_from(removal.after)
20        .map_err(|_overflow| DbError::Invariant { what: "an object's removal time fits i64" })?;
21    db.execute(
22        "INSERT INTO _coven_object_removals(path,after) VALUES(?1,?2)
23         ON CONFLICT(path) DO UPDATE SET after=max(after,excluded.after)",
24        (&removal.path, after),
25    )?;
26    Ok(())
27}
28
29/// The object at `path` is gone from storage: no deletion is owed.
30pub(crate) fn removed(db: &StoreSql, path: &str) -> Result<(), DbError> {
31    db.execute("DELETE FROM _coven_object_removals WHERE path=?1", [path])?;
32    Ok(())
33}
34
35#[cfg(test)]
36#[path = "object_removals_tests.rs"]
37pub(crate) mod tests;