Skip to main content

uqa_storage/ivf_index/
index.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Public vector-index contract for IVF.
8
9use std::sync::Arc;
10
11use uqa_core::{DocId, PostingList};
12
13use super::state::IVFIndex;
14use crate::vector_index::VectorIndex;
15use crate::StorageBackendResult;
16
17impl VectorIndex for IVFIndex {
18    fn dimensions(&self) -> u32 {
19        self.dimensions
20    }
21
22    fn index_kind(&self) -> &'static str {
23        "ivf"
24    }
25
26    fn add(&mut self, doc_id: DocId, vector: Vec<f32>) -> StorageBackendResult<()> {
27        self.replace_document_vectors(doc_id, vec![vector])
28    }
29
30    fn add_many(&mut self, doc_id: DocId, vectors: Vec<Vec<f32>>) -> StorageBackendResult<()> {
31        self.replace_document_vectors(doc_id, vectors)
32    }
33
34    fn delete(&mut self, doc_id: DocId) -> StorageBackendResult<()> {
35        self.delete_document(doc_id)
36    }
37
38    fn clear(&mut self) -> StorageBackendResult<()> {
39        self.clear_index();
40        Ok(())
41    }
42
43    fn search_knn(&self, query: &[f32], k: usize) -> StorageBackendResult<PostingList> {
44        self.search_top_k(query, k)
45    }
46
47    fn search_threshold(&self, query: &[f32], threshold: f32) -> StorageBackendResult<PostingList> {
48        self.search_above_threshold(query, threshold)
49    }
50
51    fn count(&self) -> StorageBackendResult<usize> {
52        Ok(self.vectors.lock().len())
53    }
54
55    fn initialize(&mut self) -> StorageBackendResult<()> {
56        self.train()
57    }
58
59    fn snapshot(&self) -> StorageBackendResult<Arc<dyn VectorIndex>> {
60        Ok(Arc::new(self.detached_clone()))
61    }
62
63    fn writable_snapshot(&self) -> StorageBackendResult<Box<dyn VectorIndex>> {
64        Ok(Box::new(self.detached_clone()))
65    }
66}