From 852233e4ac1b3109e2b73f876edaba68052f0c2f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 28 Nov 2015 03:08:57 +0100 Subject: [PATCH] HashDB and initial OverlayDB --- src/hashdb.rs | 6 +++--- src/lib.rs | 1 + src/memorydb.rs | 24 ++++++++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/hashdb.rs b/src/hashdb.rs index d023fe70d..48f5182bc 100644 --- a/src/hashdb.rs +++ b/src/hashdb.rs @@ -14,10 +14,10 @@ pub trait HashDB { /// let mut m = MemoryDB::new(); /// let hello_bytes = "Hello world!".as_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; /// Check for the existance of a hash-key. /// @@ -75,7 +75,7 @@ pub trait HashDB { /// m.insert(d); // OK - now it's "empty" again. /// assert!(!m.exists(key)); /// 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); diff --git a/src/lib.rs b/src/lib.rs index f7f446ddc..cb03d00b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub mod db; pub mod sha3; pub mod hashdb; pub mod memorydb; +pub mod overlaydb; pub mod bloom; //pub mod network; diff --git a/src/memorydb.rs b/src/memorydb.rs index 55fdc12da..2f54b4524 100644 --- a/src/memorydb.rs +++ b/src/memorydb.rs @@ -25,7 +25,7 @@ use std::collections::HashMap; /// /// let k = m.insert(d); /// assert!(m.exists(&k)); -/// assert_eq!(m.lookup(&k).unwrap(), &d); +/// assert_eq!(m.lookup(&k).unwrap(), d); /// /// m.insert(d); /// assert!(m.exists(&k)); @@ -38,7 +38,7 @@ use std::collections::HashMap; /// /// m.insert(d); /// assert!(m.exists(&k)); -/// assert_eq!(m.lookup(&k).unwrap(), &d); +/// assert_eq!(m.lookup(&k).unwrap(), d); /// /// m.kill(&k); /// assert!(!m.exists(&k)); @@ -84,12 +84,27 @@ impl MemoryDB { .collect(); 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 { + 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 { - fn lookup(&self, key: &H256) -> Option<&Bytes> { + fn lookup(&self, key: &H256) -> Option { 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 } } @@ -116,6 +131,7 @@ impl HashDB for MemoryDB { } key } + fn kill(&mut self, key: &H256) { if match self.data.get_mut(key) { Some(&mut (_, ref mut x)) => { *x -= 1; false }