From 2931d063f38f9d72f8634477fbb2ee21636e8ca0 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Wed, 21 Oct 2026 14:20:36 +0200 Subject: [PATCH] fix(search): use nestable SAVEPOINT in store_embeddings Co-Authored-By: Claude Sonnet 5.5 --- crates/synapse-search/src/lib.rs | 20 +++++++++++--- crates/synapse-search/tests/search.rs | 39 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/synapse-search/src/lib.rs b/crates/synapse-search/src/lib.rs index f4d7a93..8a4b479 100644 --- a/crates/synapse-search/src/lib.rs +++ b/crates/synapse-search/src/lib.rs @@ -467,14 +467,29 @@ impl Search { model_id: &str, items: &[(PendingEmbedding, Vec)], ) -> Result<()> { - let tx = graph.conn().unchecked_transaction()?; + graph.conn().execute_batch("SAVEPOINT search_store")?; + let r = self.store_rows(graph, model_id, items); + graph.conn().execute_batch(if r.is_ok() { + "RELEASE search_store" + } else { + "ROLLBACK TO search_store; RELEASE search_store" + })?; + r + } + + fn store_rows( + &self, + graph: &Graph, + model_id: &str, + items: &[(PendingEmbedding, Vec)], + ) -> Result<()> { self.ensure_loaded(graph, model_id)?; let mut vectors = self.vectors.borrow_mut(); let mv = vectors.get_mut(model_id).expect("loaded"); for (p, v) in items { let (q, scale) = quantize(&normalize(v)); let bytes: Vec = q.iter().map(|&x| x as u8).collect(); - tx.execute( + graph.conn().execute( "INSERT OR REPLACE INTO search_vectors (id, model_id, kind, page_id, stamp, dims, scale, vec) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)", @@ -495,7 +510,6 @@ impl Search { }; ix.upsert_quantized(p.id, p.page_id, p.stamp, &q, scale); } - tx.commit()?; Ok(()) } diff --git a/crates/synapse-search/tests/search.rs b/crates/synapse-search/tests/search.rs index 3713515..5b7945b 100644 --- a/crates/synapse-search/tests/search.rs +++ b/crates/synapse-search/tests/search.rs @@ -292,3 +292,42 @@ fn unlinked_references_and_apply() { 1 ); } + +#[test] +fn store_inside_graph_transaction_and_rename_delete() { + let (_d, g) = setup(); + let rust = g.ensure_page("Rust", None).unwrap(); + let b = add(&g, "Notes", "I like [[Rust]] a lot"); + let mut s = Search::open(&g).unwrap(); + s.sync(&g).unwrap(); + let emb = HashEmbedder::new(64); + // Nested inside a core transaction: must not fail with BEGIN-in-transaction. + g.transaction(|g| { + s.embed_pending(g, &emb, 10, 100).unwrap(); + Ok(()) + }) + .unwrap(); + assert_eq!(s.vector_counts(&g, "test/hash").unwrap().1, 1); + + // Rename rewrites the referencing block: index follows. + g.rename_page(rust.id, "Ferris").unwrap(); + s.sync(&g).unwrap(); + let f = Filter::default(); + assert_eq!( + s.search(&g, "ferris", &f, 10) + .unwrap() + .iter() + .filter(|h| h.id == b.id) + .count(), + 1 + ); + assert!(s.search(&g, "rust", &f, 10).unwrap().is_empty()); + + // Deleting the page removes its docs and vectors; deleting a block drops its vector. + s.embed_pending(&g, &emb, 10, 100).unwrap(); + g.delete_block(b.id).unwrap(); + g.delete_page(rust.id).unwrap(); + s.sync(&g).unwrap(); + assert!(s.search(&g, "ferris", &f, 10).unwrap().is_empty()); + assert_eq!(s.vector_counts(&g, "test/hash").unwrap(), (1, 0)); +}