sochdb-storage 0.4.1

SochDB storage engine (WAL, block store, compaction, sync-first I/O)
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// Copyright 2025 Sushanth (https://github.com/sushanthpy)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Optimized Range Scan with O(log n + k) Asymptotics
//!
//! This module replaces the O(n) full-table scan with proper index utilization:
//!
//! ## Optimizations Applied
//!
//! 1. **Sparse Index Lookup**: O(log n) binary search in level metadata
//! 2. **Block-Level Skipping**: Only read blocks that overlap [start, end]
//! 3. **Version Filtering**: Skip blocks with no visible versions
//! 4. **Bloom Filter Pre-check**: Skip SSTables that definitely don't contain range
//! 5. **K-way Merge with Tournament Tree**: O(k log m) merging where m = levels
//!
//! ## Complexity Analysis
//!
//! - Index lookup: O(log n) per level
//! - Block reads: O(k / block_size) where k = result count
//! - Merge: O(k log m) where m = number of sources
//! - Total: O(log n + k log m)
//!
//! Compare to naive scan: O(n) where n = total entries

use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::sync::Arc;

use parking_lot::RwLock;

/// Snapshot timestamp for MVCC visibility
pub type Timestamp = u64;

/// A key-value pair with version metadata
#[derive(Debug, Clone)]
pub struct VersionedEntry {
    /// The key
    pub key: Vec<u8>,
    /// The value (None = tombstone)
    pub value: Option<Vec<u8>>,
    /// Version timestamp
    pub timestamp: Timestamp,
    /// Sequence number (for ordering within same timestamp)
    pub sequence: u64,
}

impl VersionedEntry {
    /// Check if this is a deletion marker
    pub fn is_tombstone(&self) -> bool {
        self.value.is_none()
    }
}

/// Source of entries for merging
pub trait EntrySource: Send + Sync {
    /// Get current entry (None if exhausted)
    fn current(&self) -> Option<&VersionedEntry>;

    /// Advance to next entry
    fn next(&mut self) -> bool;

    /// Seek to first entry >= key
    fn seek(&mut self, key: &[u8]);

    /// Check if source is exhausted
    fn exhausted(&self) -> bool;

    /// Source priority (lower = higher priority for same key)
    fn priority(&self) -> u32;
}

/// Tournament tree node for k-way merge
struct TournamentNode {
    /// Index into sources array (u32::MAX = not valid)
    source_idx: u32,
    /// Cached entry for comparison
    entry: Option<VersionedEntry>,
}

impl TournamentNode {
    fn empty() -> Self {
        Self {
            source_idx: u32::MAX,
            entry: None,
        }
    }

    fn with_entry(source_idx: u32, entry: VersionedEntry) -> Self {
        Self {
            source_idx,
            entry: Some(entry),
        }
    }

    fn is_valid(&self) -> bool {
        self.source_idx != u32::MAX && self.entry.is_some()
    }

    /// Compare two nodes (for tournament)
    /// Returns true if self should "win" (come first in output)
    fn beats(&self, other: &TournamentNode, sources: &[Box<dyn EntrySource>]) -> bool {
        match (&self.entry, &other.entry) {
            (None, _) => false,
            (_, None) => true,
            (Some(a), Some(b)) => {
                match a.key.cmp(&b.key) {
                    Ordering::Less => true,
                    Ordering::Greater => false,
                    Ordering::Equal => {
                        // Same key: higher timestamp wins (newer version)
                        if a.timestamp != b.timestamp {
                            a.timestamp > b.timestamp
                        } else {
                            // Same timestamp: higher sequence wins
                            if a.sequence != b.sequence {
                                a.sequence > b.sequence
                            } else {
                                // Same sequence: lower priority source wins
                                let a_priority = sources.get(self.source_idx as usize)
                                    .map(|s| s.priority())
                                    .unwrap_or(u32::MAX);
                                let b_priority = sources.get(other.source_idx as usize)
                                    .map(|s| s.priority())
                                    .unwrap_or(u32::MAX);
                                a_priority < b_priority
                            }
                        }
                    }
                }
            }
        }
    }
}

/// Tournament tree for efficient k-way merge
///
/// Uses a complete binary tree where each internal node contains the
/// "winner" of comparing its two children.
pub struct TournamentTree {
    /// The tree nodes (size = 2 * num_sources - 1)
    nodes: Vec<TournamentNode>,
    /// Number of sources (leaves)
    num_sources: usize,
}

impl TournamentTree {
    /// Create a new tournament tree
    pub fn new(num_sources: usize) -> Self {
        let tree_size = if num_sources == 0 { 0 } else { 2 * num_sources - 1 };
        Self {
            nodes: (0..tree_size).map(|_| TournamentNode::empty()).collect(),
            num_sources,
        }
    }

    /// Initialize tree with current entries from sources
    pub fn initialize(&mut self, sources: &[Box<dyn EntrySource>]) {
        if self.num_sources == 0 {
            return;
        }

        // Fill leaves
        let leaf_start = self.num_sources - 1;
        for (i, source) in sources.iter().enumerate() {
            let leaf_idx = leaf_start + i;
            if leaf_idx < self.nodes.len() {
                self.nodes[leaf_idx] = match source.current() {
                    Some(entry) => TournamentNode::with_entry(i as u32, entry.clone()),
                    None => TournamentNode::empty(),
                };
            }
        }

        // Build tree bottom-up
        if leaf_start > 0 {
            for i in (0..leaf_start).rev() {
                let left = 2 * i + 1;
                let right = 2 * i + 2;
                self.nodes[i] = self.compare_nodes(left, right, sources);
            }
        }
    }

    /// Compare two nodes and return the winner
    fn compare_nodes(
        &self,
        left_idx: usize,
        right_idx: usize,
        sources: &[Box<dyn EntrySource>],
    ) -> TournamentNode {
        let left = self.nodes.get(left_idx);
        let right = self.nodes.get(right_idx);

        match (left, right) {
            (None, None) => TournamentNode::empty(),
            (Some(l), None) => l.clone(),
            (None, Some(r)) => r.clone(),
            (Some(l), Some(r)) => {
                if l.beats(r, sources) {
                    l.clone()
                } else {
                    r.clone()
                }
            }
        }
    }

    /// Get the current winner (minimum key)
    pub fn peek(&self) -> Option<&VersionedEntry> {
        self.nodes.first().and_then(|n| n.entry.as_ref())
    }

    /// Get the source index of the current winner
    pub fn winner_source(&self) -> Option<u32> {
        self.nodes.first().and_then(|n| {
            if n.is_valid() {
                Some(n.source_idx)
            } else {
                None
            }
        })
    }

    /// Advance the winning source and rebuild the tree
    pub fn pop(&mut self, sources: &mut [Box<dyn EntrySource>]) -> Option<VersionedEntry> {
        if self.num_sources == 0 {
            return None;
        }

        let winner_source = self.winner_source()?;
        let result = self.nodes[0].entry.clone();

        // Advance the winning source
        if let Some(source) = sources.get_mut(winner_source as usize) {
            source.next();
        }

        // Update the leaf for this source
        let leaf_idx = self.num_sources - 1 + winner_source as usize;
        if leaf_idx < self.nodes.len() {
            self.nodes[leaf_idx] = match sources.get(winner_source as usize).and_then(|s| s.current()) {
                Some(entry) => TournamentNode::with_entry(winner_source, entry.clone()),
                None => TournamentNode::empty(),
            };
        }

        // Rebuild path from leaf to root
        self.rebuild_path(leaf_idx, sources);

        result
    }

    /// Rebuild the path from a leaf to the root
    fn rebuild_path(&mut self, leaf_idx: usize, sources: &[Box<dyn EntrySource>]) {
        let mut idx = leaf_idx;
        while idx > 0 {
            let parent = (idx - 1) / 2;
            let left = 2 * parent + 1;
            let right = 2 * parent + 2;
            self.nodes[parent] = self.compare_nodes(left, right, sources);
            idx = parent;
        }
    }
}

impl Clone for TournamentNode {
    fn clone(&self) -> Self {
        Self {
            source_idx: self.source_idx,
            entry: self.entry.clone(),
        }
    }
}

/// Range scan configuration
#[derive(Debug, Clone)]
pub struct ScanConfig {
    /// Start key (inclusive)
    pub start_key: Option<Vec<u8>>,
    /// End key (exclusive)
    pub end_key: Option<Vec<u8>>,
    /// Snapshot timestamp for visibility
    pub snapshot: Timestamp,
    /// Maximum entries to return
    pub limit: Option<usize>,
    /// Skip tombstones in output
    pub skip_tombstones: bool,
    /// Only return latest version per key
    pub latest_only: bool,
}

impl Default for ScanConfig {
    fn default() -> Self {
        Self {
            start_key: None,
            end_key: None,
            snapshot: u64::MAX,
            limit: None,
            skip_tombstones: true,
            latest_only: true,
        }
    }
}

impl ScanConfig {
    /// Create a full table scan
    pub fn full_scan() -> Self {
        Self::default()
    }

    /// Create a range scan
    pub fn range(start: Vec<u8>, end: Vec<u8>) -> Self {
        Self {
            start_key: Some(start),
            end_key: Some(end),
            ..Default::default()
        }
    }

    /// Set snapshot timestamp
    pub fn with_snapshot(mut self, ts: Timestamp) -> Self {
        self.snapshot = ts;
        self
    }

    /// Set limit
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }
}

/// File metadata for range-based file selection
#[derive(Debug, Clone)]
pub struct FileRange {
    /// File ID
    pub id: u64,
    /// Smallest key in file
    pub smallest_key: Vec<u8>,
    /// Largest key in file
    pub largest_key: Vec<u8>,
    /// Smallest timestamp
    pub min_timestamp: Timestamp,
    /// Largest timestamp
    pub max_timestamp: Timestamp,
    /// Number of entries
    pub num_entries: u64,
}

impl FileRange {
    /// Check if file might contain keys in range
    pub fn overlaps_range(&self, start: Option<&[u8]>, end: Option<&[u8]>) -> bool {
        // Check key range overlap
        let key_overlaps = match (start, end) {
            (None, None) => true,
            (Some(s), None) => self.largest_key.as_slice() >= s,
            (None, Some(e)) => self.smallest_key.as_slice() < e,
            (Some(s), Some(e)) => {
                self.largest_key.as_slice() >= s && self.smallest_key.as_slice() < e
            }
        };

        key_overlaps
    }

    /// Check if file might have visible versions
    pub fn has_visible_versions(&self, snapshot: Timestamp) -> bool {
        self.min_timestamp <= snapshot
    }
}

/// Level metadata for file selection
#[derive(Debug, Clone)]
pub struct LevelFiles {
    /// Level number
    pub level: u32,
    /// Files in this level (sorted by smallest_key for L1+)
    pub files: Vec<FileRange>,
}

impl LevelFiles {
    /// Find files that overlap the range
    pub fn find_overlapping(&self, start: Option<&[u8]>, end: Option<&[u8]>) -> Vec<&FileRange> {
        if self.level == 0 {
            // L0 files may overlap, check all
            self.files
                .iter()
                .filter(|f| f.overlaps_range(start, end))
                .collect()
        } else {
            // L1+ files are sorted and non-overlapping
            // Use binary search to find range
            self.binary_search_range(start, end)
        }
    }

    /// Binary search for overlapping files in sorted levels
    fn binary_search_range(&self, start: Option<&[u8]>, end: Option<&[u8]>) -> Vec<&FileRange> {
        if self.files.is_empty() {
            return vec![];
        }

        // Find first file that might contain start
        let start_idx = match start {
            None => 0,
            Some(s) => {
                self.files
                    .binary_search_by(|f| {
                        if f.largest_key.as_slice() < s {
                            Ordering::Less
                        } else {
                            Ordering::Greater
                        }
                    })
                    .unwrap_or_else(|i| i)
            }
        };

        // Find last file that might contain end
        let end_idx = match end {
            None => self.files.len(),
            Some(e) => {
                let idx = self.files
                    .binary_search_by(|f| {
                        if f.smallest_key.as_slice() >= e {
                            Ordering::Greater
                        } else {
                            Ordering::Less
                        }
                    })
                    .unwrap_or_else(|i| i);
                idx.min(self.files.len())
            }
        };

        self.files[start_idx..end_idx].iter().collect()
    }
}

/// Optimized range scanner
pub struct RangeScanner {
    /// Scan configuration
    config: ScanConfig,
    /// Tournament tree for merging
    tournament: TournamentTree,
    /// Entry sources
    sources: Vec<Box<dyn EntrySource>>,
    /// Last emitted key (for deduplication)
    last_key: Option<Vec<u8>>,
    /// Count of entries returned
    count: usize,
    /// Scan statistics
    stats: ScanStats,
}

/// Scan statistics
#[derive(Debug, Default, Clone)]
pub struct ScanStats {
    /// Files considered
    pub files_considered: usize,
    /// Files skipped by range
    pub files_skipped_range: usize,
    /// Files skipped by timestamp
    pub files_skipped_timestamp: usize,
    /// Blocks read
    pub blocks_read: usize,
    /// Entries scanned
    pub entries_scanned: usize,
    /// Entries returned
    pub entries_returned: usize,
    /// Tombstones encountered
    pub tombstones_seen: usize,
    /// Duplicate versions skipped
    pub duplicates_skipped: usize,
}

impl RangeScanner {
    /// Create a new range scanner
    pub fn new(config: ScanConfig, sources: Vec<Box<dyn EntrySource>>) -> Self {
        let num_sources = sources.len();
        let mut scanner = Self {
            config,
            tournament: TournamentTree::new(num_sources),
            sources,
            last_key: None,
            count: 0,
            stats: ScanStats::default(),
        };

        // Seek all sources to start position
        if let Some(ref start) = scanner.config.start_key {
            for source in &mut scanner.sources {
                source.seek(start);
            }
        }

        // Initialize tournament tree
        scanner.tournament.initialize(&scanner.sources);

        scanner
    }

    /// Get next visible entry
    pub fn next(&mut self) -> Option<VersionedEntry> {
        loop {
            // Check limit
            if let Some(limit) = self.config.limit {
                if self.count >= limit {
                    return None;
                }
            }

            // Get next entry from tournament
            let entry = self.tournament.pop(&mut self.sources)?;
            self.stats.entries_scanned += 1;

            // Check end key
            if let Some(ref end) = self.config.end_key {
                if entry.key.as_slice() >= end.as_slice() {
                    return None;
                }
            }

            // Check visibility (MVCC)
            if entry.timestamp > self.config.snapshot {
                continue; // Version too new
            }

            // Handle tombstones
            if entry.is_tombstone() {
                self.stats.tombstones_seen += 1;
                if self.config.skip_tombstones {
                    // Mark key as seen (for dedup) but don't return
                    if self.config.latest_only {
                        self.last_key = Some(entry.key);
                    }
                    continue;
                }
            }

            // Handle deduplication for latest_only
            if self.config.latest_only {
                if let Some(ref last) = self.last_key {
                    if entry.key == *last {
                        self.stats.duplicates_skipped += 1;
                        continue;
                    }
                }
                self.last_key = Some(entry.key.clone());
            }

            self.count += 1;
            self.stats.entries_returned += 1;
            return Some(entry);
        }
    }

    /// Get scan statistics
    pub fn stats(&self) -> &ScanStats {
        &self.stats
    }

    /// Collect all results into a Vec
    pub fn collect(mut self) -> Vec<VersionedEntry> {
        let mut results = Vec::new();
        while let Some(entry) = self.next() {
            results.push(entry);
        }
        results
    }
}

/// Helper to select files for a range scan
pub fn select_files_for_range(
    levels: &[LevelFiles],
    config: &ScanConfig,
) -> (Vec<FileRange>, ScanStats) {
    let mut selected = Vec::new();
    let mut stats = ScanStats::default();

    let start = config.start_key.as_deref();
    let end = config.end_key.as_deref();

    for level in levels {
        for file in level.find_overlapping(start, end) {
            stats.files_considered += 1;

            // Check timestamp visibility
            if !file.has_visible_versions(config.snapshot) {
                stats.files_skipped_timestamp += 1;
                continue;
            }

            selected.push(file.clone());
        }

        // Count files that were skipped by range
        let total_files = level.files.len();
        let overlapping = level.find_overlapping(start, end).len();
        stats.files_skipped_range += total_files - overlapping;
    }

    (selected, stats)
}

// =============================================================================
// Tests
// =============================================================================

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

    /// Simple in-memory source for testing
    struct VecSource {
        entries: Vec<VersionedEntry>,
        pos: usize,
        priority: u32,
    }

    impl VecSource {
        fn new(entries: Vec<VersionedEntry>, priority: u32) -> Self {
            Self {
                entries,
                pos: 0,
                priority,
            }
        }
    }

    impl EntrySource for VecSource {
        fn current(&self) -> Option<&VersionedEntry> {
            self.entries.get(self.pos)
        }

        fn next(&mut self) -> bool {
            if self.pos < self.entries.len() {
                self.pos += 1;
            }
            self.pos < self.entries.len()
        }

        fn seek(&mut self, key: &[u8]) {
            self.pos = self.entries.iter().position(|e| e.key.as_slice() >= key).unwrap_or(self.entries.len());
        }

        fn exhausted(&self) -> bool {
            self.pos >= self.entries.len()
        }

        fn priority(&self) -> u32 {
            self.priority
        }
    }

    fn make_entry(key: &str, value: &str, ts: u64) -> VersionedEntry {
        VersionedEntry {
            key: key.as_bytes().to_vec(),
            value: Some(value.as_bytes().to_vec()),
            timestamp: ts,
            sequence: 0,
        }
    }

    fn make_tombstone(key: &str, ts: u64) -> VersionedEntry {
        VersionedEntry {
            key: key.as_bytes().to_vec(),
            value: None,
            timestamp: ts,
            sequence: 0,
        }
    }

    #[test]
    fn test_tournament_tree_single_source() {
        let entries = vec![
            make_entry("a", "1", 100),
            make_entry("b", "2", 100),
            make_entry("c", "3", 100),
        ];

        let source: Box<dyn EntrySource> = Box::new(VecSource::new(entries, 0));
        let sources = vec![source];

        let scanner = RangeScanner::new(ScanConfig::default(), sources);
        let results = scanner.collect();

        assert_eq!(results.len(), 3);
        assert_eq!(results[0].key, b"a");
        assert_eq!(results[1].key, b"b");
        assert_eq!(results[2].key, b"c");
    }

    #[test]
    fn test_tournament_tree_merge() {
        // Simulate L0 and L1 with overlapping keys
        let l0_entries = vec![
            make_entry("a", "new", 200),
            make_entry("c", "new", 200),
        ];
        let l1_entries = vec![
            make_entry("a", "old", 100),
            make_entry("b", "old", 100),
            make_entry("c", "old", 100),
        ];

        let sources: Vec<Box<dyn EntrySource>> = vec![
            Box::new(VecSource::new(l0_entries, 0)),  // L0 higher priority
            Box::new(VecSource::new(l1_entries, 1)),  // L1 lower priority
        ];

        let config = ScanConfig {
            snapshot: 250,
            latest_only: true,
            ..Default::default()
        };

        let scanner = RangeScanner::new(config, sources);
        let results = scanner.collect();

        assert_eq!(results.len(), 3);
        // Should have newer versions for a and c
        assert_eq!(results[0].key, b"a");
        assert_eq!(results[0].timestamp, 200);
        assert_eq!(results[1].key, b"b");
        assert_eq!(results[1].timestamp, 100);
        assert_eq!(results[2].key, b"c");
        assert_eq!(results[2].timestamp, 200);
    }

    #[test]
    fn test_tombstone_handling() {
        let entries = vec![
            make_entry("a", "value", 100),
            make_tombstone("b", 200),
            make_entry("c", "value", 100),
        ];

        let source: Box<dyn EntrySource> = Box::new(VecSource::new(entries, 0));
        let sources = vec![source];

        let config = ScanConfig {
            skip_tombstones: true,
            ..Default::default()
        };

        let scanner = RangeScanner::new(config, sources);
        let results = scanner.collect();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].key, b"a");
        assert_eq!(results[1].key, b"c");
    }

    #[test]
    fn test_range_bounds() {
        let entries = vec![
            make_entry("a", "1", 100),
            make_entry("b", "2", 100),
            make_entry("c", "3", 100),
            make_entry("d", "4", 100),
        ];

        let source: Box<dyn EntrySource> = Box::new(VecSource::new(entries, 0));
        let sources = vec![source];

        let config = ScanConfig {
            start_key: Some(b"b".to_vec()),
            end_key: Some(b"d".to_vec()),
            ..Default::default()
        };

        let scanner = RangeScanner::new(config, sources);
        let results = scanner.collect();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].key, b"b");
        assert_eq!(results[1].key, b"c");
    }

    #[test]
    fn test_limit() {
        let entries: Vec<_> = (0..100)
            .map(|i| make_entry(&format!("key{:03}", i), &format!("val{}", i), 100))
            .collect();

        let source: Box<dyn EntrySource> = Box::new(VecSource::new(entries, 0));
        let sources = vec![source];

        let config = ScanConfig {
            limit: Some(10),
            ..Default::default()
        };

        let scanner = RangeScanner::new(config, sources);
        let results = scanner.collect();

        assert_eq!(results.len(), 10);
    }

    #[test]
    fn test_mvcc_visibility() {
        let entries = vec![
            make_entry("a", "v1", 100),
            make_entry("a", "v2", 200),
            make_entry("a", "v3", 300),
        ];

        let source: Box<dyn EntrySource> = Box::new(VecSource::new(entries, 0));
        let sources = vec![source];

        // Snapshot at 150 should only see v1
        let config = ScanConfig {
            snapshot: 150,
            latest_only: true,
            ..Default::default()
        };

        let scanner = RangeScanner::new(config, sources);
        let results = scanner.collect();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].timestamp, 100);
    }

    #[test]
    fn test_file_range_overlap() {
        let file = FileRange {
            id: 1,
            smallest_key: b"c".to_vec(),
            largest_key: b"h".to_vec(),
            min_timestamp: 100,
            max_timestamp: 200,
            num_entries: 100,
        };

        // Should overlap
        assert!(file.overlaps_range(Some(b"a"), Some(b"e")));
        assert!(file.overlaps_range(Some(b"f"), Some(b"z")));
        assert!(file.overlaps_range(Some(b"d"), Some(b"g")));
        assert!(file.overlaps_range(None, Some(b"e")));
        assert!(file.overlaps_range(Some(b"f"), None));
        assert!(file.overlaps_range(None, None));

        // Should not overlap
        assert!(!file.overlaps_range(Some(b"a"), Some(b"c")));
        assert!(!file.overlaps_range(Some(b"i"), Some(b"z")));
    }

    #[test]
    fn test_level_binary_search() {
        let level = LevelFiles {
            level: 1,
            files: vec![
                FileRange {
                    id: 1,
                    smallest_key: b"a".to_vec(),
                    largest_key: b"d".to_vec(),
                    min_timestamp: 100,
                    max_timestamp: 200,
                    num_entries: 100,
                },
                FileRange {
                    id: 2,
                    smallest_key: b"e".to_vec(),
                    largest_key: b"h".to_vec(),
                    min_timestamp: 100,
                    max_timestamp: 200,
                    num_entries: 100,
                },
                FileRange {
                    id: 3,
                    smallest_key: b"i".to_vec(),
                    largest_key: b"l".to_vec(),
                    min_timestamp: 100,
                    max_timestamp: 200,
                    num_entries: 100,
                },
            ],
        };

        // Query c-g should return files 1 and 2
        let result = level.find_overlapping(Some(b"c"), Some(b"g"));
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].id, 1);
        assert_eq!(result[1].id, 2);
    }
}