-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
97 lines (84 loc) · 2.24 KB
/
lib.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
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};
use wal::WAL;
mod kvrecord;
mod sstable;
mod wal;
#[derive(Debug)]
pub enum ToyKVError {
GenericError,
DataDirMissing,
FileError(std::io::Error),
BadWALState,
}
impl From<std::io::Error> for ToyKVError {
fn from(value: std::io::Error) -> Self {
ToyKVError::FileError(value)
}
}
#[derive(Default)]
pub struct ToyKVMetrics {
pub sst_flushes: u64,
pub reads: u64,
pub writes: u64,
}
pub struct ToyKV {
/// d is the folder that the KV store owns.
d: PathBuf,
memtable: BTreeMap<Vec<u8>, Vec<u8>>,
wal: WAL,
pub metrics: ToyKVMetrics,
}
pub fn open(d: &Path) -> Result<ToyKV, ToyKVError> {
if !d.is_dir() {
return Err(ToyKVError::DataDirMissing);
}
let mut wal = wal::new(d);
let memtable = wal.replay()?;
Ok(ToyKV {
d: d.to_path_buf(),
memtable,
wal,
metrics: Default::default(),
})
}
/// How many writes to a WAL before we SSTable it.
const WAL_WRITE_THRESHOLD: u32 = 1000;
impl ToyKV {
/// Sets key k to v.
pub fn set(&mut self, k: Vec<u8>, v: Vec<u8>) -> Result<(), ToyKVError> {
self.wal.write(&k, &v)?;
self.memtable.insert(k, v);
if self.wal.wal_writes >= WAL_WRITE_THRESHOLD {
let mut sst = sstable::new_writer(self.d.as_path())?;
for entry in &self.memtable {
sst.write(entry.0, entry.1)?;
}
sst.finalise()?;
self.wal.reset()?;
self.memtable.clear();
self.metrics.sst_flushes += 1;
}
self.metrics.writes += 1;
Ok(())
}
/// Get the value for k.
pub fn get(&mut self, k: &[u8]) -> Result<Option<Vec<u8>>, ToyKVError> {
let r = self.memtable.get(k);
let r = match r {
Some(r) => Some(r.clone()),
None => {
let sst = sstable::new_reader(self.d.as_path())?;
sst.get(k)?
}
};
self.metrics.reads += 1;
Ok(r)
}
/// Perform a graceful shutdown.
pub fn shutdown(&mut self) {}
// /// Immediately terminate (for use during testing, "pretend to crash")
// pub(crate) fn terminate(&mut self) {}
}