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/>.
|
|
|
|
|
|
|
|
//! Extras db utils.
|
|
|
|
|
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::sync::RwLock;
|
|
|
|
use std::collections::HashMap;
|
2016-04-30 17:41:24 +02:00
|
|
|
use util::{DBTransaction, Database};
|
2016-04-15 18:54:35 +02:00
|
|
|
use util::rlp::{encode, Encodable, decode, Decodable};
|
|
|
|
|
2016-04-20 15:45:42 +02:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum CacheUpdatePolicy {
|
|
|
|
Overwrite,
|
|
|
|
Remove,
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
/// Should be used to get database key associated with given value.
|
|
|
|
pub trait Key<T> {
|
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-04-30 17:41:24 +02:00
|
|
|
fn write<T, R>(&self, key: &Key<T, Target = R>, value: &T) where T: Encodable, R: Deref<Target = [u8]>;
|
2016-04-20 15:45:42 +02:00
|
|
|
|
|
|
|
/// Writes the value into the database and updates the cache.
|
2016-04-30 17:41:24 +02:00
|
|
|
fn write_with_cache<K, T, R>(&self, cache: &mut HashMap<K, T>, key: K, value: T, policy: CacheUpdatePolicy) where
|
|
|
|
K: Key<T, Target = R> + Hash + Eq,
|
|
|
|
T: Encodable,
|
|
|
|
R: Deref<Target = [u8]> {
|
2016-04-20 15:45:42 +02:00
|
|
|
self.write(&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.
|
2016-04-30 17:41:24 +02:00
|
|
|
fn extend_with_cache<K, T, R>(&self, cache: &mut HashMap<K, T>, values: HashMap<K, T>, policy: CacheUpdatePolicy) where
|
|
|
|
K: Key<T, Target = R> + Hash + Eq,
|
|
|
|
T: Encodable,
|
|
|
|
R: Deref<Target = [u8]> {
|
2016-04-20 15:45:42 +02:00
|
|
|
match policy {
|
|
|
|
CacheUpdatePolicy::Overwrite => {
|
|
|
|
for (key, value) in values.into_iter() {
|
|
|
|
self.write(&key, &value);
|
|
|
|
cache.insert(key, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CacheUpdatePolicy::Remove => {
|
|
|
|
for (key, value) in &values {
|
|
|
|
self.write(key, value);
|
|
|
|
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-04-30 17:41:24 +02:00
|
|
|
fn read<T, R>(&self, key: &Key<T, Target = R>) -> Option<T> where
|
|
|
|
T: Decodable,
|
|
|
|
R: Deref<Target = [u8]>;
|
2016-04-20 15:45:42 +02:00
|
|
|
|
|
|
|
/// Returns value for given key either in cache or in database.
|
|
|
|
fn read_with_cache<K, T>(&self, cache: &RwLock<HashMap<K, T>>, key: &K) -> Option<T> where
|
|
|
|
K: Key<T> + Eq + Hash + Clone,
|
|
|
|
T: Clone + Decodable {
|
|
|
|
{
|
|
|
|
let read = cache.read().unwrap();
|
|
|
|
if let Some(v) = read.get(key) {
|
|
|
|
return Some(v.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.read(key).map(|value: T|{
|
|
|
|
let mut write = cache.write().unwrap();
|
|
|
|
write.insert(key.clone(), value.clone());
|
|
|
|
value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
/// Returns true if given value exists.
|
2016-04-30 17:41:24 +02:00
|
|
|
fn exists<T, R>(&self, 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-04-30 17:41:24 +02:00
|
|
|
fn exists_with_cache<K, T, R>(&self, cache: &RwLock<HashMap<K, T>>, key: &K) -> bool where
|
|
|
|
K: Eq + Hash + Key<T, Target = R>,
|
|
|
|
R: Deref<Target = [u8]> {
|
2016-04-20 15:45:42 +02:00
|
|
|
{
|
|
|
|
let read = cache.read().unwrap();
|
|
|
|
if read.get(key).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
self.exists::<T, R>(key)
|
2016-04-20 15:45:42 +02:00
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writable for DBTransaction {
|
2016-04-30 17:41:24 +02:00
|
|
|
fn write<T, R>(&self, key: &Key<T, Target = R>, value: &T) where T: Encodable, R: Deref<Target = [u8]> {
|
2016-04-15 19:32:30 +02:00
|
|
|
let result = self.put(&key.key(), &encode(value));
|
|
|
|
if let Err(err) = result {
|
2016-04-30 17:41:24 +02:00
|
|
|
panic!("db put failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
|
2016-04-15 19:32:30 +02:00
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Readable for Database {
|
2016-04-30 17:41:24 +02:00
|
|
|
fn read<T, R>(&self, key: &Key<T, Target = R>) -> Option<T> where T: Decodable, R: Deref<Target = [u8]> {
|
2016-04-15 19:32:30 +02:00
|
|
|
let result = self.get(&key.key());
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(option) => option.map(|v| decode(&v)),
|
|
|
|
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-04-30 17:41:24 +02:00
|
|
|
fn exists<T, R>(&self, key: &Key<T, Target = R>) -> bool where R: Deref<Target = [u8]> {
|
2016-04-15 19:32:30 +02:00
|
|
|
let result = self.get(&key.key());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|