OverlayDB complete and tested.

This commit is contained in:
Gav Wood 2015-11-28 22:54:12 +01:00
parent 358962d2ea
commit cefb1fa9ce
2 changed files with 116 additions and 8 deletions

View File

@ -232,9 +232,15 @@ macro_rules! impl_hash {
} }
} }
impl $from {
pub fn hex(&self) -> String {
format!("{}", self)
}
}
} }
} }
impl_hash!(H32, 4);
impl_hash!(H64, 8); impl_hash!(H64, 8);
impl_hash!(H128, 16); impl_hash!(H128, 16);
impl_hash!(Address, 20); impl_hash!(Address, 20);
@ -255,6 +261,7 @@ mod tests {
assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h); assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h);
assert_eq!(format!("{}", h), "0123456789abcdef"); assert_eq!(format!("{}", h), "0123456789abcdef");
assert_eq!(format!("{:?}", h), "0123456789abcdef"); assert_eq!(format!("{:?}", h), "0123456789abcdef");
assert_eq!(h.hex(), "0123456789abcdef");
assert!(h == h); assert!(h == h);
assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee])); assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee]));
assert!(h != H64([0; 8])); assert!(h != H64([0; 8]));

View File

@ -8,6 +8,7 @@ use hashdb::*;
use memorydb::*; use memorydb::*;
use std::ops::*; use std::ops::*;
use std::sync::*; use std::sync::*;
use std::env;
use rocksdb::{DB, Writable}; use rocksdb::{DB, Writable};
#[derive(Clone)] #[derive(Clone)]
@ -22,6 +23,13 @@ impl OverlayDB {
OverlayDB{ overlay: MemoryDB::new(), backing: Arc::new(backing) } OverlayDB{ overlay: MemoryDB::new(), backing: Arc::new(backing) }
} }
/// Create a new instance of OverlayDB with an anonymous temporary database.
pub fn new_temp() -> OverlayDB {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
Self::new(DB::open_default(dir.to_str().unwrap()).unwrap())
}
/// Commit all memory operations to the backing database. /// Commit all memory operations to the backing database.
pub fn commit(&mut self) -> Result<u32, EthcoreError> { pub fn commit(&mut self) -> Result<u32, EthcoreError> {
let mut ret = 0u32; let mut ret = 0u32;
@ -50,6 +58,8 @@ impl OverlayDB {
Ok(ret) Ok(ret)
} }
pub fn revert(&mut self) { self.overlay.clear(); }
/// 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, u32)> { fn payload(&self, key: &H256) -> Option<(Bytes, u32)> {
self.backing.get(&key.bytes()) self.backing.get(&key.bytes())
@ -124,15 +134,106 @@ impl HashDB for OverlayDB {
fn kill(&mut self, key: &H256) { self.overlay.kill(key); } fn kill(&mut self, key: &H256) { self.overlay.kill(key); }
} }
#[test]
fn overlaydb_overlay_insert_and_kill() {
let mut trie = OverlayDB::new_temp();
let h = trie.insert(b"hello world");
assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec()));
trie.kill(&h);
assert_eq!(trie.lookup(&h), None);
}
#[test]
fn overlaydb_backing_insert_revert() {
let mut trie = OverlayDB::new_temp();
let h = trie.insert(b"hello world");
assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec()));
trie.commit().unwrap();
assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec()));
trie.revert();
assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec()));
}
#[test]
fn overlaydb_backing_kill() {
let mut trie = OverlayDB::new_temp();
let h = trie.insert(b"hello world");
trie.commit().unwrap();
trie.kill(&h);
assert_eq!(trie.lookup(&h), None);
trie.commit().unwrap();
assert_eq!(trie.lookup(&h), None);
trie.revert();
assert_eq!(trie.lookup(&h), None);
}
#[test]
fn overlaydb_backing_kill_revert() {
let mut trie = OverlayDB::new_temp();
let h = trie.insert(b"hello world");
trie.commit().unwrap();
trie.kill(&h);
assert_eq!(trie.lookup(&h), None);
trie.revert();
assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec()));
}
#[test]
fn overlaydb_negative() {
let mut trie = OverlayDB::new_temp();
let h = trie.insert(b"hello world");
trie.commit().unwrap();
trie.kill(&h);
trie.kill(&h); //bad - sends us into negative refs.
assert_eq!(trie.lookup(&h), None);
assert!(trie.commit().is_err());
}
#[test]
fn overlaydb_complex() {
let mut trie = OverlayDB::new_temp();
let hfoo = trie.insert(b"foo");
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
let hbar = trie.insert(b"bar");
assert_eq!(trie.lookup(&hbar), Some(b"bar".to_vec()));
trie.commit().unwrap();
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
assert_eq!(trie.lookup(&hbar), Some(b"bar".to_vec()));
trie.insert(b"foo"); // two refs
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
trie.commit().unwrap();
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
assert_eq!(trie.lookup(&hbar), Some(b"bar".to_vec()));
trie.kill(&hbar); // zero refs - delete
assert_eq!(trie.lookup(&hbar), None);
trie.kill(&hfoo); // one ref - keep
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
trie.commit().unwrap();
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
trie.kill(&hfoo); // zero ref - would delete, but...
assert_eq!(trie.lookup(&hfoo), None);
trie.insert(b"foo"); // one ref - keep after all.
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
trie.commit().unwrap();
assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec()));
trie.kill(&hfoo); // zero ref - delete
assert_eq!(trie.lookup(&hfoo), None);
trie.commit().unwrap(); //
assert_eq!(trie.lookup(&hfoo), None);
}
#[test] #[test]
fn playpen() { fn playpen() {
let mut db: DB = DB::open_default("/tmp/test").unwrap(); use std::fs;
db.put(b"test", b"test2"); {
let db: DB = DB::open_default("/tmp/test").unwrap();
db.put(b"test", b"test2").unwrap();
match db.get(b"test") { match db.get(b"test") {
Ok(Some(value)) => println!("Got value {:?}", value.deref()), Ok(Some(value)) => println!("Got value {:?}", value.deref()),
Ok(None) => println!("No value for that key"), Ok(None) => println!("No value for that key"),
Err(e) => println!("Gah"), Err(..) => println!("Gah"),
} }
db.delete(b"test"); db.delete(b"test").unwrap();
assert!(false); }
fs::remove_dir_all("/tmp/test").unwrap();
} }