Update overlaydb

This commit is contained in:
Gav Wood 2016-03-13 18:07:36 +01:00
parent 9e912c7c0d
commit ac655af091

View File

@ -26,7 +26,7 @@ use std::ops::*;
use std::sync::*; use std::sync::*;
use std::env; use std::env;
use std::collections::HashMap; use std::collections::HashMap;
use kvdb::{Database}; use kvdb::{Database, DBTransaction};
/// Implementation of the HashDB trait for a disk-backed database with a memory overlay. /// Implementation of the HashDB trait for a disk-backed database with a memory overlay.
/// ///
@ -36,7 +36,7 @@ use kvdb::{Database};
/// ///
/// `lookup()` and `contains()` maintain normal behaviour - all `insert()` and `remove()` /// `lookup()` and `contains()` maintain normal behaviour - all `insert()` and `remove()`
/// queries have an immediate effect in terms of these functions. /// queries have an immediate effect in terms of these functions.
//#[derive(Clone)] #[derive(Clone)]
pub struct OverlayDB { pub struct OverlayDB {
overlay: MemoryDB, overlay: MemoryDB,
backing: Arc<Database>, backing: Arc<Database>,
@ -58,6 +58,36 @@ impl OverlayDB {
Self::new(Database::open_default(dir.to_str().unwrap()).unwrap()) Self::new(Database::open_default(dir.to_str().unwrap()).unwrap())
} }
/// Commit all operations to given batch.
pub fn commit_to_batch(&mut self, batch: &DBTransaction) -> Result<u32, UtilError> {
let mut ret = 0u32;
let mut deletes = 0usize;
for i in self.overlay.drain().into_iter() {
let (key, (value, rc)) = i;
if rc != 0 {
match self.payload(&key) {
Some(x) => {
let (back_value, back_rc) = x;
let total_rc: i32 = back_rc as i32 + rc;
if total_rc < 0 {
return Err(From::from(BaseDataError::NegativelyReferencedHash));
}
deletes += if self.put_payload(batch, &key, (back_value, total_rc as u32)) {1} else {0};
}
None => {
if rc < 0 {
return Err(From::from(BaseDataError::NegativelyReferencedHash));
}
self.put_payload(batch, &key, (value, rc as u32));
}
};
ret += 1;
}
}
trace!("OverlayDB::commit() deleted {} nodes", deletes);
Ok(ret)
}
/// Commit all memory operations to the backing database. /// Commit all memory operations to the backing database.
/// ///
/// Returns either an error or the number of items changed in the backing database. /// Returns either an error or the number of items changed in the backing database.
@ -86,32 +116,10 @@ impl OverlayDB {
/// } /// }
/// ``` /// ```
pub fn commit(&mut self) -> Result<u32, UtilError> { pub fn commit(&mut self) -> Result<u32, UtilError> {
let mut ret = 0u32; let batch = DBTransaction::new();
let mut deletes = 0usize; let r = try!(self.commit_to_batch(&batch));
for i in self.overlay.drain().into_iter() { try!(self.backing.write(batch));
let (key, (value, rc)) = i; Ok(r)
if rc != 0 {
match self.payload(&key) {
Some(x) => {
let (back_value, back_rc) = x;
let total_rc: i32 = back_rc as i32 + rc;
if total_rc < 0 {
return Err(From::from(BaseDataError::NegativelyReferencedHash));
}
deletes += if self.put_payload(&key, (back_value, total_rc as u32)) {1} else {0};
}
None => {
if rc < 0 {
return Err(From::from(BaseDataError::NegativelyReferencedHash));
}
self.put_payload(&key, (value, rc as u32));
}
};
ret += 1;
}
}
trace!("OverlayDB::commit() deleted {} nodes", deletes);
Ok(ret)
} }
/// Revert all operations on this object (i.e. `insert()`s and `kill()`s) since the /// Revert all operations on this object (i.e. `insert()`s and `kill()`s) since the
@ -148,15 +156,15 @@ impl OverlayDB {
} }
/// Put the refs and value of the given key, possibly deleting it from the db. /// Put the refs and value of the given key, possibly deleting it from the db.
fn put_payload(&self, key: &H256, payload: (Bytes, u32)) -> bool { fn put_payload(&self, batch: &DBTransaction, key: &H256, payload: (Bytes, u32)) -> bool {
if payload.1 > 0 { if payload.1 > 0 {
let mut s = RlpStream::new_list(2); let mut s = RlpStream::new_list(2);
s.append(&payload.1); s.append(&payload.1);
s.append(&payload.0); s.append(&payload.0);
self.backing.put(&key.bytes(), s.as_raw()).expect("Low-level database error. Some issue with your hard disk?"); batch.put(&key.bytes(), s.as_raw()).expect("Low-level database error. Some issue with your hard disk?");
false false
} else { } else {
self.backing.delete(&key.bytes()).expect("Low-level database error. Some issue with your hard disk?"); batch.delete(&key.bytes()).expect("Low-level database error. Some issue with your hard disk?");
true true
} }
} }