Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #972 from ethcore/db_writer
Browse files Browse the repository at this point in the history
querying extras separated to its own module
  • Loading branch information
arkpar committed Apr 21, 2016
2 parents e149402 + 273e4d6 commit f2a5630
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 50 deletions.
61 changes: 12 additions & 49 deletions ethcore/src/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use blockchain::bloom_indexer::BloomIndexer;
use blockchain::tree_route::TreeRoute;
use blockchain::update::ExtrasUpdate;
use blockchain::{CacheSize, ImportRoute};
use db::{Writable, Readable, Key};
use db::{Writable, Readable, Key, CacheUpdatePolicy};

const BLOOM_INDEX_SIZE: usize = 16;
const BLOOM_LEVELS: u8 = 3;
Expand Down Expand Up @@ -183,7 +183,7 @@ impl BlockProvider for BlockChain {
/// Returns true if the given block is known
/// (though not necessarily a part of the canon chain).
fn is_known(&self, hash: &H256) -> bool {
self.query_extras_exist(hash, &self.block_details)
self.extras_db.exists_with_cache(&self.block_details, hash)
}

// We do not store tracing information.
Expand Down Expand Up @@ -466,28 +466,22 @@ impl BlockChain {
batch.put(b"best", &update.info.hash).unwrap();

{
let mut write_details = self.block_details.write().unwrap();
for (hash, details) in update.block_details.into_iter() {
batch.write(&hash, &details);
self.note_used(CacheID::Extras(ExtrasIndex::BlockDetails, hash.clone()));
write_details.insert(hash, details);
for hash in update.block_details.keys().cloned() {
self.note_used(CacheID::Extras(ExtrasIndex::BlockDetails, hash));
}

let mut write_details = self.block_details.write().unwrap();
batch.extend_with_cache(&mut write_details, update.block_details, CacheUpdatePolicy::Overwrite);
}

{
let mut write_receipts = self.block_receipts.write().unwrap();
for (hash, receipt) in &update.block_receipts {
batch.write(hash, receipt);
write_receipts.remove(hash);
}
batch.extend_with_cache(&mut write_receipts, update.block_receipts, CacheUpdatePolicy::Remove);
}

{
let mut write_blocks_blooms = self.blocks_blooms.write().unwrap();
for (bloom_hash, blocks_bloom) in &update.blocks_blooms {
batch.write(bloom_hash, blocks_bloom);
write_blocks_blooms.remove(bloom_hash);
}
batch.extend_with_cache(&mut write_blocks_blooms, update.blocks_blooms, CacheUpdatePolicy::Remove);
}

// These cached values must be updated last and togeterh
Expand All @@ -508,15 +502,8 @@ impl BlockChain {
}
}

for (number, hash) in &update.block_hashes {
batch.write(number, hash);
write_hashes.remove(number);
}

for (hash, tx_address) in &update.transactions_addresses {
batch.write(hash, tx_address);
write_txs.remove(hash);
}
batch.extend_with_cache(&mut write_hashes, update.block_hashes, CacheUpdatePolicy::Remove);
batch.extend_with_cache(&mut write_txs, update.transactions_addresses, CacheUpdatePolicy::Remove);

// update extras database
self.extras_db.write(batch).unwrap();
Expand Down Expand Up @@ -751,32 +738,8 @@ impl BlockChain {
T: ExtrasIndexable + Clone + Decodable,
K: Key<T> + Eq + Hash + Clone,
H256: From<K> {
{
let read = cache.read().unwrap();
if let Some(v) = read.get(hash) {
return Some(v.clone());
}
}

self.note_used(CacheID::Extras(T::index(), H256::from(hash.clone())));

self.extras_db.read(hash).map(|t: T| {
let mut write = cache.write().unwrap();
write.insert(hash.clone(), t.clone());
t
})
}

fn query_extras_exist<K, T>(&self, hash: &K, cache: &RwLock<HashMap<K, T>>) -> bool where
K: Key<T> + Eq + Hash + Clone {
{
let read = cache.read().unwrap();
if let Some(_) = read.get(hash) {
return true;
}
}

self.extras_db.exists::<T>(hash)
self.extras_db.read_with_cache(cache, hash)
}

/// Get current cache size.
Expand Down
77 changes: 76 additions & 1 deletion ethcore/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@

//! Extras db utils.
use std::hash::Hash;
use std::sync::RwLock;
use std::collections::HashMap;
use util::{H264, DBTransaction, Database};
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.
pub trait Key<T> {
/// Returns db key.
Expand All @@ -27,16 +36,82 @@ pub trait Key<T> {

/// Should be used to write value into database.
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;

/// 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.
pub trait Readable {
/// Returns value for given key.
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.
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 {
Expand Down

0 comments on commit f2a5630

Please sign in to comment.