Skip to main content

nexir_mvcc/
types.rs

1use std::fmt;
2
3/// A monotonic logical timestamp used for ordering versions and intents.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct Timestamp(pub u128);
6
7/// A unique identifier for a distributed or local transaction.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct TxnId(pub u64);
10
11impl fmt::Display for Timestamp {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        write!(f, "{}", self.0)
14    }
15}
16
17impl fmt::Display for TxnId {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "{}", self.0)
20    }
21}
22
23/// Alias for a physical key.
24pub type Key = Vec<u8>;
25/// Alias for a physical value.
26pub type Value = Vec<u8>;
27
28/// Represents a logical mutation to a key.
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum Mutation {
31    /// A logical put/write.
32    Put(Value),
33    /// A logical delete/tombstone.
34    Delete,
35}
36
37impl Mutation {
38    /// Returns the optional value of the mutation.
39    pub fn value(&self) -> Option<Value> {
40        match self {
41            Mutation::Put(v) => Some(v.clone()),
42            Mutation::Delete => None,
43        }
44    }
45
46    /// Returns whether this mutation is a delete.
47    pub fn is_delete(&self) -> bool {
48        matches!(self, Mutation::Delete)
49    }
50}
51
52/// A durable, provisional lock representing an uncommitted transactional write.
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct Intent {
55    /// The key being modified.
56    pub key: Key,
57    /// The transaction holding this intent.
58    pub txn_id: TxnId,
59    /// The start timestamp of the transaction.
60    pub start_ts: Timestamp,
61    /// The physical mutation (Put or Delete).
62    pub mutation: Mutation,
63    /// An optional minimum commit timestamp required for this intent.
64    pub min_commit_ts: Option<Timestamp>,
65}
66
67/// A fully committed version of a key, visible to readers at or after `commit_ts`.
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct CommittedVersion {
70    /// The key.
71    pub key: Key,
72    /// The timestamp at which this version became visible.
73    pub commit_ts: Timestamp,
74    /// The physical value, or None if it's a tombstone.
75    pub value: Option<Value>, // None means tombstone
76}
77
78/// A raw physical write instruction used in direct and guarded batches.
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct PhysicalWrite {
81    /// The key to write.
82    pub key: Key,
83    /// The value to write, or None for a delete/tombstone.
84    pub value: Option<Value>, // None means tombstone/delete
85}
86
87/// A precondition guard evaluated against the MVCC state before applying a guarded batch.
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub enum ReadGuard {
90    /// Guard based on an expected specific version (commit timestamp).
91    ExpectedVersion {
92        /// The key to check.
93        key: Key,
94        /// The read timestamp at which to evaluate the guard.
95        read_ts: Timestamp,
96        /// The exact `commit_ts` expected, or None if expecting absence.
97        expected_commit_ts: Option<Timestamp>, // None means expected absent
98    },
99    /// Guard based on an expected logical value.
100    ExpectedValue {
101        /// The key to check.
102        key: Key,
103        /// The read timestamp at which to evaluate the guard.
104        read_ts: Timestamp,
105        /// The exact value expected, or None if expecting logical absence.
106        expected_value: Option<Value>, // None means the visible logical value is absent
107    },
108}