uqa_storage_sqlite/vector_index/hnsw/
lifecycle.rs1use std::sync::Arc;
10
11use uqa_core::{DocId, PostingList};
12
13use super::SQLiteHNSWIndex;
14use uqa_storage::vector_index::VectorIndex;
15use uqa_storage::StorageBackendResult;
16
17impl VectorIndex for SQLiteHNSWIndex {
18 fn contains_document(&self, doc_id: DocId) -> StorageBackendResult<bool> {
19 self.persistent.contains_document(doc_id)
20 }
21
22 fn dimensions(&self) -> u32 {
23 self.persistent.dimensions
24 }
25
26 fn index_kind(&self) -> &'static str {
27 "hnsw"
28 }
29
30 fn physical_configuration(&self) -> Option<uqa_storage::VectorIndexSpec> {
31 Some(uqa_storage::VectorIndexSpec::HNSW(self.params))
32 }
33
34 fn add(&mut self, doc_id: DocId, vector: Vec<f32>) -> StorageBackendResult<()> {
35 self.add_many(doc_id, vec![vector])
36 }
37
38 fn add_many(&mut self, doc_id: DocId, vectors: Vec<Vec<f32>>) -> StorageBackendResult<()> {
39 self.replace_document(doc_id, vectors)
40 }
41
42 fn delete(&mut self, doc_id: DocId) -> StorageBackendResult<()> {
43 self.delete_document(doc_id)
44 }
45
46 fn clear(&mut self) -> StorageBackendResult<()> {
47 self.clear_graph()
48 }
49
50 fn search_knn(&self, query: &[f32], k: usize) -> StorageBackendResult<PostingList> {
51 self.search_top_k(query, k)
52 }
53
54 fn search_threshold(&self, query: &[f32], threshold: f32) -> StorageBackendResult<PostingList> {
55 self.persistent.search_threshold(query, threshold)
56 }
57
58 fn count(&self) -> StorageBackendResult<usize> {
59 self.persistent.count()
60 }
61
62 fn initialize(&mut self) -> StorageBackendResult<()> {
63 self.initialize_graph()
64 }
65
66 fn snapshot(&self) -> StorageBackendResult<Arc<dyn VectorIndex>> {
67 if let Some(snapshot) = self.native_snapshot()? {
68 let graph = snapshot
69 .persistent
70 .read_native(|read| snapshot.cached_native_graph(read))?
71 .flatten();
72 if graph.is_none() && self.require_persisted_graph {
73 return Err(super::mutation::missing_metadata(self));
74 }
75 return Ok(Arc::new(snapshot));
76 }
77 if let Some(cached) = self.graph_snapshot()? {
78 cached.graph.snapshot()
79 } else if self.require_persisted_graph {
80 Err(super::mutation::missing_metadata(self))
81 } else {
82 self.persistent.snapshot()
83 }
84 }
85 fn vector_read_snapshot(
86 &self,
87 control: &uqa_storage::read_control::StorageReadControl,
88 ) -> StorageBackendResult<Option<uqa_storage::vector_index::VectorReadSnapshot>> {
89 self.persistent.vector_read_snapshot(control)
90 }
91}