ipfrs-core 0.2.0

Core content-addressing primitives and data structures for IPFRS
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! LRU cache for blocks
//!
//! This module provides an efficient LRU (Least Recently Used) cache for blocks,
//! enabling fast repeated access to frequently used blocks without hitting storage.
//!
//! # Features
//!
//! - **Thread-safe** - Can be safely shared across threads
//! - **LRU eviction** - Automatically evicts least recently used blocks when full
//! - **Size limits** - Configurable maximum cache size (in bytes and/or block count)
//! - **Statistics tracking** - Monitor cache hits, misses, and evictions
//! - **Zero-copy** - Blocks are reference-counted, so cloning is cheap
//!
//! # Example
//!
//! ```rust
//! use ipfrs_core::{BlockCache, Block};
//! use bytes::Bytes;
//!
//! // Create a cache with 10MB limit
//! let cache = BlockCache::new(10 * 1024 * 1024, None);
//!
//! // Insert a block
//! let block = Block::new(Bytes::from_static(b"Hello, cache!")).unwrap();
//! cache.insert(block.clone());
//!
//! // Retrieve the block
//! if let Some(cached_block) = cache.get(block.cid()) {
//!     println!("Cache hit!");
//! }
//!
//! // Check statistics
//! let stats = cache.stats();
//! println!("Hits: {}, Misses: {}", stats.hits, stats.misses);
//! ```

use crate::block::Block;
use crate::cid::Cid;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// LRU cache for blocks
///
/// This cache uses a Least Recently Used eviction policy to maintain a bounded
/// set of frequently accessed blocks in memory. It's thread-safe and can be
/// shared across multiple threads.
///
/// # Example
///
/// ```rust
/// use ipfrs_core::{BlockCache, Block};
/// use bytes::Bytes;
///
/// let cache = BlockCache::new(1024 * 1024, Some(100)); // 1MB or 100 blocks max
///
/// let block = Block::new(Bytes::from_static(b"cached data")).unwrap();
/// cache.insert(block.clone());
///
/// assert!(cache.get(block.cid()).is_some());
/// ```
#[derive(Clone)]
pub struct BlockCache {
    inner: Arc<RwLock<BlockCacheInner>>,
}

struct BlockCacheInner {
    blocks: HashMap<Cid, CacheEntry>,
    lru_list: Vec<Cid>,
    max_size_bytes: u64,
    max_blocks: Option<usize>,
    current_size: u64,
    stats: CacheStats,
}

struct CacheEntry {
    block: Block,
    size: u64,
    last_access_index: usize,
}

impl BlockCache {
    /// Create a new block cache
    ///
    /// # Arguments
    ///
    /// * `max_size_bytes` - Maximum total size of cached blocks in bytes
    /// * `max_blocks` - Optional maximum number of blocks (None = unlimited)
    ///
    /// # Example
    ///
    /// ```rust
    /// use ipfrs_core::BlockCache;
    ///
    /// // Cache up to 10MB of blocks
    /// let cache = BlockCache::new(10 * 1024 * 1024, None);
    ///
    /// // Cache up to 1MB or 100 blocks, whichever limit is hit first
    /// let cache2 = BlockCache::new(1024 * 1024, Some(100));
    /// ```
    pub fn new(max_size_bytes: u64, max_blocks: Option<usize>) -> Self {
        Self {
            inner: Arc::new(RwLock::new(BlockCacheInner {
                blocks: HashMap::new(),
                lru_list: Vec::new(),
                max_size_bytes,
                max_blocks,
                current_size: 0,
                stats: CacheStats::default(),
            })),
        }
    }

    /// Insert a block into the cache
    ///
    /// If the cache is full, the least recently used block will be evicted.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ipfrs_core::{BlockCache, Block};
    /// use bytes::Bytes;
    ///
    /// let cache = BlockCache::new(1024 * 1024, None);
    /// let block = Block::new(Bytes::from_static(b"data")).unwrap();
    ///
    /// cache.insert(block);
    /// ```
    pub fn insert(&self, block: Block) {
        let mut inner = self.inner.write().unwrap_or_else(|e| e.into_inner());
        let cid = *block.cid();
        let size = block.len() as u64;

        // If block already exists, update access time
        if inner.blocks.contains_key(&cid) {
            inner.update_access(&cid);
            return;
        }

        // Evict blocks if necessary
        while inner.would_exceed_limits(size) && !inner.blocks.is_empty() {
            inner.evict_lru();
        }

        // Insert the new block
        let access_index = inner.lru_list.len();
        inner.lru_list.push(cid);
        inner.blocks.insert(
            cid,
            CacheEntry {
                block,
                size,
                last_access_index: access_index,
            },
        );
        inner.current_size += size;
    }

    /// Get a block from the cache
    ///
    /// Returns `Some(block)` if found, `None` otherwise. Updates the access
    /// time for LRU tracking.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ipfrs_core::{BlockCache, Block};
    /// use bytes::Bytes;
    ///
    /// let cache = BlockCache::new(1024 * 1024, None);
    /// let block = Block::new(Bytes::from_static(b"data")).unwrap();
    /// let cid = *block.cid();
    ///
    /// cache.insert(block);
    ///
    /// if let Some(cached) = cache.get(&cid) {
    ///     assert_eq!(cached.len(), 4);
    /// }
    /// ```
    pub fn get(&self, cid: &Cid) -> Option<Block> {
        let mut inner = self.inner.write().unwrap_or_else(|e| e.into_inner());

        if inner.blocks.contains_key(cid) {
            inner.stats.hits += 1;
            let block = inner
                .blocks
                .get(cid)
                .expect("just confirmed key is present via contains_key")
                .block
                .clone();
            inner.update_access(cid);
            Some(block)
        } else {
            inner.stats.misses += 1;
            None
        }
    }

    /// Check if the cache contains a block with the given CID
    ///
    /// This does not update LRU access time.
    pub fn contains(&self, cid: &Cid) -> bool {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.blocks.contains_key(cid)
    }

    /// Remove a block from the cache
    pub fn remove(&self, cid: &Cid) -> Option<Block> {
        let mut inner = self.inner.write().unwrap_or_else(|e| e.into_inner());

        if let Some(entry) = inner.blocks.remove(cid) {
            inner.current_size -= entry.size;
            // Remove from LRU list
            if let Some(pos) = inner.lru_list.iter().position(|c| c == cid) {
                inner.lru_list.remove(pos);
            }
            Some(entry.block)
        } else {
            None
        }
    }

    /// Clear all blocks from the cache
    pub fn clear(&self) {
        let mut inner = self.inner.write().unwrap_or_else(|e| e.into_inner());
        inner.blocks.clear();
        inner.lru_list.clear();
        inner.current_size = 0;
    }

    /// Get the current cache statistics
    ///
    /// # Example
    ///
    /// ```rust
    /// use ipfrs_core::{BlockCache, Block};
    /// use bytes::Bytes;
    ///
    /// let cache = BlockCache::new(1024 * 1024, None);
    /// let block = Block::new(Bytes::from_static(b"data")).unwrap();
    ///
    /// cache.insert(block.clone());
    /// cache.get(block.cid()); // Hit
    /// cache.get(block.cid()); // Another hit
    ///
    /// let stats = cache.stats();
    /// assert_eq!(stats.hits, 2);
    /// assert_eq!(stats.misses, 0);
    /// ```
    pub fn stats(&self) -> CacheStats {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.stats.clone()
    }

    /// Get the number of blocks currently in the cache
    pub fn len(&self) -> usize {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.blocks.len()
    }

    /// Check if the cache is empty
    pub fn is_empty(&self) -> bool {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.blocks.is_empty()
    }

    /// Get the current total size of cached blocks in bytes
    pub fn size(&self) -> u64 {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.current_size
    }

    /// Get the maximum cache size in bytes
    pub fn max_size(&self) -> u64 {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.max_size_bytes
    }

    /// Get the maximum number of blocks (if configured)
    pub fn max_blocks(&self) -> Option<usize> {
        let inner = self.inner.read().unwrap_or_else(|e| e.into_inner());
        inner.max_blocks
    }
}

impl BlockCacheInner {
    fn would_exceed_limits(&self, additional_size: u64) -> bool {
        let size_exceeded = self.current_size + additional_size > self.max_size_bytes;
        let count_exceeded = self
            .max_blocks
            .map(|max| self.blocks.len() >= max)
            .unwrap_or(false);

        size_exceeded || count_exceeded
    }

    fn evict_lru(&mut self) {
        if self.lru_list.is_empty() {
            return;
        }

        // Find the least recently used CID
        let lru_cid = self
            .blocks
            .iter()
            .min_by_key(|(_, entry)| entry.last_access_index)
            .map(|(cid, _)| *cid);

        if let Some(cid) = lru_cid {
            if let Some(entry) = self.blocks.remove(&cid) {
                self.current_size -= entry.size;
                self.stats.evictions += 1;

                // Remove from LRU list
                if let Some(pos) = self.lru_list.iter().position(|c| c == &cid) {
                    self.lru_list.remove(pos);
                }
            }
        }
    }

    fn update_access(&mut self, cid: &Cid) {
        if let Some(entry) = self.blocks.get_mut(cid) {
            entry.last_access_index = self.lru_list.len();
            self.lru_list.push(*cid);
        }
    }
}

/// Statistics for block cache operations
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of cache hits
    pub hits: u64,
    /// Number of cache misses
    pub misses: u64,
    /// Number of evictions (LRU removals)
    pub evictions: u64,
}

impl CacheStats {
    /// Calculate the hit rate (hits / total_requests)
    ///
    /// Returns 0.0 if no requests have been made.
    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
        }
    }

    /// Calculate the miss rate (misses / total_requests)
    ///
    /// Returns 0.0 if no requests have been made.
    pub fn miss_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.misses as f64 / total as f64
        }
    }

    /// Get the total number of requests (hits + misses)
    pub fn total_requests(&self) -> u64 {
        self.hits + self.misses
    }
}

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

    fn make_block(data: &[u8]) -> Block {
        Block::new(Bytes::copy_from_slice(data)).unwrap()
    }

    #[test]
    fn test_cache_basic_insert_get() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"test data");
        let cid = *block.cid();

        cache.insert(block.clone());
        let retrieved = cache.get(&cid).unwrap();

        assert_eq!(retrieved.data(), block.data());
    }

    #[test]
    fn test_cache_miss() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"test");
        let fake_cid = *make_block(b"other").cid();

        cache.insert(block);

        assert!(cache.get(&fake_cid).is_none());

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

    #[test]
    fn test_cache_hit_tracking() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"data");
        let cid = *block.cid();

        cache.insert(block);
        cache.get(&cid);
        cache.get(&cid);

        let stats = cache.stats();
        assert_eq!(stats.hits, 2);
    }

    #[test]
    fn test_cache_size_limit() {
        let cache = BlockCache::new(20, None); // Small cache
        let block1 = make_block(b"12345678901234567890"); // 20 bytes
        let block2 = make_block(b"extra"); // Will exceed limit

        cache.insert(block1.clone());
        cache.insert(block2.clone());

        // block1 should be evicted
        assert!(cache.get(block1.cid()).is_none());
        assert!(cache.get(block2.cid()).is_some());

        let stats = cache.stats();
        assert_eq!(stats.evictions, 1);
    }

    #[test]
    fn test_cache_count_limit() {
        let cache = BlockCache::new(1024, Some(2)); // Max 2 blocks
        let block1 = make_block(b"a");
        let block2 = make_block(b"b");
        let block3 = make_block(b"c");

        cache.insert(block1.clone());
        cache.insert(block2.clone());
        cache.insert(block3.clone());

        // block1 should be evicted (LRU)
        assert!(cache.get(block1.cid()).is_none());
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_cache_lru_eviction() {
        let cache = BlockCache::new(1024, Some(3));
        let block1 = make_block(b"1");
        let block2 = make_block(b"2");
        let block3 = make_block(b"3");
        let block4 = make_block(b"4");

        cache.insert(block1.clone());
        cache.insert(block2.clone());
        cache.insert(block3.clone());

        // Access block1 to make it more recently used
        cache.get(block1.cid());

        // Insert block4, should evict block2 (least recently used)
        cache.insert(block4.clone());

        assert!(cache.get(block1.cid()).is_some());
        assert!(cache.get(block2.cid()).is_none());
        assert!(cache.get(block3.cid()).is_some());
        assert!(cache.get(block4.cid()).is_some());
    }

    #[test]
    fn test_cache_contains() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"test");

        cache.insert(block.clone());

        assert!(cache.contains(block.cid()));
        assert!(!cache.contains(make_block(b"other").cid()));
    }

    #[test]
    fn test_cache_remove() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"test");
        let cid = *block.cid();

        cache.insert(block.clone());
        assert!(cache.contains(&cid));

        let removed = cache.remove(&cid);
        assert!(removed.is_some());
        assert!(!cache.contains(&cid));
    }

    #[test]
    fn test_cache_clear() {
        let cache = BlockCache::new(1024, None);
        cache.insert(make_block(b"1"));
        cache.insert(make_block(b"2"));
        cache.insert(make_block(b"3"));

        assert_eq!(cache.len(), 3);

        cache.clear();

        assert_eq!(cache.len(), 0);
        assert_eq!(cache.size(), 0);
    }

    #[test]
    fn test_cache_stats() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"test");

        cache.insert(block.clone());
        cache.get(block.cid()); // hit
        cache.get(block.cid()); // hit
        cache.get(make_block(b"miss").cid()); // miss

        let stats = cache.stats();
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.total_requests(), 3);
        assert!((stats.hit_rate() - 2.0 / 3.0).abs() < 0.001);
    }

    #[test]
    fn test_cache_size_tracking() {
        let cache = BlockCache::new(1024, None);
        let block1 = make_block(&[0u8; 100]);
        let block2 = make_block(&[0u8; 200]);

        cache.insert(block1.clone());
        assert_eq!(cache.size(), 100);

        cache.insert(block2.clone());
        assert_eq!(cache.size(), 300);

        cache.remove(block1.cid());
        assert_eq!(cache.size(), 200);
    }

    #[test]
    fn test_cache_duplicate_insert() {
        let cache = BlockCache::new(1024, None);
        let block = make_block(b"data");

        cache.insert(block.clone());
        cache.insert(block.clone()); // Duplicate

        assert_eq!(cache.len(), 1);
        assert_eq!(cache.size(), block.len() as u64);
    }

    #[test]
    fn test_cache_thread_safety() {
        use std::thread;

        let cache = BlockCache::new(10240, None);
        let cache_clone = cache.clone();

        let handle = thread::spawn(move || {
            for i in 0..100 {
                let block = make_block(&[i as u8; 10]);
                cache_clone.insert(block);
            }
        });

        for i in 100..200 {
            let block = make_block(&[i as u8; 10]);
            cache.insert(block);
        }

        handle.join().unwrap();

        // Should have blocks from both threads
        assert!(!cache.is_empty());
    }
}