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 {
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 {
self.raw.get(&key.sha3())
}
}
impl<'db> TrieMut for FatDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) {
let hash = key.sha3();
self.raw.insert(&hash, value);

View File

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

View File

@ -50,7 +50,7 @@ impl<'db> SecTrieDBMut<'db> {
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 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 {
self.raw.get(&key.sha3())
}
}
impl<'db> TrieMut for SecTrieDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) {
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 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 {
self.do_lookup(&NibbleSlice::new(key))
}
}
impl<'db> TrieMut for TrieDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) {
match value.is_empty() {
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.
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.
fn root(&self) -> &H256;
@ -33,10 +30,25 @@ pub trait 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;
/// 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.
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
/// `key` from the trie.
fn insert(&mut self, key: &[u8], value: &[u8]);
@ -45,4 +57,3 @@ pub trait TrieMut: Trie {
/// value.
fn remove(&mut self, key: &[u8]);
}