-
Notifications
You must be signed in to change notification settings - Fork 16
/
db.rs
125 lines (102 loc) · 3.24 KB
/
db.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
use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
use parking_lot::RwLock;
use crate::errors::MemDBError;
/// "DB" defines the "trait" of trie and database interaction.
/// You should first write the data to the cache and write the data
/// to the database in bulk after the end of a set of operations.
pub trait DB: Send + Sync {
type Error: Error;
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error>;
/// Insert data into the cache.
fn insert(&self, key: &[u8], value: Vec<u8>) -> Result<(), Self::Error>;
/// Remove data with given key.
fn remove(&self, key: &[u8]) -> Result<(), Self::Error>;
/// Insert a batch of data into the cache.
fn insert_batch(&self, keys: Vec<Vec<u8>>, values: Vec<Vec<u8>>) -> Result<(), Self::Error> {
for i in 0..keys.len() {
let key = &keys[i];
let value = values[i].clone();
self.insert(key, value)?;
}
Ok(())
}
/// Remove a batch of data into the cache.
fn remove_batch(&self, keys: &[Vec<u8>]) -> Result<(), Self::Error> {
for key in keys {
self.remove(key)?;
}
Ok(())
}
/// Flush data to the DB from the cache.
fn flush(&self) -> Result<(), Self::Error>;
#[cfg(test)]
fn len(&self) -> Result<usize, Self::Error>;
#[cfg(test)]
fn is_empty(&self) -> Result<bool, Self::Error>;
}
#[derive(Default, Debug)]
pub struct MemoryDB {
// If "light" is true, the data is deleted from the database at the time of submission.
light: bool,
storage: Arc<RwLock<HashMap<Vec<u8>, Vec<u8>>>>,
}
impl MemoryDB {
pub fn new(light: bool) -> Self {
MemoryDB {
light,
storage: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl DB for MemoryDB {
type Error = MemDBError;
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
if let Some(value) = self.storage.read().get(key) {
Ok(Some(value.clone()))
} else {
Ok(None)
}
}
fn insert(&self, key: &[u8], value: Vec<u8>) -> Result<(), Self::Error> {
self.storage.write().insert(key.to_vec(), value);
Ok(())
}
fn remove(&self, key: &[u8]) -> Result<(), Self::Error> {
if self.light {
self.storage.write().remove(key);
}
Ok(())
}
fn flush(&self) -> Result<(), Self::Error> {
Ok(())
}
#[cfg(test)]
fn len(&self) -> Result<usize, Self::Error> {
Ok(self.storage.try_read().unwrap().len())
}
#[cfg(test)]
fn is_empty(&self) -> Result<bool, Self::Error> {
Ok(self.storage.try_read().unwrap().is_empty())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memdb_get() {
let memdb = MemoryDB::new(true);
memdb.insert(b"test-key", b"test-value".to_vec()).unwrap();
let v = memdb.get(b"test-key").unwrap().unwrap();
assert_eq!(v, b"test-value")
}
#[test]
fn test_memdb_remove() {
let memdb = MemoryDB::new(true);
memdb.insert(b"test", b"test".to_vec()).unwrap();
memdb.remove(b"test").unwrap();
let contains = memdb.get(b"test").unwrap();
assert_eq!(contains, None)
}
}