π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
vecdb: derive HNSW levels from a persisted insert ordinal; keep compaction snapshot-dirty on failure; report Closed under the search lock
laurenspriem86d88e3f3c88
diff --git a/rust/crates/ml/src/vecdb/graph.rs b/rust/crates/ml/src/vecdb/graph.rsindex b7c731a3e2..ddcb9afd9f 100644--- a/rust/crates/ml/src/vecdb/graph.rs+++ b/rust/crates/ml/src/vecdb/graph.rs@@ -93,7 +93,7 @@ pub(crate) struct GraphNodeParts {pub(crate) struct Graph {nodes: Vec<Option<Node>>,entry_point: Option<u32>,- level_state: u64,+ insert_ordinal: u64,}impl Graph {@@ -101,7 +101,7 @@ impl Graph {Self {nodes: Vec::new(),entry_point: None,- level_state: LEVEL_SEED,+ insert_ordinal: 0,}}@@ -117,6 +117,7 @@ impl Graph {entry_point: Option<u32>,parts: Vec<GraphNodeParts>,slot_count: usize,+ insert_ordinal: u64,) -> Result<Self, VecDbError> {let mut slots: HashSet<u32> = HashSet::with_capacity(parts.len());for part in &parts {@@ -181,7 +182,7 @@ impl Graph {Ok(Self {nodes,entry_point,- level_state: LEVEL_SEED,+ insert_ordinal,})}@@ -252,6 +253,10 @@ impl Graph {self.entry_point}+ pub(crate) fn insert_ordinal(&self) -> u64 {+ self.insert_ordinal+ }+pub(crate) fn node_count(&self) -> usize {self.slots().count()}@@ -353,7 +358,9 @@ impl Graph {}fn next_level(&mut self) -> usize {- let raw = splitmix64(&mut self.level_state);+ let mut state = LEVEL_SEED ^ self.insert_ordinal;+ self.insert_ordinal = self.insert_ordinal.wrapping_add(1);+ let raw = splitmix64(&mut state);let unit = ((raw >> 11) + 1) as f64 / (1u64 << 53) as f64;(-unit.ln() * (M as f64).ln().recip()) as usize}@@ -1541,7 +1548,13 @@ mod tests {let (arena, graph) = build_fixture(300, 16, 0x8000_0000);let parts = graph_parts(&graph);assert_eq!(parts.len(), graph.node_count());- let rebuilt = Graph::from_parts(graph.entry_point(), parts, arena.slot_count()).unwrap();+ let rebuilt = Graph::from_parts(+ graph.entry_point(),+ parts,+ arena.slot_count(),+ graph.insert_ordinal(),+ )+ .unwrap();assert_identical_graphs(&rebuilt, &graph);let query = arena.pack_query(&seeded_unit_vector(0x8111_0000, 16))@@ -1557,6 +1570,18 @@ mod tests {assert_eq!(from_original, from_rebuilt);}+ #[test]+ fn inserts_after_a_maximal_persisted_ordinal_wrap_instead_of_panicking() {+ let arena = build_arena(2, 8, 0x00D0_0000);+ let mut graph = Graph::from_parts(None, Vec::new(), arena.slot_count(), u64::MAX).unwrap();+ graph.insert(0, &arena);+ assert_eq!(graph.insert_ordinal(), 0);+ graph.insert(1, &arena);+ assert_eq!(graph.insert_ordinal(), 1);+ assert!(graph.level_of(0).is_some());+ assert!(graph.level_of(1).is_some());+ }+#[test]fn from_parts_rejects_malformed_parts() {let node = |slot: u32, level: u8, neighbors: Vec<Vec<u32>>| GraphNodeParts {@@ -1564,17 +1589,18 @@ mod tests {level,neighbors,};- assert!(Graph::from_parts(None, Vec::new(), 0).is_ok());- assert!(Graph::from_parts(Some(0), vec![node(0, 0, vec![Vec::new()])], 1).is_ok());- assert!(Graph::from_parts(Some(0), vec![node(0, 1, vec![Vec::new()])], 1).is_err());- assert!(Graph::from_parts(Some(0), vec![node(0, 0, vec![vec![9]])], 10).is_err());- assert!(Graph::from_parts(None, vec![node(0, 0, vec![Vec::new()])], 1).is_err());- assert!(Graph::from_parts(Some(5), vec![node(0, 0, vec![Vec::new()])], 6).is_err());+ assert!(Graph::from_parts(None, Vec::new(), 0, 0).is_ok());+ assert!(Graph::from_parts(Some(0), vec![node(0, 0, vec![Vec::new()])], 1, 0).is_ok());+ assert!(Graph::from_parts(Some(0), vec![node(0, 1, vec![Vec::new()])], 1, 0).is_err());+ assert!(Graph::from_parts(Some(0), vec![node(0, 0, vec![vec![9]])], 10, 0).is_err());+ assert!(Graph::from_parts(None, vec![node(0, 0, vec![Vec::new()])], 1, 0).is_err());+ assert!(Graph::from_parts(Some(5), vec![node(0, 0, vec![Vec::new()])], 6, 0).is_err());assert!(Graph::from_parts(Some(0),vec![node(0, 0, vec![Vec::new()]), node(0, 0, vec![Vec::new()])],- 1+ 1,+ 0).is_err());@@ -1585,7 +1611,8 @@ mod tests {node(0, 0, vec![vec![1]]),node(1, 1, vec![vec![0], Vec::new()])],- 2+ 2,+ 0).is_ok());@@ -1598,13 +1625,14 @@ mod tests {level,neighbors,};- assert!(Graph::from_parts(Some(0), vec![node(0, 0, vec![Vec::new()])], 0).is_err());- assert!(Graph::from_parts(Some(2), vec![node(2, 0, vec![Vec::new()])], 2).is_err());+ assert!(Graph::from_parts(Some(0), vec![node(0, 0, vec![Vec::new()])], 0, 0).is_err());+ assert!(Graph::from_parts(Some(2), vec![node(2, 0, vec![Vec::new()])], 2, 0).is_err());assert!(Graph::from_parts(Some(u32::MAX - 1),vec![node(u32::MAX - 1, 0, vec![Vec::new()])],- 3+ 3,+ 0).is_err());@@ -1625,6 +1653,7 @@ mod tests {node(2, 0, vec![vec![1]]),],3,+ 0,).unwrap();let mut arena = VectorArena::new(8).unwrap();@@ -1656,7 +1685,7 @@ mod tests {}],}).collect();- let graph = Graph::from_parts(Some(0), parts, arena.slot_count()).unwrap();+ let graph = Graph::from_parts(Some(0), parts, arena.slot_count(), 0).unwrap();assert!(graph.neighbors_of(0, 0).len() > neighbor_cap(0));let query = arena.pack_query(&seeded_unit_vector(0x00E1_0000, dims))@@ -1720,6 +1749,7 @@ mod tests {node(2, 2, vec![vec![0], vec![0], vec![0]]),],3,+ 0,).unwrap();let mut arena = VectorArena::new(8).unwrap();@@ -1747,9 +1777,13 @@ mod tests {graph.reinsert(victim, &arena);stale_seen |= stale_downward_edge_exists(&graph);assert_graph_invariants(&graph);- let reloaded =- Graph::from_parts(graph.entry_point(), graph_parts(&graph), arena.slot_count())- .unwrap();+ let reloaded = Graph::from_parts(+ graph.entry_point(),+ graph_parts(&graph),+ arena.slot_count(),+ graph.insert_ordinal(),+ )+ .unwrap();assert_identical_graphs(&reloaded, &graph);let query = arena.pack_query(&vector).unwrap();let top = search(&graph, &arena, &query, ¶ms(Some(1), None, false), None);@@ -1764,9 +1798,13 @@ mod tests {assert_eq!(everything.len(), 600);}assert!(stale_seen);- let rebuilt =- Graph::from_parts(graph.entry_point(), graph_parts(&graph), arena.slot_count())- .unwrap();+ let rebuilt = Graph::from_parts(+ graph.entry_point(),+ graph_parts(&graph),+ arena.slot_count(),+ graph.insert_ordinal(),+ )+ .unwrap();assert_identical_graphs(&rebuilt, &graph);let query = arena.pack_query(&seeded_unit_vector(0xB200_0000, 16))@@ -1826,9 +1864,13 @@ mod tests {let (arena, graph) = (&primary.0, &primary.1);assert_graph_invariants(graph);assert_eq!(arena.live_count(), live.len());- let reloaded =- Graph::from_parts(graph.entry_point(), graph_parts(graph), arena.slot_count())- .unwrap();+ let reloaded = Graph::from_parts(+ graph.entry_point(),+ graph_parts(graph),+ arena.slot_count(),+ graph.insert_ordinal(),+ )+ .unwrap();assert_identical_graphs(&reloaded, graph);for _ in 0..3 {let query_vector = seeded_unit_vector(splitmix64(&mut state), dims);@@ -1900,8 +1942,13 @@ mod tests {}assert_identical_graphs(&primary.1, &shadow.1);let (arena, graph) = (&primary.0, &primary.1);- let rebuilt =- Graph::from_parts(graph.entry_point(), graph_parts(graph), arena.slot_count()).unwrap();+ let rebuilt = Graph::from_parts(+ graph.entry_point(),+ graph_parts(graph),+ arena.slot_count(),+ graph.insert_ordinal(),+ )+ .unwrap();assert_identical_graphs(&rebuilt, graph);let query = arena.pack_query(&seeded_unit_vector(0xC900_0000, dims))diff --git a/rust/crates/ml/src/vecdb/snapshot.rs b/rust/crates/ml/src/vecdb/snapshot.rsindex 9cfcde69c2..b31f9ed2f8 100644--- a/rust/crates/ml/src/vecdb/snapshot.rs+++ b/rust/crates/ml/src/vecdb/snapshot.rs@@ -11,12 +11,13 @@ const MAGIC: [u8; 4] = *b"EVDG";const FORMAT_VERSION: u16 = 1;const NO_ENTRY_POINT: u32 = u32::MAX;const CRC_LEN: usize = 4;-const FIXED_PREFIX_LEN: usize = 39;+const FIXED_PREFIX_LEN: usize = 47;const MIN_SNAPSHOT_LEN: usize = FIXED_PREFIX_LEN + CRC_LEN;const MIN_NODE_LEN: usize = 7;pub(crate) struct LoadedSnapshot {pub(crate) covered_log_offset: u64,+ pub(crate) insert_ordinal: u64,pub(crate) entry_point: Option<u32>,pub(crate) parts: Vec<GraphNodeParts>,}@@ -72,6 +73,7 @@ fn encode_snapshot(generation: &[u8; 16], covered_log_offset: u64, graph: &Graphbytes.extend_from_slice(&FORMAT_VERSION.to_le_bytes());bytes.extend_from_slice(generation);bytes.extend_from_slice(&covered_log_offset.to_le_bytes());+ bytes.extend_from_slice(&graph.insert_ordinal().to_le_bytes());let entry_marker = graph.entry_point().unwrap_or(NO_ENTRY_POINT);bytes.extend_from_slice(&entry_marker.to_le_bytes());let top_level = graph@@ -136,6 +138,7 @@ fn decode_snapshot(bytes: &[u8], expected_generation: &[u8; 16]) -> Result<Loadereturn Err("generation does not match the log".to_string());}let covered_log_offset = reader.read_u64().ok_or("truncated header")?;+ let insert_ordinal = reader.read_u64().ok_or("truncated header")?;let entry_marker = reader.read_u32().ok_or("truncated header")?;let top_level = reader.read_u8().ok_or("truncated header")?;let node_count = reader.read_u32().ok_or("truncated header")? as usize;@@ -183,6 +186,7 @@ fn decode_snapshot(bytes: &[u8], expected_generation: &[u8; 16]) -> Result<Loade}Ok(LoadedSnapshot {covered_log_offset,+ insert_ordinal,entry_point,parts,})@@ -262,13 +266,14 @@ mod tests {use super::super::test_support::{assert_identical_graphs, stale_downward_edge_exists};use super::*;- const GOLDEN: [u8; 86] = [+ const GOLDEN: [u8; 94] = [0x45, 0x56, 0x44, 0x47, 0x01, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x2c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,- 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,- 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00,- 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,- 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x64, 0xa1, 0xbc, 0xba,+ 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,+ 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,+ 0x10, 0xe9, 0x5e, 0xe7,];fn golden_generation() -> [u8; 16] {@@ -296,6 +301,7 @@ mod tests {},],3,+ 7,).unwrap()}@@ -415,6 +421,7 @@ mod tests {std::fs::write(snapshot_path(&literal_path), GOLDEN).unwrap();let loaded = load_golden(&literal_path).unwrap();assert_eq!(loaded.covered_log_offset, 300);+ assert_eq!(loaded.insert_ordinal, 7);assert_eq!(loaded.entry_point, Some(1));assert_eq!(loaded.parts.len(), 3);let expected: [(u32, u8, Vec<Vec<u32>>); 3] = [@@ -427,8 +434,10 @@ mod tests {assert_eq!(part.level, *level);assert_eq!(&part.neighbors, neighbors);}- let rebuilt = Graph::from_parts(loaded.entry_point, loaded.parts, 3).unwrap();+ let rebuilt =+ Graph::from_parts(loaded.entry_point, loaded.parts, 3, loaded.insert_ordinal).unwrap();assert_identical_graphs(&rebuilt, &golden_graph());+ assert_eq!(rebuilt.insert_ordinal(), 7);}#[test]@@ -442,10 +451,17 @@ mod tests {assert!(!temp_snapshot_path(&log_path).exists());let loaded = load_snapshot(&log_path, generation).unwrap();assert_eq!(loaded.covered_log_offset, 4096);+ assert_eq!(loaded.insert_ordinal, graph.insert_ordinal());assert_eq!(loaded.entry_point, graph.entry_point());- let rebuilt =- Graph::from_parts(loaded.entry_point, loaded.parts, arena.slot_count()).unwrap();+ let rebuilt = Graph::from_parts(+ loaded.entry_point,+ loaded.parts,+ arena.slot_count(),+ loaded.insert_ordinal,+ )+ .unwrap();assert_identical_graphs(&rebuilt, &graph);+ assert_eq!(rebuilt.insert_ordinal(), graph.insert_ordinal());for seed in 0..6u64 {let query = arena.pack_query(&seeded_unit_vector(0x00A4_0000 + seed, dims))@@ -474,9 +490,11 @@ mod tests {);let loaded = load_snapshot(&log_path, [3u8; 16]).unwrap();assert_eq!(loaded.covered_log_offset, 32);+ assert_eq!(loaded.insert_ordinal, 0);assert_eq!(loaded.entry_point, None);assert!(loaded.parts.is_empty());- let rebuilt = Graph::from_parts(loaded.entry_point, loaded.parts, 0).unwrap();+ let rebuilt =+ Graph::from_parts(loaded.entry_point, loaded.parts, 0, loaded.insert_ordinal).unwrap();assert_eq!(rebuilt.entry_point(), None);assert_eq!(rebuilt.node_count(), 0);}@@ -532,8 +550,12 @@ mod tests {return;};decoded += 1;- let Ok(graph) = Graph::from_parts(loaded.entry_point, loaded.parts, arena.slot_count())- else {+ let Ok(graph) = Graph::from_parts(+ loaded.entry_point,+ loaded.parts,+ arena.slot_count(),+ loaded.insert_ordinal,+ ) else {return;};for search_params in [params(Some(2), None), params(None, Some(2.5))] {@@ -573,6 +595,7 @@ mod tests {bytes.extend_from_slice(&FORMAT_VERSION.to_le_bytes());bytes.extend_from_slice(&golden_generation());bytes.extend_from_slice(&300u64.to_le_bytes());+ bytes.extend_from_slice(&7u64.to_le_bytes());bytes.extend_from_slice(&slot.to_le_bytes());bytes.push(0);bytes.extend_from_slice(&1u32.to_le_bytes());@@ -583,7 +606,9 @@ mod tests {bytes.extend_from_slice(&crc.to_le_bytes());let loaded = decode_snapshot(&bytes, &golden_generation()).unwrap();assert_eq!(loaded.entry_point, Some(slot));- assert!(Graph::from_parts(loaded.entry_point, loaded.parts, 3).is_err());+ assert!(+ Graph::from_parts(loaded.entry_point, loaded.parts, 3, loaded.insert_ordinal).is_err()+ );}#[test]@@ -619,7 +644,7 @@ mod tests {#[test]fn flipped_body_byte_is_discarded() {- assert_discarded(|bytes| bytes[57] ^= 0x01);+ assert_discarded(|bytes| bytes[65] ^= 0x01);}#[test]@@ -635,11 +660,11 @@ mod tests {#[test]fn wrong_node_count_is_discarded() {assert_discarded(|bytes| {- bytes[35] = 4;+ bytes[43] = 4;refresh_crc(bytes);});assert_discarded(|bytes| {- bytes[35] = 2;+ bytes[43] = 2;refresh_crc(bytes);});}@@ -647,7 +672,7 @@ mod tests {#[test]fn oversized_neighbor_count_is_discarded() {assert_discarded(|bytes| {- bytes[44..46].copy_from_slice(&u16::MAX.to_le_bytes());+ bytes[52..54].copy_from_slice(&u16::MAX.to_le_bytes());refresh_crc(bytes);});}@@ -655,11 +680,11 @@ mod tests {#[test]fn wrong_top_level_is_discarded() {assert_discarded(|bytes| {- bytes[34] = 0;+ bytes[42] = 0;refresh_crc(bytes);});assert_discarded(|bytes| {- bytes[34] = 2;+ bytes[42] = 2;refresh_crc(bytes);});}@@ -667,7 +692,7 @@ mod tests {#[test]fn entry_marker_inconsistencies_are_discarded() {assert_discarded(|bytes| {- bytes[30..34].copy_from_slice(&NO_ENTRY_POINT.to_le_bytes());+ bytes[38..42].copy_from_slice(&NO_ENTRY_POINT.to_le_bytes());refresh_crc(bytes);});let dir = TempDir::new().unwrap();@@ -675,7 +700,7 @@ mod tests {write_snapshot(&log_path, golden_generation(), 300, &Graph::new()).unwrap();let path = snapshot_path(&log_path);let mut bytes = std::fs::read(&path).unwrap();- bytes[30..34].copy_from_slice(&0u32.to_le_bytes());+ bytes[38..42].copy_from_slice(&0u32.to_le_bytes());refresh_crc(&mut bytes);std::fs::write(&path, &bytes).unwrap();assert!(load_golden(&log_path).is_none());diff --git a/rust/crates/ml/src/vecdb/store.rs b/rust/crates/ml/src/vecdb/store.rsindex 1f911204fb..a02aac70e5 100644--- a/rust/crates/ml/src/vecdb/store.rs+++ b/rust/crates/ml/src/vecdb/store.rs@@ -447,6 +447,7 @@ impl VecDb {return Ok(Vec::new());}let st = self.shared.state_read();+ self.shared.ensure_open()?;let allowed_slots = resolve_allowed_slots(&st, params.allowed_keys.as_deref());search_in_state(&st, query, params, allowed_slots.as_ref())}@@ -461,6 +462,7 @@ impl VecDb {return Ok(queries.iter().map(|_| Vec::new()).collect());}let st = self.shared.state_read();+ self.shared.ensure_open()?;let allowed_slots = resolve_allowed_slots(&st, params.allowed_keys.as_deref());queries.iter()@@ -478,6 +480,7 @@ impl VecDb {) -> Result<Vec<KeyMatches>, VecDbError> {self.shared.ensure_open()?;let st = self.shared.state_read();+ self.shared.ensure_open()?;let resolved: Vec<(&String, u32)> = keys.iter().filter_map(|key| st.arena.slot_of_key(key).map(|slot| (key, slot)))@@ -532,7 +535,12 @@ impl VecDb {pub fn reset(&self) -> Result<(), VecDbError> {let mut half = self.writable_half()?;- let WriterState { lock, log, .. } = take_writer_state(&mut half)?;+ let WriterState {+ lock,+ log,+ mutations_since_snapshot,+ ..+ } = take_writer_state(&mut half)?;let old_generation = log.generation();drop(log);let dims = self.shared.dims;@@ -541,7 +549,13 @@ impl VecDb {let log = match recreated {Ok(log) => log,Err(error) => {- recover_after_failed_reset(&self.shared, &mut half, lock, old_generation);+ recover_after_failed_reset(+ &self.shared,+ &mut half,+ lock,+ old_generation,+ mutations_since_snapshot,+ );return Err(error);}};@@ -825,13 +839,14 @@ fn compact(shared: &Shared, half: &mut WriterHalf) -> Result<(), VecDbError> {let WriterState {lock,log,+ mutations_since_snapshot,last_write,- ..} = take_writer_state(half)?;drop(log);let promoted = promote_compacted_log(&temp_path, &shared.path);- if temp_path.exists() {+ let pending = if temp_path.exists() {let _ = std::fs::remove_file(&temp_path);+ mutations_since_snapshot} else {let graph = Graph::rebuild(&compacted);let total_records = compacted.live_count() as u64;@@ -840,8 +855,9 @@ fn compact(shared: &Shared, half: &mut WriterHalf) -> Result<(), VecDbError> {st.graph = graph;st.attrs = compacted_attrs;st.total_records = total_records;- }- let restored = restore_writer_mode(shared, half, lock, last_write);+ total_records as usize+ };+ let restored = restore_writer_mode(shared, half, lock, last_write, pending);promoted?;restored?;write_snapshot_now(shared, half)@@ -852,13 +868,14 @@ fn restore_writer_mode(half: &mut WriterHalf,lock: WriterLock,last_write: Option<Instant>,+ mutations_since_snapshot: usize,) -> Result<(), VecDbError> {match reopen_log(&shared.path, shared.dims) {Ok(log) => {half.mode = WriterMode::Active(WriterState {lock,log,- mutations_since_snapshot: 0,+ mutations_since_snapshot,last_write,});Ok(())@@ -882,8 +899,9 @@ fn recover_after_failed_reset(half: &mut WriterHalf,lock: WriterLock,old_generation: [u8; 16],+ old_pending: usize,) {- let old_log_restored = restore_writer_mode(shared, half, lock, None).is_ok()+ let old_log_restored = restore_writer_mode(shared, half, lock, None, old_pending).is_ok()&& matches!(&half.mode, WriterMode::Active(state) if state.log.generation() == old_generation);if old_log_restored {return;@@ -896,6 +914,9 @@ fn recover_after_failed_reset(st.total_records = 0;half.compaction_retry_at_dead = 0;}+ if let WriterMode::Active(state) = &mut half.mode {+ state.mutations_since_snapshot = 0;+ }}fn build_writer(path: &Path, registry_key: PathBuf, dims: usize) -> Result<Shared, VecDbError> {@@ -1141,7 +1162,12 @@ fn replay(log: &mut Log, dims: usize) -> Result<ReplayedState, VecDbError> {let (graph, used_snapshot) = match snapshot {Some(loaded) if loaded.covered_log_offset <= recoverable_end => {let snapshot_slots = covered_slot_count.unwrap_or(arena.slot_count());- match Graph::from_parts(loaded.entry_point, loaded.parts, snapshot_slots) {+ match Graph::from_parts(+ loaded.entry_point,+ loaded.parts,+ snapshot_slots,+ loaded.insert_ordinal,+ ) {Ok(mut graph) => {for outcome in tail_outcomes {apply_outcome(&mut graph, &arena, outcome);@@ -2355,6 +2381,59 @@ mod tests {assert_eq!(fs::read(snapshot_path(&path)).unwrap(), snapshot_bytes);}+ #[test]+ fn interrupted_sessions_draw_the_same_levels_as_one_uninterrupted_session() {+ let dir = TempDir::new().unwrap();+ let split_path = dir.path().join("split");+ let whole_path = dir.path().join("whole");+ {+ let db = open_writer(&split_path);+ bulk_add(&db, &bulk_entries(0, 40, 500)).unwrap();+ db.flush().unwrap();+ }+ assert_eq!(+ load_snapshot(&split_path, generation_of(&split_path))+ .unwrap()+ .insert_ordinal,+ 40+ );+ {+ let db = open_writer(&split_path);+ bulk_add(&db, &bulk_entries(40, 30, 500)).unwrap();+ db.add("key-3", &seeded_unit_vector(999, DIMS)).unwrap();+ }+ {+ let db = open_writer(&split_path);+ bulk_add(&db, &bulk_entries(70, 30, 500)).unwrap();+ db.flush().unwrap();+ }+ let split = open_writer(&split_path);+ let whole = open_writer(&whole_path);+ bulk_add(&whole, &bulk_entries(0, 40, 500)).unwrap();+ bulk_add(&whole, &bulk_entries(40, 30, 500)).unwrap();+ whole.add("key-3", &seeded_unit_vector(999, DIMS)).unwrap();+ bulk_add(&whole, &bulk_entries(70, 30, 500)).unwrap();+ let split_state = split.shared.state_read();+ let whole_state = whole.shared.state_read();+ assert_eq!(split_state.graph.insert_ordinal(), 101);+ assert_eq!(whole_state.graph.insert_ordinal(), 101);+ let mut upper_levels = 0usize;+ for index in 0..100 {+ let key = format!("key-{index}");+ let split_slot = split_state.arena.slot_of_key(&key).unwrap();+ let whole_slot = whole_state.arena.slot_of_key(&key).unwrap();+ assert_eq!(split_slot, whole_slot);+ let level = split_state.graph.level_of(split_slot).unwrap();+ assert_eq!(+ level,+ whole_state.graph.level_of(whole_slot).unwrap(),+ "level diverges for {key}"+ );+ upper_levels += usize::from(level > 0);+ }+ assert!(upper_levels > 0);+ }+#[test]fn compaction_rewrites_log_and_preserves_live_data() {let dir = TempDir::new().unwrap();@@ -2382,6 +2461,7 @@ mod tests {assert_ne!(generation_of(&path), generation_before);let loaded = load_snapshot(&path, generation_of(&path)).unwrap();assert_eq!(loaded.covered_log_offset, stats.log_bytes);+ assert_eq!(loaded.insert_ordinal, 90);let live_vector = |index: u64| {if index < 40 {seeded_unit_vector(5000 + index, DIMS)@@ -2529,8 +2609,14 @@ mod tests {let path = dir.path().join("db");let db = open_writer(&path);bulk_add(&db, &bulk_entries(0, 3, 260)).unwrap();- let WriterState { lock, log, .. } = take_writer(&db);+ let WriterState {+ lock,+ log,+ mutations_since_snapshot,+ ..+ } = take_writer(&db);drop(log);+ assert_eq!(mutations_since_snapshot, 3);assert!(is_degraded(&db));let vector = seeded_unit_vector(4, DIMS);assert!(matches!(@@ -2543,8 +2629,10 @@ mod tests {));{let mut half = db.shared.writer_half();- restore_writer_mode(&db.shared, &mut half, lock, None).unwrap();+ restore_writer_mode(&db.shared, &mut half, lock, None, mutations_since_snapshot)+ .unwrap();}+ assert_eq!(mutations(&db), 3);db.add("after", &vector).unwrap();assert!(matches!(WriterLock::acquire(&path),@@ -2571,7 +2659,7 @@ mod tests {{let mut half = db.shared.writer_half();assert!(matches!(- restore_writer_mode(&db.shared, &mut half, lock, None),+ restore_writer_mode(&db.shared, &mut half, lock, None, 3),Err(VecDbError::Corrupt(_))));assert!(@@ -2634,6 +2722,37 @@ mod tests {assert!(!reopened.contains("key-0"));}+ #[test]+ fn failed_post_compaction_snapshot_leaves_the_state_dirty_until_flush_succeeds() {+ let dir = TempDir::new().unwrap();+ let path = dir.path().join("db");+ let db = open_writer(&path);+ bulk_add(&db, &bulk_entries(0, 100, 970)).unwrap();+ fs::create_dir(snapshot_path(&path)).unwrap();+ let removals: Vec<String> = (0..64).map(|index| format!("key-{index}")).collect();+ assert_eq!(db.bulk_remove(&removals).unwrap(), 64);+ let stats = db.stats().unwrap();+ assert_eq!(stats.live_count, 36);+ assert_eq!(stats.dead_count, 0);+ assert_eq!(stats.records_since_snapshot, 36);+ assert!(matches!(db.flush(), Err(VecDbError::Io { .. })));+ assert_eq!(db.stats().unwrap().records_since_snapshot, 36);+ drop(db);+ let blocked = open_writer(&path);+ assert_eq!(blocked.len(), 36);+ assert!(blocked.contains("key-64"));+ assert!(!blocked.contains("key-0"));+ assert_eq!(blocked.stats().unwrap().records_since_snapshot, 36);+ fs::remove_dir(snapshot_path(&path)).unwrap();+ blocked.flush().unwrap();+ assert_eq!(blocked.stats().unwrap().records_since_snapshot, 0);+ assert!(snapshot_exists(&path));+ drop(blocked);+ let reopened = open_writer(&path);+ assert_eq!(reopened.len(), 36);+ assert_own_nearest(&reopened, "key-99", &seeded_unit_vector(970 + 99, DIMS));+ }+#[test]fn open_succeeds_and_serves_data_when_the_snapshot_cannot_be_written() {let dir = TempDir::new().unwrap();@@ -2742,6 +2861,7 @@ mod tests {};assert_eq!(db.len(), 5);assert!(db.contains("key-0"));+ assert_eq!(db.stats().unwrap().records_since_snapshot, 5);db.add("after", &seeded_unit_vector(6, DIMS)).unwrap();drop(db);let reopened = open_writer(&path);@@ -2763,11 +2883,12 @@ mod tests {drop(Log::create(&path, DIMS).unwrap().into_file());{let mut half = db.shared.writer_half();- recover_after_failed_reset(&db.shared, &mut half, lock, old_generation);+ recover_after_failed_reset(&db.shared, &mut half, lock, old_generation, 5);}assert!(db.is_empty());assert!(!db.contains("key-0"));assert_eq!(db.stats().unwrap().log_bytes, 32);+ assert_eq!(db.stats().unwrap().records_since_snapshot, 0);db.add("fresh", &seeded_unit_vector(7, DIMS)).unwrap();assert_eq!(db.len(), 1);drop(db);@@ -2813,6 +2934,14 @@ mod tests {survivor.search(&query, &limit_params(3)),Err(VecDbError::Closed)));+ assert!(matches!(+ survivor.bulk_search(std::slice::from_ref(&query), &limit_params(3)),+ Err(VecDbError::Closed)+ ));+ assert!(matches!(+ survivor.bulk_search_stored(&["key-0".to_string()], 3, None, false, false),+ Err(VecDbError::Closed)+ ));assert!(matches!(survivor.add("x", &query), Err(VecDbError::Closed)));assert!(matches!(bulk_add(&survivor, &bulk_entries(0, 2, 401)),