mirror of
https://github.com/romanz/electrs.git
synced 2025-02-24 15:02:21 +01:00
Add memory-based KV store implementation
This commit is contained in:
parent
2254d3bb30
commit
d6ea025861
1 changed files with 35 additions and 0 deletions
35
src/store.rs
35
src/store.rs
|
@ -1,6 +1,9 @@
|
|||
use bitcoin::blockdata::block::BlockHeader;
|
||||
use bitcoin::network::serialize::deserialize;
|
||||
use rocksdb;
|
||||
use std::collections::BTreeMap;
|
||||
use std::ops::Bound;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use util::Bytes;
|
||||
|
||||
|
@ -100,3 +103,35 @@ impl Store for DBStore {
|
|||
self.db.write_opt(batch, &opts).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemStore {
|
||||
map: RwLock<BTreeMap<Bytes, Bytes>>,
|
||||
}
|
||||
|
||||
impl Store for MemStore {
|
||||
fn get(&self, key: &[u8]) -> Option<Bytes> {
|
||||
self.map.read().unwrap().get(key).map(|v| v.to_vec())
|
||||
}
|
||||
fn scan(&self, prefix: &[u8]) -> Vec<Row> {
|
||||
let map = self.map.read().unwrap();
|
||||
let range = map.range((Bound::Included(prefix.to_vec()), Bound::Unbounded));
|
||||
let mut rows = Vec::new();
|
||||
for (key, value) in range {
|
||||
if !key.starts_with(prefix) {
|
||||
break;
|
||||
}
|
||||
rows.push(Row {
|
||||
key: key.to_vec(),
|
||||
value: value.to_vec(),
|
||||
});
|
||||
}
|
||||
rows
|
||||
}
|
||||
fn persist(&self, rows: Vec<Row>) {
|
||||
let mut map = self.map.write().unwrap();
|
||||
for row in rows {
|
||||
let (key, value) = row.into_pair();
|
||||
map.insert(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue