Struct memory_db::MemoryDB [−][src]
pub struct MemoryDB<H, KF, T, M = DefaultMemTracker<T>> where
H: KeyHasher,
KF: KeyFunction<H>,
M: MemTracker<T>, { /* fields omitted */ }
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()
.
If you’re not using the MallocSizeOf
implementation to track memory usage,
set the M
type parameter to NoopTracker
.
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
impl<H, KF, T, M> MemoryDB<H, KF, T, M> where
H: KeyHasher,
T: Default,
KF: KeyFunction<H>,
M: MemTracker<T>,
impl<H, KF, T, M> MemoryDB<H, KF, T, M> where
H: KeyHasher,
T: Default,
KF: KeyFunction<H>,
M: MemTracker<T>,
Create a new MemoryDB
from a given null key/data
Remove an element and delete it from storage if reference count reaches zero. If the value was purged, return the old value.
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.
impl<'a, H, KF, T, M> MemoryDB<H, KF, T, M> where
H: KeyHasher,
T: From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
impl<'a, H, KF, T, M> MemoryDB<H, KF, T, M> where
H: KeyHasher,
T: From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
Create a new MemoryDB
from a given null key/data
Create a new default instance of Self
and returns Self
and the root hash.
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));
}
Return the internal key-value HashMap, clearing the current state.
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.
Consolidate all the entries of other
into self
.
Trait Implementations
Perform upcast to HashDB for anything that derives from HashDB.
Perform mutable upcast to HashDB for anything that derives from HashDB.
Perform upcast to PlainDB for anything that derives from PlainDB.
Perform mutable upcast to PlainDB for anything that derives from PlainDB.
impl<H, KF, T, M> Clone for MemoryDB<H, KF, T, M> where
H: KeyHasher,
KF: KeyFunction<H>,
T: Clone,
M: MemTracker<T> + Copy,
impl<H, KF, T, M> Clone for MemoryDB<H, KF, T, M> where
H: KeyHasher,
KF: KeyFunction<H>,
T: Clone,
M: MemTracker<T> + Copy,
Look up a given hash into the bytes that hash to it, returning None if the hash is not known. Read more
Check for the existance of a hash-key.
Like insert()
, except you provide the key and the data is all moved.
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
impl<H, KF, T, M> MallocSizeOf for MemoryDB<H, KF, T, M> where
H: KeyHasher,
H::Out: MallocSizeOf,
T: MallocSizeOf,
KF: KeyFunction<H>,
KF::Key: MallocSizeOf,
M: MemTracker<T>,
impl<H, KF, T, M> MallocSizeOf for MemoryDB<H, KF, T, M> where
H: KeyHasher,
H::Out: MallocSizeOf,
T: MallocSizeOf,
KF: KeyFunction<H>,
KF::Key: MallocSizeOf,
M: MemTracker<T>,
Measure the heap usage of all descendant heap-allocated structures, but
not the space taken up by the value itself.
If T::size_of
is a constant, consider implementing constant_size
as well. Read more
Used to optimize MallocSizeOf
implementation for collections
like Vec
and HashMap
to avoid iterating over them unnecessarily.
The Self: Sized
bound is for object safety. Read more
impl<H, KF, T, M> PartialEq<MemoryDB<H, KF, T, M>> for MemoryDB<H, KF, T, M> where
H: KeyHasher,
KF: KeyFunction<H>,
<KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
T: Eq + MaybeDebug,
M: MemTracker<T> + PartialEq,
impl<H, KF, T, M> PartialEq<MemoryDB<H, KF, T, M>> for MemoryDB<H, KF, T, M> where
H: KeyHasher,
KF: KeyFunction<H>,
<KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
T: Eq + MaybeDebug,
M: MemTracker<T> + PartialEq,
Look up a given hash into the bytes that hash to it, returning None if the hash is not known. Read more
Insert a datum item into the DB. Insertions are counted and the equivalent
number of remove()
s must be performed before the data is considered dead.
The caller should ensure that a key only corresponds to one value. Read more
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.
The caller should ensure that a key only corresponds to one value. Read more
impl<H, KF, T, M> Eq for MemoryDB<H, KF, T, M> where
H: KeyHasher,
KF: KeyFunction<H>,
<KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
T: Eq + MaybeDebug,
M: MemTracker<T> + Eq,
Auto Trait Implementations
impl<H, KF, T, M> RefUnwindSafe for MemoryDB<H, KF, T, M> where
KF: RefUnwindSafe,
M: RefUnwindSafe,
T: RefUnwindSafe,
<KF as KeyFunction<H>>::Key: RefUnwindSafe,
<H as Hasher>::Out: RefUnwindSafe,
impl<H, KF, T, M> Unpin for MemoryDB<H, KF, T, M> where
KF: Unpin,
M: Unpin,
T: Unpin,
<KF as KeyFunction<H>>::Key: Unpin,
<H as Hasher>::Out: Unpin,
impl<H, KF, T, M> UnwindSafe for MemoryDB<H, KF, T, M> where
KF: UnwindSafe,
M: UnwindSafe,
T: UnwindSafe,
<KF as KeyFunction<H>>::Key: UnwindSafe,
<H as Hasher>::Out: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Causes self
to use its Binary
implementation when Debug
-formatted.
Causes self
to use its Display
implementation when
Debug
-formatted. Read more
Causes self
to use its LowerExp
implementation when
Debug
-formatted. Read more
Causes self
to use its LowerHex
implementation when
Debug
-formatted. Read more
Causes self
to use its Octal
implementation when Debug
-formatted.
Causes self
to use its Pointer
implementation when
Debug
-formatted. Read more
Causes self
to use its UpperExp
implementation when
Debug
-formatted. Read more
Causes self
to use its UpperHex
implementation when
Debug
-formatted. Read more
Method to launch a heapsize measurement with a fresh state. Read more
Pipes by value. This is generally the method you want to use. Read more
Borrows self
and passes that borrow into the pipe function. Read more
Mutably borrows self
and passes that borrow into the pipe function. Read more
Borrows self
, then passes self.borrow()
into the pipe function. Read more
Mutably borrows self
, then passes self.borrow_mut()
into the pipe
function. Read more
Borrows self
, then passes self.as_ref()
into the pipe function.
Mutably borrows self
, then passes self.as_mut()
into the pipe
function. Read more
Borrows self
, then passes self.deref()
into the pipe function.
fn pipe_as_ref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: AsRef<T>,
T: 'a,
R: 'a,
fn pipe_as_ref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: AsRef<T>,
T: 'a,
R: 'a,
Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more
fn pipe_borrow<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: Borrow<T>,
T: 'a,
R: 'a,
fn pipe_borrow<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: Borrow<T>,
T: 'a,
R: 'a,
Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more
fn pipe_deref<'a, R>(&'a self, func: impl FnOnce(&'a Self::Target) -> R) -> R where
Self: Deref,
R: 'a,
fn pipe_deref<'a, R>(&'a self, func: impl FnOnce(&'a Self::Target) -> R) -> R where
Self: Deref,
R: 'a,
Pipes a dereference into a function that cannot normally be called in suffix position. Read more
Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more
Immutable access to the Borrow<B>
of a value. Read more
Mutable access to the BorrowMut<B>
of a value. Read more
Immutable access to the AsRef<R>
view of a value. Read more
Mutable access to the AsMut<R>
view of a value. Read more
Immutable access to the Deref::Target
of a value. Read more
Mutable access to the Deref::Target
of a value. Read more
Calls .tap()
only in debug builds, and is erased in release builds.
Calls .tap_mut()
only in debug builds, and is erased in release
builds. Read more
Calls .tap_borrow()
only in debug builds, and is erased in release
builds. Read more
Calls .tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read more
Calls .tap_ref()
only in debug builds, and is erased in release
builds. Read more
Calls .tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more
Calls .tap_deref()
only in debug builds, and is erased in release
builds. Read more
Provides immutable access to the reference for inspection.
Calls tap_ref
in debug builds, and does nothing in release builds.
Provides mutable access to the reference for modification.
Calls tap_ref_mut
in debug builds, and does nothing in release builds.
Provides immutable access to the borrow for inspection. Read more
Calls tap_borrow
in debug builds, and does nothing in release builds.
fn tap_borrow_mut<F, R>(self, func: F) -> Self where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
fn tap_borrow_mut<F, R>(self, func: F) -> Self where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
Provides mutable access to the borrow for modification.
Immutably dereferences self
for inspection.
fn tap_deref_dbg<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
fn tap_deref_dbg<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
Calls tap_deref
in debug builds, and does nothing in release builds.
fn tap_deref_mut<F, R>(self, func: F) -> Self where
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
fn tap_deref_mut<F, R>(self, func: F) -> Self where
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
Mutably dereferences self
for modification.