Table of Contents

Internal classSealedInternal HnswGraph

Namespace
Rowles.LeanLucene.Codecs.Hnsw
Assembly
Rowles.LeanLucene.dll

In-memory hierarchical navigable small world graph. Vectors are expected to be L2-normalised so that dot product equals cosine similarity; the graph itself works in distance space (lower is better) with distance defined as negative dot product.

internal sealed class HnswGraph
HnswGraph

Remarks

The graph supports two lifecycle states. While mutable (the default after construction) only single-threaded use is permitted: Insert(int) mutates internal adjacency lists without locks. Once Freeze() has been called, search becomes thread-safe and lock-free; mutation is no longer permitted.

Pruning uses the diversity-preserving heuristic from the original HNSW paper rather than the simple top-M variant, which gives materially better recall on clustered embedding spaces typical of real-world workloads.

Constructors

Public constructor HnswGraph(IVectorSource, HnswBuildConfig, long)

Properties

Public propertyRead-only Dimension

Vector dimension; matches Dimension.

Public propertyRead-only EfConstruction

Candidate set size used during graph construction.

Public propertyRead-only IsReadOnly

True once Freeze() has been called; mutation is prohibited and search is thread-safe.

Internal propertyRead-onlyInternal LevelCount

Number of layers including layer zero.

Public propertyRead-only M

Maximum neighbours per node on layers above zero.

Public propertyRead-only M0

Maximum neighbours per node on layer zero (typically 2 * M).

Public propertyRead-only Seed

Seed used by the random number generator. Persisted to the .hnsw file for reproducibility.

Methods

Internal methodInternal ContainsNode(int)

True if a node with the given id is already present at layer 0.

Public method Freeze()

Marks the graph as immutable. Required before search can be called concurrently.

Internal methodStaticInternal FromFrozen(IVectorSource, HnswBuildConfig, long, List<Dictionary<int, int[]>>, int, int, int)

Builds an HnswGraph from pre-loaded adjacency. Used by the reader to materialise a graph from a .hnsw file. The graph is created in the read-only state immediately.

Internal methodInternal GetNeighbours(int, int)

Returns the neighbours of a node at a given layer. Used by the writer.

Internal methodInternal GetNodesAtLevel(int)

Enumerates every document identifier present at a given layer. Used by the writer.

Public method Insert(int)

Inserts a node into the graph. The node's vector must already be present in the source.

Public method Search(ReadOnlySpan<float>, HnswSearchOptions)

Searches the graph for the closest documents to a query vector. Safe for concurrent callers once Freeze() has been called.

Internal methodInternal Search(ReadOnlySpan<float>, HnswSearchOptions, out HnswSearchStats)

Searches the graph and returns per-call statistics for diagnostics and metrics.

Internal methodInternal Thaw()

Reverses Freeze(): copies frozen adjacency back into the mutable structure so further Insert(int) calls are permitted. Used by incremental merge to seed a new graph from the largest input segment's existing graph.