[−][src]Struct memory_db::MemoryDB
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>,
[src]
H: KeyHasher,
T: Default,
KF: KeyFunction<H>,
M: MemTracker<T>,
Create a new MemoryDB
from a given null key/data
pub fn remove_and_purge(
&mut self,
key: &<H as KeyHasher>::Out,
prefix: Prefix
) -> Option<T>
[src]
&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.
pub fn shrink_to_fit(&mut self)
[src]
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,
[src]
H: KeyHasher,
T: From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
pub fn from_null_node(null_key: &'a [u8], null_node_data: T) -> Self
[src]
Create a new MemoryDB
from a given null key/data
pub fn new(data: &'a [u8]) -> Self
[src]
Create a new instance of Self
.
pub fn default_with_root() -> (Self, H::Out)
[src]
Create a new default instance of Self
and returns Self
and the root hash.
pub fn clear(&mut self)
[src]
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)); }
pub fn purge(&mut self)
[src]
Purge all zero-referenced data from the database.
pub fn drain(&mut self) -> HashMap<KF::Key, (T, i32)>
[src]
Return the internal key-value HashMap, clearing the current state.
pub fn raw(
&self,
key: &<H as KeyHasher>::Out,
prefix: Prefix
) -> Option<(&T, i32)>
[src]
&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.
pub fn consolidate(&mut self, other: Self)
[src]
Consolidate all the entries of other
into self
.
pub fn keys(&self) -> HashMap<KF::Key, i32>
[src]
Get the keys in the database together with number of underlying references.
Trait Implementations
impl<H, KF, T, M> AsHashDB<H, 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,
M: MemTracker<T> + Send + Sync,
[src]
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
fn as_hash_db(&self) -> &dyn HashDB<H, T>
[src]
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<H, T>
[src]
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,
[src]
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,
fn as_plain_db(&self) -> &dyn PlainDB<H::Out, T>
[src]
fn as_plain_db_mut(&mut self) -> &mut dyn PlainDB<H::Out, T>
[src]
impl<H, KF, T, M> Clone for MemoryDB<H, KF, T, M> where
H: KeyHasher,
KF: KeyFunction<H>,
T: Clone,
M: MemTracker<T> + Copy,
[src]
H: KeyHasher,
KF: KeyFunction<H>,
T: Clone,
M: MemTracker<T> + Copy,
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<'a, H, KF, T, M> Default for MemoryDB<H, KF, T, M> where
H: KeyHasher,
T: From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
[src]
H: KeyHasher,
T: From<&'a [u8]>,
KF: KeyFunction<H>,
M: MemTracker<T> + Default,
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,
[src]
H: KeyHasher,
KF: KeyFunction<H>,
<KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
T: Eq + MaybeDebug,
M: MemTracker<T> + Eq,
impl<H, KF, T, M> HashDB<H, 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>,
M: MemTracker<T> + Send + Sync,
[src]
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: Send + Sync + KeyFunction<H>,
M: MemTracker<T> + Send + Sync,
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>
[src]
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool
[src]
fn emplace(&mut self, key: H::Out, prefix: Prefix, value: T)
[src]
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H::Out
[src]
fn remove(&mut self, key: &H::Out, prefix: Prefix)
[src]
impl<H, KF, T, M> HashDBRef<H, 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,
M: MemTracker<T> + Send + Sync,
[src]
H: KeyHasher,
T: Default + PartialEq<T> + for<'a> From<&'a [u8]> + Clone + Send + Sync,
KF: KeyFunction<H> + Send + Sync,
M: MemTracker<T> + Send + Sync,
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>
[src]
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool
[src]
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>,
[src]
H: KeyHasher,
H::Out: MallocSizeOf,
T: MallocSizeOf,
KF: KeyFunction<H>,
KF::Key: MallocSizeOf,
M: MemTracker<T>,
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
[src]
fn constant_size() -> Option<usize>
[src]
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,
[src]
H: KeyHasher,
KF: KeyFunction<H>,
<KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
T: Eq + MaybeDebug,
M: MemTracker<T> + PartialEq,
fn eq(&self, other: &MemoryDB<H, KF, T, M>) -> bool
[src]
#[must_use]fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
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,
[src]
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,
fn get(&self, key: &H::Out) -> Option<T>
[src]
fn contains(&self, key: &H::Out) -> bool
[src]
fn emplace(&mut self, key: H::Out, value: T)
[src]
fn remove(&mut self, key: &H::Out)
[src]
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,
[src]
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,
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,
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,
<KF as KeyFunction<H>>::Key: Send,
<H as Hasher>::Out: Send,
KF: Send,
M: Send,
T: Send,
<KF as KeyFunction<H>>::Key: Send,
<H as Hasher>::Out: Send,
impl<H, KF, T, M> Sync for MemoryDB<H, KF, T, M> where
KF: Sync,
M: Sync,
T: Sync,
<KF as KeyFunction<H>>::Key: Sync,
<H as Hasher>::Out: Sync,
KF: Sync,
M: Sync,
T: Sync,
<KF as KeyFunction<H>>::Key: Sync,
<H as Hasher>::Out: 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,
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,
KF: UnwindSafe,
M: UnwindSafe,
T: UnwindSafe,
<KF as KeyFunction<H>>::Key: UnwindSafe,
<H as Hasher>::Out: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> MallocSizeOfExt for T where
T: MallocSizeOf,
[src]
T: MallocSizeOf,
fn malloc_size_of(&self) -> usize
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,