Merge pull request #5766 from guanqun/db-tests
three small commits for HashDB and MemoryDB
This commit is contained in:
commit
ed20fa4da1
@ -86,7 +86,7 @@ pub trait HashDB: AsHashDB + Send + Sync {
|
|||||||
fn emplace(&mut self, key: H256, value: DBValue);
|
fn emplace(&mut self, key: H256, value: DBValue);
|
||||||
|
|
||||||
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may
|
/// 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.
|
/// happen without the data being eventually being inserted into the DB. It can be "owed" more than once.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```rust
|
/// ```rust
|
||||||
@ -100,6 +100,10 @@ pub trait HashDB: AsHashDB + Send + Sync {
|
|||||||
/// let key = &d.sha3();
|
/// let key = &d.sha3();
|
||||||
/// m.remove(key); // OK - we now owe an insertion.
|
/// m.remove(key); // OK - we now owe an insertion.
|
||||||
/// assert!(!m.contains(key));
|
/// assert!(!m.contains(key));
|
||||||
|
/// m.remove(key); // OK - we now owe two insertions.
|
||||||
|
/// assert!(!m.contains(key));
|
||||||
|
/// m.insert(d); // OK - still owed.
|
||||||
|
/// assert!(!m.contains(key));
|
||||||
/// m.insert(d); // OK - now it's "empty" again.
|
/// m.insert(d); // OK - now it's "empty" again.
|
||||||
/// assert!(!m.contains(key));
|
/// assert!(!m.contains(key));
|
||||||
/// m.insert(d); // OK - now we've
|
/// m.insert(d); // OK - now we've
|
||||||
|
@ -246,46 +246,57 @@ impl HashDB for MemoryDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn memorydb_remove_and_purge() {
|
mod tests {
|
||||||
let hello_bytes = b"Hello world!";
|
use super::*;
|
||||||
let hello_key = hello_bytes.sha3();
|
|
||||||
|
|
||||||
let mut m = MemoryDB::new();
|
#[test]
|
||||||
m.remove(&hello_key);
|
fn memorydb_remove_and_purge() {
|
||||||
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
let hello_bytes = b"Hello world!";
|
||||||
m.purge();
|
let hello_key = hello_bytes.sha3();
|
||||||
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
|
||||||
m.insert(hello_bytes);
|
|
||||||
assert_eq!(m.raw(&hello_key).unwrap().1, 0);
|
|
||||||
m.purge();
|
|
||||||
assert_eq!(m.raw(&hello_key), None);
|
|
||||||
|
|
||||||
let mut m = MemoryDB::new();
|
let mut m = MemoryDB::new();
|
||||||
assert!(m.remove_and_purge(&hello_key).is_none());
|
m.remove(&hello_key);
|
||||||
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
||||||
m.insert(hello_bytes);
|
m.purge();
|
||||||
m.insert(hello_bytes);
|
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
||||||
assert_eq!(m.raw(&hello_key).unwrap().1, 1);
|
m.insert(hello_bytes);
|
||||||
assert_eq!(&*m.remove_and_purge(&hello_key).unwrap(), hello_bytes);
|
assert_eq!(m.raw(&hello_key).unwrap().1, 0);
|
||||||
assert_eq!(m.raw(&hello_key), None);
|
m.purge();
|
||||||
assert!(m.remove_and_purge(&hello_key).is_none());
|
assert_eq!(m.raw(&hello_key), None);
|
||||||
}
|
|
||||||
|
let mut m = MemoryDB::new();
|
||||||
#[test]
|
assert!(m.remove_and_purge(&hello_key).is_none());
|
||||||
fn consolidate() {
|
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
||||||
let mut main = MemoryDB::new();
|
m.insert(hello_bytes);
|
||||||
let mut other = MemoryDB::new();
|
m.insert(hello_bytes);
|
||||||
let remove_key = other.insert(b"doggo");
|
assert_eq!(m.raw(&hello_key).unwrap().1, 1);
|
||||||
main.remove(&remove_key);
|
assert_eq!(&*m.remove_and_purge(&hello_key).unwrap(), hello_bytes);
|
||||||
|
assert_eq!(m.raw(&hello_key), None);
|
||||||
let insert_key = other.insert(b"arf");
|
assert!(m.remove_and_purge(&hello_key).is_none());
|
||||||
main.emplace(insert_key, DBValue::from_slice(b"arf"));
|
}
|
||||||
|
|
||||||
main.consolidate(other);
|
#[test]
|
||||||
|
fn consolidate() {
|
||||||
let overlay = main.drain();
|
let mut main = MemoryDB::new();
|
||||||
|
let mut other = MemoryDB::new();
|
||||||
assert_eq!(overlay.get(&remove_key).unwrap(), &(DBValue::from_slice(b"doggo"), 0));
|
let remove_key = other.insert(b"doggo");
|
||||||
assert_eq!(overlay.get(&insert_key).unwrap(), &(DBValue::from_slice(b"arf"), 2));
|
main.remove(&remove_key);
|
||||||
|
|
||||||
|
let insert_key = other.insert(b"arf");
|
||||||
|
main.emplace(insert_key, DBValue::from_slice(b"arf"));
|
||||||
|
|
||||||
|
let negative_remove_key = other.insert(b"negative");
|
||||||
|
other.remove(&negative_remove_key); // ref cnt: 0
|
||||||
|
other.remove(&negative_remove_key); // ref cnt: -1
|
||||||
|
main.remove(&negative_remove_key); // ref cnt: -1
|
||||||
|
|
||||||
|
main.consolidate(other);
|
||||||
|
|
||||||
|
let overlay = main.drain();
|
||||||
|
|
||||||
|
assert_eq!(overlay.get(&remove_key).unwrap(), &(DBValue::from_slice(b"doggo"), 0));
|
||||||
|
assert_eq!(overlay.get(&insert_key).unwrap(), &(DBValue::from_slice(b"arf"), 2));
|
||||||
|
assert_eq!(overlay.get(&negative_remove_key).unwrap(), &(DBValue::from_slice(b"negative"), -2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user