Skip to main content

HnswIndex

Struct HnswIndex 

Source
pub struct HnswIndex {
    pub params: HnswParams,
    pub distance: DistanceMetric,
    pub entry_point: Option<i64>,
    pub top_layer: usize,
    pub nodes: HashMap<i64, Node>,
    /* private fields */
}
Expand description

In-memory HNSW graph. See module docs for the model.

Fields§

§params: HnswParams§distance: DistanceMetric§entry_point: Option<i64>

Node id of the entry point. None iff the index is empty. At all times this is the id of the node with the highest max-layer; if multiple nodes tie for the top layer, the most-recently-promoted one wins.

§top_layer: usize

Highest layer currently populated. 0 when the index has at most one node, grows as new nodes get assigned higher layers.

§nodes: HashMap<i64, Node>

Node id → its per-layer neighbor lists.

Implementations§

Source§

impl HnswIndex

Source

pub fn new(distance: DistanceMetric, seed: u64) -> Self

Builds an empty HNSW index with default parameters and the given distance metric + RNG seed. A seed of 0 is mapped to a small nonzero constant — xorshift gets stuck at zero.

Source

pub fn is_empty(&self) -> bool

True if no nodes have been inserted yet.

Source

pub fn len(&self) -> usize

Number of nodes currently in the index.

Source

pub fn serialize_nodes(&self) -> Vec<(i64, Vec<Vec<i64>>)>

Phase 7d.3 — produces (node_id, layers) pairs in ascending node_id order, suitable for serializing the graph to disk via the HnswNodeCell wire format. The graph’s metadata (entry_point + top_layer) is recoverable from the nodes alone: top_layer = max(max_layer); entry_point = any node at top_layer. So we don’t ship a separate metadata cell.

Source

pub fn from_persisted_nodes<I>( distance: DistanceMetric, seed: u64, nodes: I, ) -> Self
where I: IntoIterator<Item = (i64, Vec<Vec<i64>>)>,

Phase 7d.3 — rebuilds an HnswIndex from a stream of (node_id, layers) pairs as produced by serialize_nodes and round-tripped through HnswNodeCell encode/decode. The rebuilt index has the same nodes, same neighbor lists, same entry_point + top_layer as the source. seed is fresh; the deserialized index is never inserted into via the algorithmic insert path so the seed only matters if a caller later calls insert after deserializing (then it controls layer assignment for the appended node).

Source

pub fn insert<F>(&mut self, node_id: i64, vec: &[f32], get_vec: F)
where F: Fn(i64) -> Vec<f32>,

Inserts a node into the graph. The node id must be unique; re-inserting an existing id is a no-op (returns without error). vec is the new node’s vector; get_vec looks up the vector for any other node id the algorithm touches.

Source

pub fn search<F>(&self, query: &[f32], k: usize, get_vec: F) -> Vec<i64>
where F: Fn(i64) -> Vec<f32>,

Returns the k nearest node ids to query, in distance-ascending order (closest first). Empty index returns an empty Vec.

Trait Implementations§

Source§

impl Clone for HnswIndex

Source§

fn clone(&self) -> HnswIndex

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HnswIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.