2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
//! Reference-counted memory-based `HashDB` implementation.
|
2017-09-06 20:47:45 +02:00
|
|
|
extern crate heapsize;
|
2018-01-10 13:35:18 +01:00
|
|
|
extern crate ethereum_types;
|
2017-09-06 20:47:45 +02:00
|
|
|
extern crate hashdb;
|
2018-01-10 13:35:18 +01:00
|
|
|
extern crate keccak_hash as keccak;
|
|
|
|
extern crate plain_hasher;
|
|
|
|
extern crate rlp;
|
|
|
|
extern crate elastic_array;
|
2015-11-28 00:14:40 +01:00
|
|
|
|
2015-11-28 18:29:50 +01:00
|
|
|
use std::mem;
|
2015-11-28 00:14:40 +01:00
|
|
|
use std::collections::HashMap;
|
2016-07-16 14:48:54 +02:00
|
|
|
use std::collections::hash_map::Entry;
|
2017-08-26 19:09:32 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
|
|
|
use hashdb::{HashDB, DBValue};
|
2017-08-30 16:20:21 +02:00
|
|
|
use keccak::{KECCAK_NULL_RLP, keccak};
|
2018-01-10 13:35:18 +01:00
|
|
|
use plain_hasher::H256FastMap;
|
|
|
|
use rlp::NULL_RLP;
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Reference-counted memory-based `HashDB` implementation.
|
2015-11-28 01:38:36 +01:00
|
|
|
///
|
|
|
|
/// Use `new()` to create a new database. Insert items with `insert()`, remove items
|
2017-05-27 22:35:18 +02:00
|
|
|
/// with `remove()`, check for existence with `contains()` and lookup a hash to derive
|
2016-03-30 07:36:35 +02:00
|
|
|
/// the data with `get()`. Clear with `clear()` and purge the portions of the data
|
2015-11-28 01:38:36 +01:00
|
|
|
/// that have no references with `purge()`.
|
2016-03-11 10:57:58 +01:00
|
|
|
///
|
2015-11-28 01:38:36 +01:00
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
2017-09-06 20:47:45 +02:00
|
|
|
/// extern crate hashdb;
|
|
|
|
/// extern crate memorydb;
|
|
|
|
/// use hashdb::*;
|
|
|
|
/// use memorydb::*;
|
2015-11-28 01:38:36 +01:00
|
|
|
/// fn main() {
|
|
|
|
/// let mut m = MemoryDB::new();
|
|
|
|
/// let d = "Hello world!".as_bytes();
|
|
|
|
///
|
|
|
|
/// let k = m.insert(d);
|
2016-03-30 07:36:35 +02:00
|
|
|
/// assert!(m.contains(&k));
|
|
|
|
/// assert_eq!(m.get(&k).unwrap(), d);
|
2015-11-28 01:38:36 +01:00
|
|
|
///
|
|
|
|
/// m.insert(d);
|
2016-03-30 07:36:35 +02:00
|
|
|
/// assert!(m.contains(&k));
|
2015-11-28 01:38:36 +01:00
|
|
|
///
|
2016-03-30 07:36:35 +02:00
|
|
|
/// m.remove(&k);
|
|
|
|
/// assert!(m.contains(&k));
|
2015-11-28 01:38:36 +01:00
|
|
|
///
|
2016-03-30 07:36:35 +02:00
|
|
|
/// m.remove(&k);
|
|
|
|
/// assert!(!m.contains(&k));
|
2015-11-28 01:38:36 +01:00
|
|
|
///
|
2016-03-30 07:36:35 +02:00
|
|
|
/// m.remove(&k);
|
|
|
|
/// assert!(!m.contains(&k));
|
2015-11-30 02:57:02 +01:00
|
|
|
///
|
|
|
|
/// m.insert(d);
|
2016-03-30 07:36:35 +02:00
|
|
|
/// assert!(!m.contains(&k));
|
2015-11-30 02:57:02 +01:00
|
|
|
|
2015-11-28 01:38:36 +01:00
|
|
|
/// m.insert(d);
|
2016-03-30 07:36:35 +02:00
|
|
|
/// assert!(m.contains(&k));
|
|
|
|
/// assert_eq!(m.get(&k).unwrap(), d);
|
2015-11-28 01:38:36 +01:00
|
|
|
///
|
2016-03-30 07:36:35 +02:00
|
|
|
/// m.remove(&k);
|
|
|
|
/// assert!(!m.contains(&k));
|
2015-11-28 01:38:36 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2016-08-03 18:35:48 +02:00
|
|
|
#[derive(Default, Clone, PartialEq)]
|
2015-11-28 00:14:40 +01:00
|
|
|
pub struct MemoryDB {
|
2016-10-26 13:53:47 +02:00
|
|
|
data: H256FastMap<(DBValue, i32)>,
|
2015-11-28 00:14:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemoryDB {
|
2015-11-28 01:12:59 +01:00
|
|
|
/// Create a new instance of the memory DB.
|
2015-11-28 00:14:40 +01:00
|
|
|
pub fn new() -> MemoryDB {
|
|
|
|
MemoryDB {
|
2016-08-03 22:03:40 +02:00
|
|
|
data: H256FastMap::default(),
|
2015-11-28 00:14:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-28 01:12:59 +01:00
|
|
|
/// Clear all data from the database.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```rust
|
2017-09-06 20:47:45 +02:00
|
|
|
/// extern crate hashdb;
|
|
|
|
/// extern crate memorydb;
|
|
|
|
/// use hashdb::*;
|
|
|
|
/// use memorydb::*;
|
2015-11-28 01:12:59 +01:00
|
|
|
/// fn main() {
|
|
|
|
/// let mut m = MemoryDB::new();
|
|
|
|
/// let hello_bytes = "Hello world!".as_bytes();
|
|
|
|
/// let hash = m.insert(hello_bytes);
|
2016-03-30 07:36:35 +02:00
|
|
|
/// assert!(m.contains(&hash));
|
2015-11-28 01:12:59 +01:00
|
|
|
/// m.clear();
|
2016-03-30 07:36:35 +02:00
|
|
|
/// assert!(!m.contains(&hash));
|
2015-11-28 01:12:59 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-28 00:14:40 +01:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.data.clear();
|
|
|
|
}
|
|
|
|
|
2015-11-28 01:12:59 +01:00
|
|
|
/// Purge all zero-referenced data from the database.
|
2015-11-28 00:14:40 +01:00
|
|
|
pub fn purge(&mut self) {
|
2017-06-08 19:26:46 +02:00
|
|
|
self.data.retain(|_, &mut (_, rc)| rc != 0);
|
2015-11-28 00:14:40 +01:00
|
|
|
}
|
2015-11-28 03:08:57 +01:00
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Return the internal map of hashes to data, clearing the current state.
|
2016-10-26 13:53:47 +02:00
|
|
|
pub fn drain(&mut self) -> H256FastMap<(DBValue, i32)> {
|
2016-08-03 22:03:40 +02:00
|
|
|
mem::replace(&mut self.data, H256FastMap::default())
|
2015-11-28 03:08:57 +01:00
|
|
|
}
|
2015-11-29 15:50:33 +01:00
|
|
|
|
2016-08-03 18:35:48 +02:00
|
|
|
/// 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.
|
2016-10-26 13:53:47 +02:00
|
|
|
pub fn raw(&self, key: &H256) -> Option<(DBValue, i32)> {
|
2017-08-30 16:20:21 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2017-06-07 11:58:39 +02:00
|
|
|
return Some((DBValue::from_slice(&NULL_RLP), 1));
|
2015-11-29 15:50:33 +01:00
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
self.data.get(key).cloned()
|
2015-11-29 15:50:33 +01:00
|
|
|
}
|
2016-03-06 22:39:04 +01:00
|
|
|
|
|
|
|
/// Returns the size of allocated heap memory
|
|
|
|
pub fn mem_used(&self) -> usize {
|
|
|
|
self.data.heap_size_of_children()
|
|
|
|
}
|
2016-07-16 14:48:54 +02:00
|
|
|
|
|
|
|
/// Remove an element and delete it from storage if reference count reaches zero.
|
2017-01-20 13:25:53 +01:00
|
|
|
/// If the value was purged, return the old value.
|
|
|
|
pub fn remove_and_purge(&mut self, key: &H256) -> Option<DBValue> {
|
2017-08-30 16:20:21 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2017-01-20 13:25:53 +01:00
|
|
|
return None;
|
2016-07-16 14:48:54 +02:00
|
|
|
}
|
|
|
|
match self.data.entry(key.clone()) {
|
|
|
|
Entry::Occupied(mut entry) =>
|
|
|
|
if entry.get().1 == 1 {
|
2017-01-20 13:25:53 +01:00
|
|
|
Some(entry.remove().0)
|
2016-07-16 14:48:54 +02:00
|
|
|
} else {
|
|
|
|
entry.get_mut().1 -= 1;
|
2017-01-20 13:25:53 +01:00
|
|
|
None
|
2016-07-16 14:48:54 +02:00
|
|
|
},
|
|
|
|
Entry::Vacant(entry) => {
|
2016-10-26 13:53:47 +02:00
|
|
|
entry.insert((DBValue::new(), -1));
|
2017-01-20 13:25:53 +01:00
|
|
|
None
|
2016-07-16 14:48:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
|
|
|
/// Consolidate all the entries of `other` into `self`.
|
|
|
|
pub fn consolidate(&mut self, mut other: Self) {
|
|
|
|
for (key, (value, rc)) in other.drain() {
|
|
|
|
match self.data.entry(key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
if entry.get().1 < 0 {
|
|
|
|
entry.get_mut().0 = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.get_mut().1 += rc;
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert((value, rc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-28 00:14:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HashDB for MemoryDB {
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2017-08-30 16:20:21 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2017-06-07 11:58:39 +02:00
|
|
|
return Some(DBValue::from_slice(&NULL_RLP));
|
2016-01-15 04:02:24 +01:00
|
|
|
}
|
2016-08-03 18:35:48 +02:00
|
|
|
|
2015-11-28 00:14:40 +01:00
|
|
|
match self.data.get(key) {
|
2016-10-26 13:53:47 +02:00
|
|
|
Some(&(ref d, rc)) if rc > 0 => Some(d.clone()),
|
2015-11-28 00:14:40 +01:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 18:05:59 +01:00
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
2017-08-26 18:34:16 +02:00
|
|
|
self.data.iter()
|
2017-08-28 15:08:16 +02:00
|
|
|
.filter_map(|(k, v)| if v.1 != 0 {
|
|
|
|
Some((*k, v.1))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
2017-08-26 18:34:16 +02:00
|
|
|
.collect()
|
2015-12-03 14:56:39 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool {
|
2017-08-30 16:20:21 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-01-15 04:02:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-03 18:35:48 +02:00
|
|
|
|
2015-11-28 00:14:40 +01:00
|
|
|
match self.data.get(key) {
|
|
|
|
Some(&(_, x)) if x > 0 => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
2016-01-15 04:02:24 +01:00
|
|
|
if value == &NULL_RLP {
|
2017-08-30 16:20:21 +02:00
|
|
|
return KECCAK_NULL_RLP.clone();
|
2016-01-15 04:02:24 +01:00
|
|
|
}
|
2017-08-30 16:20:21 +02:00
|
|
|
let key = keccak(value);
|
2017-08-26 18:34:16 +02:00
|
|
|
match self.data.entry(key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
let &mut (ref mut old_value, ref mut rc) = entry.get_mut();
|
|
|
|
if *rc >= -0x80000000i32 && *rc <= 0 {
|
|
|
|
*old_value = DBValue::from_slice(value);
|
|
|
|
}
|
2015-11-30 02:57:02 +01:00
|
|
|
*rc += 1;
|
2015-11-28 01:38:36 +01:00
|
|
|
},
|
2017-08-26 18:34:16 +02:00
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert((DBValue::from_slice(value), 1));
|
|
|
|
},
|
2015-11-28 00:14:40 +01:00
|
|
|
}
|
|
|
|
key
|
|
|
|
}
|
2015-11-28 03:08:57 +01:00
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, key: H256, value: DBValue) {
|
|
|
|
if &*value == &NULL_RLP {
|
2016-01-15 04:02:24 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-08-03 18:35:48 +02:00
|
|
|
|
2017-08-26 18:34:16 +02:00
|
|
|
match self.data.entry(key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
let &mut (ref mut old_value, ref mut rc) = entry.get_mut();
|
|
|
|
if *rc >= -0x80000000i32 && *rc <= 0 {
|
|
|
|
*old_value = value;
|
|
|
|
}
|
2015-11-30 02:57:02 +01:00
|
|
|
*rc += 1;
|
|
|
|
},
|
2017-08-26 18:34:16 +02:00
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert((value, 1));
|
|
|
|
},
|
2015-11-30 02:57:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, key: &H256) {
|
2017-08-30 16:20:21 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-01-15 04:02:24 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-08-03 18:35:48 +02:00
|
|
|
|
2017-08-26 18:34:16 +02:00
|
|
|
match self.data.entry(*key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
let &mut (_, ref mut rc) = entry.get_mut();
|
|
|
|
*rc -= 1;
|
|
|
|
},
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert((DBValue::new(), -1));
|
|
|
|
},
|
2015-11-28 00:14:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 05:33:18 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-08-30 16:20:21 +02:00
|
|
|
use keccak::keccak;
|
2017-05-27 05:33:18 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn memorydb_remove_and_purge() {
|
|
|
|
let hello_bytes = b"Hello world!";
|
2017-08-30 16:20:21 +02:00
|
|
|
let hello_key = keccak(hello_bytes);
|
2017-05-27 05:33:18 +02:00
|
|
|
|
|
|
|
let mut m = MemoryDB::new();
|
|
|
|
m.remove(&hello_key);
|
|
|
|
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
|
|
|
m.purge();
|
|
|
|
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();
|
|
|
|
assert!(m.remove_and_purge(&hello_key).is_none());
|
|
|
|
assert_eq!(m.raw(&hello_key).unwrap().1, -1);
|
|
|
|
m.insert(hello_bytes);
|
|
|
|
m.insert(hello_bytes);
|
|
|
|
assert_eq!(m.raw(&hello_key).unwrap().1, 1);
|
|
|
|
assert_eq!(&*m.remove_and_purge(&hello_key).unwrap(), hello_bytes);
|
|
|
|
assert_eq!(m.raw(&hello_key), None);
|
|
|
|
assert!(m.remove_and_purge(&hello_key).is_none());
|
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-05-27 05:33:18 +02:00
|
|
|
#[test]
|
|
|
|
fn consolidate() {
|
|
|
|
let mut main = MemoryDB::new();
|
|
|
|
let mut other = MemoryDB::new();
|
|
|
|
let remove_key = other.insert(b"doggo");
|
|
|
|
main.remove(&remove_key);
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-05-27 05:33:18 +02:00
|
|
|
let insert_key = other.insert(b"arf");
|
|
|
|
main.emplace(insert_key, DBValue::from_slice(b"arf"));
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-05-30 19:06:25 +02:00
|
|
|
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
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-05-27 05:33:18 +02:00
|
|
|
main.consolidate(other);
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-05-27 05:33:18 +02:00
|
|
|
let overlay = main.drain();
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-05-27 05:33:18 +02:00
|
|
|
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));
|
2017-05-30 19:06:25 +02:00
|
|
|
assert_eq!(overlay.get(&negative_remove_key).unwrap(), &(DBValue::from_slice(b"negative"), -2));
|
2017-05-27 05:33:18 +02:00
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
}
|