//! An erased row's words leave the search index in the BACKGROUND (DELETE FAST, coordinator, 24 Nov 2026).//!//! A search index keeps a deleted row's words in its earlier pages until those are merged. Until today every index//! of a table deleted securely from its first erase on (FTS5 `secure-delete`), which rewrites each word's list as//! each row goes. On 100,000 mails that made Delete everything take 0.8 s a mail on this box and minutes on a phone,//! inside the owner's tap. Now an erase deletes plainly, at once, and writes the index here. A background pass then//! merges it whole ([`step`]: FTS5 `merge` with a negative size, a bounded number of pages each time, until a step//! changes nothing). That ends the same as `optimize`, and it goes on where it stopped across launches. Its last step//! deletes the row. The guarantee (CONTRACT.md "An erase leaves no words behind"): no words are left once the purge//! has run, and it runs within the hour or at next launch. A copy cut for export or hand-over merges every index//! whole first (`snapshot`), so no copy leaves with words still in the pages.//!//! The row is DEVICE-LOCAL and never synced: what this file's pages still hold is a fact about this file.use rusqlite::types::ValueasSqlValue;usecrate::driver::Driver;usecrate::error::Result;/// The table's own name.pubconst TABLE: &str = "_fts_purge";const NOW: &str = "strftime('%Y-%m-%dT%H:%M:%fZ', 'now')";fn held(db: &Driver, name: &str) -> Result<bool> {Ok(db.get("SELECT 1 AS n FROM sqlite_master WHERE type = 'table' AND name = ?", &[SqlValue::Text(name.to_string())])?.is_some())}/// A row of `table` is being erased: each of its search indexes deletes plainly (a copy that deleted securely before/// this is set back) and is due a purge.pubfn mark(db: &Driver, table: &str) -> Result<()> {if !held(db, TABLE)? {returnOk(()); }for source incrate::schema::fts::FTS_SOURCES.iter().filter(|source| source.source == table) {if !held(db, source.fts)? {continue; }let secure = db.get(&format!("SELECT 1 AS n FROM {}_config WHERE k = 'secure-delete' AND CAST(v AS INTEGER) = 1", source.fts), &[])?.is_some();if secure { db.run(&format!("INSERT INTO {name}({name}, rank) VALUES ('secure-delete', 0)", name = source.fts), &[])?; } db.run(&format!("INSERT OR IGNORE INTO {TABLE} (fts, since) VALUES (?, {NOW})"), &[SqlValue::Text(source.fts.to_string())])?; }Ok(())}/// The indexes due a purge, oldest first.pubfn due(db: &Driver) -> Result<Vec<String>> {if !held(db, TABLE)? {returnOk(Vec::new()); }Ok(db.all(&format!("SELECT fts FROM {TABLE} ORDER BY since, fts"), &[])?.iter().filter_map(|row| row.text("fts").map(str::to_string)).collect())}/// When the oldest erase still waiting for its purge was marked, if any.pubfn oldest(db: &Driver) -> Result<Option<String>> {if !held(db, TABLE)? {returnOk(None); }Ok(db.get(&format!("SELECT min(since) AS since FROM {TABLE}"), &[])?.and_then(|row| row.text("since").map(str::to_string)))}/// Merge `fts` by about `pages` pages. `Some(true)` when it is whole and no erased row's words are left (its row is/// gone), `Some(false)` while more is left, `None` when it was not due.pubfn step(db: &Driver, fts: &str, pages: u32) -> Result<Option<bool>> {if db.get(&format!("SELECT 1 AS n FROM {TABLE} WHERE fts = ?"), &[SqlValue::Text(fts.to_string())])?.is_none() {returnOk(None); }if !held(db, fts)? { db.run(&format!("DELETE FROM {TABLE} WHERE fts = ?"), &[SqlValue::Text(fts.to_string())])?;returnOk(Some(true)); }let changes = |db: &Driver| -> Result<i64> { Ok(db.get("SELECT total_changes() AS n", &[])?.and_then(|row| row.int("n")).unwrap_or(0)) };let before = changes(db)?; db.run(&format!("INSERT INTO {fts}({fts}, rank) VALUES ('merge', ?)"), &[SqlValue::Integer(-(pages.max(16) as i64))])?;// FTS5: a negative merge run until it changes fewer than two rows has done what `optimize` does.if changes(db)? - before < 2 { db.run(&format!("DELETE FROM {TABLE} WHERE fts = ?"), &[SqlValue::Text(fts.to_string())])?;returnOk(Some(true)); }Ok(Some(false))}/// Every due index merged whole now: a copy cut for export or hand-over, and the tests.pubfn all(db: &Driver) -> Result<()> {for fts in due(db)? {while step(db, &fts, 4096)? == Some(false) {} }Ok(())}