weavatrix-search-vector 0.3.1

Persistent, mutable, bounded vector candidate search for Rust and Weavatrix
Documentation
use crate::config::IndexConfig;
use crate::error::SearchError;
use crate::hit::SearchHit;
use crate::parallel;
use crate::vector::VectorStore;
use std::sync::Arc;

/// Deterministic brute-force cosine oracle.
#[derive(Debug)]
pub struct ExactIndex {
    config: IndexConfig,
    vectors: Arc<VectorStore>,
}

impl ExactIndex {
    /// Validates and normalizes all vectors.
    ///
    /// # Errors
    ///
    /// Returns a typed configuration, vector, capacity, or allocation error.
    pub fn build(config: IndexConfig, vectors: &[(u64, &[f32])]) -> Result<Self, SearchError> {
        config.validate()?;
        let vectors = Arc::new(VectorStore::build(
            config.dimensions,
            config.metric,
            vectors,
        )?);
        Ok(Self { config, vectors })
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.vectors.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.vectors.is_empty()
    }

    #[must_use]
    pub const fn dimensions(&self) -> usize {
        self.config.dimensions
    }

    /// Returns exact top-K hits ordered by distance and then key.
    ///
    /// # Errors
    ///
    /// Returns a typed error for an invalid query.
    pub fn search(&self, query: &[f32], count: usize) -> Result<Vec<SearchHit>, SearchError> {
        self.vectors.exact(query, count)
    }

    /// Searches independent queries with bounded standard-library workers.
    ///
    /// Output order matches input order.
    ///
    /// # Errors
    ///
    /// Returns the first query error in input order or a worker-panic error.
    pub fn search_batch(
        &self,
        queries: &[&[f32]],
        count: usize,
    ) -> Result<Vec<Vec<SearchHit>>, SearchError> {
        parallel::search_batch(queries, self.config.query_threads, |query| {
            self.search(query, count)
        })
    }
}