trie factory in progress
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
//! Trie interface and implementation.
|
||||
|
||||
use std::fmt;
|
||||
use hash::H256;
|
||||
use hashdb::HashDB;
|
||||
|
||||
/// Export the trietraits module.
|
||||
pub mod trietraits;
|
||||
@@ -37,6 +39,8 @@ pub mod sectriedbmut;
|
||||
|
||||
mod fatdb;
|
||||
|
||||
mod fatdbmut;
|
||||
|
||||
pub use self::trietraits::{Trie, TrieMut};
|
||||
pub use self::standardmap::{Alphabet, StandardMap, ValueMode};
|
||||
pub use self::triedbmut::TrieDBMut;
|
||||
@@ -44,6 +48,7 @@ pub use self::triedb::{TrieDB, TrieDBIterator};
|
||||
pub use self::sectriedbmut::SecTrieDBMut;
|
||||
pub use self::sectriedb::SecTrieDB;
|
||||
pub use self::fatdb::{FatDB, FatDBIterator};
|
||||
pub use self::fatdbmut::FatDBMut;
|
||||
|
||||
/// Trie Errors
|
||||
#[derive(Debug)]
|
||||
@@ -57,3 +62,57 @@ impl fmt::Display for TrieError {
|
||||
write!(f, "Trie Error: Invalid state root.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Trie types
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TrieSpec {
|
||||
/// Secure trie.
|
||||
Secure,
|
||||
/// Secure trie with fat database.
|
||||
Fat,
|
||||
}
|
||||
|
||||
impl Default for TrieSpec {
|
||||
fn default() -> TrieSpec {
|
||||
TrieSpec::Secure
|
||||
}
|
||||
}
|
||||
|
||||
/// Trie factory.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct TrieFactory {
|
||||
spec: TrieSpec,
|
||||
}
|
||||
|
||||
impl TrieFactory {
|
||||
/// Creates new factory.
|
||||
pub fn new(spec: TrieSpec) -> Self {
|
||||
TrieFactory {
|
||||
spec: spec,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::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> {
|
||||
match self.spec {
|
||||
TrieSpec::Secure => Ok(Box::new(SecTrieDBMut::new(db, root))),
|
||||
TrieSpec::Fat => Ok(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> {
|
||||
match self.spec {
|
||||
TrieSpec::Secure => Ok(Box::new(try!(SecTrieDBMut::from_existing(db, root)))),
|
||||
TrieSpec::Fat => Ok(Box::new(try!(FatDBMut::from_existing(db, root)))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user