Skip to main content

csm_memory/
singularity_state.rs

1use crate::index::AnnIndex;
2use crate::singularity::{Concept, SingularityConfig};
3use crate::singularity_cache::{CacheMetrics, QueryCache};
4use crate::singularity_retrieval::RetrievalStats;
5use csm_core_lib::hyperdim::{HVec10240, Hypervector};
6use std::collections::HashMap;
7use std::sync::{Arc, RwLock};
8
9/// State for a single namespace in the singularity engine.
10#[derive(Debug)]
11pub struct NamespaceState<H: Hypervector = HVec10240> {
12    pub concepts: HashMap<String, Concept<H>>,
13    pub associations: HashMap<String, HashMap<String, (f32, u64)>>,
14    pub(crate) concept_indices: Vec<String>,
15    pub(crate) concept_vectors: Vec<H>,
16    pub(crate) id_to_index: HashMap<String, usize>,
17    pub(crate) query_cache: RwLock<QueryCache>,
18    pub(crate) cache_metrics: Arc<CacheMetrics>,
19    pub(crate) last_retrieval_stats: RwLock<RetrievalStats>,
20    pub index: Box<dyn AnnIndex<H>>,
21}
22
23impl<H: Hypervector> NamespaceState<H> {
24    pub fn new(
25        config: &SingularityConfig,
26        index: Box<dyn AnnIndex<H>>,
27        cache_metrics: Arc<CacheMetrics>,
28    ) -> Self {
29        Self {
30            concepts: HashMap::new(),
31            associations: HashMap::new(),
32            concept_indices: Vec::new(),
33            concept_vectors: Vec::new(),
34            id_to_index: HashMap::new(),
35            query_cache: RwLock::new(QueryCache::with_capacity(config.concept_cache_size)),
36            cache_metrics,
37            last_retrieval_stats: RwLock::new(RetrievalStats::default()),
38            index,
39        }
40    }
41}