ipfrs-semantic 0.2.0

Semantic search with HNSW vector indexing for content-addressed data
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Advanced caching for vector embeddings
//!
//! This module provides high-performance caching strategies for vector embeddings
//! including hot embedding cache, adaptive caching, and cache-aligned storage.

use lru::LruCache;
use parking_lot::RwLock;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Cache-aligned vector storage
///
/// Vectors are aligned to cache line boundaries (64 bytes) to reduce
/// cache misses and improve SIMD performance.
#[repr(align(64))]
#[derive(Debug, Clone)]
pub struct AlignedVector {
    data: Vec<f32>,
}

impl AlignedVector {
    /// Create a new cache-aligned vector
    pub fn new(data: Vec<f32>) -> Self {
        Self { data }
    }

    /// Create a zeroed cache-aligned vector
    pub fn zeros(len: usize) -> Self {
        Self {
            data: vec![0.0; len],
        }
    }

    /// Get a reference to the underlying data
    pub fn as_slice(&self) -> &[f32] {
        &self.data
    }

    /// Get a mutable reference to the underlying data
    pub fn as_mut_slice(&mut self) -> &mut [f32] {
        &mut self.data
    }

    /// Get the length of the vector
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Check if the vector is empty
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Convert into the underlying Vec
    pub fn into_vec(self) -> Vec<f32> {
        self.data
    }
}

impl From<Vec<f32>> for AlignedVector {
    fn from(data: Vec<f32>) -> Self {
        Self::new(data)
    }
}

impl AsRef<[f32]> for AlignedVector {
    fn as_ref(&self) -> &[f32] {
        &self.data
    }
}

impl AsMut<[f32]> for AlignedVector {
    fn as_mut(&mut self) -> &mut [f32] {
        &mut self.data
    }
}

/// Access statistics for a cached item
#[derive(Debug, Clone)]
struct AccessStats {
    /// Number of times accessed
    access_count: u64,
    /// Last access time
    last_access: Instant,
    /// First access time
    first_access: Instant,
    /// Total time in cache (for adaptive sizing)
    time_in_cache: Duration,
}

impl AccessStats {
    fn new() -> Self {
        let now = Instant::now();
        Self {
            access_count: 1,
            last_access: now,
            first_access: now,
            time_in_cache: Duration::from_secs(0),
        }
    }

    fn record_access(&mut self) {
        self.access_count += 1;
        self.last_access = Instant::now();
        self.time_in_cache = self.last_access.duration_since(self.first_access);
    }

    fn access_frequency(&self) -> f64 {
        if self.time_in_cache.as_secs_f64() > 0.0 {
            self.access_count as f64 / self.time_in_cache.as_secs_f64()
        } else {
            self.access_count as f64
        }
    }
}

/// Cached embedding entry
#[derive(Debug, Clone)]
struct CachedEmbedding {
    vector: AlignedVector,
    stats: AccessStats,
}

/// Hot embedding cache with LRU eviction and adaptive sizing
///
/// Caches frequently accessed embeddings in memory with cache-aligned
/// storage for optimal SIMD performance.
pub struct HotEmbeddingCache {
    /// LRU cache for embeddings
    cache: Arc<RwLock<LruCache<String, CachedEmbedding>>>,
    /// Access statistics (hits/misses)
    hits: Arc<AtomicU64>,
    misses: Arc<AtomicU64>,
    /// Total cache capacity
    capacity: usize,
    /// Prefetch queue for predicted accesses
    prefetch_queue: Arc<RwLock<Vec<String>>>,
}

impl HotEmbeddingCache {
    /// Create a new hot embedding cache
    pub fn new(capacity: usize) -> Self {
        Self {
            cache: Arc::new(RwLock::new(LruCache::new(
                NonZeroUsize::new(capacity).expect("cache capacity must be non-zero"),
            ))),
            hits: Arc::new(AtomicU64::new(0)),
            misses: Arc::new(AtomicU64::new(0)),
            capacity,
            prefetch_queue: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Get an embedding from the cache
    pub fn get(&self, key: &str) -> Option<AlignedVector> {
        let mut cache = self.cache.write();
        if let Some(entry) = cache.get_mut(key) {
            entry.stats.record_access();
            self.hits.fetch_add(1, Ordering::Relaxed);
            Some(entry.vector.clone())
        } else {
            self.misses.fetch_add(1, Ordering::Relaxed);
            None
        }
    }

    /// Insert an embedding into the cache
    pub fn insert(&self, key: String, vector: Vec<f32>) {
        let aligned = AlignedVector::new(vector);
        let entry = CachedEmbedding {
            vector: aligned,
            stats: AccessStats::new(),
        };
        self.cache.write().put(key, entry);
    }

    /// Get cache statistics
    pub fn stats(&self) -> HotCacheStats {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;
        let hit_rate = if total > 0 {
            hits as f64 / total as f64
        } else {
            0.0
        };

        let cache = self.cache.read();
        let size = cache.len();

        HotCacheStats {
            hits,
            misses,
            hit_rate,
            size,
            capacity: self.capacity,
        }
    }

    /// Clear the cache
    pub fn clear(&self) {
        self.cache.write().clear();
        self.hits.store(0, Ordering::Relaxed);
        self.misses.store(0, Ordering::Relaxed);
    }

    /// Get the current size of the cache
    pub fn len(&self) -> usize {
        self.cache.read().len()
    }

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

    /// Prefetch embeddings (add to prefetch queue)
    pub fn prefetch(&self, keys: Vec<String>) {
        let mut queue = self.prefetch_queue.write();
        queue.extend(keys);
    }

    /// Get hot embeddings (most frequently accessed)
    pub fn get_hot_keys(&self, top_n: usize) -> Vec<String> {
        let cache = self.cache.read();
        let mut entries: Vec<_> = cache
            .iter()
            .map(|(k, v)| (k.clone(), v.stats.access_frequency()))
            .collect();

        entries.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        entries.into_iter().take(top_n).map(|(k, _)| k).collect()
    }
}

/// Statistics for hot embedding cache
#[derive(Debug, Clone)]
pub struct HotCacheStats {
    /// Number of cache hits
    pub hits: u64,
    /// Number of cache misses
    pub misses: u64,
    /// Hit rate (hits / total accesses)
    pub hit_rate: f64,
    /// Current cache size
    pub size: usize,
    /// Maximum cache capacity
    pub capacity: usize,
}

/// Adaptive caching strategy
///
/// Dynamically adjusts cache size and eviction policy based on
/// access patterns and hit rates.
pub struct AdaptiveCacheStrategy {
    /// Current cache size target
    target_size: Arc<RwLock<usize>>,
    /// Minimum cache size
    min_size: usize,
    /// Maximum cache size
    max_size: usize,
    /// Target hit rate (0.0-1.0)
    target_hit_rate: f64,
    /// Adjustment factor for cache sizing
    adjustment_factor: f64,
}

impl AdaptiveCacheStrategy {
    /// Create a new adaptive caching strategy
    pub fn new(min_size: usize, max_size: usize, target_hit_rate: f64) -> Self {
        Self {
            target_size: Arc::new(RwLock::new((min_size + max_size) / 2)),
            min_size,
            max_size,
            target_hit_rate,
            adjustment_factor: 1.1, // 10% adjustment per iteration
        }
    }

    /// Adjust cache size based on current hit rate
    pub fn adjust(&self, current_hit_rate: f64) -> usize {
        let mut target = self.target_size.write();

        if current_hit_rate < self.target_hit_rate {
            // Hit rate too low, increase cache size
            let new_size =
                (*target as f64 * self.adjustment_factor).min(self.max_size as f64) as usize;
            *target = new_size;
        } else if current_hit_rate > self.target_hit_rate + 0.05 {
            // Hit rate high enough, can reduce cache size
            let new_size =
                (*target as f64 / self.adjustment_factor).max(self.min_size as f64) as usize;
            *target = new_size;
        }

        *target
    }

    /// Get the current target cache size
    pub fn target_size(&self) -> usize {
        *self.target_size.read()
    }

    /// Reset to default size
    pub fn reset(&self) {
        *self.target_size.write() = (self.min_size + self.max_size) / 2;
    }
}

/// Cache invalidation policy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InvalidationPolicy {
    /// Time-to-live based invalidation
    TTL(Duration),
    /// Event-driven invalidation (manual)
    Event,
    /// Never invalidate (manual only)
    Never,
}

/// Cache invalidation tracker
pub struct CacheInvalidator {
    /// Invalidation policy
    policy: InvalidationPolicy,
    /// Timestamp of last invalidation
    last_invalidation: Arc<RwLock<Instant>>,
}

impl CacheInvalidator {
    /// Create a new cache invalidator
    pub fn new(policy: InvalidationPolicy) -> Self {
        Self {
            policy,
            last_invalidation: Arc::new(RwLock::new(Instant::now())),
        }
    }

    /// Check if cache should be invalidated
    pub fn should_invalidate(&self) -> bool {
        match self.policy {
            InvalidationPolicy::TTL(ttl) => {
                let elapsed = self.last_invalidation.read().elapsed();
                elapsed >= ttl
            }
            InvalidationPolicy::Event => false, // Manual invalidation only
            InvalidationPolicy::Never => false,
        }
    }

    /// Mark cache as invalidated
    pub fn invalidate(&self) {
        *self.last_invalidation.write() = Instant::now();
    }

    /// Get time since last invalidation
    pub fn time_since_invalidation(&self) -> Duration {
        self.last_invalidation.read().elapsed()
    }
}

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

    #[test]
    fn test_aligned_vector_creation() {
        let data = vec![1.0, 2.0, 3.0, 4.0];
        let aligned = AlignedVector::new(data.clone());

        assert_eq!(aligned.len(), 4);
        assert_eq!(aligned.as_slice(), &data[..]);
    }

    #[test]
    fn test_aligned_vector_alignment() {
        let aligned = AlignedVector::zeros(100);

        // Check that the AlignedVector struct itself is aligned to 64 bytes
        // Note: The inner Vec's data is heap-allocated and uses standard allocator alignment
        assert_eq!(
            std::mem::align_of::<AlignedVector>(),
            64,
            "AlignedVector struct should be aligned to 64 bytes"
        );

        // Verify the Vec data pointer has at least the natural alignment for f32
        let ptr = aligned.as_slice().as_ptr() as usize;
        assert_eq!(
            ptr % std::mem::align_of::<f32>(),
            0,
            "Data pointer should be properly aligned for f32"
        );
    }

    #[test]
    fn test_hot_cache_basic() {
        let cache = HotEmbeddingCache::new(10);

        // Insert some vectors
        cache.insert("key1".to_string(), vec![1.0, 2.0, 3.0]);
        cache.insert("key2".to_string(), vec![4.0, 5.0, 6.0]);

        // Test retrieval
        let vec1 = cache
            .get("key1")
            .expect("test: key1 should be present in cache");
        assert_eq!(vec1.as_slice(), &[1.0, 2.0, 3.0]);

        let vec2 = cache
            .get("key2")
            .expect("test: key2 should be present in cache");
        assert_eq!(vec2.as_slice(), &[4.0, 5.0, 6.0]);

        // Test miss
        assert!(cache.get("key3").is_none());
    }

    #[test]
    fn test_hot_cache_stats() {
        let cache = HotEmbeddingCache::new(10);

        cache.insert("key1".to_string(), vec![1.0, 2.0, 3.0]);

        // One hit
        cache.get("key1");
        // One miss
        cache.get("key2");

        let stats = cache.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hit_rate, 0.5);
    }

    #[test]
    fn test_hot_cache_lru() {
        let cache = HotEmbeddingCache::new(2);

        cache.insert("key1".to_string(), vec![1.0]);
        cache.insert("key2".to_string(), vec![2.0]);
        cache.insert("key3".to_string(), vec![3.0]); // Should evict key1

        assert!(cache.get("key1").is_none());
        assert!(cache.get("key2").is_some());
        assert!(cache.get("key3").is_some());
    }

    #[test]
    fn test_adaptive_strategy() {
        let strategy = AdaptiveCacheStrategy::new(100, 1000, 0.8);

        let initial_size = strategy.target_size();
        assert_eq!(initial_size, 550); // (100 + 1000) / 2

        // Low hit rate should increase size
        let new_size = strategy.adjust(0.5);
        assert!(new_size > initial_size);

        // High hit rate should decrease size
        strategy.reset();
        let new_size = strategy.adjust(0.95);
        assert!(new_size < initial_size);
    }

    #[test]
    fn test_cache_invalidator_ttl() {
        let invalidator = CacheInvalidator::new(InvalidationPolicy::TTL(Duration::from_millis(10)));

        assert!(!invalidator.should_invalidate());

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

        assert!(invalidator.should_invalidate());
    }

    #[test]
    fn test_cache_invalidator_never() {
        let invalidator = CacheInvalidator::new(InvalidationPolicy::Never);

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

        assert!(!invalidator.should_invalidate());
    }

    #[test]
    fn test_hot_keys_tracking() {
        let cache = HotEmbeddingCache::new(10);

        cache.insert("key1".to_string(), vec![1.0]);
        cache.insert("key2".to_string(), vec![2.0]);
        cache.insert("key3".to_string(), vec![3.0]);

        // Add a small delay to allow time_in_cache to accumulate
        std::thread::sleep(Duration::from_millis(1));

        // Access key1 multiple times
        for _ in 0..5 {
            cache.get("key1");
        }

        // Access key2 a few times
        for _ in 0..2 {
            cache.get("key2");
        }

        let hot_keys = cache.get_hot_keys(2);
        assert_eq!(hot_keys.len(), 2);
        // key1 should be in the hot keys (either first or second)
        assert!(hot_keys.contains(&"key1".to_string()));
        assert!(hot_keys.contains(&"key2".to_string()));
    }
}