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
use crate::{
key_val_store::{
key_val_store::{IterationResult, KeyValueStore},
KeyValStoreError,
},
lmdb_store::LMDBDatabase,
};
use lmdb_zero::traits::AsLmdbBytes;
use serde::{de::DeserializeOwned, export::PhantomData, Serialize};
use std::sync::Arc;
pub struct LMDBWrapper<K, V> {
inner: Arc<LMDBDatabase>,
_k: PhantomData<K>,
_v: PhantomData<V>,
}
impl<K, V> LMDBWrapper<K, V> {
pub fn new(db: Arc<LMDBDatabase>) -> LMDBWrapper<K, V> {
LMDBWrapper {
inner: db,
_k: PhantomData,
_v: PhantomData,
}
}
pub fn inner(&self) -> Arc<LMDBDatabase> {
Arc::clone(&self.inner)
}
}
impl<K, V> KeyValueStore<K, V> for LMDBWrapper<K, V>
where
K: AsLmdbBytes + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn insert(&self, key: K, value: V) -> Result<(), KeyValStoreError> {
self.inner
.insert::<K, V>(&key, &value)
.map_err(|e| KeyValStoreError::DatabaseError(format!("{:?}", e)))
}
fn get(&self, key: &K) -> Result<Option<V>, KeyValStoreError>
where for<'t> V: serde::de::DeserializeOwned {
self.inner
.get::<K, V>(key)
.map_err(|e| KeyValStoreError::DatabaseError(format!("{:?}", e)))
}
fn size(&self) -> Result<usize, KeyValStoreError> {
self.inner
.len()
.map_err(|e| KeyValStoreError::DatabaseError(format!("{:?}", e)))
}
fn for_each<F>(&self, f: F) -> Result<(), KeyValStoreError>
where F: FnMut(Result<(K, V), KeyValStoreError>) -> IterationResult {
self.inner
.for_each::<K, V, F>(f)
.map_err(|e| KeyValStoreError::DatabaseError(format!("{:?}", e)))
}
fn exists(&self, key: &K) -> Result<bool, KeyValStoreError> {
self.inner
.contains_key::<K>(key)
.map_err(|e| KeyValStoreError::DatabaseError(format!("{:?}", e)))
}
fn delete(&self, key: &K) -> Result<(), KeyValStoreError> {
self.inner
.remove::<K>(key)
.map_err(|e| KeyValStoreError::DatabaseError(format!("{:?}", e)))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::lmdb_store::{LMDBBuilder, LMDBError, LMDBStore};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
fn get_path(name: &str) -> String {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests/data");
path.push(name);
path.to_str().unwrap().to_string()
}
fn init_datastore(name: &str) -> Result<LMDBStore, LMDBError> {
let path = get_path(name);
std::fs::create_dir(&path).unwrap_or_default();
LMDBBuilder::new()
.set_path(&path)
.set_environment_size(50)
.set_max_number_of_databases(2)
.add_database(name, lmdb_zero::db::CREATE)
.build()
}
fn clean_up_datastore(name: &str) {
std::fs::remove_dir_all(get_path(name)).unwrap();
}
#[test]
fn test_lmdb_kvstore() {
let database_name = "test_lmdb_kvstore";
{
let datastore = init_datastore(database_name).unwrap();
let db = datastore.get_handle(database_name).unwrap();
let db = LMDBWrapper::new(Arc::new(db));
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
struct Foo {
value: String,
}
let key1 = 1 as u64;
let key2 = 2 as u64;
let key3 = 3 as u64;
let key4 = 4 as u64;
let val1 = Foo {
value: "one".to_string(),
};
let val2 = Foo {
value: "two".to_string(),
};
let val3 = Foo {
value: "three".to_string(),
};
db.insert(1, val1.clone()).unwrap();
db.insert(2, val2.clone()).unwrap();
db.insert(3, val3.clone()).unwrap();
assert_eq!(db.get(&1).unwrap().unwrap(), val1);
assert_eq!(db.get(&2).unwrap().unwrap(), val2);
assert_eq!(db.get(&3).unwrap().unwrap(), val3);
assert!(db.get(&4).unwrap().is_none());
assert_eq!(db.size().unwrap(), 3);
assert!(db.exists(&key1).unwrap());
assert!(db.exists(&key2).unwrap());
assert!(db.exists(&key3).unwrap());
assert!(!db.exists(&key4).unwrap());
db.delete(&key2).unwrap();
assert_eq!(db.get(&key1).unwrap().unwrap(), val1);
assert!(db.get(&key2).unwrap().is_none());
assert_eq!(db.get(&key3).unwrap().unwrap(), val3);
assert!(db.get(&key4).unwrap().is_none());
assert_eq!(db.size().unwrap(), 2);
assert!(db.exists(&key1).unwrap());
assert!(!db.exists(&key2).unwrap());
assert!(db.exists(&key3).unwrap());
assert!(!db.exists(&key4).unwrap());
let mut key1_found = false;
let mut key3_found = false;
let _res = db.for_each(|pair| {
let (key, val) = pair.unwrap();
if key == key1 {
key1_found = true;
assert_eq!(val, val1);
} else if key == key3 {
key3_found = true;
assert_eq!(val, val3);
}
IterationResult::Continue
});
assert!(key1_found);
assert!(key3_found);
}
clean_up_datastore(database_name);
}
}