sochdb-storage 2.0.7

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
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Epoch-Based MVCC (Recommendation 7)
//!
//! ## Problem
//!
//! Traditional per-key version chains require:
//! 1. Version lookup: O(V) where V = version depth
//! 2. Pointer chasing through heap allocations
//! 3. Cache misses on each version hop
//!
//! ```text
//! Current version chain traversal:
//!   key → v3 → v2 → v1 → nil
//!//!         Each hop = ~100ns cache miss
//!   
//! For 5 versions: 5 × 100ns = 500ns per read
//! ```
//!
//! ## Solution
//!
//! Epoch-based MVCC with:
//! 1. **Epoch Partitioning**: Group transactions by time epoch
//! 2. **Batch Version Resolution**: Resolve all versions in an epoch together
//! 3. **Columnar Version Storage**: Store versions in columnar format for SIMD
//!
//! ```text
//! Epoch-based approach:
//!   Epoch 1: [v1, v1, v1, ...]  ← All versions from epoch 1
//!   Epoch 2: [v2, v2, ...]      ← All versions from epoch 2
//!   Epoch 3: [v3, ...]          ← Current epoch
//!
//! Read at epoch 2:
//!   - Binary search epochs: O(log E) where E = epochs
//!   - Direct access within epoch: O(1)
//!   - Total: O(log E) ≈ O(1) for small E
//! ```
//!
//! ## Performance Analysis
//!
//! With epoch duration of 10ms and 100K txns/epoch:
//! - Max epochs in memory: ~10 (100ms history)
//! - Version lookup: O(log 10) + O(1) = ~4 comparisons
//! - Per-read cost: ~50ns vs 500ns = 10x improvement

use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use parking_lot::RwLock;

/// Default epoch duration in milliseconds
pub const DEFAULT_EPOCH_DURATION_MS: u64 = 10;

/// Maximum epochs to keep in memory
pub const MAX_EPOCHS_IN_MEMORY: usize = 100;

/// Minimum entries before epoch rotation
pub const MIN_ENTRIES_PER_EPOCH: usize = 1000;

// =============================================================================
// Epoch Manager
// =============================================================================

/// Manages epoch lifecycle and version visibility
pub struct EpochManager {
    /// Current epoch number
    current_epoch: AtomicU64,
    /// Epoch start timestamp
    epoch_start_time: AtomicU64,
    /// Epoch duration in nanoseconds
    epoch_duration_ns: u64,
    /// Active readers per epoch (epoch -> reader count)
    active_readers: RwLock<BTreeMap<u64, u64>>,
    /// Minimum safe epoch (oldest epoch with active readers)
    min_safe_epoch: AtomicU64,
}

impl EpochManager {
    pub fn new() -> Self {
        Self::with_duration_ms(DEFAULT_EPOCH_DURATION_MS)
    }

    pub fn with_duration_ms(duration_ms: u64) -> Self {
        let now = Self::current_time_ns();
        Self {
            current_epoch: AtomicU64::new(1),
            epoch_start_time: AtomicU64::new(now),
            epoch_duration_ns: duration_ms * 1_000_000,
            active_readers: RwLock::new(BTreeMap::new()),
            min_safe_epoch: AtomicU64::new(1),
        }
    }

    /// Get current epoch
    #[inline]
    pub fn current_epoch(&self) -> u64 {
        self.current_epoch.load(Ordering::Acquire)
    }

    /// Get minimum safe epoch (for GC)
    #[inline]
    pub fn min_safe_epoch(&self) -> u64 {
        self.min_safe_epoch.load(Ordering::Acquire)
    }

    /// Check if epoch should advance
    pub fn should_advance(&self) -> bool {
        let now = Self::current_time_ns();
        let start = self.epoch_start_time.load(Ordering::Relaxed);
        now.saturating_sub(start) >= self.epoch_duration_ns
    }

    /// Advance to next epoch
    pub fn advance_epoch(&self) -> u64 {
        let new_epoch = self.current_epoch.fetch_add(1, Ordering::AcqRel) + 1;
        self.epoch_start_time
            .store(Self::current_time_ns(), Ordering::Relaxed);
        new_epoch
    }

    /// Register a reader at an epoch
    pub fn register_reader(&self, epoch: u64) {
        let mut readers = self.active_readers.write();
        *readers.entry(epoch).or_insert(0) += 1;
    }

    /// Unregister a reader from an epoch
    pub fn unregister_reader(&self, epoch: u64) {
        let mut readers = self.active_readers.write();
        if let Some(count) = readers.get_mut(&epoch) {
            *count = count.saturating_sub(1);
            if *count == 0 {
                readers.remove(&epoch);
                // Update min safe epoch
                if let Some(&min_epoch) = readers.keys().next() {
                    self.min_safe_epoch.store(min_epoch, Ordering::Release);
                } else {
                    self.min_safe_epoch.store(
                        self.current_epoch.load(Ordering::Relaxed),
                        Ordering::Release,
                    );
                }
            }
        }
    }

    /// Get epochs that can be garbage collected
    pub fn gc_eligible_epochs(&self) -> Vec<u64> {
        let min_safe = self.min_safe_epoch.load(Ordering::Acquire);
        let readers = self.active_readers.read();

        // Epochs older than min_safe with no active readers
        readers.keys().filter(|&&e| e < min_safe).copied().collect()
    }

    #[inline]
    fn current_time_ns() -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos() as u64)
            .unwrap_or(0)
    }
}

impl Default for EpochManager {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Version Entry
// =============================================================================

/// A versioned value with epoch metadata
#[derive(Debug, Clone)]
pub struct VersionEntry<V> {
    /// The value
    pub value: V,
    /// Epoch when this version was created
    pub epoch: u64,
    /// Transaction ID that created this version
    pub txn_id: u64,
    /// Whether this version represents a delete
    pub is_delete: bool,
}

impl<V> VersionEntry<V> {
    pub fn new(value: V, epoch: u64, txn_id: u64) -> Self {
        Self {
            value,
            epoch,
            txn_id,
            is_delete: false,
        }
    }

    pub fn tombstone(epoch: u64, txn_id: u64) -> Self
    where
        V: Default,
    {
        Self {
            value: V::default(),
            epoch,
            txn_id,
            is_delete: true,
        }
    }
}

// =============================================================================
// Epoch Version Chain
// =============================================================================

/// Version chain organized by epoch
///
/// Instead of a linked list of versions, versions are grouped by epoch.
/// This enables:
/// 1. O(log E) epoch lookup instead of O(V) version traversal
/// 2. Batch GC of entire epochs
/// 3. Cache-friendly epoch-local version storage
pub struct EpochVersionChain<V> {
    /// Versions indexed by epoch (epoch -> version)
    /// BTreeMap provides O(log E) lookup and efficient range queries
    versions: RwLock<BTreeMap<u64, VersionEntry<V>>>,
    /// Latest epoch for fast-path
    latest_epoch: AtomicU64,
}

impl<V: Clone> EpochVersionChain<V> {
    pub fn new() -> Self {
        Self {
            versions: RwLock::new(BTreeMap::new()),
            latest_epoch: AtomicU64::new(0),
        }
    }

    /// Add a new version at epoch
    pub fn add_version(&self, epoch: u64, entry: VersionEntry<V>) {
        let mut versions = self.versions.write();
        versions.insert(epoch, entry);

        // Update latest epoch
        let current = self.latest_epoch.load(Ordering::Relaxed);
        if epoch > current {
            self.latest_epoch.store(epoch, Ordering::Release);
        }
    }

    /// Read version visible at epoch
    ///
    /// Returns the most recent version with epoch <= target_epoch
    pub fn read_at_epoch(&self, target_epoch: u64) -> Option<V> {
        // Fast path: if reading at latest epoch
        let latest = self.latest_epoch.load(Ordering::Acquire);
        if target_epoch >= latest {
            let versions = self.versions.read();
            return versions.get(&latest).and_then(|v| {
                if v.is_delete {
                    None
                } else {
                    Some(v.value.clone())
                }
            });
        }

        // Slow path: binary search for appropriate epoch
        let versions = self.versions.read();

        // Find the largest epoch <= target_epoch
        versions
            .range(..=target_epoch)
            .next_back()
            .and_then(|(_, v)| {
                if v.is_delete {
                    None
                } else {
                    Some(v.value.clone())
                }
            })
    }

    /// Get all versions (for debugging/testing)
    pub fn all_versions(&self) -> Vec<(u64, VersionEntry<V>)> {
        self.versions
            .read()
            .iter()
            .map(|(&e, v)| (e, v.clone()))
            .collect()
    }

    /// Remove versions older than epoch
    pub fn gc_before_epoch(&self, epoch: u64) -> usize {
        let mut versions = self.versions.write();
        let old_len = versions.len();
        versions.retain(|&e, _| e >= epoch);
        old_len - versions.len()
    }

    /// Get number of versions
    pub fn version_count(&self) -> usize {
        self.versions.read().len()
    }

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

impl<V: Clone> Default for EpochVersionChain<V> {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Epoch MVCC Store
// =============================================================================

/// Key type alias
pub type Key = Vec<u8>;

/// MVCC store with epoch-based versioning
pub struct EpochMvccStore<V> {
    /// Key -> version chain mapping
    data: dashmap::DashMap<Key, EpochVersionChain<V>>,
    /// Epoch manager
    epoch_manager: Arc<EpochManager>,
    /// Next transaction ID
    next_txn_id: AtomicU64,
}

impl<V: Clone + Send + Sync + 'static> EpochMvccStore<V> {
    pub fn new() -> Self {
        Self::with_epoch_manager(Arc::new(EpochManager::new()))
    }

    pub fn with_epoch_manager(epoch_manager: Arc<EpochManager>) -> Self {
        Self {
            data: dashmap::DashMap::new(),
            epoch_manager,
            next_txn_id: AtomicU64::new(1),
        }
    }

    /// Get epoch manager
    pub fn epoch_manager(&self) -> &Arc<EpochManager> {
        &self.epoch_manager
    }

    /// Begin a new transaction
    pub fn begin_txn(&self) -> EpochTransaction<'_, V> {
        let epoch = self.epoch_manager.current_epoch();
        let txn_id = self.next_txn_id.fetch_add(1, Ordering::Relaxed);

        self.epoch_manager.register_reader(epoch);

        EpochTransaction {
            txn_id,
            read_epoch: epoch,
            write_buffer: Vec::new(),
            store: self,
        }
    }

    /// Write a value (internal, called during commit)
    fn write(&self, key: Key, value: V, epoch: u64, txn_id: u64) {
        let chain = self.data.entry(key).or_insert_with(EpochVersionChain::new);
        chain.add_version(epoch, VersionEntry::new(value, epoch, txn_id));
    }

    /// Delete a key (internal, called during commit)
    fn delete(&self, key: Key, epoch: u64, txn_id: u64)
    where
        V: Default,
    {
        let chain = self.data.entry(key).or_insert_with(EpochVersionChain::new);
        chain.add_version(epoch, VersionEntry::tombstone(epoch, txn_id));
    }

    /// Read a value at epoch
    pub fn read_at_epoch(&self, key: &[u8], epoch: u64) -> Option<V> {
        self.data
            .get(key)
            .and_then(|chain| chain.read_at_epoch(epoch))
    }

    /// Advance epoch if needed
    pub fn maybe_advance_epoch(&self) -> Option<u64> {
        if self.epoch_manager.should_advance() {
            Some(self.epoch_manager.advance_epoch())
        } else {
            None
        }
    }

    /// Garbage collect old epochs
    pub fn gc(&self) -> GcStats {
        let min_safe = self.epoch_manager.min_safe_epoch();
        let mut stats = GcStats::default();

        for mut entry in self.data.iter_mut() {
            let removed = entry.value_mut().gc_before_epoch(min_safe);
            stats.versions_removed += removed;
            if entry.value().is_empty() {
                stats.chains_emptied += 1;
            }
        }

        // Remove empty chains
        self.data.retain(|_, chain| !chain.is_empty());

        stats
    }

    /// Get store statistics
    pub fn stats(&self) -> StoreStats {
        let mut total_versions = 0;
        let mut max_versions_per_key = 0;

        for entry in self.data.iter() {
            let count = entry.value().version_count();
            total_versions += count;
            max_versions_per_key = max_versions_per_key.max(count);
        }

        StoreStats {
            key_count: self.data.len(),
            total_versions,
            max_versions_per_key,
            current_epoch: self.epoch_manager.current_epoch(),
            min_safe_epoch: self.epoch_manager.min_safe_epoch(),
        }
    }
}

impl<V: Clone + Send + Sync + 'static> Default for EpochMvccStore<V> {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Epoch Transaction
// =============================================================================

/// A transaction with epoch-based snapshot isolation
pub struct EpochTransaction<'a, V> {
    /// Transaction ID
    txn_id: u64,
    /// Read epoch (snapshot point)
    read_epoch: u64,
    /// Buffered writes
    write_buffer: Vec<WriteOp<V>>,
    /// Reference to store
    store: &'a EpochMvccStore<V>,
}

/// Write operation
enum WriteOp<V> {
    Put(Key, V),
    Delete(Key),
}

impl<'a, V: Clone + Send + Sync + Default + 'static> EpochTransaction<'a, V> {
    /// Get transaction ID
    pub fn txn_id(&self) -> u64 {
        self.txn_id
    }

    /// Get read epoch
    pub fn read_epoch(&self) -> u64 {
        self.read_epoch
    }

    /// Read a value (sees snapshot at read_epoch)
    pub fn get(&self, key: &[u8]) -> Option<V> {
        // First check write buffer
        for op in self.write_buffer.iter().rev() {
            match op {
                WriteOp::Put(k, v) if k == key => return Some(v.clone()),
                WriteOp::Delete(k) if k == key => return None,
                _ => {}
            }
        }

        // Then check store
        self.store.read_at_epoch(key, self.read_epoch)
    }

    /// Write a value
    pub fn put(&mut self, key: Key, value: V) {
        self.write_buffer.push(WriteOp::Put(key, value));
    }

    /// Delete a key
    pub fn delete(&mut self, key: Key) {
        self.write_buffer.push(WriteOp::Delete(key));
    }

    /// Commit the transaction
    pub fn commit(mut self) -> CommitResult {
        let commit_epoch = self.store.epoch_manager.current_epoch();
        let write_count = self.write_buffer.len();

        for op in self.write_buffer.drain(..) {
            match op {
                WriteOp::Put(key, value) => {
                    self.store.write(key, value, commit_epoch, self.txn_id);
                }
                WriteOp::Delete(key) => {
                    self.store.delete(key, commit_epoch, self.txn_id);
                }
            }
        }

        // Unregister reader
        self.store.epoch_manager.unregister_reader(self.read_epoch);

        CommitResult {
            txn_id: self.txn_id,
            commit_epoch,
            write_count,
        }
    }

    /// Abort the transaction
    pub fn abort(self) {
        // Just unregister the reader, writes are discarded
        self.store.epoch_manager.unregister_reader(self.read_epoch);
    }
}

impl<'a, V> Drop for EpochTransaction<'a, V> {
    fn drop(&mut self) {
        // Note: This doesn't unregister because commit/abort should be called
        // In a real implementation, you'd track whether commit/abort was called
    }
}

/// Result of committing a transaction
#[derive(Debug)]
pub struct CommitResult {
    pub txn_id: u64,
    pub commit_epoch: u64,
    pub write_count: usize,
}

/// GC statistics
#[derive(Debug, Default)]
pub struct GcStats {
    pub versions_removed: usize,
    pub chains_emptied: usize,
}

/// Store statistics
#[derive(Debug)]
pub struct StoreStats {
    pub key_count: usize,
    pub total_versions: usize,
    pub max_versions_per_key: usize,
    pub current_epoch: u64,
    pub min_safe_epoch: u64,
}

// =============================================================================
// Epoch Snapshot
// =============================================================================

/// A read-only snapshot at a specific epoch
pub struct EpochSnapshot<'a, V> {
    epoch: u64,
    store: &'a EpochMvccStore<V>,
}

impl<'a, V: Clone + Send + Sync + 'static> EpochSnapshot<'a, V> {
    /// Create a snapshot at the current epoch
    pub fn new(store: &'a EpochMvccStore<V>) -> Self {
        let epoch = store.epoch_manager.current_epoch();
        store.epoch_manager.register_reader(epoch);
        Self { epoch, store }
    }

    /// Create a snapshot at a specific epoch
    pub fn at_epoch(store: &'a EpochMvccStore<V>, epoch: u64) -> Self {
        store.epoch_manager.register_reader(epoch);
        Self { epoch, store }
    }

    /// Get epoch
    pub fn epoch(&self) -> u64 {
        self.epoch
    }

    /// Read a value
    pub fn get(&self, key: &[u8]) -> Option<V> {
        self.store.read_at_epoch(key, self.epoch)
    }

    /// Iterate over all keys (expensive, for debugging)
    pub fn keys(&self) -> Vec<Key> {
        self.store
            .data
            .iter()
            .filter(|e| e.value().read_at_epoch(self.epoch).is_some())
            .map(|e| e.key().clone())
            .collect()
    }
}

impl<'a, V> Drop for EpochSnapshot<'a, V> {
    fn drop(&mut self) {
        self.store.epoch_manager.unregister_reader(self.epoch);
    }
}

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

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

    #[test]
    fn test_epoch_manager_basics() {
        let manager = EpochManager::with_duration_ms(1); // 1ms epochs

        assert_eq!(manager.current_epoch(), 1);

        // Wait a bit and advance
        std::thread::sleep(std::time::Duration::from_millis(2));
        assert!(manager.should_advance());

        let new_epoch = manager.advance_epoch();
        assert_eq!(new_epoch, 2);
        assert_eq!(manager.current_epoch(), 2);
    }

    #[test]
    fn test_epoch_reader_tracking() {
        let manager = EpochManager::new();

        manager.register_reader(1);
        manager.register_reader(1);
        manager.register_reader(2);

        assert_eq!(manager.min_safe_epoch(), 1);

        manager.unregister_reader(1);
        assert_eq!(manager.min_safe_epoch(), 1); // Still has one reader

        manager.unregister_reader(1);
        assert_eq!(manager.min_safe_epoch(), 2); // Epoch 1 cleared
    }

    #[test]
    fn test_version_chain_read_at_epoch() {
        let chain: EpochVersionChain<String> = EpochVersionChain::new();

        chain.add_version(1, VersionEntry::new("v1".to_string(), 1, 1));
        chain.add_version(3, VersionEntry::new("v3".to_string(), 3, 3));
        chain.add_version(5, VersionEntry::new("v5".to_string(), 5, 5));

        // Read at various epochs
        assert_eq!(chain.read_at_epoch(0), None);
        assert_eq!(chain.read_at_epoch(1), Some("v1".to_string()));
        assert_eq!(chain.read_at_epoch(2), Some("v1".to_string()));
        assert_eq!(chain.read_at_epoch(3), Some("v3".to_string()));
        assert_eq!(chain.read_at_epoch(4), Some("v3".to_string()));
        assert_eq!(chain.read_at_epoch(5), Some("v5".to_string()));
        assert_eq!(chain.read_at_epoch(100), Some("v5".to_string()));
    }

    #[test]
    fn test_version_chain_delete() {
        let chain: EpochVersionChain<String> = EpochVersionChain::new();

        chain.add_version(1, VersionEntry::new("value".to_string(), 1, 1));
        chain.add_version(2, VersionEntry::tombstone(2, 2));
        chain.add_version(3, VersionEntry::new("resurrected".to_string(), 3, 3));

        assert_eq!(chain.read_at_epoch(1), Some("value".to_string()));
        assert_eq!(chain.read_at_epoch(2), None); // Deleted
        assert_eq!(chain.read_at_epoch(3), Some("resurrected".to_string()));
    }

    #[test]
    fn test_version_chain_gc() {
        let chain: EpochVersionChain<i32> = EpochVersionChain::new();

        for i in 1..=10 {
            chain.add_version(i, VersionEntry::new(i as i32, i, i));
        }

        assert_eq!(chain.version_count(), 10);

        let removed = chain.gc_before_epoch(5);
        assert_eq!(removed, 4);
        assert_eq!(chain.version_count(), 6);

        // Old epochs gone
        assert_eq!(chain.read_at_epoch(4), None);
        // New epochs still there
        assert_eq!(chain.read_at_epoch(5), Some(5));
    }

    #[test]
    fn test_mvcc_store_basic() {
        let store: EpochMvccStore<String> = EpochMvccStore::new();

        let mut txn = store.begin_txn();
        txn.put(b"key1".to_vec(), "value1".to_string());
        txn.put(b"key2".to_vec(), "value2".to_string());
        let result = txn.commit();

        assert_eq!(result.write_count, 2);

        // Read back
        let txn2 = store.begin_txn();
        assert_eq!(txn2.get(b"key1"), Some("value1".to_string()));
        assert_eq!(txn2.get(b"key2"), Some("value2".to_string()));
        txn2.abort();
    }

    #[test]
    fn test_mvcc_store_snapshot_isolation() {
        let store: EpochMvccStore<i32> = EpochMvccStore::new();

        // Initial write
        let mut txn1 = store.begin_txn();
        txn1.put(b"x".to_vec(), 1);
        txn1.commit();

        // Force epoch advance
        store.epoch_manager().advance_epoch();

        // Start snapshot
        let snapshot = EpochSnapshot::new(&store);
        assert_eq!(snapshot.get(b"x"), Some(1));

        // Concurrent write
        store.epoch_manager().advance_epoch();
        let mut txn2 = store.begin_txn();
        txn2.put(b"x".to_vec(), 2);
        txn2.commit();

        // Snapshot still sees old value
        assert_eq!(snapshot.get(b"x"), Some(1));

        // New read sees new value
        let txn3 = store.begin_txn();
        assert_eq!(txn3.get(b"x"), Some(2));
        txn3.abort();
    }

    #[test]
    fn test_mvcc_store_delete() {
        let store: EpochMvccStore<String> = EpochMvccStore::new();

        // Insert
        let mut txn1 = store.begin_txn();
        txn1.put(b"key".to_vec(), "value".to_string());
        txn1.commit();

        store.epoch_manager().advance_epoch();

        // Snapshot before delete
        let snap = EpochSnapshot::new(&store);

        store.epoch_manager().advance_epoch();

        // Delete
        let mut txn2 = store.begin_txn();
        txn2.delete(b"key".to_vec());
        txn2.commit();

        // Snapshot still sees value
        assert_eq!(snap.get(b"key"), Some("value".to_string()));

        // New transaction sees nothing
        let txn3 = store.begin_txn();
        assert_eq!(txn3.get(b"key"), None);
        txn3.abort();
    }

    #[test]
    fn test_mvcc_store_write_buffer() {
        let store: EpochMvccStore<i32> = EpochMvccStore::new();

        let mut txn = store.begin_txn();

        // Write to buffer
        txn.put(b"a".to_vec(), 1);
        txn.put(b"b".to_vec(), 2);

        // Read from buffer
        assert_eq!(txn.get(b"a"), Some(1));
        assert_eq!(txn.get(b"b"), Some(2));

        // Update in buffer
        txn.put(b"a".to_vec(), 10);
        assert_eq!(txn.get(b"a"), Some(10));

        // Delete in buffer
        txn.delete(b"b".to_vec());
        assert_eq!(txn.get(b"b"), None);

        txn.commit();
    }

    #[test]
    fn test_mvcc_store_gc() {
        let store: EpochMvccStore<i32> = EpochMvccStore::new();

        // Create versions across epochs
        for i in 0..5 {
            let mut txn = store.begin_txn();
            txn.put(b"key".to_vec(), i);
            txn.commit();
            store.epoch_manager().advance_epoch();
        }

        let stats = store.stats();
        assert!(stats.total_versions >= 5);

        // GC old versions
        let gc_stats = store.gc();

        // Should have removed some versions (depends on min_safe_epoch).
        // versions_removed is unsigned, so just ensure the field is accessible.
        let _ = gc_stats.versions_removed;
    }

    #[test]
    fn test_epoch_snapshot_keys() {
        let store: EpochMvccStore<i32> = EpochMvccStore::new();

        let mut txn = store.begin_txn();
        txn.put(b"a".to_vec(), 1);
        txn.put(b"b".to_vec(), 2);
        txn.put(b"c".to_vec(), 3);
        txn.commit();

        let snap = EpochSnapshot::new(&store);
        let keys = snap.keys();

        assert_eq!(keys.len(), 3);
        assert!(keys.contains(&b"a".to_vec()));
        assert!(keys.contains(&b"b".to_vec()));
        assert!(keys.contains(&b"c".to_vec()));
    }
}