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
//! Standard MVCC (Multi-Version Concurrency Control) implementation
//!
//! Uses textbook snapshot isolation algorithm (no custom optimizations).
/// Snapshot ID (timestamp)
pub type SnapshotId = u64;
/// MVCC Snapshot
///
/// Represents a consistent view of the database at a point in time.
#[derive(Debug, Clone)]
pub struct Snapshot {
/// Snapshot timestamp
pub id: SnapshotId,
}
impl Snapshot {
/// Create a new snapshot
pub fn new(id: SnapshotId) -> Self {
Self { id }
}
/// Check if a version is visible in this snapshot
///
/// Standard MVCC visibility rule: version is visible if its timestamp
/// is less than or equal to the snapshot timestamp.
pub fn is_visible(&self, version_timestamp: u64) -> bool {
version_timestamp <= self.id
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn test_snapshot_visibility() {
let snapshot = Snapshot::new(100);
// Versions before snapshot are visible
assert!(snapshot.is_visible(50));
assert!(snapshot.is_visible(100));
// Versions after snapshot are not visible
assert!(!snapshot.is_visible(101));
assert!(!snapshot.is_visible(200));
}
}