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
use smallvec::SmallVec;
use std::io;
mod io_stats;
pub const PREFIX_LEN: usize = 12;
pub type DBValue = Vec<u8>;
pub type DBKey = SmallVec<[u8; 32]>;
pub use io_stats::{IoStats, Kind as IoStatsKind};
#[derive(Default, Clone, PartialEq)]
pub struct DBTransaction {
pub ops: Vec<DBOp>,
}
#[derive(Clone, PartialEq)]
pub enum DBOp {
Insert { col: u32, key: DBKey, value: DBValue },
Delete { col: u32, key: DBKey },
DeletePrefix { col: u32, prefix: DBKey },
}
impl DBOp {
pub fn key(&self) -> &[u8] {
match *self {
DBOp::Insert { ref key, .. } => key,
DBOp::Delete { ref key, .. } => key,
DBOp::DeletePrefix { ref prefix, .. } => prefix,
}
}
pub fn col(&self) -> u32 {
match *self {
DBOp::Insert { col, .. } => col,
DBOp::Delete { col, .. } => col,
DBOp::DeletePrefix { col, .. } => col,
}
}
}
impl DBTransaction {
pub fn new() -> DBTransaction {
DBTransaction::with_capacity(256)
}
pub fn with_capacity(cap: usize) -> DBTransaction {
DBTransaction { ops: Vec::with_capacity(cap) }
}
pub fn put(&mut self, col: u32, key: &[u8], value: &[u8]) {
self.ops.push(DBOp::Insert { col, key: DBKey::from_slice(key), value: value.to_vec() })
}
pub fn put_vec(&mut self, col: u32, key: &[u8], value: Vec<u8>) {
self.ops.push(DBOp::Insert { col, key: DBKey::from_slice(key), value });
}
pub fn delete(&mut self, col: u32, key: &[u8]) {
self.ops.push(DBOp::Delete { col, key: DBKey::from_slice(key) });
}
pub fn delete_prefix(&mut self, col: u32, prefix: &[u8]) {
self.ops.push(DBOp::DeletePrefix { col, prefix: DBKey::from_slice(prefix) });
}
}
pub trait KeyValueDB: Sync + Send + parity_util_mem::MallocSizeOf {
fn transaction(&self) -> DBTransaction {
DBTransaction::new()
}
fn get(&self, col: u32, key: &[u8]) -> io::Result<Option<DBValue>>;
fn get_by_prefix(&self, col: u32, prefix: &[u8]) -> Option<Box<[u8]>>;
fn write(&self, transaction: DBTransaction) -> io::Result<()>;
fn iter<'a>(&'a self, col: u32) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a>;
fn iter_with_prefix<'a>(
&'a self,
col: u32,
prefix: &'a [u8],
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a>;
fn restore(&self, new_db: &str) -> io::Result<()>;
fn io_stats(&self, _kind: IoStatsKind) -> IoStats {
IoStats::empty()
}
fn has_key(&self, col: u32, key: &[u8]) -> io::Result<bool> {
self.get(col, key).map(|opt| opt.is_some())
}
fn has_prefix(&self, col: u32, prefix: &[u8]) -> bool {
self.get_by_prefix(col, prefix).is_some()
}
}
pub fn end_prefix(prefix: &[u8]) -> Option<Vec<u8>> {
let mut end_range = prefix.to_vec();
while let Some(0xff) = end_range.last() {
end_range.pop();
}
if let Some(byte) = end_range.last_mut() {
*byte += 1;
Some(end_range)
} else {
None
}
}
#[cfg(test)]
mod test {
use super::end_prefix;
#[test]
fn end_prefix_test() {
assert_eq!(end_prefix(&[5, 6, 7]), Some(vec![5, 6, 8]));
assert_eq!(end_prefix(&[5, 6, 255]), Some(vec![5, 7]));
assert_ne!(end_prefix(&[5, 255, 255]), Some(vec![5, 255]));
assert_eq!(end_prefix(&[5, 255, 255]), Some(vec![6]));
assert_eq!(end_prefix(&[255, 255, 255]), None);
assert_eq!(end_prefix(&[0x00, 0xff]), Some(vec![0x01]));
assert_eq!(end_prefix(&[0xff]), None);
assert_eq!(end_prefix(&[]), None);
assert_eq!(end_prefix(b"0"), Some(b"1".to_vec()));
}
}