Struct memorydb::MemoryDB[][src]

pub struct MemoryDB<H: KeyHasher, T> { /* 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 hashdb;
extern crate keccak_hasher;
extern crate memorydb;

use hashdb::*;
use keccak_hasher::KeccakHasher;
use memorydb::*;
fn main() {
  let mut m = MemoryDB::<KeccakHasher, Vec<u8>>::new();
  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<'a, H, T> MemoryDB<H, T> where
    H: KeyHasher,
    H::Out: HeapSizeOf,
    T: From<&'a [u8]> + Clone
[src]

Create a new instance of the memory DB.

impl<H, T> MemoryDB<H, T> where
    H: KeyHasher,
    H::Out: HeapSizeOf,
    T: Default
[src]

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

impl<H: KeyHasher, T: Clone> MemoryDB<H, T>
[src]

Create a new MemoryDB from a given null key/data

Clear all data from the database.

Examples

extern crate hashdb;
extern crate keccak_hasher;
extern crate memorydb;

use hashdb::*;
use keccak_hasher::KeccakHasher;
use memorydb::*;

fn main() {
  let mut m = MemoryDB::<KeccakHasher, Vec<u8>>::new();
  let hello_bytes = "Hello world!".as_bytes();
  let hash = m.insert(hello_bytes);
  assert!(m.contains(&hash));
  m.clear();
  assert!(!m.contains(&hash));
}

Purge all zero-referenced data from the database.

Return the internal map of hashes to data, 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.

impl<H, T> MemoryDB<H, T> where
    H: KeyHasher,
    H::Out: HeapSizeOf,
    T: HeapSizeOf
[src]

Returns the size of allocated heap memory

Trait Implementations

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<H: PartialEq + KeyHasher, T: PartialEq> PartialEq for MemoryDB<H, T> where
    H::Out: PartialEq
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a, H, T> Default for MemoryDB<H, T> where
    H: KeyHasher,
    H::Out: HeapSizeOf,
    T: From<&'a [u8]> + Clone
[src]

Returns the "default value" for a type. Read more

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

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

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

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. Read more

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

Perform upcast to HashDB for anything that derives from HashDB.

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

Auto Trait Implementations

impl<H, T> Send for MemoryDB<H, T> where
    T: Send,
    <H as Hasher>::Out: Send,
    <H as Hasher>::StdHasher: Send

impl<H, T> Sync for MemoryDB<H, T> where
    T: Sync,
    <H as Hasher>::Out: Sync,
    <H as Hasher>::StdHasher: Sync