Cotton code
82 lines · 6.0 KB · RustRaw
//! **Every place a copy keeps for itself, and whether it can hold a change the server has not acknowledged**//! (QUEUE BAD OP 5, coordinator, 27 Nov 2026).//!//! A copy's own tables (every `_` table any build makes in it) are either a place a person's change waits in until the//! server acknowledges it, or something the copy derives again from what the server holds (a cursor, an index, a//! cache, a note of its own). `cotton_client::copy_keep` asks a copy that is about to leave its place by THIS list,//! and only by it: an unsent place with rows keeps the copy whole. A table no entry names cannot be forgotten: the//! generated test (`copy_keep_tests::every_local_table_is_on_the_list`) reads every `_` table the crates' DDL makes and//! fails on one this list lacks./// What one of a copy's own tables is.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Place {    /// A change waits here until the server acknowledges it. `query` answers each one as `(op id, table, row id,    /// bytes)`; `kind` is how it is said (`queued`, `parked`, `held`, `media`).    Unsent { kind: &'static str, query: &'static str },    /// Derived again from what the server holds, or this device's own note: why.    Derivable(&'static str),}/// Every `_` table a copy may hold.pub static LOCAL_TABLES: &[(&str, Place)] = &[    ("_outbox", Place::Unsent { kind: "queued", query: "SELECT op_id, tbl, row_id, bytes FROM _outbox" }),    ("_parked", Place::Unsent { kind: "parked", query: "SELECT op_id, tbl, row_id, bytes FROM _parked WHERE direction IS NULL OR direction <> 'pull'" }),    ("_writes", Place::Unsent { kind: "held", query: "SELECT op_id, tbl, row_id, bytes FROM _writes WHERE held <> 0" }),    ("_tombstones", Place::Unsent { kind: "held", query: "SELECT op_id, tbl, row_id, bytes FROM _tombstones WHERE held <> 0" }),    // An upload not done: its bytes wait on this phone (`state` pending or failed), or a removal not yet told.    (        "_media",        Place::Unsent { kind: "media", query: "SELECT 'media:' || key, 'media', key, NULL FROM _media WHERE state IS NULL OR state IN ('pending', 'failed', 'delete_pending')" },    ),    ("_meta", Place::Derivable("the copy's own marks (schema, ids), made again with a new copy")),    ("_cursors", Place::Derivable("how far each author was pulled: a new copy pulls again")),    ("_ingest_cursors", Place::Derivable("an import's position, made again from the server's rows")),    ("_field_sets", Place::Derivable("which fields each receipt set, derived from the receipts")),    ("_trim", Place::Derivable("what a pass trimmed, derived")),    ("_writes_owners", Place::Derivable("a projection of each receipt's owners, removed with it")),    ("_gone", Place::Derivable("erases applied from the server, pulled again")),    ("_kept_taken", Place::Derivable("which kept changes this copy sent; the kept copy's own state says the same")),    ("_media_index", Place::Derivable("an index of the media rows")),    ("_media_key_names", Place::Derivable("which rows name which media key, derived")),    ("_media_unlinks", Place::Derivable("bytes this device may free, derived from the rows")),    ("_computed", Place::Derivable("values derived from the records, the same on every device")),    ("_on_demand", Place::Derivable("records opened on this device, fetched again when opened")),    ("_keep_fetch", Place::Derivable("how far a keep rule fetched, fetched again")),    ("_fts_fill", Place::Derivable("the search index's fill cursor")),    ("_landed_fill", Place::Derivable("the mail landing fill's cursor (schema 157)")),    ("_mail_landed", Place::Derivable("each mail's landing time and sender, kept beside cotton_emails by its triggers and the landing fill")),    ("_mail_cuts", Place::Derivable("the mail cuts mirrored from the kit's mail cut table, which syncs")),    ("_mail_own", Place::Derivable("his own addresses, read again from his mail accounts")),    ("_fts_purge", Place::Derivable("the search index's purge cursor")),    ("_refused_since", Place::Derivable("when a queued write was first refused; the write itself is in _outbox")),    ("_money_state", Place::Derivable("a worker's state on the server, never in a phone's copy")),    ("_ai_batches", Place::Derivable("the server's metering, never a phone's change")),    ("_seal", Place::Derivable("which rows are sealed, pulled again")),    ("_seal_parked", Place::Derivable("rows pulled from the server that did not open yet, pulled again")),    ("_seal_park_keys", Place::Derivable("an index of _seal_parked")),    ("_seal_park_state", Place::Derivable("the fill state of _seal_park_keys")),    ("_seal_waiting", Place::Derivable("tables owed a pull once the key is held")),    ("_seal_key", Place::Derivable("the seal versions the copy last read, read again from the database's seal key rows")),    ("_seal_known", Place::Derivable("tables seen sealed, owed whole again")),    ("_edit_base", Place::Derivable("the base a merge compares against, read again")),    ("_unlock_handed", Place::Derivable("which table versions this phone handed in each unlock grant; a grant is asked again")),    ("_seal_unread", Place::Derivable("when to try again to read a sealed row that did not open, pulled again")),    ("_seal_moved", Place::Derivable("when to try again a change refused as moved; the change itself waits in _outbox")),    ("_blocked_seen", Place::Derivable("the day each blocked sender was first seen, read again from the blocked senders")),    ("_blocked_clock", Place::Derivable("the blocked senders' erase timer's own marks")),];/// The entry for `table`, if any.#[must_use]pub fn place_of(table: &str) -> Option<Place> {    LOCAL_TABLES.iter().find(|(name, _)| *name == table).map(|(_, place)| *place)}/// The tables a change can wait in, with their queries.pub fn unsent_places() -> impl Iterator<Item = (&'static str, &'static str, &'static str)> {    LOCAL_TABLES.iter().filter_map(|(table, place)| match place {        Place::Unsent { kind, query } => Some((*table, *kind, *query)),        Place::Derivable(_) => None,    })}