2015-11-28 03:09:36 +01:00
|
|
|
//! Disk-backed HashDB implementation.
|
|
|
|
|
2015-11-28 18:29:50 +01:00
|
|
|
use error::*;
|
2015-11-28 03:09:36 +01:00
|
|
|
use hash::*;
|
|
|
|
use bytes::*;
|
2015-11-28 19:03:50 +01:00
|
|
|
use rlp::*;
|
2015-11-28 03:09:36 +01:00
|
|
|
use hashdb::*;
|
|
|
|
use memorydb::*;
|
|
|
|
use std::ops::*;
|
2015-11-28 18:29:50 +01:00
|
|
|
use std::sync::*;
|
2015-11-28 03:09:36 +01:00
|
|
|
use rocksdb::{DB, Writable};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct OverlayDB {
|
|
|
|
overlay: MemoryDB,
|
2015-11-28 18:29:50 +01:00
|
|
|
backing: Arc<DB>,
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OverlayDB {
|
|
|
|
/// Create a new instance of OverlayDB given a `backing` database.
|
2015-11-28 19:28:59 +01:00
|
|
|
pub fn new(backing: DB) -> OverlayDB {
|
2015-11-28 18:29:50 +01:00
|
|
|
OverlayDB{ overlay: MemoryDB::new(), backing: Arc::new(backing) }
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 18:29:50 +01:00
|
|
|
/// Commit all memory operations to the backing database.
|
2015-11-28 19:28:59 +01:00
|
|
|
pub fn commit(&mut self) -> Result<u32, EthcoreError> {
|
2015-11-28 18:29:50 +01:00
|
|
|
let mut ret = 0u32;
|
|
|
|
for i in self.overlay.drain().into_iter() {
|
|
|
|
let (key, (value, rc)) = i;
|
|
|
|
if rc != 0 {
|
2015-11-28 19:28:59 +01:00
|
|
|
match self.payload(&key) {
|
2015-11-28 18:29:50 +01:00
|
|
|
Some(x) => {
|
|
|
|
let (back_value, back_rc) = x;
|
2015-11-28 19:28:59 +01:00
|
|
|
let total_rc: i32 = back_rc as i32 + rc;
|
|
|
|
if total_rc < 0 {
|
2015-11-28 18:29:50 +01:00
|
|
|
return Err(From::from(BaseDataError::NegativelyReferencedHash));
|
|
|
|
}
|
2015-11-28 19:28:59 +01:00
|
|
|
self.put_payload(&key, (back_value, total_rc as u32));
|
2015-11-28 18:29:50 +01:00
|
|
|
}
|
|
|
|
None => {
|
2015-11-28 19:28:59 +01:00
|
|
|
if rc < 0 {
|
|
|
|
return Err(From::from(BaseDataError::NegativelyReferencedHash));
|
|
|
|
}
|
|
|
|
self.put_payload(&key, (value, rc as u32));
|
2015-11-28 18:29:50 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
ret += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ret)
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the refs and value of the given key.
|
2015-11-28 19:28:59 +01:00
|
|
|
fn payload(&self, key: &H256) -> Option<(Bytes, u32)> {
|
|
|
|
self.backing.get(&key.bytes())
|
2015-11-28 19:03:50 +01:00
|
|
|
.expect("Low-level database error. Some issue with your hard disk?")
|
|
|
|
.map(|d| {
|
2015-11-28 19:28:59 +01:00
|
|
|
let r = Rlp::new(d.deref());
|
|
|
|
(Bytes::decode(&r.at(1).unwrap()).unwrap(), u32::decode(&r.at(0).unwrap()).unwrap())
|
2015-11-28 19:03:50 +01:00
|
|
|
})
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
2015-11-28 18:29:50 +01:00
|
|
|
|
|
|
|
/// Get the refs and value of the given key.
|
2015-11-28 19:28:59 +01:00
|
|
|
fn put_payload(&self, key: &H256, payload: (Bytes, u32)) {
|
2015-11-28 19:03:50 +01:00
|
|
|
let mut s = RlpStream::new_list(2);
|
2015-11-28 19:28:59 +01:00
|
|
|
s.append(&payload.1);
|
|
|
|
s.append(&payload.0);
|
|
|
|
self.backing.put(&key.bytes(), &s.out().unwrap()).expect("Low-level database error. Some issue with your hard disk?");
|
2015-11-28 18:29:50 +01:00
|
|
|
}
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HashDB for OverlayDB {
|
|
|
|
fn lookup(&self, key: &H256) -> Option<Bytes> {
|
2015-11-28 18:29:50 +01:00
|
|
|
// return ok if positive; if negative, check backing - might be enough references there to make
|
2015-11-28 03:09:36 +01:00
|
|
|
// it positive again.
|
2015-11-28 18:29:50 +01:00
|
|
|
let k = self.overlay.raw(key);
|
2015-11-28 03:09:36 +01:00
|
|
|
match k {
|
|
|
|
Some(&(ref d, rc)) if rc > 0 => Some(d.clone()),
|
|
|
|
_ => {
|
|
|
|
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
|
|
|
match self.payload(key) {
|
2015-11-28 18:29:50 +01:00
|
|
|
Some(x) => {
|
|
|
|
let (d, rc) = x;
|
2015-11-28 19:28:59 +01:00
|
|
|
if rc as i32 + memrc > 0 {
|
2015-11-28 18:29:50 +01:00
|
|
|
Some(d)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done.
|
|
|
|
//Some((d, rc)) if rc + memrc > 0 => Some(d),
|
2015-11-28 03:09:36 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn exists(&self, key: &H256) -> bool {
|
2015-11-28 18:29:50 +01:00
|
|
|
// return ok if positive; if negative, check backing - might be enough references there to make
|
|
|
|
// it positive again.
|
|
|
|
let k = self.overlay.raw(key);
|
|
|
|
match k {
|
2015-11-28 19:28:59 +01:00
|
|
|
Some(&(_, rc)) if rc > 0 => true,
|
2015-11-28 18:29:50 +01:00
|
|
|
_ => {
|
|
|
|
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
|
|
|
match self.payload(key) {
|
|
|
|
Some(x) => {
|
2015-11-28 19:28:59 +01:00
|
|
|
let (_, rc) = x;
|
|
|
|
if rc as i32 + memrc > 0 {
|
2015-11-28 18:29:50 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done.
|
|
|
|
//Some((d, rc)) if rc + memrc > 0 => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
2015-11-28 18:29:50 +01:00
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 { self.overlay.insert(value) }
|
|
|
|
fn kill(&mut self, key: &H256) { self.overlay.kill(key); }
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn playpen() {
|
|
|
|
let mut db: DB = DB::open_default("/tmp/test").unwrap();
|
|
|
|
db.put(b"test", b"test2");
|
|
|
|
match db.get(b"test") {
|
|
|
|
Ok(Some(value)) => println!("Got value {:?}", value.deref()),
|
|
|
|
Ok(None) => println!("No value for that key"),
|
|
|
|
Err(e) => println!("Gah"),
|
|
|
|
}
|
|
|
|
db.delete(b"test");
|
|
|
|
assert!(false);
|
|
|
|
}
|