HashDB and initial OverlayDB

This commit is contained in:
Gav Wood 2015-11-28 03:08:57 +01:00
parent 705ab53948
commit 852233e4ac
3 changed files with 24 additions and 7 deletions

View File

@ -14,10 +14,10 @@ pub trait HashDB {
/// let mut m = MemoryDB::new(); /// let mut m = MemoryDB::new();
/// let hello_bytes = "Hello world!".as_bytes(); /// let hello_bytes = "Hello world!".as_bytes();
/// let hash = m.insert(hello_bytes); /// let hash = m.insert(hello_bytes);
/// assert_eq!(m.lookup(&hash).unwrap(), &hello_bytes); /// assert_eq!(m.lookup(&hash).unwrap(), hello_bytes);
/// } /// }
/// ``` /// ```
fn lookup(&self, key: &H256) -> Option<&Bytes>; fn lookup(&self, key: &H256) -> Option<Bytes>;
/// Check for the existance of a hash-key. /// Check for the existance of a hash-key.
/// ///
@ -75,7 +75,7 @@ pub trait HashDB {
/// m.insert(d); // OK - now it's "empty" again. /// m.insert(d); // OK - now it's "empty" again.
/// assert!(!m.exists(key)); /// assert!(!m.exists(key));
/// m.insert(d); // OK - now we've /// m.insert(d); // OK - now we've
/// assert_eq!(m.lookup(key).unwrap(), &d); /// assert_eq!(m.lookup(key).unwrap(), d);
/// } /// }
/// ``` /// ```
fn kill(&mut self, key: &H256); fn kill(&mut self, key: &H256);

View File

@ -16,6 +16,7 @@ pub mod db;
pub mod sha3; pub mod sha3;
pub mod hashdb; pub mod hashdb;
pub mod memorydb; pub mod memorydb;
pub mod overlaydb;
pub mod bloom; pub mod bloom;
//pub mod network; //pub mod network;

View File

@ -25,7 +25,7 @@ use std::collections::HashMap;
/// ///
/// let k = m.insert(d); /// let k = m.insert(d);
/// assert!(m.exists(&k)); /// assert!(m.exists(&k));
/// assert_eq!(m.lookup(&k).unwrap(), &d); /// assert_eq!(m.lookup(&k).unwrap(), d);
/// ///
/// m.insert(d); /// m.insert(d);
/// assert!(m.exists(&k)); /// assert!(m.exists(&k));
@ -38,7 +38,7 @@ use std::collections::HashMap;
/// ///
/// m.insert(d); /// m.insert(d);
/// assert!(m.exists(&k)); /// assert!(m.exists(&k));
/// assert_eq!(m.lookup(&k).unwrap(), &d); /// assert_eq!(m.lookup(&k).unwrap(), d);
/// ///
/// m.kill(&k); /// m.kill(&k);
/// assert!(!m.exists(&k)); /// assert!(!m.exists(&k));
@ -84,12 +84,27 @@ impl MemoryDB {
.collect(); .collect();
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
/// 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.
///
/// Even when Some is returned, this is only guaranteed to return something useful
/// when `refs(key) > 0`.
fn value(&self, key: &H256) -> Option<&Bytes> {
self.data.get(key).map(|&(ref d, _)| d)
}
} }
impl HashDB for MemoryDB { impl HashDB for MemoryDB {
fn lookup(&self, key: &H256) -> Option<&Bytes> { fn lookup(&self, key: &H256) -> Option<Bytes> {
match self.data.get(key) { match self.data.get(key) {
Some(&(ref d, rc)) if rc > 0 => Some(d), Some(&(ref d, rc)) if rc > 0 => Some(d.clone()),
_ => None _ => None
} }
} }
@ -116,6 +131,7 @@ impl HashDB for MemoryDB {
} }
key key
} }
fn kill(&mut self, key: &H256) { fn kill(&mut self, key: &H256) {
if match self.data.get_mut(key) { if match self.data.get_mut(key) {
Some(&mut (_, ref mut x)) => { *x -= 1; false } Some(&mut (_, ref mut x)) => { *x -= 1; false }