2016-04-15 18:54:35 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// 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-08-18 18:24:49 +02:00
|
|
|
//! Database utilities and definitions.
|
2016-04-15 18:54:35 +02:00
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
use std::ops::Deref;
|
2016-04-20 15:45:42 +02:00
|
|
|
use std::hash::Hash;
|
|
|
|
use std::collections::HashMap;
|
2016-07-13 19:59:59 +02:00
|
|
|
use util::{DBTransaction, Database, RwLock};
|
2016-09-01 14:29:59 +02:00
|
|
|
|
|
|
|
use rlp;
|
2016-04-15 18:54:35 +02:00
|
|
|
|
2016-08-18 18:24:49 +02:00
|
|
|
// database columns
|
|
|
|
/// Column for State
|
|
|
|
pub const COL_STATE: Option<u32> = Some(0);
|
|
|
|
/// Column for Block headers
|
|
|
|
pub const COL_HEADERS: Option<u32> = Some(1);
|
|
|
|
/// Column for Block bodies
|
|
|
|
pub const COL_BODIES: Option<u32> = Some(2);
|
|
|
|
/// Column for Extras
|
|
|
|
pub const COL_EXTRA: Option<u32> = Some(3);
|
|
|
|
/// Column for Traces
|
|
|
|
pub const COL_TRACE: Option<u32> = Some(4);
|
2016-10-03 12:02:43 +02:00
|
|
|
/// Column for Traces
|
|
|
|
pub const COL_ACCOUNT_BLOOM: Option<u32> = Some(5);
|
2016-08-18 18:24:49 +02:00
|
|
|
/// Number of columns in DB
|
2016-10-03 12:02:43 +02:00
|
|
|
pub const NUM_COLUMNS: Option<u32> = Some(6);
|
2016-08-18 18:24:49 +02:00
|
|
|
|
|
|
|
/// Modes for updating caches.
|
2016-04-20 15:45:42 +02:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum CacheUpdatePolicy {
|
2016-08-18 18:24:49 +02:00
|
|
|
/// Overwrite entries.
|
2016-04-20 15:45:42 +02:00
|
|
|
Overwrite,
|
2016-08-18 18:24:49 +02:00
|
|
|
/// Remove entries.
|
2016-04-20 15:45:42 +02:00
|
|
|
Remove,
|
|
|
|
}
|
|
|
|
|
2016-08-18 18:24:49 +02:00
|
|
|
/// A cache for arbitrary key-value pairs.
|
2016-05-26 18:24:51 +02:00
|
|
|
pub trait Cache<K, V> {
|
2016-08-18 18:24:49 +02:00
|
|
|
/// Insert an entry into the cache and get the old value.
|
2016-05-26 18:24:51 +02:00
|
|
|
fn insert(&mut self, k: K, v: V) -> Option<V>;
|
|
|
|
|
2016-08-18 18:24:49 +02:00
|
|
|
/// Remove an entry from the cache, getting the old value if it existed.
|
2016-05-26 18:24:51 +02:00
|
|
|
fn remove(&mut self, k: &K) -> Option<V>;
|
|
|
|
|
2016-08-18 18:24:49 +02:00
|
|
|
/// Query the cache for a key's associated value.
|
2016-05-26 18:24:51 +02:00
|
|
|
fn get(&self, k: &K) -> Option<&V>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> Cache<K, V> for HashMap<K, V> where K: Hash + Eq {
|
|
|
|
fn insert(&mut self, k: K, v: V) -> Option<V> {
|
|
|
|
HashMap::insert(self, k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&mut self, k: &K) -> Option<V> {
|
|
|
|
HashMap::remove(self, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, k: &K) -> Option<&V> {
|
|
|
|
HashMap::get(self, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
/// Should be used to get database key associated with given value.
|
|
|
|
pub trait Key<T> {
|
2016-08-18 18:24:49 +02:00
|
|
|
/// The db key associated with this value.
|
2016-04-30 17:41:24 +02:00
|
|
|
type Target: Deref<Target = [u8]>;
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
/// Returns db key.
|
2016-04-30 17:41:24 +02:00
|
|
|
fn key(&self) -> Self::Target;
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should be used to write value into database.
|
|
|
|
pub trait Writable {
|
2016-04-20 15:45:42 +02:00
|
|
|
/// Writes the value into the database.
|
2016-09-01 14:29:59 +02:00
|
|
|
fn write<T, R>(&mut self, col: Option<u32>, key: &Key<T, Target = R>, value: &T) where T: rlp::Encodable, R: Deref<Target = [u8]>;
|
2016-04-20 15:45:42 +02:00
|
|
|
|
2016-09-28 15:49:42 +02:00
|
|
|
/// Deletes key from the databse.
|
|
|
|
fn delete<T, R>(&mut self, col: Option<u32>, key: &Key<T, Target = R>) where T: rlp::Encodable, R: Deref<Target = [u8]>;
|
|
|
|
|
2016-04-20 15:45:42 +02:00
|
|
|
/// Writes the value into the database and updates the cache.
|
2016-08-25 16:43:56 +02:00
|
|
|
fn write_with_cache<K, T, R>(&mut self, col: Option<u32>, cache: &mut Cache<K, T>, key: K, value: T, policy: CacheUpdatePolicy) where
|
2016-04-30 17:41:24 +02:00
|
|
|
K: Key<T, Target = R> + Hash + Eq,
|
2016-09-01 14:29:59 +02:00
|
|
|
T: rlp::Encodable,
|
2016-04-30 17:41:24 +02:00
|
|
|
R: Deref<Target = [u8]> {
|
2016-07-28 23:46:24 +02:00
|
|
|
self.write(col, &key, &value);
|
2016-04-20 15:45:42 +02:00
|
|
|
match policy {
|
|
|
|
CacheUpdatePolicy::Overwrite => {
|
|
|
|
cache.insert(key, value);
|
|
|
|
},
|
|
|
|
CacheUpdatePolicy::Remove => {
|
|
|
|
cache.remove(&key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes the values into the database and updates the cache.
|
2016-08-25 16:43:56 +02:00
|
|
|
fn extend_with_cache<K, T, R>(&mut self, col: Option<u32>, cache: &mut Cache<K, T>, values: HashMap<K, T>, policy: CacheUpdatePolicy) where
|
2016-04-30 17:41:24 +02:00
|
|
|
K: Key<T, Target = R> + Hash + Eq,
|
2016-09-01 14:29:59 +02:00
|
|
|
T: rlp::Encodable,
|
2016-04-30 17:41:24 +02:00
|
|
|
R: Deref<Target = [u8]> {
|
2016-04-20 15:45:42 +02:00
|
|
|
match policy {
|
|
|
|
CacheUpdatePolicy::Overwrite => {
|
|
|
|
for (key, value) in values.into_iter() {
|
2016-07-28 23:46:24 +02:00
|
|
|
self.write(col, &key, &value);
|
2016-04-20 15:45:42 +02:00
|
|
|
cache.insert(key, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CacheUpdatePolicy::Remove => {
|
|
|
|
for (key, value) in &values {
|
2016-07-28 23:46:24 +02:00
|
|
|
self.write(col, key, value);
|
2016-04-20 15:45:42 +02:00
|
|
|
cache.remove(key);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-09-28 15:49:42 +02:00
|
|
|
|
|
|
|
/// Writes and removes the values into the database and updates the cache.
|
|
|
|
fn extend_with_option_cache<K, T, R>(&mut self, col: Option<u32>, cache: &mut Cache<K, Option<T>>, values: HashMap<K, Option<T>>, policy: CacheUpdatePolicy) where
|
|
|
|
K: Key<T, Target = R> + Hash + Eq,
|
|
|
|
T: rlp::Encodable,
|
|
|
|
R: Deref<Target = [u8]> {
|
|
|
|
match policy {
|
|
|
|
CacheUpdatePolicy::Overwrite => {
|
|
|
|
for (key, value) in values.into_iter() {
|
|
|
|
match value {
|
|
|
|
Some(ref v) => self.write(col, &key, v),
|
|
|
|
None => self.delete(col, &key),
|
|
|
|
}
|
|
|
|
cache.insert(key, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CacheUpdatePolicy::Remove => {
|
|
|
|
for (key, value) in values.into_iter() {
|
|
|
|
match value {
|
|
|
|
Some(v) => self.write(col, &key, &v),
|
|
|
|
None => self.delete(col, &key),
|
|
|
|
}
|
|
|
|
cache.remove(&key);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should be used to read values from database.
|
|
|
|
pub trait Readable {
|
|
|
|
/// Returns value for given key.
|
2016-07-28 23:46:24 +02:00
|
|
|
fn read<T, R>(&self, col: Option<u32>, key: &Key<T, Target = R>) -> Option<T> where
|
2016-09-01 14:29:59 +02:00
|
|
|
T: rlp::Decodable,
|
2016-04-30 17:41:24 +02:00
|
|
|
R: Deref<Target = [u8]>;
|
2016-04-20 15:45:42 +02:00
|
|
|
|
|
|
|
/// Returns value for given key either in cache or in database.
|
2016-07-28 23:46:24 +02:00
|
|
|
fn read_with_cache<K, T, C>(&self, col: Option<u32>, cache: &RwLock<C>, key: &K) -> Option<T> where
|
2016-04-20 15:45:42 +02:00
|
|
|
K: Key<T> + Eq + Hash + Clone,
|
2016-09-01 14:29:59 +02:00
|
|
|
T: Clone + rlp::Decodable,
|
2016-05-26 18:24:51 +02:00
|
|
|
C: Cache<K, T> {
|
2016-04-20 15:45:42 +02:00
|
|
|
{
|
2016-07-13 19:59:59 +02:00
|
|
|
let read = cache.read();
|
2016-04-20 15:45:42 +02:00
|
|
|
if let Some(v) = read.get(key) {
|
|
|
|
return Some(v.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
self.read(col, key).map(|value: T|{
|
2016-07-13 19:59:59 +02:00
|
|
|
let mut write = cache.write();
|
2016-04-20 15:45:42 +02:00
|
|
|
write.insert(key.clone(), value.clone());
|
|
|
|
value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
/// Returns true if given value exists.
|
2016-07-28 23:46:24 +02:00
|
|
|
fn exists<T, R>(&self, col: Option<u32>, key: &Key<T, Target = R>) -> bool where R: Deref<Target= [u8]>;
|
2016-04-20 15:45:42 +02:00
|
|
|
|
|
|
|
/// Returns true if given value exists either in cache or in database.
|
2016-07-28 23:46:24 +02:00
|
|
|
fn exists_with_cache<K, T, R, C>(&self, col: Option<u32>, cache: &RwLock<C>, key: &K) -> bool where
|
2016-04-30 17:41:24 +02:00
|
|
|
K: Eq + Hash + Key<T, Target = R>,
|
2016-05-26 18:24:51 +02:00
|
|
|
R: Deref<Target = [u8]>,
|
|
|
|
C: Cache<K, T> {
|
2016-04-20 15:45:42 +02:00
|
|
|
{
|
2016-07-13 19:59:59 +02:00
|
|
|
let read = cache.read();
|
2016-04-20 15:45:42 +02:00
|
|
|
if read.get(key).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
self.exists::<T, R>(col, key)
|
2016-04-20 15:45:42 +02:00
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writable for DBTransaction {
|
2016-09-01 14:29:59 +02:00
|
|
|
fn write<T, R>(&mut self, col: Option<u32>, key: &Key<T, Target = R>, value: &T) where T: rlp::Encodable, R: Deref<Target = [u8]> {
|
|
|
|
self.put(col, &key.key(), &rlp::encode(value));
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
2016-09-28 15:49:42 +02:00
|
|
|
|
|
|
|
fn delete<T, R>(&mut self, col: Option<u32>, key: &Key<T, Target = R>) where T: rlp::Encodable, R: Deref<Target = [u8]> {
|
|
|
|
self.delete(col, &key.key());
|
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Readable for Database {
|
2016-09-01 14:29:59 +02:00
|
|
|
fn read<T, R>(&self, col: Option<u32>, key: &Key<T, Target = R>) -> Option<T> where T: rlp::Decodable, R: Deref<Target = [u8]> {
|
2016-07-28 23:46:24 +02:00
|
|
|
let result = self.get(col, &key.key());
|
2016-04-15 19:32:30 +02:00
|
|
|
|
|
|
|
match result {
|
2016-09-01 14:29:59 +02:00
|
|
|
Ok(option) => option.map(|v| rlp::decode(&v)),
|
2016-04-15 19:32:30 +02:00
|
|
|
Err(err) => {
|
2016-04-30 17:41:24 +02:00
|
|
|
panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
|
2016-04-15 19:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
fn exists<T, R>(&self, col: Option<u32>, key: &Key<T, Target = R>) -> bool where R: Deref<Target = [u8]> {
|
|
|
|
let result = self.get(col, &key.key());
|
2016-04-15 19:32:30 +02:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(v) => v.is_some(),
|
|
|
|
Err(err) => {
|
2016-04-30 17:41:24 +02:00
|
|
|
panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
|
2016-04-15 19:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
}
|