1use crate::handle::Handle;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[repr(transparent)]
13pub struct VectorStoreHandle(pub Handle);
14
15impl VectorStoreHandle {
16 #[inline]
18 #[must_use]
19 pub const fn new(id: u32, generation: u32) -> Self {
20 Self(Handle::new(id, generation))
21 }
22
23 #[inline]
25 #[must_use]
26 pub const fn null() -> Self {
27 Self(Handle::null())
28 }
29
30 #[inline]
32 #[must_use]
33 pub const fn is_null(&self) -> bool {
34 self.0.is_null()
35 }
36
37 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55#[repr(transparent)]
56pub struct VectorKey(pub u64);
57
58impl VectorKey {
59 #[inline]
61 #[must_use]
62 pub const fn new(id: u64) -> Self {
63 Self(id)
64 }
65
66 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91#[repr(C)]
92pub struct CoherenceMeta {
93 pub coherence_score: u16,
96
97 pub mutation_epoch: u64,
100
101 pub proof_attestation_hash: [u8; 32],
103
104 pub last_access_ns: u64,
106
107 pub access_count: u32,
109}
110
111impl CoherenceMeta {
112 #[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 #[inline]
131 #[must_use]
132 pub fn coherence_as_f32(&self) -> f32 {
133 self.coherence_score as f32 / 10000.0
134 }
135
136 #[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, mutation_epoch: 0,
156 proof_attestation_hash: [0; 32],
157 last_access_ns: 0,
158 access_count: 0,
159 }
160 }
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165#[repr(C)]
166pub struct VectorStoreConfig {
167 pub dimensions: u32,
169
170 pub capacity: u32,
172
173 pub hnsw_m: u16,
175
176 pub hnsw_ef_construction: u16,
178
179 pub use_quantization: bool,
181}
182
183impl VectorStoreConfig {
184 #[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 #[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 #[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) }
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}