ipfrs-storage 0.1.0

Storage backends and block management for IPFRS content-addressed system
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
//! In-memory block cache

use crate::traits::BlockStore;
use async_trait::async_trait;
use ipfrs_core::{Block, Cid, Result};
use lru::LruCache;
use parking_lot::Mutex;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

/// Cache statistics
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of cache hits
    pub hits: u64,
    /// Number of cache misses
    pub misses: u64,
    /// Current number of items in cache
    pub size: usize,
    /// Cache capacity
    pub capacity: usize,
}

impl CacheStats {
    /// Calculate 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
        }
    }

    /// Calculate miss rate (0.0 to 1.0)
    pub fn miss_rate(&self) -> f64 {
        1.0 - self.hit_rate()
    }
}

/// In-memory LRU cache for blocks
pub struct BlockCache {
    cache: Arc<Mutex<LruCache<Cid, Block>>>,
    capacity: usize,
    hits: Arc<AtomicU64>,
    misses: Arc<AtomicU64>,
}

impl BlockCache {
    /// Create a new LRU cache with the given capacity (number of blocks)
    pub fn new(capacity: usize) -> Self {
        let cap_val = capacity;
        let capacity = NonZeroUsize::new(capacity).unwrap_or(NonZeroUsize::new(1000).unwrap());
        Self {
            cache: Arc::new(Mutex::new(LruCache::new(capacity))),
            capacity: cap_val,
            hits: Arc::new(AtomicU64::new(0)),
            misses: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Get a block from cache
    #[inline]
    pub fn get(&self, cid: &Cid) -> Option<Block> {
        let result = self.cache.lock().get(cid).cloned();
        if result.is_some() {
            self.hits.fetch_add(1, Ordering::Relaxed);
        } else {
            self.misses.fetch_add(1, Ordering::Relaxed);
        }
        result
    }

    /// Put a block into cache
    #[inline]
    pub fn put(&self, block: Block) {
        self.cache.lock().put(*block.cid(), block);
    }

    /// Remove a block from cache
    pub fn remove(&self, cid: &Cid) {
        self.cache.lock().pop(cid);
    }

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

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

    /// Get cache statistics (for backward compatibility)
    pub fn len(&self) -> usize {
        self.cache.lock().len()
    }

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

/// Caching wrapper around a block store
pub struct CachedBlockStore<S: BlockStore> {
    store: S,
    cache: BlockCache,
}

impl<S: BlockStore> CachedBlockStore<S> {
    /// Create a new caching block store
    pub fn new(store: S, cache_capacity: usize) -> Self {
        Self {
            store,
            cache: BlockCache::new(cache_capacity),
        }
    }

    /// Get reference to the underlying store
    pub fn store(&self) -> &S {
        &self.store
    }

    /// Get reference to the cache
    pub fn cache(&self) -> &BlockCache {
        &self.cache
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> CacheStats {
        self.cache.stats()
    }
}

#[async_trait]
impl<S: BlockStore> BlockStore for CachedBlockStore<S> {
    async fn put(&self, block: &Block) -> Result<()> {
        // Write-through: update both cache and store
        self.cache.put(block.clone());
        self.store.put(block).await
    }

    async fn get(&self, cid: &Cid) -> Result<Option<Block>> {
        // Check cache first
        if let Some(block) = self.cache.get(cid) {
            return Ok(Some(block));
        }

        // Cache miss: fetch from store
        if let Some(block) = self.store.get(cid).await? {
            self.cache.put(block.clone());
            Ok(Some(block))
        } else {
            Ok(None)
        }
    }

    async fn has(&self, cid: &Cid) -> Result<bool> {
        // Check cache first
        if self.cache.get(cid).is_some() {
            return Ok(true);
        }
        self.store.has(cid).await
    }

    async fn delete(&self, cid: &Cid) -> Result<()> {
        self.cache.remove(cid);
        self.store.delete(cid).await
    }

    fn list_cids(&self) -> Result<Vec<Cid>> {
        self.store.list_cids()
    }

    fn len(&self) -> usize {
        self.store.len()
    }

    fn is_empty(&self) -> bool {
        self.store.is_empty()
    }

    async fn flush(&self) -> Result<()> {
        self.store.flush().await
    }

    async fn close(&self) -> Result<()> {
        self.cache.clear();
        self.store.close().await
    }

    // Optimized batch operations to reduce lock contention
    async fn get_many(&self, cids: &[Cid]) -> Result<Vec<Option<Block>>> {
        let mut results = Vec::with_capacity(cids.len());
        let mut cache_misses = Vec::new();
        let mut miss_indices = Vec::new();

        // Single lock acquisition for all cache lookups
        {
            let cache = self.cache.cache.lock();
            for (i, cid) in cids.iter().enumerate() {
                if let Some(block) = cache.peek(cid) {
                    results.push(Some(block.clone()));
                } else {
                    results.push(None);
                    cache_misses.push(*cid);
                    miss_indices.push(i);
                }
            }
        }

        // Fetch cache misses from store
        if !cache_misses.is_empty() {
            let fetched = self.store.get_many(&cache_misses).await?;

            // Update cache and results in a single lock acquisition
            {
                let mut cache = self.cache.cache.lock();
                for (idx, block_opt) in miss_indices.iter().zip(fetched.iter()) {
                    if let Some(block) = block_opt {
                        cache.put(*block.cid(), block.clone());
                        results[*idx] = Some(block.clone());
                    }
                }
            }
        }

        Ok(results)
    }

    async fn put_many(&self, blocks: &[Block]) -> Result<()> {
        // Single lock acquisition for all cache updates
        {
            let mut cache = self.cache.cache.lock();
            for block in blocks {
                cache.put(*block.cid(), block.clone());
            }
        }

        // Write to underlying store
        self.store.put_many(blocks).await
    }

    async fn has_many(&self, cids: &[Cid]) -> Result<Vec<bool>> {
        let mut results = Vec::with_capacity(cids.len());
        let mut cache_misses = Vec::new();
        let mut miss_indices = Vec::new();

        // Single lock acquisition for all cache checks
        {
            let cache = self.cache.cache.lock();
            for (i, cid) in cids.iter().enumerate() {
                if cache.contains(cid) {
                    results.push(true);
                } else {
                    results.push(false);
                    cache_misses.push(*cid);
                    miss_indices.push(i);
                }
            }
        }

        // Check cache misses in store
        if !cache_misses.is_empty() {
            let store_results = self.store.has_many(&cache_misses).await?;
            for (idx, &exists) in miss_indices.iter().zip(store_results.iter()) {
                results[*idx] = exists;
            }
        }

        Ok(results)
    }

    async fn delete_many(&self, cids: &[Cid]) -> Result<()> {
        // Single lock acquisition for all cache deletions
        {
            let mut cache = self.cache.cache.lock();
            for cid in cids {
                cache.pop(cid);
            }
        }

        self.store.delete_many(cids).await
    }
}

/// Multi-level cache with hot (L1) and warm (L2) tiers
///
/// L1 is smaller and faster, L2 is larger but may have more contention
pub struct TieredBlockCache {
    /// L1 cache - hot blocks (small, fast)
    l1_cache: Arc<Mutex<LruCache<Cid, Block>>>,
    /// L2 cache - warm blocks (larger, slower)
    l2_cache: Arc<Mutex<LruCache<Cid, Block>>>,
    /// L1 capacity
    l1_capacity: usize,
    /// L2 capacity
    l2_capacity: usize,
    /// L1 hits
    l1_hits: Arc<AtomicU64>,
    /// L2 hits
    l2_hits: Arc<AtomicU64>,
    /// Total misses
    misses: Arc<AtomicU64>,
}

impl TieredBlockCache {
    /// Create a new tiered cache
    ///
    /// # Arguments
    /// * `l1_capacity` - Capacity of L1 (hot) cache in number of blocks
    /// * `l2_capacity` - Capacity of L2 (warm) cache in number of blocks
    pub fn new(l1_capacity: usize, l2_capacity: usize) -> Self {
        let l1_cap = NonZeroUsize::new(l1_capacity).unwrap_or(NonZeroUsize::new(100).unwrap());
        let l2_cap = NonZeroUsize::new(l2_capacity).unwrap_or(NonZeroUsize::new(1000).unwrap());

        Self {
            l1_cache: Arc::new(Mutex::new(LruCache::new(l1_cap))),
            l2_cache: Arc::new(Mutex::new(LruCache::new(l2_cap))),
            l1_capacity,
            l2_capacity,
            l1_hits: Arc::new(AtomicU64::new(0)),
            l2_hits: Arc::new(AtomicU64::new(0)),
            misses: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Get a block from cache (checks L1 first, then L2)
    #[inline]
    pub fn get(&self, cid: &Cid) -> Option<Block> {
        // Try L1 first
        if let Some(block) = self.l1_cache.lock().get(cid) {
            self.l1_hits.fetch_add(1, Ordering::Relaxed);
            return Some(block.clone());
        }

        // Try L2
        if let Some(block) = self.l2_cache.lock().get(cid) {
            self.l2_hits.fetch_add(1, Ordering::Relaxed);
            let block_clone = block.clone();
            // Promote to L1 on hit
            self.l1_cache.lock().put(*cid, block_clone.clone());
            return Some(block_clone);
        }

        self.misses.fetch_add(1, Ordering::Relaxed);
        None
    }

    /// Put a block into cache (goes to L1)
    #[inline]
    pub fn put(&self, block: Block) {
        let cid = *block.cid();

        // If block is being evicted from L1, move it to L2
        if let Some(evicted) = self.l1_cache.lock().push(cid, block.clone()) {
            // evicted is (Cid, Block)
            self.l2_cache.lock().put(evicted.0, evicted.1);
        }
    }

    /// Remove a block from cache
    pub fn remove(&self, cid: &Cid) {
        self.l1_cache.lock().pop(cid);
        self.l2_cache.lock().pop(cid);
    }

    /// Clear all caches
    pub fn clear(&self) {
        self.l1_cache.lock().clear();
        self.l2_cache.lock().clear();
        self.l1_hits.store(0, Ordering::Relaxed);
        self.l2_hits.store(0, Ordering::Relaxed);
        self.misses.store(0, Ordering::Relaxed);
    }

    /// Get cache statistics
    pub fn stats(&self) -> TieredCacheStats {
        TieredCacheStats {
            l1_size: self.l1_cache.lock().len(),
            l1_capacity: self.l1_capacity,
            l2_size: self.l2_cache.lock().len(),
            l2_capacity: self.l2_capacity,
            l1_hits: self.l1_hits.load(Ordering::Relaxed),
            l2_hits: self.l2_hits.load(Ordering::Relaxed),
            misses: self.misses.load(Ordering::Relaxed),
        }
    }
}

/// Statistics for tiered cache
#[derive(Debug, Clone)]
pub struct TieredCacheStats {
    /// Current L1 cache size
    pub l1_size: usize,
    /// L1 cache capacity
    pub l1_capacity: usize,
    /// Current L2 cache size
    pub l2_size: usize,
    /// L2 cache capacity
    pub l2_capacity: usize,
    /// L1 cache hits
    pub l1_hits: u64,
    /// L2 cache hits
    pub l2_hits: u64,
    /// Total misses
    pub misses: u64,
}

impl TieredCacheStats {
    /// Calculate overall hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let total_hits = self.l1_hits + self.l2_hits;
        let total = total_hits + self.misses;
        if total == 0 {
            0.0
        } else {
            total_hits as f64 / total as f64
        }
    }

    /// Calculate L1 hit rate (0.0 to 1.0)
    pub fn l1_hit_rate(&self) -> f64 {
        let total = self.l1_hits + self.l2_hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.l1_hits as f64 / total as f64
        }
    }

    /// Calculate L2 hit rate (0.0 to 1.0)
    pub fn l2_hit_rate(&self) -> f64 {
        let total = self.l1_hits + self.l2_hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.l2_hits as f64 / total as f64
        }
    }

    /// Calculate miss rate (0.0 to 1.0)
    pub fn miss_rate(&self) -> f64 {
        1.0 - self.hit_rate()
    }
}

/// Tiered caching wrapper around a block store
pub struct TieredCachedBlockStore<S: BlockStore> {
    store: S,
    cache: TieredBlockCache,
}

impl<S: BlockStore> TieredCachedBlockStore<S> {
    /// Create a new tiered caching block store
    ///
    /// # Arguments
    /// * `store` - Underlying block store
    /// * `l1_capacity` - L1 cache capacity (number of blocks)
    /// * `l2_capacity` - L2 cache capacity (number of blocks)
    pub fn new(store: S, l1_capacity: usize, l2_capacity: usize) -> Self {
        Self {
            store,
            cache: TieredBlockCache::new(l1_capacity, l2_capacity),
        }
    }

    /// Get reference to the underlying store
    pub fn store(&self) -> &S {
        &self.store
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> TieredCacheStats {
        self.cache.stats()
    }
}

#[async_trait]
impl<S: BlockStore> BlockStore for TieredCachedBlockStore<S> {
    async fn put(&self, block: &Block) -> Result<()> {
        // Write-through: update cache and store
        self.cache.put(block.clone());
        self.store.put(block).await
    }

    async fn get(&self, cid: &Cid) -> Result<Option<Block>> {
        // Check cache first
        if let Some(block) = self.cache.get(cid) {
            return Ok(Some(block));
        }

        // Cache miss: fetch from store
        if let Some(block) = self.store.get(cid).await? {
            self.cache.put(block.clone());
            Ok(Some(block))
        } else {
            Ok(None)
        }
    }

    async fn has(&self, cid: &Cid) -> Result<bool> {
        // Check cache first
        if self.cache.get(cid).is_some() {
            return Ok(true);
        }
        self.store.has(cid).await
    }

    async fn delete(&self, cid: &Cid) -> Result<()> {
        self.cache.remove(cid);
        self.store.delete(cid).await
    }

    fn list_cids(&self) -> Result<Vec<Cid>> {
        self.store.list_cids()
    }

    fn len(&self) -> usize {
        self.store.len()
    }

    fn is_empty(&self) -> bool {
        self.store.is_empty()
    }

    async fn flush(&self) -> Result<()> {
        self.store.flush().await
    }

    async fn close(&self) -> Result<()> {
        self.cache.clear();
        self.store.close().await
    }
}