pub struct SideIndex {
pub ivf_drift: u64,
/* private fields */
}Fields§
§ivf_drift: u64Count of vector inserts/removes since last fit. When dst-side drift
exceeds IVF_DRIFT_REBUILD on an approximate rule, apply queues a
RebuildRule second commit (fit resets this to zero).
Implementations§
Source§impl SideIndex
impl SideIndex
pub fn insert( &mut self, spec: &CandidateSpec<'_>, node: u32, get: &dyn Fn(&str) -> Option<Value>, )
pub fn remove( &mut self, spec: &CandidateSpec<'_>, node: u32, get: &dyn Fn(&str) -> Option<Value>, )
Sourcepub fn vec_dim(&self, node: u32) -> Option<u32>
pub fn vec_dim(&self, node: u32) -> Option<u32>
Cached vector dimension for a ScanAll member, if present.
Sourcepub fn vec_meta(&self, node: u32) -> Option<(u32, f64)>
pub fn vec_meta(&self, node: u32) -> Option<(u32, f64)>
Cached (dim, L2 norm) for tests / debug.
pub fn candidates( &self, spec: &CandidateSpec<'_>, get: &dyn Fn(&str) -> Option<Value>, ) -> BTreeSet<u32>
Sourcepub fn fit_ivf_clusters(&mut self, rule_name: &str)
pub fn fit_ivf_clusters(&mut self, rule_name: &str)
Fit (or re-fit) the IVF k-means index for this side using all currently
stored raw vectors. Called by the engine after reindexing all nodes in
create_rule and rebuild.
rule_name is hashed via FNV-1a to produce a stable seed, ensuring the
same rule+data always yields the same clusters (WAL replay identity).
Clears all existing cluster assignments and by_key cluster entries, then
assigns every non-zero vector (L2-normalized) to its nearest new centroid.
Resets ivf_drift to zero.
Sourcepub fn ivf_cluster_of(&self, node: u32) -> Option<usize>
pub fn ivf_cluster_of(&self, node: u32) -> Option<usize>
Cluster assignment for a node (None if not fitted or node not in index).
Sourcepub fn export_ivf_state(&self) -> (Vec<Vec<f64>>, BTreeMap<u32, usize>, u64)
pub fn export_ivf_state(&self) -> (Vec<Vec<f64>>, BTreeMap<u32, usize>, u64)
Export IVF state for snapshot persistence: (centroids, clusters, drift).
The caller stores this in the V4 snapshot and passes it back to
load_ivf_state on the next open, avoiding a full k-means re-fit.
Sourcepub fn load_ivf_state(
&mut self,
centroids: Vec<Vec<f64>>,
clusters: BTreeMap<u32, usize>,
drift: u64,
)
pub fn load_ivf_state( &mut self, centroids: Vec<Vec<f64>>, clusters: BTreeMap<u32, usize>, drift: u64, )
Restore IVF state from a V4 snapshot.
This must be called AFTER the normal insert() pass (which populates
ivf_raw) but INSTEAD OF fit_ivf_clusters. It:
- Removes any stale cluster-key entries from
by_key. - Installs the persisted centroids and drift counter.
- Rebuilds
by_keycluster buckets from the persisted assignments.
Nodes present in ivf_raw but absent from clusters (e.g. inserted
post-snapshot via WAL replay before this is called) are left unassigned;
on_node_changed will assign them to the nearest centroid incrementally.
Sourcepub fn init_hnsw(&mut self, rule_name: &str)
pub fn init_hnsw(&mut self, rule_name: &str)
Initialise the HNSW graph for this side, seeding it with FNV-1a(rule_name).
Must be called before inserting nodes via CandidateSpec::Hnsw.
Idempotent: calling again with the same name replaces the existing graph.
Sourcepub fn export_hnsw_blob(&self) -> Vec<u8> ⓘ
pub fn export_hnsw_blob(&self) -> Vec<u8> ⓘ
Export the HNSW graph as an opaque bincoded blob.
Returns an empty Vec when the HNSW is not initialized.
Sourcepub fn load_hnsw_blob(&mut self, blob: &[u8])
pub fn load_hnsw_blob(&mut self, blob: &[u8])
Restore the HNSW graph from a previously exported blob.
The hnsw_tracked set is populated from the restored graph’s node ids
so candidates/remove work correctly after restore.
Silently ignores empty or corrupt blobs (HNSW stays uninitialized).