# 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]

#### `fn new() -> MemoryDB`

Create a new instance of the memory DB.

#### `fn clear(&mut self)`

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

#### `fn purge(&mut self)`

Purge all zero-referenced data from the database.

#### `fn drain(&mut self) -> H256FastMap<(DBValue, i32)>`

Return the internal map of hashes to data, clearing the current state.

#### `fn raw(&self, key: &H256) -> Option<(DBValue, 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.

#### `fn mem_used(&self) -> usize`

Returns the size of allocated heap memory

#### `fn remove_and_purge(&mut self, key: &H256) -> Option<DBValue>`

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

#### `fn consolidate(&mut self, other: Self)`

Consolidate all the entries of `other` into `self`.

## Trait Implementations

### `impl Default for MemoryDB`
[src]

#### `fn default() -> MemoryDB`

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

### `impl Clone for MemoryDB`
[src]

#### `fn clone(&self) -> MemoryDB`

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`
1.0.0

Performs copy-assignment from `source`. Read more

### `impl PartialEq for MemoryDB`
[src]

#### `fn eq(&self, __arg_0: &MemoryDB) -> bool`

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

#### `fn ne(&self, __arg_0: &MemoryDB) -> bool`

This method tests for `!=`.

### `impl HashDB for MemoryDB`
[src]

#### `fn get(&self, key: &H256) -> Option<DBValue>`

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

#### `fn keys(&self) -> HashMap<H256, i32>`

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

#### `fn contains(&self, key: &H256) -> bool`

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

#### `fn insert(&mut self, value: &[u8]) -> H256`

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

#### `fn emplace(&mut self, key: H256, value: DBValue)`

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

#### `fn remove(&mut self, key: &H256)`

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

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)