simplified writing and reading from database with cache
This commit is contained in:
parent
9ce9fd390d
commit
bffa1e1ec9
@ -30,7 +30,7 @@ use blockchain::bloom_indexer::BloomIndexer;
|
|||||||
use blockchain::tree_route::TreeRoute;
|
use blockchain::tree_route::TreeRoute;
|
||||||
use blockchain::update::ExtrasUpdate;
|
use blockchain::update::ExtrasUpdate;
|
||||||
use blockchain::{CacheSize, ImportRoute};
|
use blockchain::{CacheSize, ImportRoute};
|
||||||
use db::{Writable, Readable, Key, DatabaseReader, BatchWriter, CacheUpdatePolicy};
|
use db::{Writable, Readable, Key, CacheUpdatePolicy};
|
||||||
|
|
||||||
const BLOOM_INDEX_SIZE: usize = 16;
|
const BLOOM_INDEX_SIZE: usize = 16;
|
||||||
const BLOOM_LEVELS: u8 = 3;
|
const BLOOM_LEVELS: u8 = 3;
|
||||||
@ -183,7 +183,7 @@ impl BlockProvider for BlockChain {
|
|||||||
/// Returns true if the given block is known
|
/// Returns true if the given block is known
|
||||||
/// (though not necessarily a part of the canon chain).
|
/// (though not necessarily a part of the canon chain).
|
||||||
fn is_known(&self, hash: &H256) -> bool {
|
fn is_known(&self, hash: &H256) -> bool {
|
||||||
DatabaseReader::new(&self.extras_db, &self.block_details).exists(hash)
|
self.extras_db.exists_with_cache(&self.block_details, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We do not store tracing information.
|
// We do not store tracing information.
|
||||||
@ -471,20 +471,17 @@ impl BlockChain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut write_details = self.block_details.write().unwrap();
|
let mut write_details = self.block_details.write().unwrap();
|
||||||
BatchWriter::new(&batch, &mut write_details)
|
batch.extend_with_cache(&mut write_details, update.block_details, CacheUpdatePolicy::Overwrite);
|
||||||
.extend(update.block_details, CacheUpdatePolicy::Overwrite);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut write_receipts = self.block_receipts.write().unwrap();
|
let mut write_receipts = self.block_receipts.write().unwrap();
|
||||||
BatchWriter::new(&batch, &mut write_receipts)
|
batch.extend_with_cache(&mut write_receipts, update.block_receipts, CacheUpdatePolicy::Remove);
|
||||||
.extend(update.block_receipts, CacheUpdatePolicy::Remove);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut write_blocks_blooms = self.blocks_blooms.write().unwrap();
|
let mut write_blocks_blooms = self.blocks_blooms.write().unwrap();
|
||||||
BatchWriter::new(&batch, &mut write_blocks_blooms)
|
batch.extend_with_cache(&mut write_blocks_blooms, update.blocks_blooms, CacheUpdatePolicy::Remove);
|
||||||
.extend(update.blocks_blooms, CacheUpdatePolicy::Remove);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// These cached values must be updated last and togeterh
|
// These cached values must be updated last and togeterh
|
||||||
@ -505,11 +502,8 @@ impl BlockChain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BatchWriter::new(&batch, &mut write_hashes)
|
batch.extend_with_cache(&mut write_hashes, update.block_hashes, CacheUpdatePolicy::Remove);
|
||||||
.extend(update.block_hashes, CacheUpdatePolicy::Remove);
|
batch.extend_with_cache(&mut write_txs, update.transactions_addresses, CacheUpdatePolicy::Remove);
|
||||||
|
|
||||||
BatchWriter::new(&batch, &mut write_txs)
|
|
||||||
.extend(update.transactions_addresses, CacheUpdatePolicy::Remove);
|
|
||||||
|
|
||||||
// update extras database
|
// update extras database
|
||||||
self.extras_db.write(batch).unwrap();
|
self.extras_db.write(batch).unwrap();
|
||||||
@ -745,7 +739,7 @@ impl BlockChain {
|
|||||||
K: Key<T> + Eq + Hash + Clone,
|
K: Key<T> + Eq + Hash + Clone,
|
||||||
H256: From<K> {
|
H256: From<K> {
|
||||||
self.note_used(CacheID::Extras(T::index(), H256::from(hash.clone())));
|
self.note_used(CacheID::Extras(T::index(), H256::from(hash.clone())));
|
||||||
DatabaseReader::new(&self.extras_db, cache).read(hash)
|
self.extras_db.read_with_cache(cache, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current cache size.
|
/// Get current cache size.
|
||||||
|
@ -16,9 +16,18 @@
|
|||||||
|
|
||||||
//! Extras db utils.
|
//! Extras db utils.
|
||||||
|
|
||||||
|
use std::hash::Hash;
|
||||||
|
use std::sync::RwLock;
|
||||||
|
use std::collections::HashMap;
|
||||||
use util::{H264, DBTransaction, Database};
|
use util::{H264, DBTransaction, Database};
|
||||||
use util::rlp::{encode, Encodable, decode, Decodable};
|
use util::rlp::{encode, Encodable, decode, Decodable};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum CacheUpdatePolicy {
|
||||||
|
Overwrite,
|
||||||
|
Remove,
|
||||||
|
}
|
||||||
|
|
||||||
/// Should be used to get database key associated with given value.
|
/// Should be used to get database key associated with given value.
|
||||||
pub trait Key<T> {
|
pub trait Key<T> {
|
||||||
/// Returns db key.
|
/// Returns db key.
|
||||||
@ -27,16 +36,82 @@ pub trait Key<T> {
|
|||||||
|
|
||||||
/// Should be used to write value into database.
|
/// Should be used to write value into database.
|
||||||
pub trait Writable {
|
pub trait Writable {
|
||||||
/// Writes key into database.
|
/// Writes the value into the database.
|
||||||
fn write<T>(&self, key: &Key<T>, value: &T) where T: Encodable;
|
fn write<T>(&self, key: &Key<T>, value: &T) where T: Encodable;
|
||||||
|
|
||||||
|
/// Writes the value into the database and updates the cache.
|
||||||
|
fn write_with_cache<K, T>(&self, cache: &mut HashMap<K, T>, key: K, value: T, policy: CacheUpdatePolicy) where
|
||||||
|
K: Key<T> + Hash + Eq,
|
||||||
|
T: Encodable {
|
||||||
|
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.
|
||||||
|
fn extend_with_cache<K, T>(&self, cache: &mut HashMap<K, T>, values: HashMap<K, T>, policy: CacheUpdatePolicy)
|
||||||
|
where K: Key<T> + Hash + Eq, T: Encodable {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should be used to read values from database.
|
/// Should be used to read values from database.
|
||||||
pub trait Readable {
|
pub trait Readable {
|
||||||
/// Returns value for given key.
|
/// Returns value for given key.
|
||||||
fn read<T>(&self, key: &Key<T>) -> Option<T> where T: Decodable;
|
fn read<T>(&self, key: &Key<T>) -> Option<T> where T: Decodable;
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true if given value exists.
|
/// Returns true if given value exists.
|
||||||
fn exists<T>(&self, key: &Key<T>) -> bool;
|
fn exists<T>(&self, key: &Key<T>) -> bool;
|
||||||
|
|
||||||
|
/// Returns true if given value exists either in cache or in database.
|
||||||
|
fn exists_with_cache<K, T>(&self, cache: &RwLock<HashMap<K, T>>, key: &K) -> bool where
|
||||||
|
K: Eq + Hash + Key<T> {
|
||||||
|
{
|
||||||
|
let read = cache.read().unwrap();
|
||||||
|
if read.get(key).is_some() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.exists::<T>(key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Writable for DBTransaction {
|
impl Writable for DBTransaction {
|
||||||
|
@ -15,9 +15,4 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
pub mod db;
|
pub mod db;
|
||||||
mod reader;
|
pub use self::db::{Key, Writable, Readable, CacheUpdatePolicy};
|
||||||
mod writer;
|
|
||||||
|
|
||||||
pub use self::db::{Key, Writable, Readable};
|
|
||||||
pub use self::reader::DatabaseReader;
|
|
||||||
pub use self::writer::{BatchWriter, CacheUpdatePolicy};
|
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
// 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/>.
|
|
||||||
|
|
||||||
use std::hash::Hash;
|
|
||||||
use std::sync::RwLock;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use util::Database;
|
|
||||||
use util::rlp::Decodable;
|
|
||||||
use super::{Readable, Key};
|
|
||||||
|
|
||||||
pub struct DatabaseReader<'a, K, V> where K: 'a, V: 'a {
|
|
||||||
db: &'a Database,
|
|
||||||
cache: &'a RwLock<HashMap<K, V>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, K, V> DatabaseReader<'a, K, V> {
|
|
||||||
pub fn new(db: &'a Database, cache: &'a RwLock<HashMap<K, V>>) -> Self {
|
|
||||||
DatabaseReader {
|
|
||||||
db: db,
|
|
||||||
cache: cache,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read(&self, key: &K) -> Option<V> where
|
|
||||||
K: Eq + Hash + Clone + Key<V>,
|
|
||||||
V: Clone + Decodable {
|
|
||||||
{
|
|
||||||
let read = self.cache.read().unwrap();
|
|
||||||
if let Some(v) = read.get(key) {
|
|
||||||
return Some(v.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.db.read(key).map(|value: V|{
|
|
||||||
let mut write = self.cache.write().unwrap();
|
|
||||||
write.insert(key.clone(), value.clone());
|
|
||||||
value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exists(&self, key: &K) -> bool where
|
|
||||||
K: Eq + Hash + Key<V> {
|
|
||||||
{
|
|
||||||
let read = self.cache.read().unwrap();
|
|
||||||
if read.get(key).is_some() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.db.exists::<V>(key)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
// 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/>.
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::hash::Hash;
|
|
||||||
use util::DBTransaction;
|
|
||||||
use util::rlp::Encodable;
|
|
||||||
use super::{Key, Writable};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub enum CacheUpdatePolicy {
|
|
||||||
Overwrite,
|
|
||||||
Remove,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct BatchWriter<'a, K, V> where K: 'a, V: 'a {
|
|
||||||
batch: &'a DBTransaction,
|
|
||||||
cache: &'a mut HashMap<K, V>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, K, V> BatchWriter<'a, K, V> {
|
|
||||||
pub fn new(batch: &'a DBTransaction, cache: &'a mut HashMap<K, V>) -> Self {
|
|
||||||
BatchWriter {
|
|
||||||
batch: batch,
|
|
||||||
cache: cache,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extend(&mut self, map: HashMap<K, V>, policy: CacheUpdatePolicy) where
|
|
||||||
K: Key<V> + Hash + Eq,
|
|
||||||
V: Encodable {
|
|
||||||
match policy {
|
|
||||||
CacheUpdatePolicy::Overwrite => {
|
|
||||||
for (key, value) in map.into_iter() {
|
|
||||||
self.batch.write(&key, &value);
|
|
||||||
self.cache.insert(key, value);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
CacheUpdatePolicy::Remove => {
|
|
||||||
for (key, value) in &map {
|
|
||||||
self.batch.write(key, value);
|
|
||||||
self.cache.remove(key);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user