Cotton code
57 lines · 6.3 KB · RustRaw
//! Audit receipt query shapes against a synthetic file with the shipped schema.//! Run with `cargo +1.98.1 run -p cotton-store --example receipt-query-plans --features fixtures`.use cotton_store::{fixtures, migrate::SCHEMA_VERSION, Driver};use rusqlite::types::Value;fn main() -> cotton_store::Result<()> {    let dir = tempfile::tempdir().unwrap();    let db = Driver::open(dir.path().join("synthetic.db"))?;    fixtures::build_at_version(&db, SCHEMA_VERSION);    db.exec("INSERT INTO _writes(tbl,row_id,hlc,op_id,author,fields,bytes) VALUES ('cotton_photos','synthetic','0001','synthetic-op','synthetic','[]',X'7B7D')")?;    for (source, sql) in [        ("media_keys/name_sweep.rs:8 (first page)", "SELECT row_id,hlc,bytes FROM _writes WHERE tbl=?1 ORDER BY row_id,hlc LIMIT ?2"),        ("media_keys/name_sweep.rs:6 (cursor page)", "SELECT row_id,hlc,bytes FROM _writes WHERE tbl=?1 AND (row_id,hlc)>(?2,?3) ORDER BY row_id,hlc LIMIT ?4"),        ("snapshot.rs:189 (whole-table removal)", "DELETE FROM _writes WHERE tbl=?"),        ("runner_change.rs:54,72", "SELECT COUNT(*) FROM _writes WHERE author=? AND held=0"),        ("runner_change.rs:56", "SELECT MAX(hlc) FROM _writes WHERE author=? AND held=0"),        ("runner_change.rs:79", "SELECT 1 FROM _writes WHERE author=? AND hlc=? AND held=0"),        ("mint.rs:972 (row clock)", "SELECT MAX(hlc) FROM (SELECT hlc FROM _writes WHERE tbl=?1 AND row_id=?2 UNION ALL SELECT hlc FROM _tombstones WHERE tbl=?1 AND row_id=?2)"),        ("../cotton-client/src/bootstrap/claim.rs:144", "DELETE FROM _cursors"),        ("receipts.rs:222 (clear owner projection)", "DELETE FROM _writes_owners"),        ("take_back.rs (first naming page)", "WITH page AS MATERIALIZED (SELECT * FROM _writes WHERE tbl>=? ORDER BY tbl,row_id,hlc LIMIT 64) SELECT *,COALESCE((hlc>=? AND NOT (tbl=? AND row_id=?) AND instr(CAST(bytes AS TEXT),?)>0),0) AS matches FROM page"),        ("take_back.rs (cursor naming page)", "WITH page AS MATERIALIZED (SELECT * FROM _writes WHERE (tbl,row_id,hlc)>(?,?,?) ORDER BY tbl,row_id,hlc LIMIT 64) SELECT *,COALESCE((hlc>=? AND NOT (tbl=? AND row_id=?) AND instr(CAST(bytes AS TEXT),?)>0),0) AS matches FROM page"),        ("mint_clock.rs:116", "SELECT MAX(top) AS top FROM (SELECT MAX(hlc) AS top FROM _writes WHERE hlc < ?1 UNION ALL SELECT MAX(hlc) FROM _tombstones WHERE hlc < ?1)"),        ("fact_hooks.rs (first undo page)", "WITH page AS MATERIALIZED (SELECT * FROM _writes WHERE tbl>=? ORDER BY tbl,row_id,hlc LIMIT 64) SELECT *,COALESCE((json_extract(CAST(bytes AS TEXT),'$.undo')=?),0) AS matches FROM page"),        ("fact_hooks.rs (cursor undo page)", "WITH page AS MATERIALIZED (SELECT * FROM _writes WHERE (tbl,row_id,hlc)>(?,?,?) ORDER BY tbl,row_id,hlc LIMIT 64) SELECT *,COALESCE((json_extract(CAST(bytes AS TEXT),'$.undo')=?),0) AS matches FROM page"),        ("fact_hooks.rs (tombstone undo page)", "WITH page AS MATERIALIZED (SELECT * FROM _tombstones WHERE (tbl,row_id)>(?,?) ORDER BY tbl,row_id LIMIT 64) SELECT *,COALESCE((json_extract(CAST(bytes AS TEXT),'$.undo')=?),0) AS matches FROM page"),        ("cursors.rs:32", "SELECT author_device_id,hlc_seen FROM _cursors ORDER BY author_device_id"),        ("../cotton-client/src/backup/restore.rs:395", "SELECT COUNT(*) FROM _cursors"),        ("receipts.rs:106; take_back.rs:69", "SELECT tbl,row_id,hlc,op_id,held,bytes FROM _writes WHERE tbl = ? AND row_id = ? ORDER BY hlc"),        ("receipts.rs:143; fold.rs:1074; mint_clock.rs:523,880", "SELECT bytes FROM _writes WHERE op_id = ?"),        ("receipts.rs:287; keep.rs:937,1196; mint.rs:970", "SELECT op_id FROM _writes WHERE tbl = ? AND row_id = ?"),        ("receipts.rs:230", "SELECT op_id,tbl,json_extract(CAST(bytes AS TEXT), '$.fields.owner') FROM _writes WHERE tbl = ? AND op_id IS NOT NULL AND json_type(CAST(bytes AS TEXT), '$.fields.owner') = 'text'"),        ("receipts.rs:213 (owner membership join)", "SELECT w.row_id,o.owner FROM _writes_owners o JOIN _writes w ON w.op_id=o.receipt WHERE o.tbl=? AND o.owner=? AND json_extract(CAST(w.bytes AS TEXT),'$.fields.owner')=o.owner"),        ("mint_clock.rs:174,290; ../cotton-client/src/unsent.rs:159; snapshot.rs:227", "SELECT op_id,hlc,bytes FROM _writes WHERE held=1"),        ("mint_clock.rs:384; changes.rs:333,398,411,480,488,558", "SELECT op_id,hlc,bytes FROM _writes WHERE author=? AND hlc>? AND held=0 ORDER BY hlc LIMIT ?"),        ("cursors.rs:124,126", "SELECT MIN(author) FROM _writes WHERE held=0 AND author>?"),        ("cursors.rs:129", "SELECT hlc FROM _writes WHERE author=? AND held=0 AND tbl IN ('cotton_photos','cotton_msg_messages') ORDER BY hlc DESC LIMIT 1"),        ("snapshot.rs:183", "SELECT DISTINCT tbl FROM _writes"),        ("snapshot.rs:189; keep.rs:1253; erase.rs:184", "DELETE FROM _writes WHERE tbl=? AND row_id=?"),        ("snapshot.rs:206", "DELETE FROM _writes WHERE tbl=? AND NOT EXISTS (SELECT 1 FROM cotton_photos r WHERE r.id=_writes.row_id)"),        ("snapshot.rs:241", "DELETE FROM _writes WHERE held=1"),        ("fold.rs:1115; receipts.rs:299; mint_clock.rs:498", "DELETE FROM _writes WHERE op_id=?"),        ("keep.rs:1323", "SELECT row_id FROM _writes WHERE tbl=?1 UNION SELECT row_id FROM _tombstones WHERE tbl=?1"),        ("keep.rs:1485", "SELECT DISTINCT w.row_id FROM _writes w WHERE w.tbl=?1 AND NOT EXISTS (SELECT 1 FROM cotton_photos t WHERE t.id=w.row_id) AND NOT EXISTS (SELECT 1 FROM _tombstones d WHERE d.tbl=?1 AND d.row_id=w.row_id) LIMIT ?2"),        ("revocation.rs:72,178", "SELECT MIN(hlc) FROM _writes WHERE tbl='cotton_devices' AND row_id=? AND kind='create'"),        ("../cotton-tools/src/mcp/handlers/emails/extract.rs:105", "SELECT DISTINCT f.value FROM _writes w,json_each(w.fields) f WHERE w.tbl='cotton_email_threads' AND w.row_id=? AND w.actor LIKE 'owner:%' AND f.value IN ('synthetic')"),        ("../cotton-tools/src/test_fixtures.rs:130", "SELECT row_id FROM _writes WHERE tbl=? AND kind='create'"),        ("../cotton-client/src/bootstrap/previous.rs:78", "SELECT 1 FROM _writes LIMIT 1"),    ] {        let explain = format!("EXPLAIN QUERY PLAN {sql}");        let count = db.connection().prepare(&explain)?.parameter_count();        let params = vec![Value::Text("synthetic".into()); count];        let plans: Vec<String> = db.all(&explain, &params)?.iter().filter_map(|row| row.text("detail").map(str::to_string)).collect();        println!("{source}: {}", plans.join("; "));    }    Ok(())}