Skip to main content

trine_kv/
types.rs

1use std::ops::Bound;
2
3/// Monotonic commit sequence used for MVCC visibility.
4#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct Sequence(u64);
6
7impl Sequence {
8    /// Sequence value used before the first committed write.
9    pub const ZERO: Self = Self(0);
10
11    /// Creates a sequence from its raw numeric value.
12    #[must_use]
13    pub const fn new(value: u64) -> Self {
14        Self(value)
15    }
16
17    /// Returns the raw numeric sequence value.
18    #[must_use]
19    pub const fn get(self) -> u64 {
20        self.0
21    }
22
23    /// Returns the next sequence, or `None` if the value would overflow.
24    #[must_use]
25    pub const fn next(self) -> Option<Self> {
26        match self.0.checked_add(1) {
27            Some(value) => Some(Self(value)),
28            None => None,
29        }
30    }
31}
32
33/// Value bytes stored for a key.
34pub type Value = Vec<u8>;
35
36/// Owned key/value row returned by eager iterators.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct KeyValue {
39    /// User key bytes.
40    pub key: Vec<u8>,
41    /// Value bytes visible for the key.
42    pub value: Value,
43}
44
45impl KeyValue {
46    /// Creates an owned key/value row.
47    #[must_use]
48    pub fn new(key: impl Into<Vec<u8>>, value: impl Into<Value>) -> Self {
49        Self {
50            key: key.into(),
51            value: value.into(),
52        }
53    }
54}
55
56/// User-key range used by range scans and range deletes.
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub struct KeyRange {
59    /// Inclusive, exclusive, or unbounded start key.
60    pub start: Bound<Vec<u8>>,
61    /// Inclusive, exclusive, or unbounded end key.
62    pub end: Bound<Vec<u8>>,
63}
64
65impl KeyRange {
66    /// Returns an unbounded range over all user keys.
67    #[must_use]
68    pub const fn all() -> Self {
69        Self {
70            start: Bound::Unbounded,
71            end: Bound::Unbounded,
72        }
73    }
74
75    /// Creates a half-open range `[start, end)`.
76    #[must_use]
77    pub fn half_open(start: impl Into<Vec<u8>>, end: impl Into<Vec<u8>>) -> Self {
78        Self {
79            start: Bound::Included(start.into()),
80            end: Bound::Excluded(end.into()),
81        }
82    }
83}
84
85impl Default for KeyRange {
86    fn default() -> Self {
87        Self::all()
88    }
89}
90
91/// Information returned after a write becomes committed.
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub struct CommitInfo {
94    sequence: Sequence,
95}
96
97impl CommitInfo {
98    /// Creates commit information for `sequence`.
99    #[must_use]
100    pub const fn new(sequence: Sequence) -> Self {
101        Self { sequence }
102    }
103
104    /// Returns the commit sequence assigned to the write.
105    #[must_use]
106    pub const fn sequence(self) -> Sequence {
107        self.sequence
108    }
109}