Struct memorydb::MemoryDB [−][src]
pub struct MemoryDB<H: KeyHasher, T> { /* fields omitted */ }
Reference-counted memory-based HashDB
implementation.
Use new()
to create a new database. Insert items with insert()
, remove items
with remove()
, check for existence with contains()
and lookup a hash to derive
the data with get()
. Clear with clear()
and purge the portions of the data
that have no references with purge()
.
Example
extern crate hashdb; extern crate keccak_hasher; extern crate memorydb; use hashdb::*; use keccak_hasher::KeccakHasher; use memorydb::*; fn main() { let mut m = MemoryDB::<KeccakHasher, Vec<u8>>::new(); let d = "Hello world!".as_bytes(); let k = m.insert(d); assert!(m.contains(&k)); assert_eq!(m.get(&k).unwrap(), d); m.insert(d); assert!(m.contains(&k)); m.remove(&k); assert!(m.contains(&k)); m.remove(&k); assert!(!m.contains(&k)); m.remove(&k); assert!(!m.contains(&k)); m.insert(d); assert!(!m.contains(&k)); m.insert(d); assert!(m.contains(&k)); assert_eq!(m.get(&k).unwrap(), d); m.remove(&k); assert!(!m.contains(&k)); }
Methods
impl<'a, H, T> MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: From<&'a [u8]> + Clone,
[src]
impl<'a, H, T> MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: From<&'a [u8]> + Clone,
impl<H, T> MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: Default,
[src]
impl<H, T> MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: Default,
pub fn remove_and_purge(&mut self, key: &<H as KeyHasher>::Out) -> Option<T>
[src]
pub fn remove_and_purge(&mut self, key: &<H as KeyHasher>::Out) -> Option<T>
Remove an element and delete it from storage if reference count reaches zero. If the value was purged, return the old value.
impl<H: KeyHasher, T: Clone> MemoryDB<H, T>
[src]
impl<H: KeyHasher, T: Clone> MemoryDB<H, T>
pub fn from_null_node(null_key: &[u8], null_node_data: T) -> Self
[src]
pub fn from_null_node(null_key: &[u8], null_node_data: T) -> Self
Create a new MemoryDB
from a given null key/data
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Clear all data from the database.
Examples
extern crate hashdb; extern crate keccak_hasher; extern crate memorydb; use hashdb::*; use keccak_hasher::KeccakHasher; use memorydb::*; fn main() { let mut m = MemoryDB::<KeccakHasher, Vec<u8>>::new(); let hello_bytes = "Hello world!".as_bytes(); let hash = m.insert(hello_bytes); assert!(m.contains(&hash)); m.clear(); assert!(!m.contains(&hash)); }
pub fn purge(&mut self)
[src]
pub fn purge(&mut self)
Purge all zero-referenced data from the database.
pub fn drain(
&mut self
) -> HashMap<<H as KeyHasher>::Out, (T, i32), BuildHasherDefault<<H as KeyHasher>::StdHasher>>
[src]
pub fn drain(
&mut self
) -> HashMap<<H as KeyHasher>::Out, (T, i32), BuildHasherDefault<<H as KeyHasher>::StdHasher>>
Return the internal map of hashes to data, clearing the current state.
pub fn raw(&self, key: &<H as KeyHasher>::Out) -> Option<(T, i32)>
[src]
pub fn raw(&self, key: &<H as KeyHasher>::Out) -> Option<(T, i32)>
Grab the raw information associated with a key. Returns None if the key doesn't exist.
Even when Some is returned, the data is only guaranteed to be useful when the refs > 0.
pub fn consolidate(&mut self, other: Self)
[src]
pub fn consolidate(&mut self, other: Self)
Consolidate all the entries of other
into self
.
impl<H, T> MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: HeapSizeOf,
[src]
impl<H, T> MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: HeapSizeOf,
Trait Implementations
impl<H: Clone + KeyHasher, T: Clone> Clone for MemoryDB<H, T> where
H::Out: Clone,
[src]
impl<H: Clone + KeyHasher, T: Clone> Clone for MemoryDB<H, T> where
H::Out: Clone,
fn clone(&self) -> MemoryDB<H, T>
[src]
fn clone(&self) -> MemoryDB<H, T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<H: PartialEq + KeyHasher, T: PartialEq> PartialEq for MemoryDB<H, T> where
H::Out: PartialEq,
[src]
impl<H: PartialEq + KeyHasher, T: PartialEq> PartialEq for MemoryDB<H, T> where
H::Out: PartialEq,
fn eq(&self, other: &MemoryDB<H, T>) -> bool
[src]
fn eq(&self, other: &MemoryDB<H, T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &MemoryDB<H, T>) -> bool
[src]
fn ne(&self, other: &MemoryDB<H, T>) -> bool
This method tests for !=
.
impl<'a, H, T> Default for MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: From<&'a [u8]> + Clone,
[src]
impl<'a, H, T> Default for MemoryDB<H, T> where
H: KeyHasher,
H::Out: HeapSizeOf,
T: From<&'a [u8]> + Clone,
impl<H, T> HashDB<H, T> for MemoryDB<H, T> where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Send + Sync + Clone,
[src]
impl<H, T> HashDB<H, T> for MemoryDB<H, T> where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Send + Sync + Clone,
fn keys(&self) -> HashMap<H::Out, i32>
[src]
fn keys(&self) -> HashMap<H::Out, i32>
Get the keys in the database together with number of underlying references.
fn get(&self, key: &H::Out) -> Option<T>
[src]
fn get(&self, key: &H::Out) -> Option<T>
Look up a given hash into the bytes that hash to it, returning None if the hash is not known. Read more
fn contains(&self, key: &H::Out) -> bool
[src]
fn contains(&self, key: &H::Out) -> bool
Check for the existance of a hash-key.
fn emplace(&mut self, key: H::Out, value: T)
[src]
fn emplace(&mut self, key: H::Out, value: T)
Like insert()
, except you provide the key and the data is all moved.
fn insert(&mut self, value: &[u8]) -> H::Out
[src]
fn insert(&mut self, value: &[u8]) -> H::Out
Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions are counted and the equivalent number of remove()
s must be performed before the data is considered dead. Read more
fn remove(&mut self, key: &H::Out)
[src]
fn remove(&mut self, key: &H::Out)
Remove a datum previously inserted. Insertions can be "owed" such that the same number of insert()
s may happen without the data being eventually being inserted into the DB. It can be "owed" more than once. Read more
impl<H, T> AsHashDB<H, T> for MemoryDB<H, T> where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Send + Sync + Clone,
[src]
impl<H, T> AsHashDB<H, T> for MemoryDB<H, T> where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Send + Sync + Clone,