weavatrix-search-vector 0.3.1

Persistent, mutable, bounded vector candidate search for Rust and Weavatrix
Documentation
mod lifecycle;
mod mutations;
mod rebuild;
mod search;
mod support;

use crate::config::IndexConfig;
use crate::hnsw::VectorIndex;
use crate::metadata::{Metadata, MetadataIndex};
use crate::simd::DistanceKernel;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Arc, RwLock};

/// Owned vector plus optional filter metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct VectorRecord {
    pub key: u64,
    pub vector: Vec<f32>,
    pub metadata: Metadata,
}

impl VectorRecord {
    #[must_use]
    pub fn new(key: u64, vector: Vec<f32>) -> Self {
        Self {
            key,
            vector,
            metadata: Metadata::new(),
        }
    }
}

/// Result of an upsert operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MutationOutcome {
    Inserted,
    Updated,
}

/// Thread-safe mutable overlay over an immutable HNSW base.
///
/// Inserts and updates are searched exactly from a bounded delta. Deletions
/// are tombstones. [`Self::compact`] deterministically folds both into a new
/// immutable base without making readers observe a partial rebuild.
#[derive(Debug)]
pub struct MutableVectorIndex {
    pub(super) config: IndexConfig,
    pub(super) state: RwLock<MutableState>,
    pub(super) distance_kernel: DistanceKernel,
}

#[derive(Debug)]
pub(super) struct MutableState {
    pub(super) base: Arc<VectorIndex>,
    pub(super) sealed: Option<Arc<VectorIndex>>,
    pub(super) pending: BTreeMap<u64, Vec<f32>>,
    pub(super) deleted: BTreeSet<u64>,
    pub(super) metadata: MetadataIndex,
    pub(super) generation: u64,
}

pub(crate) struct MutableSnapshot {
    pub(crate) config: IndexConfig,
    pub(crate) base: Arc<VectorIndex>,
    pub(crate) sealed: Option<Arc<VectorIndex>>,
    pub(crate) pending: BTreeMap<u64, Vec<f32>>,
    pub(crate) deleted: BTreeSet<u64>,
    pub(crate) metadata: MetadataIndex,
}