pub struct MemoryDB<H: KeyHasher> { /* 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()
.
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>::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));
}
Create a new instance of the memory DB.
Clear all data from the database.
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>::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
.
Performs copy-assignment from source
. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Returns the "default value" for a type. Read more
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.
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. It can be "owed" more than once. Read more
Perform upcast to HashDB for anything that derives from HashDB.
Perform mutable upcast to HashDB for anything that derives from HashDB.