Reference-counted rocksdb backed DB. Just need DB I/O now.
This commit is contained in:
parent
71dc9c0ad4
commit
4640d64965
29
src/error.rs
29
src/error.rs
@ -1,9 +1,17 @@
|
|||||||
|
#![feature(concat_idents)]
|
||||||
|
|
||||||
use rustc_serialize::hex::*;
|
use rustc_serialize::hex::*;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum BaseDataError {
|
||||||
|
NegativelyReferencedHash,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum EthcoreError {
|
pub enum EthcoreError {
|
||||||
FromHex(FromHexError),
|
FromHex(FromHexError),
|
||||||
BadSize
|
BaseData(BaseDataError),
|
||||||
|
BadSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FromHexError> for EthcoreError {
|
impl From<FromHexError> for EthcoreError {
|
||||||
@ -11,3 +19,22 @@ impl From<FromHexError> for EthcoreError {
|
|||||||
EthcoreError::FromHex(err)
|
EthcoreError::FromHex(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<BaseDataError> for EthcoreError {
|
||||||
|
fn from(err: BaseDataError) -> EthcoreError {
|
||||||
|
EthcoreError::BaseData(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted.
|
||||||
|
/*macro_rules! assimilate {
|
||||||
|
($name:ident) => (
|
||||||
|
impl From<concat_idents!($name, Error)> for EthcoreError {
|
||||||
|
fn from(err: concat_idents!($name, Error)) -> EthcoreError {
|
||||||
|
EthcoreError:: $name (err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
assimilate!(FromHex);
|
||||||
|
assimilate!(BaseData);*/
|
@ -4,6 +4,7 @@ use hash::*;
|
|||||||
use bytes::*;
|
use bytes::*;
|
||||||
use sha3::*;
|
use sha3::*;
|
||||||
use hashdb::*;
|
use hashdb::*;
|
||||||
|
use std::mem;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Debug,Clone)]
|
||||||
@ -85,19 +86,19 @@ impl MemoryDB {
|
|||||||
for empty in empties { self.data.remove(&empty); }
|
for empty in empties { self.data.remove(&empty); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Grab the number of references a particular `key` has. Returns None if the key
|
/// Grab the raw information associated with a key. Returns None if the key
|
||||||
/// doesn't exist.
|
|
||||||
fn refs(&self, key: &H256) -> Option<i32> {
|
|
||||||
self.data.get(key).map(|&(_, rc)| rc)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Grab the value associated with a particular `key`. Returns None if the key
|
|
||||||
/// doesn't exist.
|
/// doesn't exist.
|
||||||
///
|
///
|
||||||
/// Even when Some is returned, this is only guaranteed to return something useful
|
/// Even when Some is returned, the data is only guaranteed to be useful
|
||||||
/// when `refs(key) > 0`.
|
/// when the refs > 0.
|
||||||
fn value(&self, key: &H256) -> Option<&Bytes> {
|
pub fn raw(&self, key: &H256) -> Option<&(Bytes, i32)> {
|
||||||
self.data.get(key).map(|&(ref d, _)| d)
|
self.data.get(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn drain(&mut self) -> HashMap<H256, (Bytes, i32)> {
|
||||||
|
let mut data = HashMap::new();
|
||||||
|
mem::swap(&mut self.data, &mut data);
|
||||||
|
data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,59 +1,115 @@
|
|||||||
//! Disk-backed HashDB implementation.
|
//! Disk-backed HashDB implementation.
|
||||||
|
|
||||||
|
use error::*;
|
||||||
use hash::*;
|
use hash::*;
|
||||||
use bytes::*;
|
use bytes::*;
|
||||||
use sha3::*;
|
use sha3::*;
|
||||||
use hashdb::*;
|
use hashdb::*;
|
||||||
use memorydb::*;
|
use memorydb::*;
|
||||||
use std::ops::*;
|
use std::ops::*;
|
||||||
|
use std::sync::*;
|
||||||
use rocksdb::{DB, Writable};
|
use rocksdb::{DB, Writable};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OverlayDB {
|
pub struct OverlayDB {
|
||||||
overlay: MemoryDB,
|
overlay: MemoryDB,
|
||||||
backing: DB,
|
backing: Arc<DB>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OverlayDB {
|
impl OverlayDB {
|
||||||
/// Create a new instance of OverlayDB given a `backing` database.
|
/// Create a new instance of OverlayDB given a `backing` database.
|
||||||
fn new(backing: DB) {
|
fn new(backing: DB) -> OverlayDB {
|
||||||
self.backing = backing;
|
OverlayDB{ overlay: MemoryDB::new(), backing: Arc::new(backing) }
|
||||||
overlay = MemoryDB::new();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commit all memory operations to the backing database.
|
/// Commit all memory operations to the backing database.
|
||||||
fn commit(&mut self) {
|
fn commit(&mut self) -> Result<u32, EthcoreError> {
|
||||||
unimplemented!();
|
let mut ret = 0u32;
|
||||||
|
for i in self.overlay.drain().into_iter() {
|
||||||
|
let (key, (value, rc)) = i;
|
||||||
|
if rc != 0 {
|
||||||
|
let new_entry = match self.payload(&key) {
|
||||||
|
Some(x) => {
|
||||||
|
let (back_value, back_rc) = x;
|
||||||
|
if back_rc + rc < 0 {
|
||||||
|
return Err(From::from(BaseDataError::NegativelyReferencedHash));
|
||||||
|
}
|
||||||
|
self.put_payload(&key, (&back_value, rc + back_rc));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
self.put_payload(&key, (&value, rc));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ret += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the refs and value of the given key.
|
/// Get the refs and value of the given key.
|
||||||
fn payload(&self, key: &H256) -> Option<(Bytes, i32)> {
|
fn payload(&self, key: &H256) -> Option<(Bytes, i32)> {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the refs and value of the given key.
|
||||||
|
fn put_payload(&self, key: &H256, payload: (&Bytes, i32)) {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HashDB for OverlayDB {
|
impl HashDB for OverlayDB {
|
||||||
fn lookup(&self, key: &H256) -> Option<Bytes> {
|
fn lookup(&self, key: &H256) -> Option<Bytes> {
|
||||||
// TODO: return ok if positive; if negative, check backing - might be enough references there to make
|
// return ok if positive; if negative, check backing - might be enough references there to make
|
||||||
// it positive again.
|
// it positive again.
|
||||||
let k = self.overlay.data.get(key);
|
let k = self.overlay.raw(key);
|
||||||
match k {
|
match k {
|
||||||
Some(&(ref d, rc)) if rc > 0 => Some(d.clone()),
|
Some(&(ref d, rc)) if rc > 0 => Some(d.clone()),
|
||||||
_ => {
|
_ => {
|
||||||
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
||||||
match self.payload(key) {
|
match self.payload(key) {
|
||||||
Some((d, rc)) if rc + memrc > 0 => Some(d),
|
Some(x) => {
|
||||||
|
let (d, rc) = x;
|
||||||
|
if rc + memrc > 0 {
|
||||||
|
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),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn exists(&self, key: &H256) -> bool {
|
fn exists(&self, key: &H256) -> bool {
|
||||||
// TODO: copy and adapt code above.
|
// return ok if positive; if negative, check backing - might be enough references there to make
|
||||||
m_overlay.exists(key)
|
// it positive again.
|
||||||
|
let k = self.overlay.raw(key);
|
||||||
|
match k {
|
||||||
|
Some(&(ref d, rc)) if rc > 0 => true,
|
||||||
|
_ => {
|
||||||
|
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
||||||
|
match self.payload(key) {
|
||||||
|
Some(x) => {
|
||||||
|
let (d, rc) = x;
|
||||||
|
if rc + memrc > 0 {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
fn insert(&mut self, value: &[u8]) -> H256 { m_overlay.insert(value) }
|
else {
|
||||||
fn kill(&mut self, key: &H256) { m_overlay.kill(key); }
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn insert(&mut self, value: &[u8]) -> H256 { self.overlay.insert(value) }
|
||||||
|
fn kill(&mut self, key: &H256) { self.overlay.kill(key); }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user