Skip to main content

HnswIndex

Struct HnswIndex 

Source
pub struct HnswIndex { /* private fields */ }
Expand description

HNSW vector index backed by hnsw_rs.

Each collective gets its own HnswIndex instance, providing complete data isolation between collectives.

§Persistence Strategy

Metadata (ID mappings, deleted set) is persisted to a JSON .hnsw.meta file. The graph itself is rebuilt from redb embeddings on open, because hnsw_rs::HnswIo::load_hnsw has lifetime constraints that create self-referential struct issues. The graph dump files (via file_dump) are saved for future optimization but not currently loaded.

Implementations§

Source§

impl HnswIndex

Source

pub fn new(dimension: usize, config: &HnswConfig) -> Self

Creates a new empty HNSW index.

§Arguments
  • dimension - Expected embedding dimension (validated on insert)
  • config - HNSW tuning parameters
Source

pub fn insert_experience( &self, exp_id: ExperienceId, embedding: &[f32], ) -> Result<()>

Inserts an experience embedding into the index.

Assigns a new internal usize ID and records the mapping. If the ExperienceId is already present, this is a no-op.

Source

pub fn delete_experience(&self, exp_id: ExperienceId) -> Result<()>

Marks an experience as deleted in the index.

The vector remains in the graph but is excluded from search results via filtered search. Returns Ok even if the experience is not in the index (idempotent).

Source

pub fn search_experiences( &self, query: &[f32], k: usize, ef_search: usize, ) -> Result<Vec<(ExperienceId, f32)>>

Searches for the k nearest experiences, excluding deleted ones.

Returns (ExperienceId, distance) pairs sorted by distance ascending (closest first). Distance is cosine distance: 0.0 = identical, 2.0 = opposite.

Source

pub fn contains(&self, exp_id: ExperienceId) -> bool

Returns true if the given experience is in the index (and not deleted).

Source

pub fn active_count(&self) -> usize

Returns the number of active (non-deleted) vectors.

Source

pub fn total_count(&self) -> usize

Returns the total number of vectors (including deleted).

Source

pub fn restore_deleted_set(&self, deleted_exp_ids: &[String]) -> Result<()>

Restores the deleted set from persisted metadata.

Called during PulseDB::open() after rebuilding the graph from redb. Accepts ExperienceId UUID strings and maps them to the current internal IDs (which may differ from the previous session’s IDs after a rebuild).

Source

pub fn save_to_dir(&self, dir: &Path, name: &str) -> Result<()>

Saves index metadata to a JSON file.

Creates {dir}/{name}.hnsw.meta with ID mappings and deleted set. Also attempts to save the HNSW graph via file_dump for future optimization (graph loading is not yet implemented due to lifetime constraints in hnsw_rs).

Source

pub fn rebuild_from_embeddings( dimension: usize, config: &HnswConfig, embeddings: Vec<(ExperienceId, Vec<f32>)>, ) -> Result<Self>

Rebuilds an index from a set of embeddings.

Used during PulseDB::open() to reconstruct the HNSW graph from embeddings stored in redb (the source of truth).

Source

pub fn remove_files(dir: &Path, name: &str) -> Result<()>

Removes HNSW files for a collective from disk.

Trait Implementations§

Source§

impl VectorIndex for HnswIndex

Source§

fn insert(&self, id: usize, embedding: &[f32]) -> Result<()>

Inserts a single vector with the given ID.
Source§

fn insert_batch(&self, items: &[(&Vec<f32>, usize)]) -> Result<()>

Inserts a batch of vectors. Read more
Source§

fn search( &self, query: &[f32], k: usize, ef_search: usize, ) -> Result<Vec<(usize, f32)>>

Searches for the k nearest neighbors to the query vector. Read more
Source§

fn search_filtered( &self, query: &[f32], k: usize, ef_search: usize, filter: &(dyn Fn(&usize) -> bool + Sync), ) -> Result<Vec<(usize, f32)>>

Searches with a filter predicate applied during traversal. Read more
Source§

fn delete(&self, id: usize) -> Result<()>

Marks an ID as deleted (soft-delete). Read more
Source§

fn is_deleted(&self, id: usize) -> bool

Returns true if the given ID is marked as deleted.
Source§

fn len(&self) -> usize

Returns the number of active (non-deleted) vectors.
Source§

fn save(&self, dir: &Path, name: &str) -> Result<()>

Persists index metadata to disk.
Source§

fn is_empty(&self) -> bool

Returns true if the index has no active vectors.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more