use rusqlite::params; use crate::db::*; use crate::error::Result; use crate::time; // --------------------------------------------------------- world snapshots -- pub fn put_world_snapshot(writer: &Writer, members: &[WorldMember]) -> Result { let mut sorted = members.to_vec(); sorted.sort(); sorted.dedup(); let hash = crate::hash::canonical_json(&sorted)?; let json = serde_json::to_string(&sorted)?; let at = time::now(); writer.tx(move |tx| { tx.execute( "INSERT INTO world_snapshot (hash, members_json, created_at) VALUES (?1,?2,?3) ON CONFLICT(hash) DO NOTHING", params![hash, json, at], )?; tx.query_row("SELECT id FROM world_snapshot WHERE hash=?1", params![hash], |r| r.get(0)) .map_err(Into::into) }) } /// Read the members of a world snapshot. /// /// Test-tier only: production writes world snapshots and never reads one back /// member by member; the index is served from the release rows. #[cfg(feature = "test-support")] pub fn world_members(db: &Db, id: i64) -> Result> { db.read(|c| { let s: String = c.query_row( "SELECT members_json FROM world_snapshot WHERE id=?1", params![id], |r| r.get(0), )?; Ok(json_or(&s, Vec::new())) }) }