[][src]Struct memory_db::MemoryDB

pub struct MemoryDB<H, KF, T> where
    H: KeyHasher,
    KF: KeyFunction<H>, 
{ /* fields omitted */ }

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

extern crate hash_db;
extern crate keccak_hasher;
extern crate memory_db;

use hash_db::{Hasher, HashDB};
use keccak_hasher::KeccakHasher;
use memory_db::{MemoryDB, HashKey};
fn main() {
  let mut m = MemoryDB::<KeccakHasher, HashKey<_>, Vec<u8>>::default();
  let d = "Hello world!".as_bytes();

  let k = m.insert(&[], d);
  assert!(m.contains(&k, &[]));
  assert_eq!(m.get(&k, &[]).unwrap(), d);

  m.insert(&[], d);
  assert!(m.contains(&k, &[]));

  m.remove(&k, &[]);
  assert!(m.contains(&k, &[]));

  m.remove(&k, &[]);
  assert!(!m.contains(&k, &[]));

  m.remove(&k, &[]);
  assert!(!m.contains(&k, &[]));

  m.insert(&[], d);
  assert!(!m.contains(&k, &[]));
  m.insert(&[], d);
  assert!(m.contains(&k, &[]));
  assert_eq!(m.get(&k, &[]).unwrap(), d);

  m.remove(&k, &[]);
  assert!(!m.contains(&k, &[]));
}

Methods

impl<H, KF, T> MemoryDB<H, KF, T> where
    H: KeyHasher,
    T: Default,
    KF: KeyFunction<H>, 
[src]

Create a new MemoryDB from a given null key/data

pub fn remove_and_purge(
    &mut self,
    key: &<H as KeyHasher>::Out,
    prefix: &[u8]
) -> Option<T>
[src]

Remove an element and delete it from storage if reference count reaches zero. If the value was purged, return the old value.

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

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]

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};
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(&[], hello_bytes);
  assert!(m.contains(&hash, &[]));
  m.clear();
  assert!(!m.contains(&hash, &[]));
}

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 map of hashes to data, clearing the current state.

pub fn raw(
    &self,
    key: &<H as KeyHasher>::Out,
    prefix: &[u8]
) -> Option<(&T, i32)>
[src]

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> Eq for MemoryDB<H, KF, T> where
    H: KeyHasher,
    KF: KeyFunction<H>,
    <KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
    T: Eq + MaybeDebug
[src]

impl<H, KF, T> PartialEq<MemoryDB<H, KF, T>> for MemoryDB<H, KF, T> where
    H: KeyHasher,
    KF: KeyFunction<H>,
    <KF as KeyFunction<H>>::Key: Eq + MaybeDebug,
    T: Eq + MaybeDebug
[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<H: Clone, KF: Clone, T: Clone> Clone for MemoryDB<H, KF, T> where
    H: KeyHasher,
    KF: KeyFunction<H>,
    KF::Key: Clone,
    H::Out: Clone
[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

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

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]>, 
[src]

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]>, 
[src]

impl<H, KF, T> HashDB<H, 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>, 
[src]

impl<H, KF, T> HashDBRef<H, 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>, 
[src]

impl<H, KF, T> AsHashDB<H, 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>, 
[src]

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: Send + Sync + KeyFunction<H>,
    KF::Key: Borrow<[u8]> + for<'a> From<&'a [u8]>, 
[src]

impl<H, KF, T> MallocSizeOf for MemoryDB<H, KF, T> where
    H: KeyHasher,
    H::Out: MallocSizeOf,
    T: MallocSizeOf,
    KF: KeyFunction<H>,
    KF::Key: MallocSizeOf
[src]

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> MallocSizeOfExt for T where
    T: MallocSizeOf
[src]

fn malloc_size_of(&self) -> usize[src]

Method to launch a heapsize measurement with a fresh state. Read more