Skip to main content

HnswConfigBuilder

Struct HnswConfigBuilder 

Source
pub struct HnswConfigBuilder {
    pub config: HnswConfig,
}
Expand description

Builder pattern for HnswConfig construction

Provides a fluent interface for creating HnswConfig instances with validation and sensible defaults.

§Examples

use sqlitegraph::hnsw::{HnswConfig, DistanceMetric};

// Single-layer configuration (default, backward compatible)
let config = HnswConfigBuilder::new()
    .dimension(512)
    .m_connections(24)
    .ef_construction(300)
    .ef_search(80)
    .max_layers(20)
    .distance_metric(DistanceMetric::Euclidean)
    .build()
    .unwrap();

// Multi-layer configuration for large datasets
let multilayer_config = HnswConfigBuilder::new()
    .dimension(768)
    .m_connections(16)
    .ef_construction(200)
    .ef_search(50)
    .max_layers(16)
    .distance_metric(DistanceMetric::Cosine)
    .enable_multilayer(true)
    .multilayer_level_distribution_base(Some(16))
    .multilayer_deterministic_seed(Some(42))
    .build()
    .unwrap();

Fields§

§config: HnswConfig

Implementations§

Source§

impl HnswConfigBuilder

Source

pub fn new() -> Self

Create a new builder with default configuration

Source

pub fn dimension(self, dimension: usize) -> Self

Set vector dimension count

§Arguments
  • dimension - Number of dimensions in vectors (1-4096)
§Panics

Panics if dimension is 0 or exceeds practical limits (>4096)

Source

pub fn m_connections(self, m: usize) -> Self

Set number of connections per node (M parameter)

§Arguments
  • m - Number of connections (5-48 typical)
§Panics

Panics if m is 0 or exceeds practical limits (>48)

Source

pub fn ef_construction(self, ef: usize) -> Self

Set construction ef parameter

§Arguments
  • ef - Construction ef value (100-800 typical)
§Panics

Panics if ef is less than m (must be >= M for proper construction)

Set search ef parameter

§Arguments
  • ef - Search ef value (10-200 typical)
§Panics

Panics if ef is 0 or exceeds practical limits (>200)

Source

pub fn max_layers(self, ml: u8) -> Self

Set maximum number of layers

§Arguments
  • ml - Maximum layer count (8-32 typical)
§Panics

Panics if ml is 0 or exceeds practical limits (>32)

Source

pub fn distance_metric(self, metric: DistanceMetric) -> Self

Set distance metric

§Arguments
  • metric - Distance metric to use
Source

pub fn enable_multilayer(self, enable: bool) -> Self

Enable multi-layer HNSW functionality

When enabled, uses proper multi-layer HNSW with exponential distribution. When disabled, uses single-layer mode for backward compatibility.

§Arguments
  • enable - Whether to enable multi-layer functionality
Source

pub fn multilayer_level_distribution_base(self, base: Option<usize>) -> Self

Set base value for exponential level distribution in multi-layer mode

Higher values create flatter layer distributions (more vectors in higher layers). When None, uses the m parameter value as default.

§Arguments
  • base - Base value for level distribution, or None to use m
Source

pub fn multilayer_deterministic_seed(self, seed: Option<u64>) -> Self

Set deterministic seed for multi-layer operations

When provided, ensures reproducible level assignments across runs. When None, uses non-deterministic behavior (default for production).

§Arguments
  • seed - Seed value, or None for non-deterministic behavior
Source

pub fn build(self) -> Result<HnswConfig, HnswConfigError>

Build the HnswConfig with validation

§Returns

Returns Ok(HnswConfig) if all parameters are valid, or Err(HnswConfigError) if validation fails.

§Errors
  • InvalidDimension if dimension is 0
  • InvalidMParameter if m is 0
  • InvalidEfConstruction if ef_construction < m
  • InvalidEfSearch if ef_search is 0
  • InvalidMaxLayers if ml is 0
§Examples
use sqlitegraph::hnsw::{HnswConfigBuilder, DistanceMetric};

let config = HnswConfigBuilder::new()
    .dimension(256)
    .distance_metric(DistanceMetric::Cosine)
    .build()
    .unwrap();

Trait Implementations§

Source§

impl Default for HnswConfigBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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> 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, 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