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
use std::ops::Bound;
/// Monotonic commit sequence used for MVCC visibility.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sequence(u64);
impl Sequence {
/// Sequence value used before the first committed write.
pub const ZERO: Self = Self(0);
/// Creates a sequence from its raw numeric value.
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
/// Returns the raw numeric sequence value.
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
/// Returns the next sequence, or `None` if the value would overflow.
#[must_use]
pub const fn next(self) -> Option<Self> {
match self.0.checked_add(1) {
Some(value) => Some(Self(value)),
None => None,
}
}
}
/// Value bytes stored for a key.
pub type Value = Vec<u8>;
/// Owned key/value row returned by eager iterators.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyValue {
/// User key bytes.
pub key: Vec<u8>,
/// Value bytes visible for the key.
pub value: Value,
}
impl KeyValue {
/// Creates an owned key/value row.
#[must_use]
pub fn new(key: impl Into<Vec<u8>>, value: impl Into<Value>) -> Self {
Self {
key: key.into(),
value: value.into(),
}
}
}
/// User-key range used by range scans and range deletes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyRange {
/// Inclusive, exclusive, or unbounded start key.
pub start: Bound<Vec<u8>>,
/// Inclusive, exclusive, or unbounded end key.
pub end: Bound<Vec<u8>>,
}
impl KeyRange {
/// Returns an unbounded range over all user keys.
#[must_use]
pub const fn all() -> Self {
Self {
start: Bound::Unbounded,
end: Bound::Unbounded,
}
}
/// Creates a half-open range `[start, end)`.
#[must_use]
pub fn half_open(start: impl Into<Vec<u8>>, end: impl Into<Vec<u8>>) -> Self {
Self {
start: Bound::Included(start.into()),
end: Bound::Excluded(end.into()),
}
}
}
impl Default for KeyRange {
fn default() -> Self {
Self::all()
}
}
/// Information returned after a write becomes committed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommitInfo {
sequence: Sequence,
}
impl CommitInfo {
/// Creates commit information for `sequence`.
#[must_use]
pub const fn new(sequence: Sequence) -> Self {
Self { sequence }
}
/// Returns the commit sequence assigned to the write.
#[must_use]
pub const fn sequence(self) -> Sequence {
self.sequence
}
}