commonware-storage 2026.4.0

Persist and retrieve data from an abstract store.
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! A prunable cache for ordered data with index-based lookups.
//!
//! Data is stored in [crate::journal::segmented::variable::Journal] (an append-only log) and the location of
//! written data is tracked in-memory by index to enable **single-read lookups** for cached data.
//!
//! Unlike [crate::archive::Archive], the [Cache] is optimized for simplicity and does
//! not support key-based lookups (only index-based access is provided). This makes it ideal for
//! caching sequential data where you know the exact index of the item you want to retrieve.
//!
//! # Memory Overhead
//!
//! [Cache] maintains a single in-memory map to track the location of each index item. The memory
//! used to track each item is `8 + 4 + 4` bytes (where `8` is the index, `4` is the offset, and
//! `4` is the length). This results in approximately `16` bytes of memory overhead per cached item.
//!
//! # Pruning
//!
//! [Cache] supports pruning up to a minimum `index` using the `prune` method. After `prune` is
//! called on a `section`, all interaction with a `section` less than the pruned `section` will
//! return an error. The pruning granularity is determined by `items_per_blob` in the configuration.
//!
//! # Single Operation Reads
//!
//! To enable single operation reads (i.e. reading all of an item in a single call to
//! [commonware_runtime::Blob]), [Cache] stores the length of each item in its in-memory index.
//! This ensures that reading a cached item requires only one disk operation.
//!
//! # Compression
//!
//! [Cache] supports compressing data before storing it on disk. This can be enabled by setting
//! the `compression` field in the `Config` struct to a valid `zstd` compression level. This setting
//! can be changed between initializations of [Cache], however, it must remain populated if any
//! data was written with compression enabled.
//!
//! # Querying for Gaps
//!
//! [Cache] tracks gaps in the index space to enable the caller to efficiently fetch unknown keys
//! using `next_gap`. This is a very common pattern when syncing blocks in a blockchain.
//!
//! # Example
//!
//! ```rust
//! use commonware_runtime::{Spawner, Runner, deterministic, buffer::paged::CacheRef};
//! use commonware_storage::cache::{Cache, Config};
//! use commonware_utils::{NZUsize, NZU16, NZU64};
//!
//! let executor = deterministic::Runner::default();
//! executor.start(|context| async move {
//!     // Create a cache
//!     let cfg = Config {
//!         partition: "cache".into(),
//!         compression: Some(3),
//!         codec_config: (),
//!         items_per_blob: NZU64!(1024),
//!         write_buffer: NZUsize!(1024 * 1024),
//!         replay_buffer: NZUsize!(4096),
//!         page_cache: CacheRef::from_pooler(&context, NZU16!(1024), NZUsize!(10)),
//!     };
//!     let mut cache = Cache::init(context, cfg).await.unwrap();
//!
//!     // Put data at index
//!     cache.put(1, 100u32).await.unwrap();
//!
//!     // Get data by index
//!     let data: Option<u32> = cache.get(1).await.unwrap();
//!     assert_eq!(data, Some(100));
//!
//!     // Check for gaps in the index space
//!     cache.put(10, 200u32).await.unwrap();
//!     let (current_end, start_next) = cache.next_gap(5);
//!     assert!(current_end.is_none());
//!     assert_eq!(start_next, Some(10));
//!
//!     // Sync the cache
//!     cache.sync().await.unwrap();
//! });
//! ```

use commonware_runtime::buffer::paged::CacheRef;
use std::num::{NonZeroU64, NonZeroUsize};
use thiserror::Error;

mod storage;
pub use storage::Cache;

/// Errors that can occur when interacting with the cache.
#[derive(Debug, Error)]
pub enum Error {
    #[error("journal error: {0}")]
    Journal(#[from] crate::journal::Error),
    #[error("record corrupted")]
    RecordCorrupted,
    #[error("already pruned to: {0}")]
    AlreadyPrunedTo(u64),
    #[error("record too large")]
    RecordTooLarge,
}

/// Configuration for [Cache] storage.
#[derive(Clone)]
pub struct Config<C> {
    /// The partition to use for the cache's [crate::journal] storage.
    pub partition: String,

    /// The compression level to use for the cache's [crate::journal] storage.
    pub compression: Option<u8>,

    /// The [commonware_codec::Codec] configuration to use for the value stored in the cache.
    pub codec_config: C,

    /// The number of items per section (the granularity of pruning).
    pub items_per_blob: NonZeroU64,

    /// The amount of bytes that can be buffered in a section before being written to a
    /// [commonware_runtime::Blob].
    pub write_buffer: NonZeroUsize,

    /// The buffer size to use when replaying a [commonware_runtime::Blob].
    pub replay_buffer: NonZeroUsize,

    /// The page cache to use for the underlying [crate::journal] storage.
    pub page_cache: CacheRef,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::journal::Error as JournalError;
    use commonware_macros::{test_group, test_traced};
    use commonware_runtime::{deterministic, Metrics, Runner};
    use commonware_utils::{NZUsize, NZU16, NZU64};
    use rand::Rng;
    use std::{collections::BTreeMap, num::NonZeroU16};

    const DEFAULT_ITEMS_PER_BLOB: u64 = 65536;
    const DEFAULT_WRITE_BUFFER: usize = 1024;
    const DEFAULT_REPLAY_BUFFER: usize = 4096;
    const PAGE_SIZE: NonZeroU16 = NZU16!(1024);
    const PAGE_CACHE_SIZE: NonZeroUsize = NZUsize!(10);

    #[test_traced]
    fn test_cache_compression_then_none() {
        // Initialize the deterministic context
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            // Initialize the cache
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: Some(3),
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.with_label("first"), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Put the data
            let index = 1u64;
            let data = 1;
            cache.put(index, data).await.expect("Failed to put data");

            // Sync and drop the cache
            cache.sync().await.expect("Failed to sync cache");
            drop(cache);

            // Initialize the cache again without compression
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let result = Cache::<_, i32>::init(context.with_label("second"), cfg.clone()).await;
            assert!(matches!(
                result,
                Err(Error::Journal(JournalError::Codec(_)))
            ));
        });
    }

    #[test_traced]
    fn test_cache_prune() {
        // Initialize the deterministic context
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            // Initialize the cache
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(1), // no mask - each item is its own section
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Insert multiple items across different sections
            let items = vec![(1u64, 1), (2u64, 2), (3u64, 3), (4u64, 4), (5u64, 5)];
            for (index, data) in &items {
                cache.put(*index, *data).await.expect("Failed to put data");
            }
            assert_eq!(cache.first(), Some(1));

            // Check metrics
            let buffer = context.encode();
            assert!(buffer.contains("items_tracked 5"));

            // Prune sections less than 3
            cache.prune(3).await.expect("Failed to prune");

            // Ensure items 1 and 2 are no longer present
            for (index, data) in items {
                let retrieved = cache.get(index).await.expect("Failed to get data");
                if index < 3 {
                    assert!(retrieved.is_none());
                } else {
                    assert_eq!(retrieved.expect("Data not found"), data);
                }
            }
            assert_eq!(cache.first(), Some(3));

            // Check metrics
            let buffer = context.encode();
            assert!(buffer.contains("items_tracked 3"));

            // Try to prune older section
            cache.prune(2).await.expect("Failed to prune");
            assert_eq!(cache.first(), Some(3));

            // Try to prune current section again
            cache.prune(3).await.expect("Failed to prune");
            assert_eq!(cache.first(), Some(3));

            // Try to put older index
            let result = cache.put(1, 1).await;
            assert!(matches!(result, Err(Error::AlreadyPrunedTo(3))));
        });
    }

    fn test_cache_restart(num_items: usize) -> String {
        // Initialize the deterministic context
        let executor = deterministic::Runner::default();
        executor.start(|mut context| async move {
            // Initialize the cache
            let items_per_blob = 256u64;
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(items_per_blob),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.with_label("init1"), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Insert multiple items
            let mut items = BTreeMap::new();
            while items.len() < num_items {
                let index = items.len() as u64;
                let mut data = [0u8; 1024];
                context.fill(&mut data);
                items.insert(index, data);

                cache.put(index, data).await.expect("Failed to put data");
            }

            // Ensure all items can be retrieved
            for (index, data) in &items {
                let retrieved = cache
                    .get(*index)
                    .await
                    .expect("Failed to get data")
                    .expect("Data not found");
                assert_eq!(retrieved, *data);
            }

            // Check metrics
            let buffer = context.encode();
            let tracked = format!("items_tracked {num_items:?}");
            assert!(buffer.contains(&tracked));

            // Sync and drop the cache
            cache.sync().await.expect("Failed to sync cache");
            drop(cache);

            // Reinitialize the cache
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(items_per_blob),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::<_, [u8; 1024]>::init(context.with_label("init2"), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Ensure all items can be retrieved
            for (index, data) in &items {
                let retrieved = cache
                    .get(*index)
                    .await
                    .expect("Failed to get data")
                    .expect("Data not found");
                assert_eq!(&retrieved, data);
            }

            // Prune first half
            let min = (items.len() / 2) as u64;
            cache.prune(min).await.expect("Failed to prune");

            // Ensure all items can be retrieved that haven't been pruned
            let min = (min / items_per_blob) * items_per_blob;
            let mut removed = 0;
            for (index, data) in items {
                if index >= min {
                    let retrieved = cache
                        .get(index)
                        .await
                        .expect("Failed to get data")
                        .expect("Data not found");
                    assert_eq!(retrieved, data);
                } else {
                    let retrieved = cache.get(index).await.expect("Failed to get data");
                    assert!(retrieved.is_none());
                    removed += 1;
                }
            }

            // Check metrics
            let buffer = context.encode();
            let tracked = format!("items_tracked {:?}", num_items - removed);
            assert!(buffer.contains(&tracked));

            context.auditor().state()
        })
    }

    #[test_group("slow")]
    #[test_traced]
    fn test_cache_many_items_and_restart() {
        test_cache_restart(100_000);
    }

    #[test_group("slow")]
    #[test_traced]
    fn test_determinism() {
        let state1 = test_cache_restart(5_000);
        let state2 = test_cache_restart(5_000);
        assert_eq!(state1, state2);
    }

    #[test_traced]
    fn test_cache_next_gap() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Check first
            assert_eq!(cache.first(), None);

            // Insert values with gaps
            cache.put(1, 1).await.unwrap();
            cache.put(10, 10).await.unwrap();
            cache.put(11, 11).await.unwrap();
            cache.put(14, 14).await.unwrap();

            // Check gaps
            let (current_end, start_next) = cache.next_gap(0);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(1));
            assert_eq!(cache.first(), Some(1));

            let (current_end, start_next) = cache.next_gap(1);
            assert_eq!(current_end, Some(1));
            assert_eq!(start_next, Some(10));

            let (current_end, start_next) = cache.next_gap(10);
            assert_eq!(current_end, Some(11));
            assert_eq!(start_next, Some(14));

            let (current_end, start_next) = cache.next_gap(11);
            assert_eq!(current_end, Some(11));
            assert_eq!(start_next, Some(14));

            let (current_end, start_next) = cache.next_gap(12);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(14));

            let (current_end, start_next) = cache.next_gap(14);
            assert_eq!(current_end, Some(14));
            assert!(start_next.is_none());
        });
    }

    #[test_traced]
    fn test_cache_missing_items() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Test 1: Empty cache - should return no items
            assert_eq!(cache.first(), None);
            assert_eq!(cache.missing_items(0, 5), Vec::<u64>::new());
            assert_eq!(cache.missing_items(100, 10), Vec::<u64>::new());

            // Test 2: Insert values with gaps
            cache.put(1, 1).await.unwrap();
            cache.put(2, 2).await.unwrap();
            cache.put(5, 5).await.unwrap();
            cache.put(6, 6).await.unwrap();
            cache.put(10, 10).await.unwrap();

            // Test 3: Find missing items from the beginning
            assert_eq!(cache.missing_items(0, 5), vec![0, 3, 4, 7, 8]);
            assert_eq!(cache.missing_items(0, 6), vec![0, 3, 4, 7, 8, 9]);
            assert_eq!(cache.missing_items(0, 7), vec![0, 3, 4, 7, 8, 9]);

            // Test 4: Find missing items from within a gap
            assert_eq!(cache.missing_items(3, 3), vec![3, 4, 7]);
            assert_eq!(cache.missing_items(4, 2), vec![4, 7]);

            // Test 5: Find missing items from within a range
            assert_eq!(cache.missing_items(1, 3), vec![3, 4, 7]);
            assert_eq!(cache.missing_items(2, 4), vec![3, 4, 7, 8]);
            assert_eq!(cache.missing_items(5, 2), vec![7, 8]);

            // Test 6: Find missing items after the last range (no more gaps)
            assert_eq!(cache.missing_items(11, 5), Vec::<u64>::new());
            assert_eq!(cache.missing_items(100, 10), Vec::<u64>::new());

            // Test 7: Large gap scenario
            cache.put(1000, 1000).await.unwrap();

            // Gap between 10 and 1000
            let items = cache.missing_items(11, 10);
            assert_eq!(items, vec![11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);

            // Request more items than available in gap
            let items = cache.missing_items(990, 15);
            assert_eq!(
                items,
                vec![990, 991, 992, 993, 994, 995, 996, 997, 998, 999]
            );

            // Test 8: After syncing (data should remain consistent)
            cache.sync().await.unwrap();
            assert_eq!(cache.missing_items(0, 5), vec![0, 3, 4, 7, 8]);
            assert_eq!(cache.missing_items(3, 3), vec![3, 4, 7]);

            // Test 9: Cross-section boundary scenario
            cache.put(DEFAULT_ITEMS_PER_BLOB - 1, 99).await.unwrap();
            cache.put(DEFAULT_ITEMS_PER_BLOB + 1, 101).await.unwrap();

            // Find missing items across section boundary
            let items = cache.missing_items(DEFAULT_ITEMS_PER_BLOB - 2, 5);
            assert_eq!(
                items,
                vec![DEFAULT_ITEMS_PER_BLOB - 2, DEFAULT_ITEMS_PER_BLOB]
            );
        });
    }

    #[test_traced]
    fn test_cache_intervals_after_restart() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };

            // Insert data and sync
            {
                let mut cache = Cache::init(context.with_label("first"), cfg.clone())
                    .await
                    .expect("Failed to initialize cache");

                cache.put(0, 0).await.expect("Failed to put data");
                cache.put(100, 100).await.expect("Failed to put data");
                cache.put(1000, 1000).await.expect("Failed to put data");

                cache.sync().await.expect("Failed to sync cache");
            }

            // Reopen and verify intervals are preserved
            {
                let cache = Cache::<_, i32>::init(context.with_label("second"), cfg.clone())
                    .await
                    .expect("Failed to initialize cache");

                // Check gaps are preserved
                let (current_end, start_next) = cache.next_gap(0);
                assert_eq!(current_end, Some(0));
                assert_eq!(start_next, Some(100));

                let (current_end, start_next) = cache.next_gap(100);
                assert_eq!(current_end, Some(100));
                assert_eq!(start_next, Some(1000));

                // Check missing items
                let items = cache.missing_items(1, 5);
                assert_eq!(items, vec![1, 2, 3, 4, 5]);
            }
        });
    }

    #[test_traced]
    fn test_cache_intervals_with_pruning() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(100), // Smaller sections for easier testing
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Insert values across multiple sections
            cache.put(50, 50).await.unwrap();
            cache.put(150, 150).await.unwrap();
            cache.put(250, 250).await.unwrap();
            cache.put(350, 350).await.unwrap();

            // Check gaps before pruning
            let (current_end, start_next) = cache.next_gap(0);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(50));

            // Prune sections less than 200
            cache.prune(200).await.expect("Failed to prune");

            // Check that pruned indices are not accessible
            assert!(!cache.has(50));
            assert!(!cache.has(150));

            // Check gaps after pruning - should not include pruned ranges
            let (current_end, start_next) = cache.next_gap(200);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(250));

            // Missing items should not include pruned ranges
            let items = cache.missing_items(200, 5);
            assert_eq!(items, vec![200, 201, 202, 203, 204]);

            // Verify remaining data is still accessible
            assert!(cache.has(250));
            assert!(cache.has(350));
            assert_eq!(cache.get(250).await.unwrap(), Some(250));
            assert_eq!(cache.get(350).await.unwrap(), Some(350));
        });
    }

    #[test_traced]
    fn test_cache_sparse_indices() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(100), // Smaller sections for testing
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Insert sparse values
            let indices = vec![
                (0u64, 0),
                (99u64, 99),   // End of first section
                (100u64, 100), // Start of second section
                (500u64, 500), // Start of sixth section
            ];

            for (index, value) in &indices {
                cache.put(*index, *value).await.expect("Failed to put data");
            }

            // Check that intermediate indices don't exist
            assert!(!cache.has(1));
            assert!(!cache.has(50));
            assert!(!cache.has(101));
            assert!(!cache.has(499));

            // Verify gap detection works correctly
            let (current_end, start_next) = cache.next_gap(50);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(99));

            let (current_end, start_next) = cache.next_gap(99);
            assert_eq!(current_end, Some(100));
            assert_eq!(start_next, Some(500));

            // Sync and verify
            cache.sync().await.expect("Failed to sync");

            for (index, value) in &indices {
                let retrieved = cache
                    .get(*index)
                    .await
                    .expect("Failed to get data")
                    .expect("Data not found");
                assert_eq!(retrieved, *value);
            }
        });
    }

    #[test_traced]
    fn test_cache_intervals_edge_cases() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Test edge case: single item
            cache.put(42, 42).await.unwrap();

            let (current_end, start_next) = cache.next_gap(42);
            assert_eq!(current_end, Some(42));
            assert!(start_next.is_none());

            let (current_end, start_next) = cache.next_gap(41);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(42));

            let (current_end, start_next) = cache.next_gap(43);
            assert!(current_end.is_none());
            assert!(start_next.is_none());

            // Test edge case: consecutive items
            cache.put(43, 43).await.unwrap();
            cache.put(44, 44).await.unwrap();

            let (current_end, start_next) = cache.next_gap(42);
            assert_eq!(current_end, Some(44));
            assert!(start_next.is_none());

            // Test edge case: boundary values
            cache.put(u64::MAX - 1, 999).await.unwrap();

            let (current_end, start_next) = cache.next_gap(u64::MAX - 2);
            assert!(current_end.is_none());
            assert_eq!(start_next, Some(u64::MAX - 1));

            let (current_end, start_next) = cache.next_gap(u64::MAX - 1);
            assert_eq!(current_end, Some(u64::MAX - 1));
            assert!(start_next.is_none());
        });
    }

    #[test_traced]
    fn test_cache_intervals_duplicate_inserts() {
        let executor = deterministic::Runner::default();
        executor.start(|context| async move {
            let cfg = Config {
                partition: "test-partition".into(),
                codec_config: (),
                compression: None,
                write_buffer: NZUsize!(DEFAULT_WRITE_BUFFER),
                replay_buffer: NZUsize!(DEFAULT_REPLAY_BUFFER),
                items_per_blob: NZU64!(DEFAULT_ITEMS_PER_BLOB),
                page_cache: CacheRef::from_pooler(&context, PAGE_SIZE, PAGE_CACHE_SIZE),
            };
            let mut cache = Cache::init(context.clone(), cfg.clone())
                .await
                .expect("Failed to initialize cache");

            // Insert initial value
            cache.put(10, 10).await.unwrap();
            assert!(cache.has(10));
            assert_eq!(cache.get(10).await.unwrap(), Some(10));

            // Try to insert duplicate - should be no-op
            cache.put(10, 20).await.unwrap();
            assert!(cache.has(10));
            assert_eq!(cache.get(10).await.unwrap(), Some(10)); // Should still be original value

            // Verify intervals are correct
            let (current_end, start_next) = cache.next_gap(10);
            assert_eq!(current_end, Some(10));
            assert!(start_next.is_none());

            // Insert adjacent values
            cache.put(9, 9).await.unwrap();
            cache.put(11, 11).await.unwrap();

            // Verify intervals updated correctly
            let (current_end, start_next) = cache.next_gap(9);
            assert_eq!(current_end, Some(11));
            assert!(start_next.is_none());
        });
    }
}