mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 07:16:17 +00:00
fix(search): use nestable SAVEPOINT in store_embeddings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3266ddda3a
commit
2931d063f3
2 files changed
+56
-3
No files matched your search
@@ -467,14 +467,29 @@ impl Search {
|
||||
model_id: &str,
|
||||
items: &[(PendingEmbedding, Vec<f32>)],
|
||||
) -> 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<f32>)],
|
||||
) -> 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<u8> = 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(())
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
Reference in new issue
Block a user