Struct memory_db::MemoryDB

source ·
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>where H: KeyHasher, T: Default, KF: KeyFunction<H>,

Create a new MemoryDB from a given null key/data

source

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.

source

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>where H: KeyHasher, T: for<'a> From<&'a [u8]>, KF: KeyFunction<H>,

source

pub fn from_null_node(null_key: &[u8], null_node_data: T) -> Self

Create a new MemoryDB from a given null key/data

source

pub fn new(data: &[u8]) -> Self

Create a new instance of Self.

source

pub fn default_with_root() -> (Self, H::Out)

Create a new default instance of Self and returns Self and the root hash.

source

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));
}
source

pub fn purge(&mut self)

Purge all zero-referenced data from the database.

source

pub fn drain(&mut self) -> Map<KF::Key, (T, i32)>

Return the internal key-value Map, clearing the current state.

source

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.

source

pub fn consolidate(&mut self, other: Self)

Consolidate all the entries of other into self.

source

pub fn keys(&self) -> Map<KF::Key, i32>

Get the keys in the database together with number of underlying references.

Trait Implementations§

source§

impl<H, KF, T> AsHashDB<H, T> for MemoryDB<H, KF, T>where H: KeyHasher, T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction<H> + Send + Sync,

source§

fn as_hash_db(&self) -> &dyn HashDB<H, T>

Perform upcast to HashDB for anything that derives from HashDB.
source§

fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<H, T>

Perform mutable upcast to HashDB for anything that derives from HashDB.
source§

impl<H, KF, T> AsPlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>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]>,

source§

fn as_plain_db(&self) -> &dyn PlainDB<H::Out, T>

Perform upcast to PlainDB for anything that derives from PlainDB.
source§

fn as_plain_db_mut(&mut self) -> &mut dyn PlainDB<H::Out, T>

Perform mutable upcast to PlainDB for anything that derives from PlainDB.
source§

impl<H, KF, T> Clone for MemoryDB<H, KF, T>where H: KeyHasher, KF: KeyFunction<H>, T: Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<H, KF, T> Default for MemoryDB<H, KF, T>where H: KeyHasher, T: for<'a> From<&'a [u8]>, KF: KeyFunction<H>,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<H, KF, T> HashDB<H, T> for MemoryDB<H, KF, T>where H: KeyHasher, T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction<H> + Send + Sync,

source§

fn get(&self, key: &H::Out, prefix: Prefix<'_>) -> Option<T>

Look up a given hash into the bytes that hash to it, returning None if the hash is not known.
source§

fn contains(&self, key: &H::Out, prefix: Prefix<'_>) -> bool

Check for the existence of a hash-key.
source§

fn emplace(&mut self, key: H::Out, prefix: Prefix<'_>, value: T)

Like insert(), except you provide the key and the data is all moved.
source§

fn insert(&mut self, prefix: Prefix<'_>, value: &[u8]) -> H::Out

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.
source§

fn remove(&mut self, key: &H::Out, prefix: Prefix<'_>)

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.
source§

impl<H, KF, T> HashDBRef<H, T> for MemoryDB<H, KF, T>where H: KeyHasher, T: Default + PartialEq<T> + AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Send + Sync, KF: KeyFunction<H> + Send + Sync,

source§

fn get(&self, key: &H::Out, prefix: Prefix<'_>) -> Option<T>

Look up a given hash into the bytes that hash to it, returning None if the hash is not known.
source§

fn contains(&self, key: &H::Out, prefix: Prefix<'_>) -> bool

Check for the existance of a hash-key.
source§

impl<H, KF, T> PartialEq<MemoryDB<H, KF, T>> for MemoryDB<H, KF, T>where H: KeyHasher, KF: KeyFunction<H>, T: Eq + MaybeDebug,

source§

fn eq(&self, other: &MemoryDB<H, KF, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<H, KF, T> PlainDB<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>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]>,

source§

fn get(&self, key: &H::Out) -> Option<T>

Look up a given hash into the bytes that hash to it, returning None if the hash is not known.
source§

fn contains(&self, key: &H::Out) -> bool

Check for the existence of a hash-key.
source§

fn emplace(&mut self, key: H::Out, value: T)

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.
source§

fn remove(&mut self, key: &H::Out)

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.
source§

impl<H, KF, T> PlainDBRef<<H as Hasher>::Out, T> for MemoryDB<H, KF, T>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]>,

source§

fn get(&self, key: &H::Out) -> Option<T>

Look up a given hash into the bytes that hash to it, returning None if the hash is not known.
source§

fn contains(&self, key: &H::Out) -> bool

Check for the existance of a hash-key.
source§

impl<H, KF, T> Eq for MemoryDB<H, KF, T>where H: KeyHasher, KF: KeyFunction<H>, T: Eq + MaybeDebug,

Auto Trait Implementations§

§

impl<H, KF, T> RefUnwindSafe for MemoryDB<H, KF, T>where KF: RefUnwindSafe, T: RefUnwindSafe, <KF as KeyFunction<H>>::Key: RefUnwindSafe, <H as Hasher>::Out: RefUnwindSafe,

§

impl<H, KF, T> Send for MemoryDB<H, KF, T>where KF: Send, T: Send,

§

impl<H, KF, T> Sync for MemoryDB<H, KF, T>where KF: Sync, T: Sync,

§

impl<H, KF, T> Unpin for MemoryDB<H, KF, T>where KF: Unpin, T: Unpin, <KF as KeyFunction<H>>::Key: Unpin, <H as Hasher>::Out: Unpin,

§

impl<H, KF, T> UnwindSafe for MemoryDB<H, KF, T>where KF: UnwindSafe, T: UnwindSafe, <KF as KeyFunction<H>>::Key: UnwindSafe, <H as Hasher>::Out: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.