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()
.
extern crate hash_db;
extern crate keccak_hasher;
extern crate memory_db;
use hash_db::*;
use keccak_hasher::KeccakHasher;
use memory_db::*;
fn main() {
let mut m = MemoryDB::<KeccakHasher, Vec<u8>>::default();
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));
}
Remove an element and delete it from storage if reference count reaches zero.
If the value was purged, return the old value.
Create a new MemoryDB
from a given null key/data
Create a new MemoryDB
from a given null key/data
Clear all data from the database.
extern crate hash_db;
extern crate keccak_hasher;
extern crate memory_db;
use hash_db::*;
use keccak_hasher::KeccakHasher;
use memory_db::*;
fn main() {
let mut m = MemoryDB::<KeccakHasher, Vec<u8>>::default();
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
.
Returns the size of allocated heap memory
Returns the "default value" for a type. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. 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.
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
Perform upcast to HashDB for anything that derives from HashDB.
Perform mutable upcast to HashDB for anything that derives from HashDB.
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static