//! The one place a peer's ops become part of a file. //! //! The server is not a merge authority — [`cotton_store::fold::fold_ops`] is, //! and it runs identically here and on the phone. Since version 8 the server //! adds NOTHING to that: there is no `_ops` log to write, no `server_seq` to //! mint and no order to impose, because the tables answer for an author //! ([`cotton_store::changes`]) and the cursor that matters names the WRITER //! rather than the server. //! //! [`apply_ops`] is the write CHOKEPOINT: the exchange route calls it, the //! operator push calls it, and the MCP writer calls it. One function, so an //! assistant's write and a phone's write land by the same rules. //! use cotton_store::exchange::{fold_incoming, Folded}; use cotton_store::protocol::types::CottonOp; use cotton_store::schema::ddl::SCHEMA_VERSION; use cotton_store::{Driver, Result}; use serde_json::Value; /// At most this many ops in one exchange. pub const MAX_OPS_PER_EXCHANGE: usize = 1000; /// What a batch did — ROWS per table, never a count of ops. /// /// One row arrives as several ops (a base patch and a column at its own stamp), /// so counting ops over-reports; and counting rows before against after reports /// zero for two devices that converged on the same derived id. The answer comes /// from the fold's own `applied_rows`, which is the only place that knows. pub type ApplyOutcome = Folded; /// The schema this file is actually stamped at — what its ops are written /// against. pub fn file_schema_version(db: &Driver) -> Result { cotton_store::meta::file_schema_version(db) } /// Fold ops into the file, in ONE transaction. /// /// Shared with the operator push and the MCP writer: everything that changes a /// file goes through here, so there is exactly one definition of what an /// accepted write is. /// /// A replayed batch lands as a no-op: the fold has already decided each column's /// fate by hlc and a stamp equal to what the column carries loses, so /// idempotency is a property of the clock rather than a code path that has to /// remember to check. /// /// AN OP NAMING A RETIRED TABLE IS ACCEPTED AND DISCARDED (R109) by the fold /// itself — the second of the three readers of /// `cotton_store::migrate::Migration`'s `retired_tables`. It is NOT a /// rejection, because the client did nothing wrong and a rejection would come /// back with a reason it cannot act on. pub fn apply_ops(db: &Driver, ops: &[CottonOp]) -> Result { crate::derive::install(); fold_incoming(db, ops) } /// Parse one op off the wire. Answers a REASON rather than throwing, so one /// malformed op in a batch costs its neighbours nothing. pub fn parse_op(raw: &Value) -> std::result::Result { // The one reading of an op off the wire is the store's (slice 2a); a // receipt is taken on the floor rule the exchange admits its reader by // (`cotton_store::exchange::admission`): one minted at or above this // build's floor folds as it stands, and one minted ABOVE this build goes // on to the fold, which parks it `newer-shape` or holds it // (`cotton_store::exchange::fold_incoming`). Only one below the floor is // refused here. let op = cotton_store::wire::op(raw)?; let floor = cotton_store::schema::move_intents::floor_of(SCHEMA_VERSION); if op.schema_version < floor { return Err(format!( "a receipt of schema {} is below what this file folds ({floor} and up)", op.schema_version )); } Ok(op) } #[cfg(test)] #[path = "apply_tests.rs"] mod apply_tests;