//! BEFORE A MAC BECOMES A RUNNER, THE SERVING MAC SAYS IT HOLDS EVERYTHING. //! //! A runner holds no rows and no files of any database (lane macrunner, 14 Oct //! 2026). Dropping a Mac's copy is safe only when nothing in it lives nowhere //! else: every receipt the Mac authored has reached the serving Mac, nothing is //! queued, held or parked, and no original picture is held by this Mac alone. //! The Mac states what its own file holds ([`claim_of`]); the serving Mac holds //! that claim to ITS file ([`short_of`]). An empty answer is the confirmation; //! anything else is what is still owed, by name, and the copy stays. //! //! Counted, never guessed: a claim that cannot be read is a refusal upstream, //! and the comparison only ever errs towards keeping the copy. use rusqlite::types::Value as SqlValue; use serde::{Deserialize, Serialize}; use crate::driver::Driver; use crate::error::Result; /// What one database copy on the Mac holds of its own making. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct Claim { pub database: String, /// Receipts this device authored that are not held back. pub authored: i64, /// The newest of them, by its HLC. pub newest: Option, /// Changes queued, held for judge time, or parked on the way up. pub unsent: i64, } /// What the serving Mac does not hold yet, one reason each. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase", tag = "kind")] pub enum Short { /// The Mac still has changes it has not sent. Unsent { count: i64 }, /// The serving Mac holds fewer of the Mac's receipts than the Mac does. Receipts { mac: i64, serving: i64 }, /// The serving Mac lacks the Mac's newest receipt. Newest { hlc: String }, /// Originals no other keeping Mac holds. OnlyCopy { objects: i64 }, } fn count(db: &Driver, sql: &str, args: &[SqlValue]) -> Result { Ok(db.get(sql, args)?.and_then(|row| row.int("n")).unwrap_or(0)) } /// What `device`'s own copy of `database` holds of its own making. pub fn claim_of(copy: &Driver, database: &str, device: &str) -> Result { let me = [SqlValue::Text(device.into())]; let authored = count(copy, "SELECT COUNT(*) AS n FROM _writes WHERE author = ? AND held = 0", &me)?; let newest = copy .get("SELECT MAX(hlc) AS hlc FROM _writes WHERE author = ? AND held = 0", &me)? .and_then(|row| row.text("hlc").map(str::to_string)); let unsent = crate::outbox::pending_count(copy)? + crate::mint::clock::held_count(copy)? + count(copy, "SELECT COUNT(*) AS n FROM _parked WHERE direction = 'push'", &[])?; Ok(Claim { database: database.into(), authored, newest, unsent }) } /// Hold `claim` to the serving Mac's own file: empty when it holds everything /// `device`'s copy of that database holds that lives nowhere else. pub fn short_of(served: &Driver, claim: &Claim, device: &str) -> Result> { let mut short = Vec::new(); if claim.unsent > 0 { short.push(Short::Unsent { count: claim.unsent }); } let me = [SqlValue::Text(device.into())]; let serving = count(served, "SELECT COUNT(*) AS n FROM _writes WHERE author = ? AND held = 0", &me)?; if serving < claim.authored { short.push(Short::Receipts { mac: claim.authored, serving }); } if let Some(newest) = &claim.newest { let held = served .get( "SELECT 1 AS n FROM _writes WHERE author = ? AND hlc = ? AND held = 0", &[SqlValue::Text(device.into()), SqlValue::Text(newest.clone())], )? .is_some(); if !held { short.push(Short::Newest { hlc: newest.clone() }); } } let objects = crate::media_holdings::only_holder_of(served, &claim.database, device)?; if objects > 0 { short.push(Short::OnlyCopy { objects }); } Ok(short) } #[cfg(test)] #[path = "runner_change_tests.rs"] mod tests;