separated TrieMut from Trie, added Generic Trie type to TrieFactory

This commit is contained in:
debris 2016-06-27 11:19:27 +02:00
parent 5ecbeaa82f
commit 36626f96a8
5 changed files with 28 additions and 18 deletions

View File

@ -52,7 +52,7 @@ impl<'db> FatDBMut<'db> {
} }
} }
impl<'db> Trie for FatDBMut<'db> { impl<'db> TrieMut for FatDBMut<'db> {
fn root(&self) -> &H256 { fn root(&self) -> &H256 {
self.raw.root() self.raw.root()
} }
@ -64,9 +64,7 @@ impl<'db> Trie for FatDBMut<'db> {
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key { fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key {
self.raw.get(&key.sha3()) self.raw.get(&key.sha3())
} }
}
impl<'db> TrieMut for FatDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) { fn insert(&mut self, key: &[u8], value: &[u8]) {
let hash = key.sha3(); let hash = key.sha3();
self.raw.insert(&hash, value); self.raw.insert(&hash, value);

View File

@ -66,6 +66,8 @@ impl fmt::Display for TrieError {
/// Trie types /// Trie types
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TrieSpec { pub enum TrieSpec {
/// Generic trie.
Generic,
/// Secure trie. /// Secure trie.
Secure, Secure,
/// Secure trie with fat database. /// Secure trie with fat database.
@ -95,22 +97,25 @@ impl TrieFactory {
/// Create new immutable instance of Trie. /// Create new immutable instance of Trie.
pub fn create<'db>(&self, db: &'db HashDB, root: &'db H256) -> Result<Box<Trie + 'db>, TrieError> { pub fn create<'db>(&self, db: &'db HashDB, root: &'db H256) -> Result<Box<Trie + 'db>, TrieError> {
match self.spec { match self.spec {
TrieSpec::Generic => Ok(Box::new(try!(TrieDB::new(db, root)))),
TrieSpec::Secure => Ok(Box::new(try!(SecTrieDB::new(db, root)))), TrieSpec::Secure => Ok(Box::new(try!(SecTrieDB::new(db, root)))),
TrieSpec::Fat => Ok(Box::new(try!(FatDB::new(db, root)))), TrieSpec::Fat => Ok(Box::new(try!(FatDB::new(db, root)))),
} }
} }
/// Create new mutable instance of Trie. /// Create new mutable instance of Trie.
pub fn create_mut<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Result<Box<Trie + 'db>, TrieError> { pub fn create_mut<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Box<TrieMut + 'db> {
match self.spec { match self.spec {
TrieSpec::Secure => Ok(Box::new(SecTrieDBMut::new(db, root))), TrieSpec::Generic => Box::new(TrieDBMut::new(db, root)),
TrieSpec::Fat => Ok(Box::new(FatDBMut::new(db, root))), TrieSpec::Secure => Box::new(SecTrieDBMut::new(db, root)),
TrieSpec::Fat => Box::new(FatDBMut::new(db, root)),
} }
} }
/// Create new mutable instance of trie and check for errors. /// Create new mutable instance of trie and check for errors.
pub fn from_existing<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Result<Box<Trie + 'db>, TrieError> { pub fn from_existing<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Result<Box<TrieMut + 'db>, TrieError> {
match self.spec { match self.spec {
TrieSpec::Generic => Ok(Box::new(try!(TrieDBMut::from_existing(db, root)))),
TrieSpec::Secure => Ok(Box::new(try!(SecTrieDBMut::from_existing(db, root)))), TrieSpec::Secure => Ok(Box::new(try!(SecTrieDBMut::from_existing(db, root)))),
TrieSpec::Fat => Ok(Box::new(try!(FatDBMut::from_existing(db, root)))), TrieSpec::Fat => Ok(Box::new(try!(FatDBMut::from_existing(db, root)))),
} }

View File

@ -50,7 +50,7 @@ impl<'db> SecTrieDBMut<'db> {
pub fn db_mut(&mut self) -> &mut HashDB { self.raw.db_mut() } pub fn db_mut(&mut self) -> &mut HashDB { self.raw.db_mut() }
} }
impl<'db> Trie for SecTrieDBMut<'db> { impl<'db> TrieMut for SecTrieDBMut<'db> {
fn root(&self) -> &H256 { self.raw.root() } fn root(&self) -> &H256 { self.raw.root() }
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {
@ -60,9 +60,7 @@ impl<'db> Trie for SecTrieDBMut<'db> {
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key { fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key {
self.raw.get(&key.sha3()) self.raw.get(&key.sha3())
} }
}
impl<'db> TrieMut for SecTrieDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) { fn insert(&mut self, key: &[u8], value: &[u8]) {
self.raw.insert(&key.sha3(), value); self.raw.insert(&key.sha3(), value);
} }

View File

@ -642,7 +642,7 @@ impl<'db> TrieDBMut<'db> {
} }
} }
impl<'db> Trie for TrieDBMut<'db> { impl<'db> TrieMut for TrieDBMut<'db> {
fn root(&self) -> &H256 { &self.root } fn root(&self) -> &H256 { &self.root }
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {
@ -652,9 +652,7 @@ impl<'db> Trie for TrieDBMut<'db> {
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key { fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key {
self.do_lookup(&NibbleSlice::new(key)) self.do_lookup(&NibbleSlice::new(key))
} }
}
impl<'db> TrieMut for TrieDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) { fn insert(&mut self, key: &[u8], value: &[u8]) {
match value.is_empty() { match value.is_empty() {
false => self.insert_ns(&NibbleSlice::new(key), value), false => self.insert_ns(&NibbleSlice::new(key), value),

View File

@ -19,9 +19,6 @@ use rlp::SHA3_NULL_RLP;
/// A key-value datastore implemented as a database-backed modified Merkle tree. /// A key-value datastore implemented as a database-backed modified Merkle tree.
pub trait Trie { pub trait Trie {
/// Returns an iterator over elements of trie.
fn iter<'a>(&'a self) -> Box<Iterator<Item = (Vec<u8>, &[u8])> + 'a> { unimplemented!() }
/// Return the root of the trie. /// Return the root of the trie.
fn root(&self) -> &H256; fn root(&self) -> &H256;
@ -33,10 +30,25 @@ pub trait Trie {
/// What is the value of the given key in this trie? /// What is the value of the given key in this trie?
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key; fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key;
/// Returns an iterator over elements of trie.
fn iter<'a>(&'a self) -> Box<Iterator<Item = (Vec<u8>, &[u8])> + 'a>;
} }
/// A key-value datastore implemented as a database-backed modified Merkle tree. /// A key-value datastore implemented as a database-backed modified Merkle tree.
pub trait TrieMut: Trie { pub trait TrieMut {
/// Return the root of the trie.
fn root(&self) -> &H256;
/// Is the trie empty?
fn is_empty(&self) -> bool { *self.root() == SHA3_NULL_RLP }
/// Does the trie contain a given key?
fn contains(&self, key: &[u8]) -> bool;
/// What is the value of the given key in this trie?
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key;
/// Insert a `key`/`value` pair into the trie. An `empty` value is equivalent to removing /// Insert a `key`/`value` pair into the trie. An `empty` value is equivalent to removing
/// `key` from the trie. /// `key` from the trie.
fn insert(&mut self, key: &[u8], value: &[u8]); fn insert(&mut self, key: &[u8], value: &[u8]);
@ -45,4 +57,3 @@ pub trait TrieMut: Trie {
/// value. /// value.
fn remove(&mut self, key: &[u8]); fn remove(&mut self, key: &[u8]);
} }