Struct ethcore_util::memorydb::MemoryDB [] [src]

pub struct MemoryDB { /* 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 containce() 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 ethcore_util;
use ethcore_util::hashdb::*;
use ethcore_util::memorydb::*;
fn main() {
  let mut m = MemoryDB::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 MemoryDB
[src]

Create a new instance of the memory DB.

Clear all data from the database.

Examples

extern crate ethcore_util;
use ethcore_util::hashdb::*;
use ethcore_util::memorydb::*;
fn main() {
  let mut m = MemoryDB::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.

Returns the size of allocated heap memory

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

Consolidate all the entries of other into self.

Trait Implementations

impl Default for MemoryDB
[src]

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

impl Clone for MemoryDB
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for MemoryDB
[src]

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

This method tests for !=.

impl HashDB for MemoryDB
[src]

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

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

Check for the existance of a hash-key. Read more

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

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

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