Skip to main content

ruvix_types/
vector.rs

1//! Vector store types.
2//!
3//! Vector stores are kernel-resident objects containing HNSW-indexed vectors.
4//! Unlike conventional databases where vectors are userspace constructs,
5//! RuVix makes vector stores kernel objects with the same protection as
6//! capability tables.
7
8use crate::handle::Handle;
9
10/// Handle to a kernel-resident vector store.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[repr(transparent)]
13pub struct VectorStoreHandle(pub Handle);
14
15impl VectorStoreHandle {
16    /// Creates a new vector store handle.
17    #[inline]
18    #[must_use]
19    pub const fn new(id: u32, generation: u32) -> Self {
20        Self(Handle::new(id, generation))
21    }
22
23    /// Creates a null (invalid) vector store handle.
24    #[inline]
25    #[must_use]
26    pub const fn null() -> Self {
27        Self(Handle::null())
28    }
29
30    /// Checks if this handle is null.
31    #[inline]
32    #[must_use]
33    pub const fn is_null(&self) -> bool {
34        self.0.is_null()
35    }
36
37    /// Returns the raw handle.
38    #[inline]
39    #[must_use]
40    pub const fn raw(&self) -> Handle {
41        self.0
42    }
43}
44
45impl Default for VectorStoreHandle {
46    fn default() -> Self {
47        Self::null()
48    }
49}
50
51/// Key for addressing vectors in a vector store.
52///
53/// Vector keys are 64-bit identifiers unique within a store.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55#[repr(transparent)]
56pub struct VectorKey(pub u64);
57
58impl VectorKey {
59    /// Creates a new vector key.
60    #[inline]
61    #[must_use]
62    pub const fn new(id: u64) -> Self {
63        Self(id)
64    }
65
66    /// Returns the raw key value.
67    #[inline]
68    #[must_use]
69    pub const fn raw(&self) -> u64 {
70        self.0
71    }
72}
73
74impl From<u64> for VectorKey {
75    fn from(id: u64) -> Self {
76        Self(id)
77    }
78}
79
80impl From<VectorKey> for u64 {
81    fn from(key: VectorKey) -> Self {
82        key.0
83    }
84}
85
86/// Coherence metadata associated with each vector.
87///
88/// Every vector in a kernel vector store carries coherence metadata
89/// enabling the scheduler and proof engine to make informed decisions.
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91#[repr(C)]
92pub struct CoherenceMeta {
93    /// Coherence score (0-10000 representing 0.0000-1.0000).
94    /// Higher scores indicate stronger structural consistency.
95    pub coherence_score: u16,
96
97    /// Epoch of the last mutation.
98    /// Incremented on each vector_put_proved.
99    pub mutation_epoch: u64,
100
101    /// Hash of the proof attestation that authorized the last mutation.
102    pub proof_attestation_hash: [u8; 32],
103
104    /// Timestamp of last access (nanoseconds since boot).
105    pub last_access_ns: u64,
106
107    /// Number of times this vector has been read.
108    pub access_count: u32,
109}
110
111impl CoherenceMeta {
112    /// Creates new coherence metadata.
113    #[inline]
114    #[must_use]
115    pub const fn new(
116        coherence_score: u16,
117        mutation_epoch: u64,
118        proof_attestation_hash: [u8; 32],
119    ) -> Self {
120        Self {
121            coherence_score,
122            mutation_epoch,
123            proof_attestation_hash,
124            last_access_ns: 0,
125            access_count: 0,
126        }
127    }
128
129    /// Returns the coherence score as a float (0.0-1.0).
130    #[inline]
131    #[must_use]
132    pub fn coherence_as_f32(&self) -> f32 {
133        self.coherence_score as f32 / 10000.0
134    }
135
136    /// Creates coherence metadata from a float score.
137    #[inline]
138    #[must_use]
139    pub fn with_coherence_f32(coherence: f32, mutation_epoch: u64) -> Self {
140        let score = (coherence.clamp(0.0, 1.0) * 10000.0) as u16;
141        Self {
142            coherence_score: score,
143            mutation_epoch,
144            proof_attestation_hash: [0; 32],
145            last_access_ns: 0,
146            access_count: 0,
147        }
148    }
149}
150
151impl Default for CoherenceMeta {
152    fn default() -> Self {
153        Self {
154            coherence_score: 10000, // 1.0 = fully coherent
155            mutation_epoch: 0,
156            proof_attestation_hash: [0; 32],
157            last_access_ns: 0,
158            access_count: 0,
159        }
160    }
161}
162
163/// Configuration for creating a kernel vector store.
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165#[repr(C)]
166pub struct VectorStoreConfig {
167    /// Number of dimensions per vector.
168    pub dimensions: u32,
169
170    /// Maximum number of vectors the store can hold.
171    pub capacity: u32,
172
173    /// HNSW M parameter (number of bidirectional links per node).
174    pub hnsw_m: u16,
175
176    /// HNSW ef_construction parameter.
177    pub hnsw_ef_construction: u16,
178
179    /// Whether to use quantization (reduces memory, slightly reduces accuracy).
180    pub use_quantization: bool,
181}
182
183impl VectorStoreConfig {
184    /// Creates a new vector store configuration.
185    #[inline]
186    #[must_use]
187    pub const fn new(dimensions: u32, capacity: u32) -> Self {
188        Self {
189            dimensions,
190            capacity,
191            hnsw_m: 16,
192            hnsw_ef_construction: 200,
193            use_quantization: false,
194        }
195    }
196
197    /// Sets HNSW parameters.
198    #[inline]
199    #[must_use]
200    pub const fn with_hnsw(mut self, m: u16, ef_construction: u16) -> Self {
201        self.hnsw_m = m;
202        self.hnsw_ef_construction = ef_construction;
203        self
204    }
205
206    /// Enables quantization.
207    #[inline]
208    #[must_use]
209    pub const fn with_quantization(mut self) -> Self {
210        self.use_quantization = true;
211        self
212    }
213}
214
215impl Default for VectorStoreConfig {
216    fn default() -> Self {
217        Self::new(768, 10000) // Default: 768-dim embeddings, 10K capacity
218    }
219}
220
221#[cfg(test)]
222mod tests {
223    use super::*;
224
225    #[test]
226    fn test_vector_store_handle() {
227        let h = VectorStoreHandle::new(42, 1);
228        assert!(!h.is_null());
229        assert_eq!(h.raw().id, 42);
230    }
231
232    #[test]
233    fn test_vector_key() {
234        let key = VectorKey::new(12345);
235        assert_eq!(key.raw(), 12345);
236    }
237
238    #[test]
239    fn test_coherence_meta_score() {
240        let meta = CoherenceMeta::with_coherence_f32(0.95, 1);
241        assert!((meta.coherence_as_f32() - 0.95).abs() < 0.001);
242    }
243
244    #[test]
245    fn test_vector_store_config() {
246        let config = VectorStoreConfig::new(384, 5000)
247            .with_hnsw(32, 400)
248            .with_quantization();
249
250        assert_eq!(config.dimensions, 384);
251        assert_eq!(config.capacity, 5000);
252        assert_eq!(config.hnsw_m, 32);
253        assert!(config.use_quantization);
254    }
255}