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) -> HnswService

Create a new service with the given 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.

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<HnswService, 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<HnswService, Error>

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

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

Trait Implementations§

Source§

impl Debug for HnswService

Source§

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

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 + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, HnswService: 'async_trait,

Start the service.
Source§

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

Stop the service.
Source§

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

Perform a health check.
Source§

fn liveness_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ProbeResult> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: '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 'life0: 'async_trait, Self: '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> GetSetFdFlags for T

Source§

fn get_fd_flags(&self) -> Result<FdFlags, Error>
where T: AsFilelike,

Query the “status” flags for the self file descriptor.
Source§

fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>
where T: AsFilelike,

Create a new SetFdFlags value for use with set_fd_flags. Read more
Source§

fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>
where T: Sized + AsFilelike,

Set the “status” flags for the self file descriptor. Read more
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