Skip to main content

weavatrix_search_vector/
exact.rs

1use crate::config::IndexConfig;
2use crate::error::SearchError;
3use crate::hit::SearchHit;
4use crate::parallel;
5use crate::vector::VectorStore;
6use std::sync::Arc;
7
8/// Deterministic brute-force cosine oracle.
9#[derive(Debug)]
10pub struct ExactIndex {
11    config: IndexConfig,
12    vectors: Arc<VectorStore>,
13}
14
15impl ExactIndex {
16    /// Validates and normalizes all vectors.
17    ///
18    /// # Errors
19    ///
20    /// Returns a typed configuration, vector, capacity, or allocation error.
21    pub fn build(config: IndexConfig, vectors: &[(u64, &[f32])]) -> Result<Self, SearchError> {
22        config.validate()?;
23        let vectors = Arc::new(VectorStore::build(
24            config.dimensions,
25            config.metric,
26            vectors,
27        )?);
28        Ok(Self { config, vectors })
29    }
30
31    #[must_use]
32    pub fn len(&self) -> usize {
33        self.vectors.len()
34    }
35
36    #[must_use]
37    pub fn is_empty(&self) -> bool {
38        self.vectors.is_empty()
39    }
40
41    #[must_use]
42    pub const fn dimensions(&self) -> usize {
43        self.config.dimensions
44    }
45
46    /// Returns exact top-K hits ordered by distance and then key.
47    ///
48    /// # Errors
49    ///
50    /// Returns a typed error for an invalid query.
51    pub fn search(&self, query: &[f32], count: usize) -> Result<Vec<SearchHit>, SearchError> {
52        self.vectors.exact(query, count)
53    }
54
55    /// Searches independent queries with bounded standard-library workers.
56    ///
57    /// Output order matches input order.
58    ///
59    /// # Errors
60    ///
61    /// Returns the first query error in input order or a worker-panic error.
62    pub fn search_batch(
63        &self,
64        queries: &[&[f32]],
65        count: usize,
66    ) -> Result<Vec<Vec<SearchHit>>, SearchError> {
67        parallel::search_batch(queries, self.config.query_threads, |query| {
68            self.search(query, count)
69        })
70    }
71}