1use std::collections::HashMap;
21use crate::{Database, Change, ColumnId, Transaction, error};
22use parking_lot::RwLock;
23
24#[derive(Default)]
25pub struct MemDb<H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash>
27 (RwLock<(HashMap<ColumnId, HashMap<Vec<u8>, Vec<u8>>>, HashMap<H, Vec<u8>>)>);
28
29impl<H> Database<H> for MemDb<H>
30 where H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash
31{
32 fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
33 let mut s = self.0.write();
34 for change in transaction.0.into_iter() {
35 match change {
36 Change::Set(col, key, value) => { s.0.entry(col).or_default().insert(key, value); },
37 Change::Remove(col, key) => { s.0.entry(col).or_default().remove(&key); },
38 Change::Store(hash, preimage) => { s.1.insert(hash, preimage); },
39 Change::Release(hash) => { s.1.remove(&hash); },
40 }
41 }
42
43 Ok(())
44 }
45
46 fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
47 let s = self.0.read();
48 s.0.get(&col).and_then(|c| c.get(key).cloned())
49 }
50
51 fn lookup(&self, hash: &H) -> Option<Vec<u8>> {
52 let s = self.0.read();
53 s.1.get(hash).cloned()
54 }
55}
56
57impl<H> MemDb<H>
58 where H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash
59{
60 pub fn new() -> Self {
62 MemDb::default()
63 }
64
65 pub fn count(&self, col: ColumnId) -> usize {
67 let s = self.0.read();
68 s.0.get(&col).map(|c| c.len()).unwrap_or(0)
69 }
70}
71