pub struct MemoryDB<H: KeyHasher, T> { /* private fields */ }Expand description
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));
}Implementations§
Source§impl<H, T> MemoryDB<H, T>
impl<H, T> MemoryDB<H, T>
Sourcepub fn remove_and_purge(&mut self, key: &<H as KeyHasher>::Out) -> Option<T>
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.
Source§impl<H: KeyHasher, T: Clone> MemoryDB<H, T>
impl<H: KeyHasher, T: Clone> MemoryDB<H, T>
Sourcepub fn from_null_node(null_key: &[u8], null_node_data: T) -> Self
pub fn from_null_node(null_key: &[u8], null_node_data: T) -> Self
Create a new MemoryDB from a given null key/data
Sourcepub fn clear(&mut self)
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));
}Sourcepub fn drain(
&mut self,
) -> HashMap<<H as KeyHasher>::Out, (T, i32), BuildHasherDefault<<H as KeyHasher>::StdHasher>>
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.
Sourcepub fn raw(&self, key: &<H as KeyHasher>::Out) -> Option<(T, i32)>
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.
Sourcepub fn consolidate(&mut self, other: Self)
pub fn consolidate(&mut self, other: Self)
Consolidate all the entries of other into self.
Trait Implementations§
Source§impl<H, T> HashDB<H, T> for MemoryDB<H, T>
impl<H, T> HashDB<H, T> for MemoryDB<H, T>
Source§fn keys(&self) -> HashMap<H::Out, i32>
fn keys(&self) -> HashMap<H::Out, i32>
Source§fn get(&self, key: &H::Out) -> Option<T>
fn get(&self, key: &H::Out) -> Option<T>
Source§fn emplace(&mut self, key: H::Out, value: T)
fn emplace(&mut self, key: H::Out, value: T)
insert(), except you provide the key and the data is all moved.