Skip to main content

HnswIndex

Struct HnswIndex 

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

Hierarchical Navigable Small World graph index.

  • FP32 construction for structural integrity
  • Heuristic neighbor selection (Algorithm 4)
  • Beam search with configurable ef parameter

Implementations§

Source§

impl HnswIndex

Source

pub fn insert(&mut self, vector: Vec<f32>) -> Result<(), VectorError>

Insert a vector into the index.

  1. Assign a random layer using the exponential distribution
  2. Greedily descend from the entry point to the new node’s layer + 1
  3. At each layer from the node’s layer down to 0, search for nearest neighbors, select via the diversity heuristic, and add bidirectional edges
  4. Prune over-connected nodes to maintain the M/M0 invariant
Source§

impl HnswIndex

Source

pub fn checkpoint_to_bytes(&self) -> Vec<u8>

Serialize the index to rkyv bytes (with magic header) for storage.

Magic header RKHNS\0 allows from_checkpoint to detect format.

Source

pub fn from_checkpoint(bytes: &[u8]) -> Option<Self>

Restore an index from a checkpoint snapshot.

Auto-detects format: rkyv (magic RKHNS\0) or legacy MessagePack.

Source§

impl HnswIndex

Source

pub fn new(dim: usize, params: HnswParams) -> Self

Create a new empty HNSW index.

Source

pub fn with_seed(dim: usize, params: HnswParams, seed: u64) -> Self

Create with a specific RNG seed (for deterministic testing).

Source

pub fn len(&self) -> usize

Source

pub fn live_count(&self) -> usize

Source

pub fn tombstone_count(&self) -> usize

Source

pub fn tombstone_ratio(&self) -> f64

Tombstone ratio: fraction of nodes that are deleted.

Source

pub fn is_empty(&self) -> bool

Source

pub fn delete(&mut self, id: u32) -> bool

Soft-delete a vector by internal node ID.

Source

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

Source

pub fn undelete(&mut self, id: u32) -> bool

Source

pub fn dim(&self) -> usize

Source

pub fn get_vector(&self, id: u32) -> Option<&[f32]>

Source

pub fn params(&self) -> &HnswParams

Source

pub fn entry_point(&self) -> Option<u32>

Source

pub fn max_layer(&self) -> usize

Source

pub fn rng_state(&self) -> u64

Current RNG state (for snapshot reproducibility).

Source

pub fn memory_usage_bytes(&self) -> usize

Approximate memory usage in bytes (vector data + neighbor lists).

Source

pub fn export_vectors(&self) -> Vec<Vec<f32>>

Export all vectors for snapshot transfer.

Source

pub fn export_neighbors(&self) -> Vec<Vec<Vec<u32>>>

Export all neighbor lists for snapshot transfer.

Source

pub fn compact(&mut self) -> usize

Compact the index by removing all tombstoned nodes.

Returns the number of removed nodes. See compact_with_map for the variant that also returns the old→new id remapping.

Source

pub fn compact_with_map(&mut self) -> (usize, Vec<u32>)

Compact and return both the removed count and the old→new id map.

id_map[old_local] = new_local, or u32::MAX if the node was tombstoned (removed).

Source§

impl HnswIndex

Source

pub fn search(&self, query: &[f32], k: usize, ef: usize) -> Vec<SearchResult>

K-NN search: find the k closest vectors to query.

ef controls the search beam width (higher = better recall, slower). Must be >= k. Typical values: ef = 2k to 10k.

Source

pub fn search_filtered( &self, query: &[f32], k: usize, ef: usize, filter: &RoaringBitmap, ) -> Vec<SearchResult>

Filtered K-NN search with Roaring bitmap pre-filtering.

Source

pub fn search_filtered_offset( &self, query: &[f32], k: usize, ef: usize, filter: &RoaringBitmap, id_offset: u32, ) -> Vec<SearchResult>

Filtered K-NN search where the bitmap is keyed in a shifted ID space.

id_offset is added to local node IDs before testing filter.contains. Used by multi-segment collections where the bitmap holds GLOBAL ids and each segment’s HNSW nodes are numbered starting at base_id.

Source

pub fn search_with_bitmap_bytes( &self, query: &[f32], k: usize, ef: usize, bitmap_bytes: &[u8], ) -> Vec<SearchResult>

Deserialize a Roaring bitmap from bytes and perform filtered search.

Source

pub fn search_with_bitmap_bytes_offset( &self, query: &[f32], k: usize, ef: usize, bitmap_bytes: &[u8], id_offset: u32, ) -> Vec<SearchResult>

Deserialize a Roaring bitmap and search with an ID offset applied before testing membership. See search_filtered_offset for rationale.

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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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, 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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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