2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
//! Disk-backed `HashDB` implementation.
|
2015-11-28 03:09:36 +01:00
|
|
|
|
2017-08-26 19:09:32 +02:00
|
|
|
use std::{
|
|
|
|
collections::{hash_map::Entry, HashMap},
|
2018-07-06 15:09:39 +02:00
|
|
|
io,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
use super::error_negatively_reference_hash;
|
2021-03-03 22:44:35 +01:00
|
|
|
use ethcore_db::{DBTransaction, DBValue, KeyValueDB};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2019-02-20 19:09:34 +01:00
|
|
|
use hash_db::HashDB;
|
2018-07-02 18:50:05 +02:00
|
|
|
use keccak_hasher::KeccakHasher;
|
2019-02-20 19:09:34 +01:00
|
|
|
use memory_db::*;
|
2018-10-09 22:07:25 +02:00
|
|
|
use rlp::{decode, encode, Decodable, DecoderError, Encodable, Rlp, RlpStream};
|
2015-11-28 03:09:36 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay.
|
2015-11-28 23:49:07 +01:00
|
|
|
///
|
2016-01-18 12:41:31 +01:00
|
|
|
/// The operations `insert()` and `remove()` take place on the memory overlay; batches of
|
2015-11-28 23:49:07 +01:00
|
|
|
/// such operations may be flushed to the disk-backed DB with `commit()` or discarded with
|
|
|
|
/// `revert()`.
|
|
|
|
///
|
2016-03-30 07:36:35 +02:00
|
|
|
/// `lookup()` and `contains()` maintain normal behaviour - all `insert()` and `remove()`
|
2015-11-28 23:49:07 +01:00
|
|
|
/// queries have an immediate effect in terms of these functions.
|
2016-03-13 18:07:36 +01:00
|
|
|
#[derive(Clone)]
|
2015-11-28 03:09:36 +01:00
|
|
|
pub struct OverlayDB {
|
2018-10-09 22:07:25 +02:00
|
|
|
overlay: MemoryDB<KeccakHasher, DBValue>,
|
2020-07-29 10:36:15 +02:00
|
|
|
backing: Arc<dyn KeyValueDB>,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: Option<u32>,
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
struct Payload {
|
|
|
|
count: u32,
|
|
|
|
value: DBValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Payload {
|
|
|
|
fn new(count: u32, value: DBValue) -> Self {
|
|
|
|
Payload { count, value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for Payload {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(2);
|
|
|
|
s.append(&self.count);
|
|
|
|
s.append(&&*self.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for Payload {
|
2018-07-06 15:09:39 +02:00
|
|
|
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
|
2018-03-15 11:14:38 +01:00
|
|
|
let payload = Payload {
|
|
|
|
count: rlp.val_at(0)?,
|
|
|
|
value: DBValue::from_slice(rlp.at(1)?.data()?),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
Ok(payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-28 03:09:36 +01:00
|
|
|
impl OverlayDB {
|
|
|
|
/// Create a new instance of OverlayDB given a `backing` database.
|
2020-07-29 10:36:15 +02:00
|
|
|
pub fn new(backing: Arc<dyn KeyValueDB>, col: Option<u32>) -> OverlayDB {
|
2019-02-20 19:09:34 +01:00
|
|
|
OverlayDB {
|
|
|
|
overlay: ::new_memory_db(),
|
|
|
|
backing: backing,
|
|
|
|
column: col,
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2015-11-28 22:54:12 +01:00
|
|
|
/// Create a new instance of OverlayDB with an anonymous temporary database.
|
2016-07-28 23:46:24 +02:00
|
|
|
#[cfg(test)]
|
2015-11-28 22:54:12 +01:00
|
|
|
pub fn new_temp() -> OverlayDB {
|
2021-03-03 22:44:35 +01:00
|
|
|
let backing = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));
|
2017-02-20 17:21:55 +01:00
|
|
|
Self::new(backing, None)
|
2016-07-28 23:46:24 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
/// Commit all operations in a single batch.
|
|
|
|
#[cfg(test)]
|
2018-07-06 15:09:39 +02:00
|
|
|
pub fn commit(&mut self) -> io::Result<u32> {
|
2016-08-25 16:43:56 +02:00
|
|
|
let mut batch = self.backing.transaction();
|
2016-12-27 12:53:56 +01:00
|
|
|
let res = self.commit_to_batch(&mut batch)?;
|
2016-07-28 23:46:24 +02:00
|
|
|
self.backing.write(batch).map(|_| res).map_err(|e| e.into())
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-13 18:07:36 +01:00
|
|
|
/// Commit all operations to given batch.
|
2018-07-06 15:09:39 +02:00
|
|
|
pub fn commit_to_batch(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
|
2016-03-13 18:07:36 +01:00
|
|
|
let mut ret = 0u32;
|
|
|
|
let mut deletes = 0usize;
|
2016-10-27 08:28:12 +02:00
|
|
|
for i in self.overlay.drain() {
|
2016-03-13 18:07:36 +01:00
|
|
|
let (key, (value, rc)) = i;
|
|
|
|
if rc != 0 {
|
|
|
|
match self.payload(&key) {
|
|
|
|
Some(x) => {
|
2018-03-15 11:14:38 +01:00
|
|
|
let total_rc: i32 = x.count as i32 + rc;
|
2016-03-13 18:07:36 +01:00
|
|
|
if total_rc < 0 {
|
2018-07-06 15:09:39 +02:00
|
|
|
return Err(error_negatively_reference_hash(&key));
|
2016-03-13 18:07:36 +01:00
|
|
|
}
|
2018-03-15 11:14:38 +01:00
|
|
|
let payload = Payload::new(total_rc as u32, x.value);
|
|
|
|
deletes += if self.put_payload_in_batch(batch, &key, &payload) {
|
2020-08-05 06:08:03 +02:00
|
|
|
1
|
2018-03-15 11:14:38 +01:00
|
|
|
} else {
|
2020-08-05 06:08:03 +02:00
|
|
|
0
|
2018-03-15 11:14:38 +01:00
|
|
|
};
|
2016-03-13 18:07:36 +01:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if rc < 0 {
|
2018-07-06 15:09:39 +02:00
|
|
|
return Err(error_negatively_reference_hash(&key));
|
2016-03-13 18:07:36 +01:00
|
|
|
}
|
2018-03-15 11:14:38 +01:00
|
|
|
let payload = Payload::new(rc as u32, value);
|
|
|
|
self.put_payload_in_batch(batch, &key, &payload);
|
2016-03-13 18:07:36 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
ret += 1;
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-03-13 18:07:36 +01:00
|
|
|
}
|
|
|
|
trace!("OverlayDB::commit() deleted {} nodes", deletes);
|
|
|
|
Ok(ret)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-30 07:36:35 +02:00
|
|
|
/// Revert all operations on this object (i.e. `insert()`s and `remove()`s) since the
|
2015-11-28 23:49:07 +01:00
|
|
|
/// last `commit()`.
|
2015-11-28 22:54:12 +01:00
|
|
|
pub fn revert(&mut self) {
|
|
|
|
self.overlay.clear();
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-13 21:21:30 +01:00
|
|
|
/// Get the number of references that would be committed.
|
2016-08-03 18:35:48 +02:00
|
|
|
pub fn commit_refs(&self, key: &H256) -> i32 {
|
|
|
|
self.overlay.raw(key).map_or(0, |(_, refs)| refs)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2015-11-28 03:09:36 +01:00
|
|
|
/// Get the refs and value of the given key.
|
2018-03-15 11:14:38 +01:00
|
|
|
fn payload(&self, key: &H256) -> Option<Payload> {
|
2016-07-28 23:46:24 +02:00
|
|
|
self.backing
|
2021-03-12 10:12:42 +01:00
|
|
|
.get(self.column, key.as_bytes())
|
2015-11-28 19:03:50 +01:00
|
|
|
.expect("Low-level database error. Some issue with your hard disk?")
|
2018-10-09 22:07:25 +02:00
|
|
|
.map(|ref d| decode(d).expect("decoding db value failed"))
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-04 20:19:36 +01:00
|
|
|
/// Put the refs and value of the given key, possibly deleting it from the db.
|
2018-03-15 11:14:38 +01:00
|
|
|
fn put_payload_in_batch(
|
|
|
|
&self,
|
|
|
|
batch: &mut DBTransaction,
|
|
|
|
key: &H256,
|
|
|
|
payload: &Payload,
|
|
|
|
) -> bool {
|
|
|
|
if payload.count > 0 {
|
2021-03-12 10:12:42 +01:00
|
|
|
batch.put(self.column, key.as_bytes(), &encode(payload));
|
2016-01-18 14:44:06 +01:00
|
|
|
false
|
2016-01-18 13:54:46 +01:00
|
|
|
} else {
|
2021-03-12 10:12:42 +01:00
|
|
|
batch.delete(self.column, key.as_bytes());
|
2016-03-13 19:22:42 +01:00
|
|
|
true
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 19:09:34 +01:00
|
|
|
impl crate::KeyedHashDB for OverlayDB {
|
2015-12-04 18:05:59 +01:00
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
2017-08-26 19:09:32 +02:00
|
|
|
let mut ret: HashMap<H256, i32> = self
|
|
|
|
.backing
|
|
|
|
.iter(self.column)
|
|
|
|
.map(|(key, _)| {
|
|
|
|
let h = H256::from_slice(&*key);
|
2018-03-15 11:14:38 +01:00
|
|
|
let r = self.payload(&h).unwrap().count;
|
2017-08-26 19:09:32 +02:00
|
|
|
(h, r as i32)
|
|
|
|
})
|
|
|
|
.collect();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-27 08:28:12 +02:00
|
|
|
for (key, refs) in self.overlay.keys() {
|
2017-08-26 19:09:32 +02:00
|
|
|
match ret.entry(key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
*entry.get_mut() += refs;
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
2017-08-26 19:16:08 +02:00
|
|
|
entry.insert(refs);
|
2017-08-26 19:09:32 +02:00
|
|
|
}
|
2015-12-03 14:56:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2015-12-03 14:56:39 +01:00
|
|
|
ret
|
|
|
|
}
|
2019-02-20 19:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HashDB<KeccakHasher, DBValue> for OverlayDB {
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2015-11-28 18:29:50 +01:00
|
|
|
// return ok if positive; if negative, check backing - might be enough references there to make
|
2015-11-28 03:09:36 +01:00
|
|
|
// it positive again.
|
2015-11-28 18:29:50 +01:00
|
|
|
let k = self.overlay.raw(key);
|
2016-10-26 13:53:47 +02:00
|
|
|
let memrc = {
|
|
|
|
if let Some((d, rc)) = k {
|
2019-02-20 19:09:34 +01:00
|
|
|
if rc > 0 {
|
|
|
|
return Some(d.clone());
|
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
rc
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match self.payload(key) {
|
|
|
|
Some(x) => {
|
2018-03-15 11:14:38 +01:00
|
|
|
if x.count as i32 + memrc > 0 {
|
|
|
|
Some(x.value)
|
2016-10-26 13:53:47 +02:00
|
|
|
} else {
|
|
|
|
None
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
// Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done.
|
|
|
|
//Some((d, rc)) if rc + memrc > 0 => Some(d),
|
|
|
|
_ => None,
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool {
|
2015-11-28 18:29:50 +01:00
|
|
|
// return ok if positive; if negative, check backing - might be enough references there to make
|
|
|
|
// it positive again.
|
|
|
|
let k = self.overlay.raw(key);
|
|
|
|
match k {
|
2016-08-03 18:35:48 +02:00
|
|
|
Some((_, rc)) if rc > 0 => true,
|
2015-11-28 18:29:50 +01:00
|
|
|
_ => {
|
2016-08-03 18:35:48 +02:00
|
|
|
let memrc = k.map_or(0, |(_, rc)| rc);
|
2015-11-28 18:29:50 +01:00
|
|
|
match self.payload(key) {
|
2018-03-15 11:14:38 +01:00
|
|
|
Some(x) => x.count as i32 + memrc > 0,
|
2015-11-28 18:29:50 +01:00
|
|
|
// Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done.
|
|
|
|
//Some((d, rc)) if rc + memrc > 0 => true,
|
|
|
|
_ => false,
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-28 18:29:50 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2015-11-28 18:29:50 +01:00
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
|
|
|
self.overlay.insert(value)
|
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, key: H256, value: DBValue) {
|
|
|
|
self.overlay.emplace(key, value);
|
|
|
|
}
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, key: &H256) {
|
|
|
|
self.overlay.remove(key);
|
|
|
|
}
|
2015-11-28 03:09:36 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
#[test]
|
|
|
|
fn overlaydb_revert() {
|
|
|
|
let mut m = OverlayDB::new_temp();
|
|
|
|
let foo = m.insert(b"foo"); // insert foo.
|
2016-08-25 16:43:56 +02:00
|
|
|
let mut batch = m.backing.transaction();
|
|
|
|
m.commit_to_batch(&mut batch).unwrap(); // commit - new operations begin here...
|
2016-07-28 23:46:24 +02:00
|
|
|
m.backing.write(batch).unwrap();
|
|
|
|
let bar = m.insert(b"bar"); // insert bar.
|
|
|
|
m.remove(&foo); // remove foo.
|
|
|
|
assert!(!m.contains(&foo)); // foo is gone.
|
|
|
|
assert!(m.contains(&bar)); // bar is here.
|
|
|
|
m.revert(); // revert the last two operations.
|
|
|
|
assert!(m.contains(&foo)); // foo is here.
|
|
|
|
assert!(!m.contains(&bar)); // bar is gone.
|
|
|
|
}
|
|
|
|
|
2015-11-28 22:54:12 +01:00
|
|
|
#[test]
|
2016-03-30 07:36:35 +02:00
|
|
|
fn overlaydb_overlay_insert_and_remove() {
|
2015-11-28 22:54:12 +01:00
|
|
|
let mut trie = OverlayDB::new_temp();
|
|
|
|
let h = trie.insert(b"hello world");
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&h).unwrap(), DBValue::from_slice(b"hello world"));
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&h);
|
|
|
|
assert_eq!(trie.get(&h), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlaydb_backing_insert_revert() {
|
|
|
|
let mut trie = OverlayDB::new_temp();
|
|
|
|
let h = trie.insert(b"hello world");
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&h).unwrap(), DBValue::from_slice(b"hello world"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.commit().unwrap();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&h).unwrap(), DBValue::from_slice(b"hello world"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.revert();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&h).unwrap(), DBValue::from_slice(b"hello world"));
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-03-30 07:36:35 +02:00
|
|
|
fn overlaydb_backing_remove() {
|
2015-11-28 22:54:12 +01:00
|
|
|
let mut trie = OverlayDB::new_temp();
|
|
|
|
let h = trie.insert(b"hello world");
|
|
|
|
trie.commit().unwrap();
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&h);
|
|
|
|
assert_eq!(trie.get(&h), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.commit().unwrap();
|
2016-03-30 07:36:35 +02:00
|
|
|
assert_eq!(trie.get(&h), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.revert();
|
2016-03-30 07:36:35 +02:00
|
|
|
assert_eq!(trie.get(&h), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-03-30 07:36:35 +02:00
|
|
|
fn overlaydb_backing_remove_revert() {
|
2015-11-28 22:54:12 +01:00
|
|
|
let mut trie = OverlayDB::new_temp();
|
|
|
|
let h = trie.insert(b"hello world");
|
|
|
|
trie.commit().unwrap();
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&h);
|
|
|
|
assert_eq!(trie.get(&h), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.revert();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&h).unwrap(), DBValue::from_slice(b"hello world"));
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlaydb_negative() {
|
|
|
|
let mut trie = OverlayDB::new_temp();
|
|
|
|
let h = trie.insert(b"hello world");
|
|
|
|
trie.commit().unwrap();
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&h);
|
|
|
|
trie.remove(&h); //bad - sends us into negative refs.
|
|
|
|
assert_eq!(trie.get(&h), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
assert!(trie.commit().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlaydb_complex() {
|
|
|
|
let mut trie = OverlayDB::new_temp();
|
|
|
|
let hfoo = trie.insert(b"foo");
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
2015-11-28 22:54:12 +01:00
|
|
|
let hbar = trie.insert(b"bar");
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hbar).unwrap(), DBValue::from_slice(b"bar"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.commit().unwrap();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
|
|
|
assert_eq!(trie.get(&hbar).unwrap(), DBValue::from_slice(b"bar"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.insert(b"foo"); // two refs
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.commit().unwrap();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
|
|
|
assert_eq!(trie.get(&hbar).unwrap(), DBValue::from_slice(b"bar"));
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&hbar); // zero refs - delete
|
|
|
|
assert_eq!(trie.get(&hbar), None);
|
|
|
|
trie.remove(&hfoo); // one ref - keep
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.commit().unwrap();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&hfoo); // zero ref - would delete, but...
|
|
|
|
assert_eq!(trie.get(&hfoo), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.insert(b"foo"); // one ref - keep after all.
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
2015-11-28 22:54:12 +01:00
|
|
|
trie.commit().unwrap();
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(trie.get(&hfoo).unwrap(), DBValue::from_slice(b"foo"));
|
2016-03-30 07:36:35 +02:00
|
|
|
trie.remove(&hfoo); // zero ref - delete
|
|
|
|
assert_eq!(trie.get(&hfoo), None);
|
|
|
|
trie.commit().unwrap(); //
|
|
|
|
assert_eq!(trie.get(&hfoo), None);
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|