Skip to main content

HnswService

Struct HnswService 

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

Kernel service wrapping the HNSW vector store.

All mutable access to the inner HnswStore is serialized through a Mutex. Atomic counters track insert and search operations for observability without requiring the lock.

Implementations§

Source§

impl HnswService

Source

pub fn new(config: HnswServiceConfig) -> Self

Create a new service with the given configuration.

Source

pub fn from_store(config: HnswServiceConfig, store: HnswStore) -> Self

Wrap an existing HnswStore (e.g. after loading a snapshot).

Counters and epoch start at zero; callers that restore backend-level epoch (e.g. crate::vector_hnsw::HnswBackend) set it separately.

Source

pub fn snapshot_entries(&self) -> Vec<HnswEntry>

Snapshot all stored entries for composite backend persistence.

Source

pub fn ef_params(&self) -> (usize, usize)

Current (ef_search, ef_construction) used by the inner store.

Source

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

Hard-delete an entry by string id. Returns true if it was present.

Source

pub fn with_eml(config: HnswServiceConfig, eml_config: HnswEmlConfig) -> Self

Create a new service with custom EML configuration.

Source

pub fn set_chain_manager(&mut self, cm: Arc<ChainManager>)

Set the chain manager for exochain event logging.

Source

pub fn set_governance_engine(&mut self, engine: Arc<GovernanceEngine>)

Set the governance engine for gating destructive operations.

Source

pub fn insert(&self, id: String, embedding: Vec<f32>, metadata: Value)

Insert an embedding with associated metadata (upsert semantics).

Source

pub fn current_epoch(&self) -> u64

Return the current monotonic epoch.

Source

pub fn search(&self, query: &[f32], top_k: usize) -> Vec<HnswSearchResult>

Search for the top_k nearest embeddings to query.

When EML is enabled and trained, the beam width (ef_search) is adapted per-query and the rebuild model is consulted after each search. When a region → entry-node path table is available (WEFT-385 / HNSW-EML #4), search is guided through predicted entry nodes. Training data is recorded for continuous learning.

Source

pub fn rebuild_path_table(&self)

Explicitly rebuild the region → entry-node path table from the current store contents (WEFT-385).

Source

pub fn path_table_ready(&self) -> bool

Whether guided path prediction is active.

Source

pub fn brute_force_topk(&self, query: &[f32], top_k: usize) -> Vec<String>

Brute-force ground-truth search for recall measurement.

Source

pub fn measure_recall(&self, queries: &[Vec<f32>], top_k: usize) -> f64

Measure recall against brute-force and feed the EML rebuild model.

Source

pub fn build_tiered(&self, top_k: usize) -> TieredSearch

Build a tiered index from the current store contents.

Uses the EML distance model’s learned dimension ordering when available, falling back to the first N dimensions otherwise. The returned TieredSearch owns separate coarse/full indexes.

Source

pub fn eml_status(&self) -> HnswEmlStatus

Borrow the EML manager (for status/benchmarking).

Source

pub fn search_batch( &self, queries: &[&[f32]], top_k: usize, ) -> Vec<Vec<HnswSearchResult>>

Batch search: acquire the lock once, perform all queries, release.

This avoids per-query mutex acquisition overhead when processing multiple embeddings in a single tick (e.g., DEMOCRITUS loop).

Source

pub fn insert_multi_key( &self, primary_id: String, keys: &[MultiKey], metadata: Value, max_keys: usize, )

Insert an entity with multiple search keys.

Each key generates a separate embedding entry in the store, all sharing the same primary_id prefix. The stored ID has the form "{primary_id}::{key_type}". This allows the entity to be found via different query formulations (name, context, description, etc.).

Keys beyond max_keys are silently dropped.

Source

pub fn search_dedup(&self, query: &[f32], top_k: usize) -> Vec<HnswSearchResult>

Search with deduplication by primary entity ID.

Because multi-key insertion stores several embeddings for one entity, a single query may match multiple keys of the same entity. This method searches for top_k * 3 candidates, deduplicates by stripping the "::key_type" suffix, and returns up to top_k unique entities. The best (highest) score among duplicates is kept.

Source

pub fn len(&self) -> usize

Return the number of entries currently in the store.

Source

pub fn is_empty(&self) -> bool

Return true if the store contains no entries.

Source

pub fn insert_count(&self) -> u64

Total number of insert operations since service creation.

Source

pub fn search_count(&self) -> u64

Total number of search operations since service creation.

Source

pub fn clear(&self) -> Result<(), String>

Replace the inner store with a fresh, empty instance.

Useful for calibration cleanup. Counters are not reset.

Returns Err if the governance engine denies the bulk destruction.

Source

pub fn config(&self) -> &HnswServiceConfig

Borrow the service configuration.

Source

pub fn save_to_file(&self, path: &Path) -> Result<(), Error>

Persist the HNSW store to a JSON file.

Delegates to the underlying HnswStore::save. The HNSW index graph is not serialized; it will be rebuilt on load.

Source

pub fn load_from_file(path: &Path) -> Result<Self, Error>

Load an HNSW store from a JSON file and create a new service.

Delegates to HnswStore::load. The HNSW index is rebuilt from the saved entries. Counters start at zero.

Note: chain logging for hnsw.load requires calling [set_chain_manager] after construction; use [load_from_file_logged] when a chain manager is available.

Source

pub fn load_from_file_logged( path: &Path, cm: Arc<ChainManager>, ) -> Result<Self, Error>

Load an HNSW store from a JSON file with chain logging.

Same as [load_from_file] but emits an hnsw.load chain event.

Source§

impl HnswService

Source

pub fn insert_relationship( &self, stable_id: &str, embedding: Vec<f32>, metadata: Value, )

Insert a relationship (edge) embedding under rel::{stable_id}.

Metadata should include at least kind: "relationship" and the edge endpoints so consumers can reconstruct the hit without a separate graph lookup.

Source

pub fn search_relationships( &self, query: &[f32], top_k: usize, ) -> Vec<HnswSearchResult>

Search and keep only relationship-prefixed hits (rel::).

Oversamples the underlying store so entity vectors do not starve edge results when both share one HNSW index.

Trait Implementations§

Source§

impl Debug for HnswService

Source§

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

Formats the value using the given formatter. Read more
Source§

impl SystemService for HnswService

Source§

fn name(&self) -> &str

Human-readable service name.
Source§

fn service_type(&self) -> ServiceType

Service type category.
Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Start the service.
Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stop the service.
Source§

fn health_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = HealthStatus> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Perform a health check.
Source§

fn health_detail(&self) -> Option<String>

Optional operator-facing detail (metrics, endpoints, …). Read more
Source§

fn liveness_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ProbeResult> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Liveness probe (K2b-G2, os-patterns). Read more
Source§

fn readiness_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ProbeResult> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Readiness probe (K2b-G2, os-patterns). 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more