pub struct MemoryDB<H, KF, T>where
H: KeyHasher,
KF: KeyFunction<H>,{ /* 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
use hash_db::{Hasher, HashDB, EMPTY_PREFIX};
use keccak_hasher::KeccakHasher;
use memory_db::{MemoryDB, HashKey};
let mut m = MemoryDB::<KeccakHasher, HashKey<_>, Vec<u8>>::default();
let d = "Hello world!".as_bytes();
let k = m.insert(EMPTY_PREFIX, d);
assert!(m.contains(&k, EMPTY_PREFIX));
assert_eq!(m.get(&k, EMPTY_PREFIX).unwrap(), d);
m.insert(EMPTY_PREFIX, d);
assert!(m.contains(&k, EMPTY_PREFIX));
m.remove(&k, EMPTY_PREFIX);
assert!(m.contains(&k, EMPTY_PREFIX));
m.remove(&k, EMPTY_PREFIX);
assert!(!m.contains(&k, EMPTY_PREFIX));
m.remove(&k, EMPTY_PREFIX);
assert!(!m.contains(&k, EMPTY_PREFIX));
m.insert(EMPTY_PREFIX, d);
assert!(!m.contains(&k, EMPTY_PREFIX));
m.insert(EMPTY_PREFIX, d);
assert!(m.contains(&k, EMPTY_PREFIX));
assert_eq!(m.get(&k, EMPTY_PREFIX).unwrap(), d);
m.remove(&k, EMPTY_PREFIX);
assert!(!m.contains(&k, EMPTY_PREFIX));
Implementations§
source§impl<H, KF, T> MemoryDB<H, KF, T>
impl<H, KF, T> MemoryDB<H, KF, T>
Create a new MemoryDB
from a given null key/data
sourcepub fn remove_and_purge(
&mut self,
key: &<H as KeyHasher>::Out,
prefix: Prefix<'_>,
) -> Option<T>
pub fn remove_and_purge( &mut self, key: &<H as KeyHasher>::Out, prefix: Prefix<'_>, ) -> Option<T>
Remove an element and delete it from storage if reference count reaches zero. If the value was purged, return the old value.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
source§impl<H, KF, T> MemoryDB<H, KF, T>
impl<H, KF, T> MemoryDB<H, KF, 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 default_with_root() -> (Self, H::Out)
pub fn default_with_root() -> (Self, H::Out)
Create a new default instance of Self
and returns Self
and the root hash.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all data from the database.
§Examples
extern crate hash_db;
extern crate keccak_hasher;
extern crate memory_db;
use hash_db::{Hasher, HashDB, EMPTY_PREFIX};
use keccak_hasher::KeccakHasher;
use memory_db::{MemoryDB, HashKey};
fn main() {
let mut m = MemoryDB::<KeccakHasher, HashKey<_>, Vec<u8>>::default();
let hello_bytes = "Hello world!".as_bytes();
let hash = m.insert(EMPTY_PREFIX, hello_bytes);
assert!(m.contains(&hash, EMPTY_PREFIX));
m.clear();
assert!(!m.contains(&hash, EMPTY_PREFIX));
}
sourcepub fn drain(&mut self) -> Map<KF::Key, (T, i32)>
pub fn drain(&mut self) -> Map<KF::Key, (T, i32)>
Return the internal key-value Map, clearing the current state.
sourcepub fn raw(
&self,
key: &<H as KeyHasher>::Out,
prefix: Prefix<'_>,
) -> Option<(&T, i32)>
pub fn raw( &self, key: &<H as KeyHasher>::Out, prefix: Prefix<'_>, ) -> 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, KF, T> AsHashDB<H, T> for MemoryDB<H, KF, T>
impl<H, KF, T> AsHashDB<H, T> for MemoryDB<H, KF, T>
source§fn as_hash_db(&self) -> &dyn HashDB<H, T>
fn as_hash_db(&self) -> &dyn HashDB<H, T>
source§fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<H, T>
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<H, T>
source§impl<H, KF, T> AsPlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>
impl<H, KF, T> AsPlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>
source§fn as_plain_db(&self) -> &dyn PlainDB<H::Out, T>
fn as_plain_db(&self) -> &dyn PlainDB<H::Out, T>
source§fn as_plain_db_mut(&mut self) -> &mut dyn PlainDB<H::Out, T>
fn as_plain_db_mut(&mut self) -> &mut dyn PlainDB<H::Out, T>
source§impl<H, KF, T> HashDB<H, T> for MemoryDB<H, KF, T>
impl<H, KF, T> HashDB<H, T> for MemoryDB<H, KF, T>
source§fn get(&self, key: &H::Out, prefix: Prefix<'_>) -> Option<T>
fn get(&self, key: &H::Out, prefix: Prefix<'_>) -> Option<T>
source§fn contains(&self, key: &H::Out, prefix: Prefix<'_>) -> bool
fn contains(&self, key: &H::Out, prefix: Prefix<'_>) -> bool
source§fn emplace(&mut self, key: H::Out, prefix: Prefix<'_>, value: T)
fn emplace(&mut self, key: H::Out, prefix: Prefix<'_>, value: T)
insert()
, except you provide the key and the data is all moved.source§impl<H, KF, T> PartialEq for MemoryDB<H, KF, T>
impl<H, KF, T> PartialEq for MemoryDB<H, KF, T>
source§impl<H, KF, T> PlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>
impl<H, KF, T> PlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>
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)
remove()
s must be performed before the data is considered dead.
The caller should ensure that a key only corresponds to one value.source§fn remove(&mut self, key: &H::Out)
fn remove(&mut self, key: &H::Out)
insert()
s may happen without the data being eventually
being inserted into the DB. It can be “owed” more than once.
The caller should ensure that a key only corresponds to one value.impl<H, KF, T> Eq for MemoryDB<H, KF, T>
Auto Trait Implementations§
impl<H, KF, T> Freeze for MemoryDB<H, KF, T>
impl<H, KF, T> RefUnwindSafe for MemoryDB<H, KF, T>where
<H as Hasher>::Out: RefUnwindSafe,
T: RefUnwindSafe,
KF: RefUnwindSafe,
<KF as KeyFunction<H>>::Key: RefUnwindSafe,
impl<H, KF, T> Send for MemoryDB<H, KF, T>
impl<H, KF, T> Sync for MemoryDB<H, KF, T>
impl<H, KF, T> Unpin for MemoryDB<H, KF, T>
impl<H, KF, T> UnwindSafe for MemoryDB<H, KF, T>where
<H as Hasher>::Out: UnwindSafe,
T: UnwindSafe,
<KF as KeyFunction<H>>::Key: UnwindSafe,
KF: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)