uqa_storage_sqlite/vector_index/ivf/
lifecycle.rs1use std::sync::Arc;
10
11use uqa_core::{DocId, PostingList};
12
13use super::writing::drop_metadata;
14use super::SQLiteIVFIndex;
15use crate::vector_index::SQLiteVectorIndex;
16use crate::{ManagedConnection, Result as SQLiteResult};
17use uqa_storage::vector_index::{IVFIndexParams, VectorIndex};
18use uqa_storage::StorageBackendResult;
19
20impl SQLiteIVFIndex {
21 pub fn new(
22 conn: ManagedConnection,
23 table: impl Into<String>,
24 field: impl Into<String>,
25 dimensions: u32,
26 ) -> Self {
27 Self::from_params(conn, table, field, dimensions, IVFIndexParams::default())
28 }
29
30 pub fn with_params(
31 conn: ManagedConnection,
32 table: impl Into<String>,
33 field: impl Into<String>,
34 dimensions: u32,
35 nlist: usize,
36 nprobe: usize,
37 train_threshold: usize,
38 ) -> Self {
39 Self::from_params(
40 conn,
41 table,
42 field,
43 dimensions,
44 IVFIndexParams {
45 nlist: nlist.max(1),
46 nprobe: nprobe.max(1),
47 train_threshold: train_threshold.max(1),
48 },
49 )
50 }
51
52 fn from_params(
53 conn: ManagedConnection,
54 table: impl Into<String>,
55 field: impl Into<String>,
56 dimensions: u32,
57 params: IVFIndexParams,
58 ) -> Self {
59 Self {
60 persistent: SQLiteVectorIndex::new(conn, table, field, dimensions),
61 params,
62 native_state: Arc::default(),
63 }
64 }
65
66 pub fn open_existing(
67 conn: ManagedConnection,
68 table: impl Into<String>,
69 field: impl Into<String>,
70 dimensions: u32,
71 nlist: usize,
72 nprobe: usize,
73 train_threshold: usize,
74 ) -> Self {
75 Self::with_params(
76 conn,
77 table,
78 field,
79 dimensions,
80 nlist,
81 nprobe,
82 train_threshold,
83 )
84 }
85
86 pub fn drop_metadata(conn: &ManagedConnection, table: &str, field: &str) -> SQLiteResult<()> {
87 drop_metadata(conn, table, field)
88 }
89}
90
91impl VectorIndex for SQLiteIVFIndex {
92 fn contains_document(&self, doc_id: DocId) -> StorageBackendResult<bool> {
93 self.persistent.contains_document(doc_id)
94 }
95
96 fn dimensions(&self) -> u32 {
97 self.persistent.dimensions
98 }
99
100 fn index_kind(&self) -> &'static str {
101 "sqlite-ivf"
102 }
103
104 fn physical_configuration(&self) -> Option<uqa_storage::VectorIndexSpec> {
105 Some(uqa_storage::VectorIndexSpec::IVF(self.params))
106 }
107
108 fn add(&mut self, doc_id: DocId, vector: Vec<f32>) -> StorageBackendResult<()> {
109 self.replace_document(doc_id, vec![vector])
110 }
111
112 fn add_many(&mut self, doc_id: DocId, vectors: Vec<Vec<f32>>) -> StorageBackendResult<()> {
113 self.replace_document(doc_id, vectors)
114 }
115
116 fn delete(&mut self, doc_id: DocId) -> StorageBackendResult<()> {
117 self.delete_document(doc_id)
118 }
119
120 fn clear(&mut self) -> StorageBackendResult<()> {
121 self.clear_index()
122 }
123
124 fn search_knn(&self, query: &[f32], k: usize) -> StorageBackendResult<PostingList> {
125 self.search_top_k(query, k)
126 }
127
128 fn search_threshold(&self, query: &[f32], threshold: f32) -> StorageBackendResult<PostingList> {
129 self.persistent.search_threshold(query, threshold)
130 }
131
132 fn count(&self) -> StorageBackendResult<usize> {
133 self.persistent.count()
134 }
135
136 fn initialize(&mut self) -> StorageBackendResult<()> {
137 self.initialize_metadata()
138 }
139
140 fn snapshot(&self) -> StorageBackendResult<Arc<dyn VectorIndex>> {
141 Ok(Arc::new(Self {
142 persistent: self.persistent.retained_snapshot()?,
143 params: self.params,
144 native_state: Arc::default(),
145 }))
146 }
147 fn vector_read_snapshot(
148 &self,
149 control: &uqa_storage::read_control::StorageReadControl,
150 ) -> StorageBackendResult<Option<uqa_storage::vector_index::VectorReadSnapshot>> {
151 self.persistent.vector_read_snapshot(control)
152 }
153}