Skip to main content

uqa_storage_sqlite/vector_index/hnsw/
lifecycle.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! `VectorIndex` lifecycle implementation for persistent HNSW.
8
9use 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            // The retained native view already fixes canonical vectors and graph records. Loading the graph here would hydrate every captured index, including indexes the query never searches.
69            match snapshot.load_meta()? {
70                Some((dimensions, params, _, _)) => {
71                    snapshot.validate_header(dimensions, params)?;
72                }
73                None if self.require_persisted_graph => {
74                    return Err(super::mutation::missing_metadata(self));
75                }
76                None => {}
77            }
78            return Ok(Arc::new(snapshot));
79        }
80        if let Some(cached) = self.graph_snapshot()? {
81            cached.graph.snapshot()
82        } else if self.require_persisted_graph {
83            Err(super::mutation::missing_metadata(self))
84        } else {
85            self.persistent.snapshot()
86        }
87    }
88    fn vector_read_snapshot(
89        &self,
90        control: &uqa_storage::read_control::StorageReadControl,
91    ) -> StorageBackendResult<Option<uqa_storage::vector_index::VectorReadSnapshot>> {
92        self.persistent.vector_read_snapshot(control)
93    }
94}