Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(vector): compact the HNSW graph when overwrites leave it mostly dead (#2799)

xerj-org08b5a42c3d6f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a4f659fc2..8f77cb17d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -76,6 +76,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Re-indexing the same doc ids no longer grows `hnsw/graph.bin` without
+ bound. Each overwrite tombstoned the old graph node and nothing ever
+ removed one, so the graph (on disk and in memory) kept every vector ever
+ written: 87.7 MB for 1,000 live vectors. The graph save now compacts the
+ graph once dead nodes reach the live count (and at least 1,024): it
+ rebuilds from the live vectors off-lock, replays writes that land during
+ the rebuild, and swaps the result in, so the graph stays within about
+ twice the live set. (#2796)
+
- Code-extraction size skips now report exact file/limit byte counts and
explain that `--max-file-gb` / `--max-file-mb` / `--max-file-bytes` do not
raise the separate fixed 2 MiB AST source-file cap. The reason no longer
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index d8162813d..ee21d17c6 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -188,6 +188,15 @@ async fn await_publication_barrier<T>(rx: tokio::sync::oneshot::Receiver<T>, wha
}
}
+/// Dead HNSW nodes below this count are never worth a rebuild (#2796).
+const HNSW_COMPACT_MIN_DEAD: usize = 1024;
+
+/// Whether a graph with `live` and `dead` nodes should be compacted:
+/// once dead nodes are at least as many as live ones (#2796).
+fn hnsw_compaction_due(live: usize, dead: usize) -> bool {
+ dead >= HNSW_COMPACT_MIN_DEAD && dead >= live
+}
+
/// Heap held by the bucket arrays of the HNSW id maps (#2427). hashbrown
/// keeps one `(K, V)` slot and one control byte per bucket, and its bucket
/// count is the power of two above `capacity * 8 / 7`. The doc-id strings
@@ -8424,6 +8433,87 @@ mod semantic_deadline_regression_tests {
assert!(sync_idx.passage_chunks_require_exact("body_vector"));
}
+ /// #2796: re-indexing the same ids tombstoned each old node and nothing
+ /// ever removed one, so `graph.bin` held every vector ever written
+ /// (87.7 MB for 1,000 live 1024-d vectors after 20 rounds). The graph
+ /// save now compacts once dead nodes reach the live count, so the node
+ /// count and the file stay within about twice the live set.
+ #[tokio::test]
+ async fn overwriting_the_same_ids_keeps_the_hnsw_graph_bounded_2796() {
+ const LIVE: usize = 1200;
+ const ROUNDS: usize = 6;
+ let dir = TempDir::new().unwrap();
+ let engine_instance = engine(&dir);
+ let mut schema = Schema::empty();
+ let mut vector = FieldConfig::new("v", FieldType::Vector);
+ vector.options.dimensions = Some(8);
+ vector.options.similarity = Some("cosine".into());
+ schema.add_field(vector).unwrap();
+ engine_instance
+ .create_index("overwrite-hnsw", schema)
+ .unwrap();
+ let idx = engine_instance.get_index("overwrite-hnsw").unwrap();
+
+ let vector_for = |doc: usize, round: usize| -> Vec<f32> {
+ (0..8u64)
+ .map(|d| {
+ let seed = (round * LIVE + doc) as u64 * 8 + d + 1;
+ let h = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15);
+ ((h >> 40) % 2001) as f32 / 1000.0 - 1.0
+ })
+ .collect()
+ };
+ let mut first_graph_bytes = 0;
+ for round in 0..ROUNDS {
+ let docs: Vec<(String, Value)> = (0..LIVE)
+ .map(|doc| {
+ (
+ format!("doc-{doc}"),
+ serde_json::json!({ "text": "t", "v": vector_for(doc, round) }),
+ )
+ })
+ .collect();
+ idx.index_batch_turbo(docs, true, false).await.unwrap();
+ idx.save_hnsw_to_disk().await.unwrap();
+
+ let (live, dead) = {
+ let guard = idx.hnsw.read().await;
+ guard.as_ref().expect("graph built").live_and_dead_counts()
+ };
+ assert_eq!(live, LIVE, "round {round}");
+ assert!(
+ dead < LIVE.max(HNSW_COMPACT_MIN_DEAD),
+ "round {round}: {dead} dead nodes for {live} live ones"
+ );
+ let graph_bytes = std::fs::metadata(idx.hnsw_graph_path()).unwrap().len();
+ if round == 0 {
+ first_graph_bytes = graph_bytes;
+ }
+ assert!(
+ graph_bytes < first_graph_bytes * 5 / 2,
+ "round {round}: graph.bin {graph_bytes} bytes, {first_graph_bytes} after round 0"
+ );
+ }
+
+ // Ids and vectors survive the rebuilds: the id maps still resolve,
+ // the heap gauge tracks the compacted graph, and kNN finds every
+ // probe by its latest vector, never by an overwritten one.
+ assert_eq!(idx.hnsw_id_map.read().await.len(), LIVE);
+ assert_eq!(idx.vector_doc_count.load(Ordering::Relaxed), LIVE as u64);
+ let heap = idx.hnsw.read().await.as_ref().unwrap().heap_bytes();
+ assert_eq!(idx.hnsw_heap_bytes.load(Ordering::Relaxed), heap);
+ for probe in [0usize, 599, LIVE - 1] {
+ let hits = idx.knn_search(&vector_for(probe, ROUNDS - 1), 1).await;
+ assert_eq!(
+ hits.first().map(|h| h.0.as_str()),
+ Some(format!("doc-{probe}").as_str())
+ );
+ }
+ let saved = HnswIndex::load_from(&idx.hnsw_graph_path()).unwrap();
+ assert_eq!(saved.live_and_dead_counts().0, LIVE);
+ assert!(!idx.hnsw_stale.load(Ordering::Acquire));
+ }
+
/// #2427: a vector-bearing batch goes into the graph through one
/// parallel-planned `insert_batch`. Every doc must still get exactly one
/// live node, a doc id repeated inside the batch must tombstone its
@@ -18226,6 +18316,53 @@ impl Index {
Ok(false)
}
+ /// Rebuild the HNSW graph without its dead nodes once they are at least
+ /// as many as the live ones (#2796).
+ ///
+ /// Every overwrite or delete of a graphed doc tombstones its node, and
+ /// nothing else ever removed one: re-indexing the same 1,000 ids 20
+ /// times left a 87.7 MB `graph.bin` for 1,000 live vectors, and the
+ /// dead nodes' vectors and edges stayed in RSS as well. Rebuilding when
+ /// dead >= live keeps the graph within about twice its live size and
+ /// costs at most one extra insert per overwrite, amortised. The rebuild
+ /// runs on the deprioritised background pool while searches and inserts
+ /// keep using the old graph (`HnswIndex::compact`).
+ async fn maybe_compact_hnsw(&self) {
+ let hnsw = Arc::clone(&self.hnsw);
+ let heap_bytes = Arc::clone(&self.hnsw_heap_bytes);
+ let outcome = tokio::task::spawn_blocking(move || {
+ let guard = hnsw.blocking_read();
+ let graph = guard.as_ref()?;
+ let (live, dead) = graph.live_and_dead_counts();
+ if !hnsw_compaction_due(live, dead) {
+ return None;
+ }
+ let result = crate::background_pool().install(|| graph.compact());
+ heap_bytes.store(graph.heap_bytes(), Ordering::Relaxed);
+ Some(result)
+ })
+ .await;
+ match outcome {
+ Ok(Some(Ok(Some(stats)))) => info!(
+ index = self.name.as_str(),
+ nodes_before = stats.nodes_before,
+ nodes_after = stats.nodes_after,
+ "HNSW graph compacted"
+ ),
+ Ok(Some(Ok(None))) => debug!(
+ index = self.name.as_str(),
+ "HNSW compaction skipped: concurrent compaction or writes outpaced it"
+ ),
+ Ok(Some(Err(error))) => {
+ warn!(index = self.name.as_str(), %error, "HNSW compaction failed")
+ }
+ Err(error) => {
+ warn!(index = self.name.as_str(), %error, "HNSW compaction task failed")
+ }
+ Ok(None) => {}
+ }
+ }
+
/// True when `doc_id` has a graph node whose stored vector bit-matches
/// `vector` (rebuild fast path — no insert needed).
async fn hnsw_vector_current(&self, doc_id: &str, vector: &[f32]) -> bool {
@@ -21814,6 +21951,10 @@ impl Index {
/// `self.hnsw` is None (no vector field has been indexed yet).
/// Called on flush completion and graceful shutdown.
pub async fn save_hnsw_to_disk(&self) -> Result<()> {
+ // Drop dead nodes first so the file written below is the compacted
+ // graph (#2796). Ids and vectors are unchanged, so this cannot move
+ // the freshness stamp taken next.
+ self.maybe_compact_hnsw().await;
let publication_generation_before =
self.hnsw_publication_generation.load(Ordering::Acquire);
// This stamp brackets graph, identity-map, and passage-guard
diff --git a/engine/crates/xerj-vector/src/hnsw.rs b/engine/crates/xerj-vector/src/hnsw.rs
index 3990b2941..a8a333271 100644
--- a/engine/crates/xerj-vector/src/hnsw.rs
+++ b/engine/crates/xerj-vector/src/hnsw.rs
@@ -289,8 +289,79 @@ struct SlabGraphStorage {
/// Sum of `node_level` over all slots (layers above 0), for the O(1)
/// heap estimate in [`SlabGraphStorage::heap_bytes`].
upper_layer_slots: usize,
+ /// Ids mutated while [`HnswIndex::compact`] rebuilds this graph
+ /// (#2796); `None` when no compaction is running.
+ journal: Option<CompactionJournal>,
}
+/// Ids inserted or (un)tombstoned while a compaction rebuilds the graph
+/// (#2796). Recorded under the graph write lock, so every mutation the
+/// rebuild's snapshot missed is in here, and replayed onto the rebuilt
+/// graph before it replaces this one.
+#[derive(Default)]
+struct CompactionJournal {
+ inserted: Vec<u64>,
+ flagged: Vec<u64>,
+}
+
+impl CompactionJournal {
+ fn len(&self) -> usize {
+ self.inserted.len() + self.flagged.len()
+ }
+}
+
+/// One journaled id brought up to date on the rebuilt graph: its current
+/// vector when it was (re)inserted, and whether it is tombstoned now.
+struct CompactionReplay {
+ id: u64,
+ vector: Option<Vec<f32>>,
+ deleted: bool,
+}
+
+impl CompactionReplay {
+ fn collect(graph: &SlabGraphStorage, journal: &CompactionJournal) -> Vec<Self> {
+ let view = graph.read_view();
+ let inserted: HashSet<u64> = journal.inserted.iter().copied().collect();
+ let mut seen = HashSet::new();
+ journal
+ .inserted
+ .iter()
+ .chain(&journal.flagged)
+ .copied()
+ .filter(|id| seen.insert(*id))
+ .map(|id| CompactionReplay {
+ id,
+ vector: inserted
+ .contains(&id)
+ .then(|| view.current_slot(id).map(|slot| view.vector(slot).to_vec()))
+ .flatten(),
+ deleted: graph.tomb_ids.contains(&id),
+ })
+ .collect()
+ }
+}
+
+/// A node's external id and vector, as fed to [`HnswIndex::insert_batch`].
+type IdVector = (u64, Vec<f32>);
+
+/// What [`HnswIndex::compact`] did (#2796).
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct CompactStats {
+ /// Nodes before the rebuild, dead ones included.
+ pub nodes_before: usize,
+ /// Nodes in the rebuilt graph that replaced it.
+ pub nodes_after: usize,
+}
+
+/// A compaction swaps in the rebuilt graph once the mutations journaled
+/// since its last catch-up round are at most this many. The final round
+/// replays them serially under the write lock, so this bounds how long
+/// searches and inserts wait on the swap.
+const COMPACT_FINAL_ROUND_MAX: usize = 256;
+/// Unlocked catch-up rounds before a compaction under sustained writes
+/// gives up (the graph is left as it was; a later call tries again).
+const COMPACT_MAX_CATCH_UP_ROUNDS: usize = 8;
+
/// Read-only, slot-stable graph storage consumed by HNSW traversal and
/// persistence.
///
@@ -443,6 +514,7 @@ impl SlabGraphStorage {
entry_slot: None,
entry_layer: 0,
upper_layer_slots: 0,
+ journal: None,
}
}
@@ -986,10 +1058,10 @@ fn encode_graph_bytes(
/// A thread-safe HNSW approximate nearest-neighbor index.
///
/// Soft deletes: deleting a doc removes its entry from the caller's id
-/// maps but the node and its neighbour edges stay in the graph until a
-/// graph compaction (planned v0.7+). Tombstoned nodes are skipped as
-/// results but still traversed. Tombstones persist with the graph
-/// (format v2+).
+/// maps but the node and its neighbour edges stay in the graph until
+/// [`HnswIndex::compact`] rebuilds it from the live nodes (#2796).
+/// Tombstoned nodes are skipped as results but still traversed.
+/// Tombstones persist with the graph (format v2+).
pub struct HnswIndex {
params: HnswParams,
inner: RwLock<SlabGraphStorage>,
@@ -1026,13 +1098,16 @@ impl HnswIndex {
/// Mark `id` as deleted. Search will skip the node and never
/// return it as a hit. The node and its neighbour edges remain
- /// until a graph compaction (planned v0.7+). Idempotent.
+ /// until [`Self::compact`] (#2796). Idempotent.
pub fn mark_deleted(&self, id: u64) {
let mut g = self.inner.write().unwrap();
if let Some(&slot) = g.slot_of.get(&id) {
g.tomb[slot as usize] = true;
}
g.tomb_ids.insert(id);
+ if let Some(journal) = g.journal.as_mut() {
+ journal.flagged.push(id);
+ }
}
/// Reverse of `mark_deleted` — used in tests and on un-delete /
@@ -1043,6 +1118,9 @@ impl HnswIndex {
g.tomb[slot as usize] = false;
}
g.tomb_ids.remove(&id);
+ if let Some(journal) = g.journal.as_mut() {
+ journal.flagged.push(id);
+ }
}
/// True if `id` has been tombstoned via `mark_deleted`.
@@ -1060,6 +1138,136 @@ impl HnswIndex {
self.inner.read().unwrap().read_view().tombstone_count()
}
+ /// `(live, dead)` node counts (#2796). A node is dead when it is
+ /// tombstoned or was superseded by a later insert under the same id;
+ /// dead nodes keep their vector and edges in memory and in the saved
+ /// graph until [`Self::compact`] drops them. O(live).
+ pub fn live_and_dead_counts(&self) -> (usize, usize) {
+ let g = self.inner.read().unwrap();
+ let live = g
+ .slot_of
+ .values()
+ .filter(|&&slot| !g.tomb[slot as usize])
+ .count();
+ (live, g.ids.len() - live)
+ }
+
+ /// Rebuild the graph from its live nodes only, dropping every
+ /// tombstoned or superseded node and the tombstone list (#2796).
+ ///
+ /// External ids and vectors are unchanged, so callers' id maps stay
+ /// valid. The rebuild runs without holding the graph lock, planning
+ /// inserts in parallel on the caller's rayon pool; searches and
+ /// inserts continue against the old graph meanwhile. Mutations made
+ /// during the rebuild are journaled and replayed onto the new graph,
+ /// in unlocked rounds until at most `COMPACT_FINAL_ROUND_MAX` remain,
+ /// and the final round and the swap happen under the write lock.
+ ///
+ /// Peak memory is the old graph plus the new one (live nodes only).
+ /// Returns `Ok(None)` without changing anything when another
+ /// compaction is running, or when writes outpace the catch-up rounds.
+ /// Tombstones of dropped nodes are forgotten: re-inserting such an id
+ /// later makes it live, as on a graph that never held it.
+ pub fn compact(&self) -> Result<Option<CompactStats>> {
+ let Some((nodes_before, items)) = self.compact_begin() else {
+ return Ok(None);
+ };
+ let fresh = HnswIndex::new(self.params.clone());
+ let outcome = self.compact_into(&fresh, items, nodes_before);
+ if !matches!(outcome, Ok(Some(_))) {
+ self.inner.write().unwrap().journal = None;
+ }
+ outcome
+ }
+
+ /// Start journaling and snapshot the live nodes in slot order.
+ /// `None` when a compaction is already running.
+ fn compact_begin(&self) -> Option<(usize, Vec<IdVector>)> {
+ {
+ let mut g = self.inner.write().unwrap();
+ if g.journal.is_some() {
+ return None;
+ }
+ g.journal = Some(CompactionJournal::default());
+ }
+ // Journaling is on before the snapshot, so a write landing between
+ // the two is replayed (at worst twice, which leaves a dead node).
+ let g = self.inner.read().unwrap();
+ let view = g.read_view();
+ let items = (0..view.node_count() as u32)
+ .filter(|&slot| is_live_slot(&view, slot) && !view.tombstoned(slot))
+ .map(|slot| (view.external_id(slot), view.vector(slot).to_vec()))
+ .collect();
+ Some((view.node_count(), items))
+ }
+
+ fn compact_into(
+ &self,
+ fresh: &HnswIndex,
+ items: Vec<IdVector>,
+ nodes_before: usize,
+ ) -> Result<Option<CompactStats>> {
+ fresh.insert_batch(items)?;
+ for _ in 0..=COMPACT_MAX_CATCH_UP_ROUNDS {
+ let journal = {
+ let mut g = self.inner.write().unwrap();
+ let pending = g.journal.as_ref().map_or(0, CompactionJournal::len);
+ if pending <= COMPACT_FINAL_ROUND_MAX {
+ // Final round: serial inserts only. A rayon wait here
+ // could steal a job that needs this graph's lock.
+ let journal = g.journal.take().unwrap_or_default();
+ for replay in CompactionReplay::collect(&g, &journal) {
+ fresh.apply_replay(replay, false)?;
+ }
+ let slab = std::mem::replace(
+ &mut *fresh.inner.write().unwrap(),
+ SlabGraphStorage::new(self.params.dim, self.params.metric),
+ );
+ let nodes_after = slab.ids.len();
+ let old = std::mem::replace(&mut *g, slab);
+ drop(g);
+ drop(old);
+ debug!(nodes_before, nodes_after, "HNSW graph compacted");
+ return Ok(Some(CompactStats {
+ nodes_before,
+ nodes_after,
+ }));
+ }
+ std::mem::take(g.journal.as_mut().expect("journal checked above"))
+ };
+ let replays = {
+ let g = self.inner.read().unwrap();
+ CompactionReplay::collect(&g, &journal)
+ };
+ let (with_vectors, flags_only): (Vec<_>, Vec<_>) =
+ replays.into_iter().partition(|r| r.vector.is_some());
+ fresh.insert_batch(
+ with_vectors
+ .iter()
+ .map(|r| (r.id, r.vector.clone().expect("partitioned on vector")))
+ .collect(),
+ )?;
+ for replay in with_vectors.into_iter().chain(flags_only) {
+ fresh.apply_replay(replay, true)?;
+ }
+ }
+ Ok(None)
+ }
+
+ /// Bring one journaled id up to date on a graph being rebuilt by
+ /// [`Self::compact`]. `inserted` says the vector is already in.
+ fn apply_replay(&self, replay: CompactionReplay, inserted: bool) -> Result<()> {
+ if let Some(vector) = replay.vector.filter(|_| !inserted) {
+ self.insert(replay.id, vector)?;
+ }
+ if replay.deleted {
+ self.mark_deleted(replay.id);
+ } else {
+ self.unmark_deleted(replay.id);
+ }
+ Ok(())
+ }
+
/// True when node `id` exists and stores exactly `v` (bit-exact f32
/// compare). Used by the stale-graph rebuild (RC4 W2 item 17) to skip
/// re-inserting docs whose graph vector already matches the document
@@ -1174,6 +1382,9 @@ impl HnswIndex {
} = plan;
let mut g = self.inner.write().unwrap();
+ if let Some(journal) = g.journal.as_mut() {
+ journal.inserted.push(id);
+ }
let current_entry = g.entry_slot.map(|slot| (slot, g.entry_layer));
let grown = g.ids.len().saturating_sub(nodes);
let entry_rose =
@@ -2652,4 +2863,127 @@ mod tests {
);
}
}
+
+ fn spread_vec(seed: u64, dim: usize) -> Vec<f32> {
+ (0..dim as u64)
+ .map(|d| {
+ let h = (seed * dim as u64 + d + 1).wrapping_mul(0x9E37_79B9_7F4A_7C15);
+ ((h >> 40) % 2001) as f32 / 1000.0 - 1.0
+ })
+ .collect()
+ }
+
+ /// #2796: overwriting docs the way the engine does (a fresh node id
+ /// per write, the old node tombstoned) left every old vector in the
+ /// graph and in `graph.bin` forever. `compact` must drop them and keep
+ /// every live node findable under its id.
+ #[test]
+ fn compact_drops_dead_nodes_and_keeps_live_ones_2796() {
+ const DIM: usize = 16;
+ const LIVE: usize = 200;
+ const ROUNDS: u64 = 5;
+ let idx = make_index(DIM);
+ let mut next_id = 0u64;
+ let mut current: Vec<(u64, Vec<f32>)> = Vec::new();
+ for doc in 0..LIVE {
+ let v = spread_vec(doc as u64, DIM);
+ idx.insert(next_id, v.clone()).unwrap();
+ current.push((next_id, v));
+ next_id += 1;
+ }
+ let mut superseded = Vec::new();
+ for round in 1..=ROUNDS {
+ for (doc, slot) in current.iter_mut().enumerate() {
+ let v = spread_vec(round * 10_000 + doc as u64, DIM);
+ idx.mark_deleted(slot.0);
+ superseded.push((slot.0, slot.1.clone()));
+ idx.insert(next_id, v.clone()).unwrap();
+ *slot = (next_id, v);
+ next_id += 1;
+ }
+ }
+ // A same-id re-insert orphans the earlier slot, which is dead too.
+ idx.insert(current[0].0, current[0].1.clone()).unwrap();
+
+ let dead_before = LIVE * ROUNDS as usize + 1;
+ assert_eq!(idx.live_and_dead_counts(), (LIVE, dead_before));
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("graph.bin");
+ idx.save_to(&path).unwrap();
+ let bytes_before = std::fs::metadata(&path).unwrap().len();
+
+ let stats = idx.compact().unwrap().expect("no concurrent writer");
+ assert_eq!(stats.nodes_before, LIVE + dead_before);
+ assert_eq!(stats.nodes_after, LIVE);
+ assert_eq!(idx.live_and_dead_counts(), (LIVE, 0));
+ assert_eq!(idx.len(), LIVE);
+ assert_eq!(idx.tombstone_count(), 0);
+
+ idx.save_to(&path).unwrap();
+ let bytes_after = std::fs::metadata(&path).unwrap().len();
+ assert!(
+ bytes_after * 5 < bytes_before,
+ "graph.bin {bytes_before} -> {bytes_after} bytes for {LIVE} live of {} nodes",
+ LIVE + dead_before
+ );
+
+ for (id, v) in &current {
+ assert!(idx.vector_matches(*id, v));
+ assert_eq!(idx.search(v, 1, 64).unwrap()[0].0, *id);
+ }
+ for (id, v) in &superseded {
+ assert!(!idx.vector_matches(*id, v), "dead node {id} survived");
+ }
+ let reloaded = HnswIndex::load_from(&path).unwrap();
+ assert_eq!(reloaded.live_and_dead_counts(), (LIVE, 0));
+ let (id, v) = &current[LIVE / 2];
+ assert_eq!(reloaded.search(v, 1, 64).unwrap()[0].0, *id);
+ }
+
+ /// #2796: writes landing while `compact` rebuilds must not be lost.
+ /// More than `COMPACT_FINAL_ROUND_MAX` of them force an unlocked
+ /// catch-up round before the locked final one.
+ #[test]
+ fn compact_replays_writes_made_during_the_rebuild_2796() {
+ const DIM: usize = 16;
+ let idx = make_index(DIM);
+ for id in 0..300u64 {
+ idx.insert(id, spread_vec(id, DIM)).unwrap();
+ }
+ idx.mark_deleted(3);
+
+ let (nodes_before, items) = idx.compact_begin().expect("not running");
+ assert_eq!(items.len(), 299, "the snapshot skips the dead node");
+ // Writes made between the snapshot and the swap.
+ let added = 300..300 + COMPACT_FINAL_ROUND_MAX as u64 * 2;
+ for id in added.clone() {
+ idx.insert(id, spread_vec(id, DIM)).unwrap();
+ }
+ idx.mark_deleted(5);
+ idx.mark_deleted(400);
+ let moved = spread_vec(99_999, DIM);
+ idx.insert(7, moved.clone()).unwrap();
+ assert!(idx.compact().unwrap().is_none(), "one compaction at a time");
+
+ let fresh = HnswIndex::new(idx.params.clone());
+ let stats = idx
+ .compact_into(&fresh, items, nodes_before)
+ .unwrap()
+ .expect("catch-up converges without further writes");
+ assert_eq!(stats.nodes_before, 300);
+ assert!(idx.inner.read().unwrap().journal.is_none());
+
+ let live = 300 - 1 + added.clone().count() - 2;
+ assert_eq!(idx.live_and_dead_counts().0, live);
+ assert!(!idx.is_deleted(3), "dropped node's tombstone is gone");
+ assert!(idx.is_deleted(5) && idx.is_deleted(400));
+ assert!(idx.vector_matches(7, &moved));
+ assert_eq!(idx.search(&moved, 1, 64).unwrap()[0].0, 7);
+ for id in [0u64, 299, 300, 555, added.end - 1] {
+ assert_eq!(idx.search(&spread_vec(id, DIM), 1, 64).unwrap()[0].0, id);
+ }
+ for (id, _) in idx.search(&spread_vec(5, DIM), 10, 64).unwrap() {
+ assert!(id != 5 && id != 400, "tombstoned node {id} returned");
+ }
+ }
}
diff --git a/engine/crates/xerj-vector/src/lib.rs b/engine/crates/xerj-vector/src/lib.rs
index 302ce6c0a..6c16e59fc 100644
--- a/engine/crates/xerj-vector/src/lib.rs
+++ b/engine/crates/xerj-vector/src/lib.rs
@@ -15,7 +15,7 @@ pub mod search;
pub mod sq8_codes;
pub use distance::{compute_distance, cosine, dot_product, l2_squared, DistanceMetric};
-pub use hnsw::{HnswIndex, HnswParams};
+pub use hnsw::{CompactStats, HnswIndex, HnswParams};
pub use quantizer::{
NoneQuantizer, QuantizedData, QuantizedVectors, Quantizer, Scalar8Quantizer, Sq8Params,
};