trie_test/database/
memory.rs1use ::Trie;
2use super::{Database, DatabaseOwned, DatabaseGuard};
3use bigint::H256;
4use std::collections::HashMap;
5use std::sync::Mutex;
6
7pub struct MemoryDatabase(Mutex<HashMap<H256, Vec<u8>>>);
8pub struct MemoryDatabaseGuard<'a>(&'a Mutex<HashMap<H256, Vec<u8>>>);
9
10impl MemoryDatabase {
11 pub fn new() -> Self {
12 MemoryDatabase(Mutex::new(HashMap::new()))
13 }
14}
15
16impl<'a> Database<'a> for MemoryDatabase {
17 type Guard = MemoryDatabaseGuard<'a>;
18
19 fn create_guard(&'a self) -> Self::Guard {
20 MemoryDatabaseGuard(&self.0)
21 }
22}
23
24impl Default for MemoryDatabase {
25 fn default() -> MemoryDatabase {
26 Self::new()
27 }
28}
29
30impl<'a> DatabaseGuard for MemoryDatabaseGuard<'a> {
31 fn get(&self, hash: H256) -> Option<Vec<u8>> {
32 self.0.lock().unwrap().get(&hash).map(|v| v.clone())
33 }
34
35 fn set(&mut self, hash: H256, value: Vec<u8>) {
36 self.0.lock().unwrap().insert(hash, value);
37 }
38}