Move and rename MemStore to mempool module

This commit is contained in:
Roman Zeyde 2018-05-20 14:47:20 +03:00
parent 042991106b
commit d9b0e0c917
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 62 additions and 54 deletions

View File

@ -1,15 +1,61 @@
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::util::hash::Sha256dHash;
use daemon::{Daemon, MempoolEntry};
use std::collections::BTreeMap;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use std::ops::Bound;
use std::sync::RwLock;
use std::time::{Duration, Instant};
use daemon::{Daemon, MempoolEntry};
use index::index_transaction;
use store::{Row, Store};
use util::Bytes;
error_chain!{}
const VSIZE_BIN_WIDTH: u32 = 100_000; // in vbytes
struct MempoolStore {
map: RwLock<BTreeMap<Bytes, Bytes>>,
}
impl MempoolStore {
pub fn new() -> MempoolStore {
MempoolStore {
map: RwLock::new(BTreeMap::new()),
}
}
}
impl Store for MempoolStore {
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);
}
}
}
pub struct Stats {
tx: Transaction,
entry: MempoolEntry,
@ -77,7 +123,7 @@ impl Tracker {
}
};
trace!("new tx: {}, {:.3}", txid, entry.fee_per_vbyte(),);
self.add(txid, Stats::new(tx, entry));
self.add(txid, tx, entry);
}
for txid in old_txids.difference(&new_txids) {
self.remove(txid);
@ -90,13 +136,23 @@ impl Tracker {
Ok(())
}
fn add(&mut self, txid: &Sha256dHash, stats: Stats) {
self.stats.insert(*txid, stats);
fn add(&mut self, txid: &Sha256dHash, tx: Transaction, entry: MempoolEntry) {
self.stats.insert(*txid, Stats { tx, entry });
}
fn remove(&mut self, txid: &Sha256dHash) {
self.stats.remove(txid);
}
pub fn build_index(&self) -> Box<Store> {
let mut rows = Vec::new();
for stats in self.stats.values() {
index_transaction(&stats.tx, 0, &mut rows)
}
let store = MempoolStore::new();
store.persist(rows);
Box::new(store)
}
}
trait InSeconds {

View File

@ -1,9 +1,6 @@
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;
@ -13,7 +10,7 @@ pub struct Row {
}
impl Row {
fn into_pair(self) -> (Bytes, Bytes) {
pub fn into_pair(self) -> (Bytes, Bytes) {
(self.key, self.value)
}
}
@ -103,48 +100,3 @@ impl Store for DBStore {
self.db.write_opt(batch, &opts).unwrap();
}
}
pub struct MemStore {
map: RwLock<BTreeMap<Bytes, Bytes>>,
}
impl MemStore {
pub fn new() -> MemStore {
MemStore {
map: RwLock::new(BTreeMap::new()),
}
}
pub fn remove(&self, key: &Bytes) -> Option<Bytes> {
let mut map = self.map.write().unwrap();
map.remove(key)
}
}
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);
}
}
}