pub struct MemoryDB<H, KF, T, M = DefaultMemTracker<T>>where
H: KeyHasher,
KF: KeyFunction<H>,
M: MemTracker<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()
.
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§
source§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
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, M> MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: for<'a> From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
impl<H, KF, T, M> MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: for<'a> From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
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) -> HashMap<KF::Key, (T, i32)>
pub fn drain(&mut self) -> HashMap<KF::Key, (T, i32)>
Return the internal key-value HashMap, 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, M> AsHashDB<H, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
impl<H, KF, T, M> AsHashDB<H, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
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, M> AsPlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>,
M: MemTracker<T> + Send + Sync,
impl<H, KF, T, M> AsPlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>,
M: MemTracker<T> + Send + Sync,
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, 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,
source§impl<H, KF, T, M> Default for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: for<'a> From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
impl<H, KF, T, M> Default for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: for<'a> From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
source§impl<H, KF, T, M> HashDB<H, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
impl<H, KF, T, M> HashDB<H, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
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, M> HashDBRef<H, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
impl<H, KF, T, M> HashDBRef<H, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
source§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>,
source§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,
source§impl<H, KF, T, M> PlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: Send + Sync + KeyFunction<H>,
KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>,
M: MemTracker<T> + Send + Sync,
impl<H, KF, T, M> PlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: Send + Sync + KeyFunction<H>,
KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>,
M: MemTracker<T> + Send + Sync,
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. Read moresource§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. Read moresource§impl<H, KF, T, M> PlainDBRef<<H as Hasher>::Out, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: Send + Sync + KeyFunction<H>,
KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>,
M: MemTracker<T> + Send + Sync,
impl<H, KF, T, M> PlainDBRef<<H as Hasher>::Out, T> for MemoryDB<H, KF, T, M>where
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: Send + Sync + KeyFunction<H>,
KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>,
M: MemTracker<T> + Send + Sync,
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> Send for MemoryDB<H, KF, T, M>where
KF: Send,
M: Send,
T: Send,
impl<H, KF, T, M> Sync for MemoryDB<H, KF, T, M>where
KF: Sync,
M: Sync,
T: Sync,
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§
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
source§impl<T> MallocSizeOfExt for Twhere
T: MallocSizeOf,
impl<T> MallocSizeOfExt for Twhere
T: MallocSizeOf,
source§fn malloc_size_of(&self) -> usize
fn malloc_size_of(&self) -> usize
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more