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: usizeHighest 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
impl HnswIndex
Sourcepub fn new(distance: DistanceMetric, seed: u64) -> Self
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.
Sourcepub fn serialize_nodes(&self) -> Vec<(i64, Vec<Vec<i64>>)>
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.
Sourcepub fn from_persisted_nodes<I>(
distance: DistanceMetric,
seed: u64,
nodes: I,
) -> Self
pub fn from_persisted_nodes<I>( distance: DistanceMetric, seed: u64, nodes: I, ) -> Self
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).