// 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 .
//! Database utilities and definitions.
use std::ops::Deref;
use std::hash::Hash;
use std::collections::HashMap;
use util::{DBTransaction, Database, RwLock};
use rlp;
// database columns
/// Column for State
pub const COL_STATE: Option = Some(0);
/// Column for Block headers
pub const COL_HEADERS: Option = Some(1);
/// Column for Block bodies
pub const COL_BODIES: Option = Some(2);
/// Column for Extras
pub const COL_EXTRA: Option = Some(3);
/// Column for Traces
pub const COL_TRACE: Option = Some(4);
/// Number of columns in DB
pub const NUM_COLUMNS: Option = Some(5);
/// Modes for updating caches.
#[derive(Clone, Copy)]
pub enum CacheUpdatePolicy {
/// Overwrite entries.
Overwrite,
/// Remove entries.
Remove,
}
/// A cache for arbitrary key-value pairs.
pub trait Cache {
/// Insert an entry into the cache and get the old value.
fn insert(&mut self, k: K, v: V) -> Option;
/// Remove an entry from the cache, getting the old value if it existed.
fn remove(&mut self, k: &K) -> Option;
/// Query the cache for a key's associated value.
fn get(&self, k: &K) -> Option<&V>;
}
impl Cache for HashMap where K: Hash + Eq {
fn insert(&mut self, k: K, v: V) -> Option {
HashMap::insert(self, k, v)
}
fn remove(&mut self, k: &K) -> Option {
HashMap::remove(self, k)
}
fn get(&self, k: &K) -> Option<&V> {
HashMap::get(self, k)
}
}
/// Should be used to get database key associated with given value.
pub trait Key {
/// The db key associated with this value.
type Target: Deref;
/// Returns db key.
fn key(&self) -> Self::Target;
}
/// Should be used to write value into database.
pub trait Writable {
/// Writes the value into the database.
fn write(&mut self, col: Option, key: &Key, value: &T) where T: rlp::Encodable, R: Deref;
/// Writes the value into the database and updates the cache.
fn write_with_cache(&mut self, col: Option, cache: &mut Cache, key: K, value: T, policy: CacheUpdatePolicy) where
K: Key + Hash + Eq,
T: rlp::Encodable,
R: Deref {
self.write(col, &key, &value);
match policy {
CacheUpdatePolicy::Overwrite => {
cache.insert(key, value);
},
CacheUpdatePolicy::Remove => {
cache.remove(&key);
}
}
}
/// Writes the values into the database and updates the cache.
fn extend_with_cache(&mut self, col: Option, cache: &mut Cache, values: HashMap, policy: CacheUpdatePolicy) where
K: Key + Hash + Eq,
T: rlp::Encodable,
R: Deref {
match policy {
CacheUpdatePolicy::Overwrite => {
for (key, value) in values.into_iter() {
self.write(col, &key, &value);
cache.insert(key, value);
}
},
CacheUpdatePolicy::Remove => {
for (key, value) in &values {
self.write(col, key, value);
cache.remove(key);
}
},
}
}
}
/// Should be used to read values from database.
pub trait Readable {
/// Returns value for given key.
fn read(&self, col: Option, key: &Key) -> Option where
T: rlp::Decodable,
R: Deref;
/// Returns value for given key either in cache or in database.
fn read_with_cache(&self, col: Option, cache: &RwLock, key: &K) -> Option where
K: Key + Eq + Hash + Clone,
T: Clone + rlp::Decodable,
C: Cache {
{
let read = cache.read();
if let Some(v) = read.get(key) {
return Some(v.clone());
}
}
self.read(col, key).map(|value: T|{
let mut write = cache.write();
write.insert(key.clone(), value.clone());
value
})
}
/// Returns true if given value exists.
fn exists(&self, col: Option, key: &Key) -> bool where R: Deref;
/// Returns true if given value exists either in cache or in database.
fn exists_with_cache(&self, col: Option, cache: &RwLock, key: &K) -> bool where
K: Eq + Hash + Key,
R: Deref,
C: Cache {
{
let read = cache.read();
if read.get(key).is_some() {
return true;
}
}
self.exists::(col, key)
}
}
impl Writable for DBTransaction {
fn write(&mut self, col: Option, key: &Key, value: &T) where T: rlp::Encodable, R: Deref {
self.put(col, &key.key(), &rlp::encode(value));
}
}
impl Readable for Database {
fn read(&self, col: Option, key: &Key) -> Option where T: rlp::Decodable, R: Deref {
let result = self.get(col, &key.key());
match result {
Ok(option) => option.map(|v| rlp::decode(&v)),
Err(err) => {
panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
}
}
}
fn exists(&self, col: Option, key: &Key) -> bool where R: Deref {
let result = self.get(col, &key.key());
match result {
Ok(v) => v.is_some(),
Err(err) => {
panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
}
}
}
}