-
Notifications
You must be signed in to change notification settings - Fork 581
/
engine.rs
386 lines (334 loc) · 13.3 KB
/
engine.rs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use crate::encoding::keycode;
use crate::error::Result;
use serde::{Deserialize, Serialize};
/// A key/value storage engine storing arbitrary byte strings in lexicographical
/// key order. Storing keys in order allows for efficient range scans, which is
/// needed to e.g. scan a single table during SQL execution (where all rows have
/// keys with a common key prefix for the table). Keys should use the KeyCode
/// order-preserving encoding, see src/encoding/keycode. Writes are only
/// guaranteed durable after calling flush().
///
/// Only supports single-threaded use since all methods (including reads) take a
/// mutable reference -- serialized access can't be avoided anyway, since both
/// Raft execution and file access is serial.
pub trait Engine: Send {
/// The iterator returned by scan().
type ScanIterator<'a>: ScanIterator + 'a
where
Self: Sized + 'a; // omit in trait objects, for object safety
/// Deletes a key, or does nothing if it does not exist.
fn delete(&mut self, key: &[u8]) -> Result<()>;
/// Flushes any buffered data to the underlying storage medium.
fn flush(&mut self) -> Result<()>;
/// Gets a value for a key, if it exists.
fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>>;
/// Iterates over an ordered range of key/value pairs.
fn scan(&mut self, range: impl std::ops::RangeBounds<Vec<u8>>) -> Self::ScanIterator<'_>
where
Self: Sized; // omit in trait objects, for object safety
/// Like scan, but can be used from trait objects. The iterator will use
/// dynamic dispatch, which has a minor performance penalty.
fn scan_dyn(
&mut self,
range: (std::ops::Bound<Vec<u8>>, std::ops::Bound<Vec<u8>>),
) -> Box<dyn ScanIterator + '_>;
/// Iterates over all key/value pairs starting with prefix.
fn scan_prefix(&mut self, prefix: &[u8]) -> Self::ScanIterator<'_>
where
Self: Sized, // omit in trait objects, for object safety
{
self.scan(keycode::prefix_range(prefix))
}
/// Sets a value for a key, replacing the existing value if any.
fn set(&mut self, key: &[u8], value: Vec<u8>) -> Result<()>;
/// Returns engine status.
fn status(&mut self) -> Result<Status>;
}
/// A scan iterator, with a blanket implementation (in lieu of trait aliases).
pub trait ScanIterator: DoubleEndedIterator<Item = Result<(Vec<u8>, Vec<u8>)>> {}
impl<I: DoubleEndedIterator<Item = Result<(Vec<u8>, Vec<u8>)>>> ScanIterator for I {}
/// Engine status.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Status {
/// The name of the storage engine.
pub name: String,
/// The number of live keys in the engine.
pub keys: u64,
/// The logical size of live key/value pairs.
pub size: u64,
/// The on-disk size of all data, live and garbage.
pub total_disk_size: u64,
/// The on-disk size of live data.
pub live_disk_size: u64,
/// The on-disk size of garbage data.
pub garbage_disk_size: u64,
}
impl Status {
pub fn garbage_percent(&self) -> f64 {
if self.total_disk_size == 0 {
return 0.0;
}
self.garbage_disk_size as f64 / self.total_disk_size as f64 * 100.0
}
}
/// Test helpers for engines.
#[cfg(test)]
pub mod test {
use super::*;
use crate::encoding::format::{self, Formatter as _};
use crossbeam::channel::Sender;
use itertools::Itertools as _;
use regex::Regex;
use std::fmt::Write as _;
use std::{error::Error as StdError, result::Result as StdResult};
/// Goldenscript runner for engines. All engines use a common set of
/// goldenscripts in src/storage/testscripts/engine, as well as their own
/// engine-specific tests.
pub struct Runner<E: Engine> {
pub engine: E,
}
impl<E: Engine> Runner<E> {
pub fn new(engine: E) -> Self {
Self { engine }
}
}
impl<E: Engine> goldenscript::Runner for Runner<E> {
fn run(&mut self, command: &goldenscript::Command) -> StdResult<String, Box<dyn StdError>> {
let mut output = String::new();
match command.name.as_str() {
// delete KEY
"delete" => {
let mut args = command.consume_args();
let key = decode_binary(&args.next_pos().ok_or("key not given")?.value);
args.reject_rest()?;
self.engine.delete(&key)?;
}
// get KEY
"get" => {
let mut args = command.consume_args();
let key = decode_binary(&args.next_pos().ok_or("key not given")?.value);
args.reject_rest()?;
let value = self.engine.get(&key)?;
writeln!(output, "{}", format::Raw::key_maybe_value(&key, value.as_deref()))?;
}
// scan [reverse=BOOL] RANGE
"scan" => {
let mut args = command.consume_args();
let reverse = args.lookup_parse("reverse")?.unwrap_or(false);
let range =
parse_key_range(args.next_pos().map(|a| a.value.as_str()).unwrap_or(".."))?;
args.reject_rest()?;
let items: Vec<_> = if reverse {
self.engine.scan(range).rev().try_collect()?
} else {
self.engine.scan(range).try_collect()?
};
for (key, value) in items {
let fmtkv = format::Raw::key_value(&key, &value);
writeln!(output, "{fmtkv}")?;
}
}
// scan_prefix PREFIX
"scan_prefix" => {
let mut args = command.consume_args();
let prefix = decode_binary(&args.next_pos().ok_or("prefix not given")?.value);
args.reject_rest()?;
let mut scan = self.engine.scan_prefix(&prefix);
while let Some((key, value)) = scan.next().transpose()? {
let fmtkv = format::Raw::key_value(&key, &value);
writeln!(output, "{fmtkv}")?;
}
}
// set KEY=VALUE
"set" => {
let mut args = command.consume_args();
let kv = args.next_key().ok_or("key=value not given")?.clone();
let key = decode_binary(&kv.key.unwrap());
let value = decode_binary(&kv.value);
args.reject_rest()?;
self.engine.set(&key, value)?;
}
// status
"status" => {
command.consume_args().reject_rest()?;
writeln!(output, "{:#?}", self.engine.status()?)?;
}
name => return Err(format!("invalid command {name}").into()),
}
Ok(output)
}
}
/// Decodes a raw byte vector from a Unicode string. Code points in the
/// range U+0080 to U+00FF are converted back to bytes 0x80 to 0xff.
/// This allows using e.g. \xff in the input string literal, and getting
/// back a 0xff byte in the byte vector. Otherwise, char(0xff) yields
/// the UTF-8 bytes 0xc3bf, which is the U+00FF code point as UTF-8.
/// These characters are effectively represented as ISO-8859-1 rather
/// than UTF-8, but it allows precise use of the entire u8 value range.
pub fn decode_binary(s: &str) -> Vec<u8> {
let mut buf = [0; 4];
let mut bytes = Vec::new();
for c in s.chars() {
// u32 is the Unicode code point, not the UTF-8 encoding.
match c as u32 {
b @ 0x80..=0xff => bytes.push(b as u8),
_ => bytes.extend(c.encode_utf8(&mut buf).as_bytes()),
}
}
bytes
}
/// Parses an binary key range, using Rust range syntax.
pub fn parse_key_range(
s: &str,
) -> StdResult<impl std::ops::RangeBounds<Vec<u8>>, Box<dyn StdError>> {
use std::ops::Bound;
let mut bound = (Bound::<Vec<u8>>::Unbounded, Bound::<Vec<u8>>::Unbounded);
let re = Regex::new(r"^(\S+)?\.\.(=)?(\S+)?").expect("invalid regex");
let groups = re.captures(s).ok_or_else(|| format!("invalid range {s}"))?;
if let Some(start) = groups.get(1) {
bound.0 = Bound::Included(decode_binary(start.as_str()));
}
if let Some(end) = groups.get(3) {
let end = decode_binary(end.as_str());
if groups.get(2).is_some() {
bound.1 = Bound::Included(end)
} else {
bound.1 = Bound::Excluded(end)
}
}
Ok(bound)
}
/// Wraps another engine and emits write events to the given channel.
pub struct Emit<E: Engine> {
/// The wrapped engine.
inner: E,
/// Sends operation events.
tx: Sender<Operation>,
}
/// An engine operation emitted by the Emit engine.
pub enum Operation {
Delete { key: Vec<u8> },
Flush,
Set { key: Vec<u8>, value: Vec<u8> },
}
impl<E: Engine> Emit<E> {
pub fn new(inner: E, tx: Sender<Operation>) -> Self {
Self { inner, tx }
}
}
impl<E: Engine> Engine for Emit<E> {
type ScanIterator<'a> = E::ScanIterator<'a> where E: 'a;
fn flush(&mut self) -> Result<()> {
self.inner.flush()?;
self.tx.send(Operation::Flush)?;
Ok(())
}
fn delete(&mut self, key: &[u8]) -> Result<()> {
self.inner.delete(key)?;
self.tx.send(Operation::Delete { key: key.to_vec() })?;
Ok(())
}
fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>> {
self.inner.get(key)
}
fn scan(&mut self, range: impl std::ops::RangeBounds<Vec<u8>>) -> Self::ScanIterator<'_> {
self.inner.scan(range)
}
fn scan_dyn(
&mut self,
range: (std::ops::Bound<Vec<u8>>, std::ops::Bound<Vec<u8>>),
) -> Box<dyn ScanIterator + '_> {
Box::new(self.scan(range))
}
fn set(&mut self, key: &[u8], value: Vec<u8>) -> Result<()> {
self.inner.set(key, value.clone())?;
self.tx.send(Operation::Set { key: key.to_vec(), value })?;
Ok(())
}
fn status(&mut self) -> Result<Status> {
self.inner.status()
}
}
/// An engine that wraps two others and mirrors operations across them,
/// panicking if they produce different results. Engine implementations
/// should not have any observable differences in behavior.
pub struct Mirror<A: Engine, B: Engine> {
pub a: A,
pub b: B,
}
impl<A: Engine, B: Engine> Mirror<A, B> {
pub fn new(a: A, b: B) -> Self {
Self { a, b }
}
}
impl<A: Engine, B: Engine> Engine for Mirror<A, B> {
type ScanIterator<'a> = MirrorIterator<'a, A, B>
where
Self: Sized,
A: 'a,
B: 'a;
fn delete(&mut self, key: &[u8]) -> Result<()> {
self.a.delete(key)?;
self.b.delete(key)
}
fn flush(&mut self) -> Result<()> {
self.a.flush()?;
self.b.flush()
}
fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>> {
let a = self.a.get(key)?;
let b = self.b.get(key)?;
assert_eq!(a, b);
Ok(a)
}
fn scan(&mut self, range: impl std::ops::RangeBounds<Vec<u8>>) -> Self::ScanIterator<'_>
where
Self: Sized,
{
let a = self.a.scan((range.start_bound().cloned(), range.end_bound().cloned()));
let b = self.b.scan(range);
MirrorIterator { a, b }
}
fn scan_dyn(
&mut self,
range: (std::ops::Bound<Vec<u8>>, std::ops::Bound<Vec<u8>>),
) -> Box<dyn ScanIterator + '_> {
let a = self.a.scan(range.clone());
let b = self.b.scan(range);
Box::new(MirrorIterator::<A, B> { a, b })
}
fn set(&mut self, key: &[u8], value: Vec<u8>) -> Result<()> {
self.a.set(key, value.clone())?;
self.b.set(key, value)
}
fn status(&mut self) -> Result<Status> {
let a = self.a.status()?;
let b = self.b.status()?;
// Only some items are comparable.
assert_eq!(a.keys, b.keys);
assert_eq!(a.size, b.size);
Ok(a)
}
}
pub struct MirrorIterator<'a, A: Engine + 'a, B: Engine + 'a> {
a: A::ScanIterator<'a>,
b: B::ScanIterator<'a>,
}
impl<'a, A: Engine, B: Engine> Iterator for MirrorIterator<'a, A, B> {
type Item = Result<(Vec<u8>, Vec<u8>)>;
fn next(&mut self) -> Option<Self::Item> {
let a = self.a.next();
let b = self.b.next();
assert_eq!(a, b);
a
}
}
impl<'a, A: Engine, B: Engine> DoubleEndedIterator for MirrorIterator<'a, A, B> {
fn next_back(&mut self) -> Option<Self::Item> {
let a = self.a.next_back();
let b = self.b.next_back();
assert_eq!(a, b);
a
}
}
}