//! HOW A COPY'S FIRST SYNC SPENT ITS TIME (PHONE MEASURES, the coordinator 27 Nov 2026): kept beside the copy //! (`first-sync.json`) from its first page to the pass that leaves it level, for the device's reported state //! ([`FirstSyncTimes`]): waiting on the server, opening sealed rows, merging them in, and filling the search indexes //! after. Kinds and times only. A copy made again starts a new record; one cut short (the app killed) carries on. use std::path::{Path, PathBuf}; pub use cotton_core::device::reported_state::FirstSyncTimes; /// The file the record is kept in, beside the copy. pub const FILE: &str = "first-sync.json"; fn kept_at(replica: &Path) -> PathBuf { replica.with_file_name(FILE) } /// The record kept beside the copy at `replica`, if any. pub fn kept(replica: &Path) -> Option { serde_json::from_slice(&std::fs::read(kept_at(replica)).ok()?).ok() } fn keep(replica: &Path, times: &FirstSyncTimes) { let path = kept_at(replica); let partial = path.with_extension("json.partial"); // not a copy: the first sync's measured times beside the copy, written whole and put in place. let written = serde_json::to_vec(times).map_err(|error| error.to_string()).and_then(|bytes| std::fs::write(&partial, bytes).and_then(|()| std::fs::rename(&partial, &path)).map_err(|error| error.to_string())); if let Err(why) = written { // A measure, never a reason a pass fails. eprintln!("[first-sync] the first sync's times could not be kept ({why})"); } } /// A pull from the very start of the copy (no cursor): a first sync begins, unless one is still under way (cut /// short and begun again from nothing, it carries on). A copy made again, whose record says done, starts anew. pub fn begin(replica: &Path, now_ms: i64) { if kept(replica).is_some_and(|times| times.done_at_ms.is_none()) { return; } keep(replica, &FirstSyncTimes { started_at_ms: now_ms, ..FirstSyncTimes::default() }); } /// Add to the first sync under way, if one is: `change` takes the record. Nothing once it is done. pub fn add(replica: &Path, change: impl FnOnce(&mut FirstSyncTimes)) { let Some(mut times) = kept(replica).filter(|times| times.done_at_ms.is_none()) else { return }; change(&mut times); keep(replica, ×); } /// The search fill's time, added to a first sync's record, done or not, until the fill is whole (`whole`): the /// fill goes on after the copy is level, and is part of what the first sync cost. pub fn add_indexing(replica: &Path, ms: u64, whole: bool) { let Some(mut times) = kept(replica) else { return }; if times.indexed { return; } times.indexing_ms += ms; times.indexed = whole; keep(replica, ×); } #[cfg(test)] mod tests { use super::*; /// RULE (PHONE MEASURES): a first sync's times are kept beside the copy from its first page to level, carried on /// when cut short, started anew for a copy made again, and its search fill counted until the fill is whole. #[test] fn a_first_syncs_times_are_kept_until_level_and_its_fill_until_whole() { let dir = tempfile::tempdir().unwrap(); let replica = dir.path().join("replica.db"); add(&replica, |times| times.pages += 1); assert_eq!(kept(&replica), None, "nothing before a first sync begins"); begin(&replica, 1_000); add(&replica, |times| { times.waiting_ms += 30; times.pages += 1; }); begin(&replica, 2_000); assert_eq!(kept(&replica).unwrap().started_at_ms, 1_000, "cut short and begun again, it carries on"); add(&replica, |times| times.done_at_ms = Some(3_000)); add(&replica, |times| times.pages += 1); assert_eq!(kept(&replica).unwrap().pages, 1, "nothing added once level"); add_indexing(&replica, 40, false); add_indexing(&replica, 2, true); add_indexing(&replica, 500, false); let times = kept(&replica).unwrap(); assert_eq!((times.waiting_ms, times.indexing_ms, times.indexed), (30, 42, true), "{times:?}"); begin(&replica, 9_000); assert_eq!(kept(&replica).unwrap(), FirstSyncTimes { started_at_ms: 9_000, ..FirstSyncTimes::default() }, "a copy made again starts anew"); } }