MemoryDB

Struct MemoryDB 

Source
pub struct MemoryDB<H: KeyHasher, T> { /* 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

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

Implementations§

Source§

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

Source

pub fn new() -> Self

Create a new instance of the memory DB.

Source§

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

Source

pub fn remove_and_purge(&mut self, key: &<H as KeyHasher>::Out) -> 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§

impl<H: KeyHasher, T: Clone> MemoryDB<H, T>

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 clear(&mut self)

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

pub fn purge(&mut self)

Purge all zero-referenced data from the database.

Source

pub fn drain( &mut self, ) -> HashMap<<H as KeyHasher>::Out, (T, i32), BuildHasherDefault<<H as KeyHasher>::StdHasher>>

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

Source

pub fn raw(&self, key: &<H as KeyHasher>::Out) -> 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§

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

Source

pub fn mem_used(&self) -> usize

Returns the size of allocated heap memory

Trait Implementations§

Source§

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,

Source§

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

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

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

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

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

Source§

fn clone(&self) -> MemoryDB<H, T>

Returns a duplicate 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<'a, H, T> Default for MemoryDB<H, T>
where H: KeyHasher, H::Out: HeapSizeOf, T: From<&'a [u8]> + Clone,

Source§

fn default() -> Self

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

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,

Source§

fn keys(&self) -> HashMap<H::Out, i32>

Get the keys in the database together with number of underlying references.
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§

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

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

fn insert(&mut self, 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)

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: PartialEq + KeyHasher, T: PartialEq> PartialEq for MemoryDB<H, T>
where H::Out: PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<H: KeyHasher, T> StructuralPartialEq for MemoryDB<H, T>

Auto Trait Implementations§

§

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

§

impl<H, T> RefUnwindSafe for MemoryDB<H, T>

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.