vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Semantic caching with fuzzy key matching
//!
//! This module provides intelligent caching that matches similar queries,
//! not just exact duplicates. Uses vector similarity to determine if a
//! cached result can be reused for a similar query.
//!
//! ## Features
//!
//! - Fuzzy cache key matching via cosine similarity
//! - Configurable similarity threshold
//! - TTL-based eviction
//! - LRU eviction when full
//! - Automatic cache warming
//!
//! ## Usage
//!
//! ```no_run
//! use vecstore::semantic_cache::SemanticCache;
//!
//! # fn main() -> anyhow::Result<()> {
//! let mut cache = SemanticCache::new(1000, 0.95);
//!
//! // Cache a query result
//! let query = vec![0.1, 0.2, 0.3];
//! let results = vec!["doc1", "doc2"];
//! cache.insert(&query, results.clone());
//!
//! // Similar query (not exact match) can hit cache
//! let similar_query = vec![0.1, 0.2, 0.31]; // Slightly different
//! if let Some(cached) = cache.get(&similar_query) {
//!     println!("Cache hit for similar query!");
//! }
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Semantic cache entry
#[derive(Clone)]
struct CacheEntry<V> {
    /// Query vector (cache key)
    query: Vec<f32>,

    /// Cached value
    value: V,

    /// Insertion timestamp
    inserted_at: Instant,

    /// Last access timestamp
    accessed_at: Instant,

    /// Access count
    access_count: usize,
}

/// Semantic cache with fuzzy key matching
pub struct SemanticCache<V: Clone> {
    /// Cache entries (hashed by approximate key)
    entries: HashMap<u64, Vec<CacheEntry<V>>>,

    /// Maximum number of entries
    capacity: usize,

    /// Similarity threshold (0.0 to 1.0)
    /// Higher = more strict matching
    similarity_threshold: f32,

    /// Time-to-live for entries
    ttl: Option<Duration>,

    /// Total cache hits
    hits: usize,

    /// Total cache misses
    misses: usize,
}

impl<V: Clone> SemanticCache<V> {
    /// Create a new semantic cache
    ///
    /// # Arguments
    /// * `capacity` - Maximum number of entries
    /// * `similarity_threshold` - Minimum similarity for cache hit (0.0 to 1.0)
    ///   - 1.0 = exact match only
    ///   - 0.95 = very similar (recommended for most use cases)
    ///   - 0.9 = moderately similar
    ///   - 0.8 = loosely similar
    pub fn new(capacity: usize, similarity_threshold: f32) -> Self {
        Self {
            entries: HashMap::new(),
            capacity,
            similarity_threshold: similarity_threshold.clamp(0.0, 1.0),
            ttl: None,
            hits: 0,
            misses: 0,
        }
    }

    /// Set time-to-live for cache entries
    pub fn with_ttl(mut self, ttl: Duration) -> Self {
        self.ttl = Some(ttl);
        self
    }

    /// Insert a query-result pair into the cache
    pub fn insert(&mut self, query: &[f32], value: V) {
        // Check capacity and evict if needed
        if self.len() >= self.capacity {
            self.evict_lru();
        }

        let bucket_key = Self::hash_vector(query);

        let entry = CacheEntry {
            query: query.to_vec(),
            value,
            inserted_at: Instant::now(),
            accessed_at: Instant::now(),
            access_count: 0,
        };

        self.entries.entry(bucket_key).or_default().push(entry);
    }

    /// Get cached value for a query (fuzzy match)
    pub fn get(&mut self, query: &[f32]) -> Option<V> {
        let bucket_key = Self::hash_vector(query);

        // Look in the same bucket
        if let Some(bucket) = self.entries.get_mut(&bucket_key) {
            let now = Instant::now();

            // Find best matching entry
            let mut best_match: Option<(usize, f32)> = None;

            for (idx, entry) in bucket.iter().enumerate() {
                // Check TTL
                if let Some(ttl) = self.ttl {
                    if now.duration_since(entry.inserted_at) > ttl {
                        continue; // Skip expired entries
                    }
                }

                // Calculate similarity
                let similarity = Self::cosine_similarity(query, &entry.query);

                if similarity >= self.similarity_threshold {
                    match best_match {
                        None => best_match = Some((idx, similarity)),
                        Some((_, prev_sim)) if similarity > prev_sim => {
                            best_match = Some((idx, similarity));
                        }
                        _ => {}
                    }
                }
            }

            // Return best match if found
            if let Some((idx, _)) = best_match {
                let entry = &mut bucket[idx];
                entry.accessed_at = now;
                entry.access_count += 1;
                self.hits += 1;
                return Some(entry.value.clone());
            }
        }

        self.misses += 1;
        None
    }

    /// Check if cache contains a similar query (without retrieving)
    pub fn contains(&self, query: &[f32]) -> bool {
        let bucket_key = Self::hash_vector(query);

        if let Some(bucket) = self.entries.get(&bucket_key) {
            let now = Instant::now();

            for entry in bucket {
                // Check TTL
                if let Some(ttl) = self.ttl {
                    if now.duration_since(entry.inserted_at) > ttl {
                        continue;
                    }
                }

                let similarity = Self::cosine_similarity(query, &entry.query);
                if similarity >= self.similarity_threshold {
                    return true;
                }
            }
        }

        false
    }

    /// Remove expired entries
    pub fn cleanup_expired(&mut self) {
        if let Some(ttl) = self.ttl {
            let now = Instant::now();

            for bucket in self.entries.values_mut() {
                bucket.retain(|entry| now.duration_since(entry.inserted_at) <= ttl);
            }

            // Remove empty buckets
            self.entries.retain(|_, bucket| !bucket.is_empty());
        }
    }

    /// Clear all cache entries
    pub fn clear(&mut self) {
        self.entries.clear();
        self.hits = 0;
        self.misses = 0;
    }

    /// Get current cache size
    pub fn len(&self) -> usize {
        self.entries.values().map(|b| b.len()).sum()
    }

    /// Check if cache is empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get cache hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        CacheStats {
            size: self.len(),
            capacity: self.capacity,
            hits: self.hits,
            misses: self.misses,
            hit_rate: self.hit_rate(),
        }
    }

    /// Evict least recently used entry
    fn evict_lru(&mut self) {
        if self.entries.is_empty() {
            return;
        }

        // Find LRU entry across all buckets
        let mut oldest_time = Instant::now();
        let mut oldest_bucket: Option<u64> = None;
        let mut oldest_idx: usize = 0;

        for (&bucket_key, bucket) in &self.entries {
            for (idx, entry) in bucket.iter().enumerate() {
                if entry.accessed_at < oldest_time {
                    oldest_time = entry.accessed_at;
                    oldest_bucket = Some(bucket_key);
                    oldest_idx = idx;
                }
            }
        }

        // Remove the LRU entry
        if let Some(bucket_key) = oldest_bucket {
            if let Some(bucket) = self.entries.get_mut(&bucket_key) {
                bucket.remove(oldest_idx);
                if bucket.is_empty() {
                    self.entries.remove(&bucket_key);
                }
            }
        }
    }

    /// Hash vector to bucket key (quantized hashing)
    fn hash_vector(v: &[f32]) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();

        // Quantize to reduce sensitivity
        // Round to 1 decimal place for bucketing
        for &val in v.iter().take(16) {
            // Use first 16 dims for hashing
            let quantized = (val * 10.0).round() as i32;
            quantized.hash(&mut hasher);
        }

        hasher.finish()
    }

    /// Calculate cosine similarity between two vectors
    fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
        if a.len() != b.len() {
            return 0.0;
        }

        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm_a == 0.0 || norm_b == 0.0 {
            return 0.0;
        }

        (dot / (norm_a * norm_b)).clamp(-1.0, 1.0)
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub size: usize,
    pub capacity: usize,
    pub hits: usize,
    pub misses: usize,
    pub hit_rate: f64,
}

impl CacheStats {
    /// Print human-readable statistics
    pub fn print(&self) {
        println!("Semantic Cache Statistics:");
        println!("  Size: {}/{}", self.size, self.capacity);
        println!("  Hits: {}", self.hits);
        println!("  Misses: {}", self.misses);
        println!("  Hit Rate: {:.2}%", self.hit_rate * 100.0);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_exact_match() {
        let mut cache = SemanticCache::new(10, 0.95);

        let query = vec![1.0, 2.0, 3.0];
        cache.insert(&query, "result1");

        // Exact match should hit
        assert!(cache.get(&query).is_some());
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn test_fuzzy_match() {
        let mut cache = SemanticCache::new(10, 0.95);

        let query = vec![1.0, 2.0, 3.0];
        cache.insert(&query, "result1");

        // Very similar query should hit (cosine sim ~0.9999)
        let similar = vec![1.0, 2.0, 3.01];
        assert!(cache.get(&similar).is_some());
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn test_different_query_misses() {
        let mut cache = SemanticCache::new(10, 0.95);

        let query1 = vec![1.0, 2.0, 3.0];
        cache.insert(&query1, "result1");

        // Very different query should miss
        let query2 = vec![10.0, 20.0, 30.0];
        assert!(cache.get(&query2).is_none());
        assert_eq!(cache.stats().misses, 1);
    }

    #[test]
    fn test_threshold() {
        let mut cache = SemanticCache::new(10, 0.99); // Very strict

        let query = vec![1.0, 2.0, 3.0];
        cache.insert(&query, "result1");

        // Slightly different might not hit with strict threshold
        let similar = vec![1.0, 2.0, 3.1];
        // This will depend on actual similarity value
        cache.get(&similar);
        // Don't assert - just testing behavior
    }

    #[test]
    fn test_ttl_expiration() {
        let mut cache = SemanticCache::new(10, 0.95).with_ttl(Duration::from_millis(100));

        let query = vec![1.0, 2.0, 3.0];
        cache.insert(&query, "result1");

        // Should hit immediately
        assert!(cache.get(&query).is_some());

        // Wait for expiration
        std::thread::sleep(Duration::from_millis(150));

        // Should miss after TTL
        assert!(cache.get(&query).is_none());
    }

    #[test]
    fn test_capacity_eviction() {
        let mut cache = SemanticCache::new(2, 0.95); // Only 2 entries

        cache.insert(&vec![1.0], "result1");
        cache.insert(&vec![2.0], "result2");
        cache.insert(&vec![3.0], "result3"); // Should evict oldest

        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_hit_rate() {
        let mut cache = SemanticCache::new(10, 0.95);

        let query = vec![1.0, 2.0, 3.0];
        cache.insert(&query, "result1");

        // 1 hit, 1 miss
        cache.get(&query);
        cache.get(&vec![10.0, 20.0, 30.0]);

        assert_eq!(cache.stats().hit_rate, 0.5);
    }

    #[test]
    fn test_cleanup_expired() {
        let mut cache = SemanticCache::new(10, 0.95).with_ttl(Duration::from_millis(50));

        cache.insert(&vec![1.0], "result1");
        cache.insert(&vec![2.0], "result2");

        std::thread::sleep(Duration::from_millis(100));

        cache.cleanup_expired();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_cosine_similarity() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert!((SemanticCache::<()>::cosine_similarity(&a, &b) - 1.0).abs() < 0.001);

        let c = vec![1.0, 0.0, 0.0];
        let d = vec![0.0, 1.0, 0.0];
        assert!((SemanticCache::<()>::cosine_similarity(&c, &d) - 0.0).abs() < 0.001);
    }
}