2016-02-05 13:40:41 +01: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-01-18 12:41:31 +01:00
//! Disk-backed HashDB implementation.
use common ::* ;
use rlp ::* ;
use hashdb ::* ;
2016-02-04 02:40:35 +01:00
use memorydb ::* ;
2016-03-06 22:39:04 +01:00
use kvdb ::{ Database , DBTransaction , DatabaseConfig } ;
2016-01-31 17:01:36 +01:00
#[ cfg(test) ]
use std ::env ;
2016-01-18 12:41:31 +01:00
2016-03-11 12:54:48 +01:00
/// A HashDB which can manage a short-term journal potentially containing many forks of mutually
/// exclusive actions.
2016-03-11 13:19:10 +01:00
pub trait JournalDB : HashDB + Sync + Send {
2016-03-11 12:54:48 +01:00
/// Return a copy of ourself, in a box.
2016-03-11 13:19:10 +01:00
fn spawn ( & self ) -> Box < Box < JournalDB > > ;
2016-03-11 12:54:48 +01:00
/// Returns heap memory size used
fn mem_used ( & self ) -> usize ;
/// Check if this database has any commits
fn is_empty ( & self ) -> bool ;
/// Commit all recent insert operations.
fn commit ( & mut self , now : u64 , id : & H256 , end : Option < ( u64 , H256 ) > ) -> Result < u32 , UtilError > ;
}
2016-01-18 12:41:31 +01:00
/// Implementation of the HashDB trait for a disk-backed database with a memory overlay
2016-03-06 21:57:55 +01:00
/// and latent-removal semantics.
2016-01-18 12:41:31 +01:00
///
2016-03-07 14:33:00 +01:00
/// Like OverlayDB, there is a memory overlay; `commit()` must be called in order to
2016-01-18 12:41:31 +01:00
/// write operations out to disk. Unlike OverlayDB, `remove()` operations do not take effect
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
/// the removals actually take effect.
2016-03-11 12:54:48 +01:00
pub struct OptionOneDB {
2016-02-04 02:40:35 +01:00
overlay : MemoryDB ,
2016-03-06 22:39:04 +01:00
backing : Arc < Database > ,
counters : Option < Arc < RwLock < HashMap < H256 , i32 > > > > ,
2016-01-18 12:41:31 +01:00
}
2016-03-06 22:39:04 +01:00
// all keys must be at least 12 bytes
const LATEST_ERA_KEY : [ u8 ; 12 ] = [ b 'l' , b 'a' , b 's' , b 't' , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
const VERSION_KEY : [ u8 ; 12 ] = [ b 'j' , b 'v' , b 'e' , b 'r' , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
2016-02-18 03:46:24 +01:00
2016-03-06 22:39:04 +01:00
const DB_VERSION : u32 = 3 ;
const DB_VERSION_NO_JOURNAL : u32 = 3 + 256 ;
const PADDING : [ u8 ; 10 ] = [ 0 u8 ; 10 ] ;
2016-02-04 21:33:30 +01:00
2016-03-11 12:54:48 +01:00
impl OptionOneDB {
2016-03-06 22:39:04 +01:00
/// Create a new instance from file
2016-03-11 12:54:48 +01:00
pub fn new ( path : & str ) -> OptionOneDB {
2016-03-06 22:39:04 +01:00
Self ::from_prefs ( path , true )
2016-03-04 20:19:36 +01:00
}
2016-03-06 22:39:04 +01:00
/// Create a new instance from file
2016-03-11 12:54:48 +01:00
pub fn from_prefs ( path : & str , prefer_journal : bool ) -> OptionOneDB {
2016-03-06 22:39:04 +01:00
let opts = DatabaseConfig {
prefix_size : Some ( 12 ) //use 12 bytes as prefix, this must match account_db prefix
} ;
let backing = Database ::open ( & opts , path ) . unwrap_or_else ( | e | {
panic! ( " Error opening state db: {} " , e ) ;
} ) ;
let with_journal ;
if ! backing . is_empty ( ) {
2016-02-05 01:49:06 +01:00
match backing . get ( & VERSION_KEY ) . map ( | d | d . map ( | v | decode ::< u32 > ( & v ) ) ) {
2016-03-06 22:39:04 +01:00
Ok ( Some ( DB_VERSION ) ) = > { with_journal = true ; } ,
Ok ( Some ( DB_VERSION_NO_JOURNAL ) ) = > { with_journal = false ; } ,
2016-02-05 01:49:06 +01:00
v = > panic! ( " Incompatible DB version, expected {} , got {:?} " , DB_VERSION , v )
}
} else {
2016-03-06 22:39:04 +01:00
backing . put ( & VERSION_KEY , & encode ( & ( if prefer_journal { DB_VERSION } else { DB_VERSION_NO_JOURNAL } ) ) ) . expect ( " Error writing version to database " ) ;
with_journal = prefer_journal ;
2016-02-05 01:49:06 +01:00
}
2016-03-06 22:39:04 +01:00
let counters = if with_journal {
2016-03-11 12:54:48 +01:00
Some ( Arc ::new ( RwLock ::new ( OptionOneDB ::read_counters ( & backing ) ) ) )
2016-03-06 22:39:04 +01:00
} else {
None
} ;
2016-03-11 12:54:48 +01:00
OptionOneDB {
2016-02-04 02:40:35 +01:00
overlay : MemoryDB ::new ( ) ,
2016-03-06 22:39:04 +01:00
backing : Arc ::new ( backing ) ,
counters : counters ,
2016-01-21 23:33:52 +01:00
}
}
2016-01-18 12:41:31 +01:00
/// Create a new instance with an anonymous temporary database.
2016-01-31 10:52:07 +01:00
#[ cfg(test) ]
2016-03-11 12:54:48 +01:00
fn new_temp ( ) -> OptionOneDB {
2016-01-18 12:41:31 +01:00
let mut dir = env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-03-06 22:43:21 +01:00
Self ::new ( dir . to_str ( ) . unwrap ( ) )
2016-01-18 12:41:31 +01:00
}
2016-03-06 22:39:04 +01:00
/// Drain the overlay and place it into a batch for the DB.
fn batch_overlay_insertions ( overlay : & mut MemoryDB , batch : & DBTransaction ) -> usize {
let mut inserts = 0 usize ;
let mut deletes = 0 usize ;
for i in overlay . drain ( ) . into_iter ( ) {
let ( key , ( value , rc ) ) = i ;
if rc > 0 {
assert! ( rc = = 1 ) ;
batch . put ( & key . bytes ( ) , & value ) . expect ( " Low-level database error. Some issue with your hard disk? " ) ;
inserts + = 1 ;
}
if rc < 0 {
assert! ( rc = = - 1 ) ;
deletes + = 1 ;
}
}
trace! ( " commit: Inserted {}, Deleted {} nodes " , inserts , deletes ) ;
inserts + deletes
}
/// Just commit the overlay into the backing DB.
fn commit_without_counters ( & mut self ) -> Result < u32 , UtilError > {
let batch = DBTransaction ::new ( ) ;
let ret = Self ::batch_overlay_insertions ( & mut self . overlay , & batch ) ;
try ! ( self . backing . write ( batch ) ) ;
Ok ( ret as u32 )
2016-02-05 01:49:06 +01:00
}
2016-01-18 12:41:31 +01:00
2016-03-06 21:57:55 +01:00
fn morph_key ( key : & H256 , index : u8 ) -> Bytes {
let mut ret = key . bytes ( ) . to_owned ( ) ;
ret . push ( index ) ;
ret
2016-03-04 20:19:36 +01:00
}
2016-03-06 21:57:55 +01:00
// The next three are valid only as long as there is an insert operation of `key` in the journal.
2016-03-06 22:39:04 +01:00
fn set_already_in ( batch : & DBTransaction , key : & H256 ) { batch . put ( & Self ::morph_key ( key , 0 ) , & [ 1 u8 ] ) . expect ( " Low-level database error. Some issue with your hard disk? " ) ; }
fn reset_already_in ( batch : & DBTransaction , key : & H256 ) { batch . delete ( & Self ::morph_key ( key , 0 ) ) . expect ( " Low-level database error. Some issue with your hard disk? " ) ; }
fn is_already_in ( backing : & Database , key : & H256 ) -> bool {
2016-03-06 21:57:55 +01:00
backing . get ( & Self ::morph_key ( key , 0 ) ) . expect ( " Low-level database error. Some issue with your hard disk? " ) . is_some ( )
}
2016-03-07 14:33:00 +01:00
fn insert_keys ( inserts : & [ ( H256 , Bytes ) ] , backing : & Database , counters : & mut HashMap < H256 , i32 > , batch : & DBTransaction ) {
2016-03-06 21:57:55 +01:00
for & ( ref h , ref d ) in inserts {
if let Some ( c ) = counters . get_mut ( h ) {
// already counting. increment.
* c + = 1 ;
continue ;
2016-03-04 20:19:36 +01:00
}
2016-03-06 21:57:55 +01:00
// this is the first entry for this node in the journal.
if backing . get ( & h . bytes ( ) ) . expect ( " Low-level database error. Some issue with your hard disk? " ) . is_some ( ) {
// already in the backing DB. start counting, and remember it was already in.
Self ::set_already_in ( batch , & h ) ;
counters . insert ( h . clone ( ) , 1 ) ;
continue ;
2016-03-04 20:19:36 +01:00
}
2016-03-06 21:57:55 +01:00
// Gets removed when a key leaves the journal, so should never be set when we're placing a new key.
//Self::reset_already_in(&h);
assert! ( ! Self ::is_already_in ( backing , & h ) ) ;
2016-03-06 22:05:12 +01:00
batch . put ( & h . bytes ( ) , d ) . expect ( " Low-level database error. Some issue with your hard disk? " ) ;
2016-03-04 20:19:36 +01:00
}
}
2016-03-07 14:33:00 +01:00
fn replay_keys ( inserts : & [ H256 ] , backing : & Database , counters : & mut HashMap < H256 , i32 > ) {
2016-03-07 10:56:39 +01:00
trace! ( " replay_keys: inserts={:?}, counters={:?} " , inserts , counters ) ;
2016-03-06 21:57:55 +01:00
for h in inserts {
if let Some ( c ) = counters . get_mut ( h ) {
// already counting. increment.
* c + = 1 ;
continue ;
}
// this is the first entry for this node in the journal.
// it is initialised to 1 if it was already in.
2016-03-07 06:58:43 +01:00
if Self ::is_already_in ( backing , h ) {
2016-03-07 10:56:39 +01:00
trace! ( " replace_keys: Key {} was already in! " , h ) ;
2016-03-07 06:58:43 +01:00
counters . insert ( h . clone ( ) , 1 ) ;
}
2016-03-06 21:57:55 +01:00
}
2016-03-07 10:56:39 +01:00
trace! ( " replay_keys: (end) counters={:?} " , counters ) ;
2016-03-06 21:57:55 +01:00
}
2016-03-06 22:39:04 +01:00
fn kill_keys ( deletes : Vec < H256 > , counters : & mut HashMap < H256 , i32 > , batch : & DBTransaction ) {
2016-03-06 21:57:55 +01:00
for h in deletes . into_iter ( ) {
let mut n : Option < i32 > = None ;
if let Some ( c ) = counters . get_mut ( & h ) {
if * c > 1 {
* c - = 1 ;
continue ;
} else {
n = Some ( * c ) ;
}
}
2016-03-07 14:33:00 +01:00
match n {
Some ( i ) if i = = 1 = > {
2016-03-06 21:57:55 +01:00
counters . remove ( & h ) ;
Self ::reset_already_in ( batch , & h ) ;
}
2016-03-07 14:33:00 +01:00
None = > {
2016-03-06 21:57:55 +01:00
// Gets removed when moving from 1 to 0 additional refs. Should never be here at 0 additional refs.
//assert!(!Self::is_already_in(db, &h));
2016-03-06 22:05:12 +01:00
batch . delete ( & h . bytes ( ) ) . expect ( " Low-level database error. Some issue with your hard disk? " ) ;
2016-03-06 21:57:55 +01:00
}
_ = > panic! ( " Invalid value in counters: {:?} " , n ) ,
}
}
2016-03-04 20:19:36 +01:00
}
2016-01-18 12:41:31 +01:00
/// Commit all recent insert operations and historical removals from the old era
/// to the backing database.
2016-03-06 22:39:04 +01:00
fn commit_with_counters ( & mut self , now : u64 , id : & H256 , end : Option < ( u64 , H256 ) > ) -> Result < u32 , UtilError > {
2016-03-07 14:33:00 +01:00
// journal format:
2016-01-18 12:41:31 +01:00
// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
// [era, 1] => [ id, [insert_0, ...], [remove_0, ...] ]
// [era, n] => [ ... ]
2016-02-05 01:49:06 +01:00
// TODO: store reclaim_period.
2016-01-18 12:41:31 +01:00
2016-03-06 21:57:55 +01:00
// When we make a new commit, we make a journal of all blocks in the recent history and record
// all keys that were inserted and deleted. The journal is ordered by era; multiple commits can
// share the same era. This forms a data structure similar to a queue but whose items are tuples.
// By the time comes to remove a tuple from the queue (i.e. then the era passes from recent history
// into ancient history) then only one commit from the tuple is considered canonical. This commit
// is kept in the main backing database, whereas any others from the same era are reverted.
2016-03-07 14:33:00 +01:00
//
2016-03-06 21:57:55 +01:00
// It is possible that a key, properly available in the backing database be deleted and re-inserted
// in the recent history queue, yet have both operations in commits that are eventually non-canonical.
// To avoid the original, and still required, key from being deleted, we maintain a reference count
// which includes an original key, if any.
2016-03-07 14:33:00 +01:00
//
2016-03-06 21:57:55 +01:00
// The semantics of the `counter` are:
// insert key k:
// counter already contains k: count += 1
// counter doesn't contain k:
// backing db contains k: count = 1
// backing db doesn't contain k: insert into backing db, count = 0
// delete key k:
2016-03-07 14:33:00 +01:00
// counter contains k (count is asserted to be non-zero):
2016-03-06 21:57:55 +01:00
// count > 1: counter -= 1
// count == 1: remove counter
// count == 0: remove key from backing db
// counter doesn't contain k: remove key from backing db
2016-02-05 22:54:33 +01:00
//
2016-03-06 21:57:55 +01:00
// Practically, this means that for each commit block turning from recent to ancient we do the
// following:
// is_canonical:
// inserts: Ignored (left alone in the backing database).
// deletes: Enacted; however, recent history queue is checked for ongoing references. This is
// reduced as a preference to deletion from the backing database.
// !is_canonical:
// inserts: Reverted; however, recent history queue is checked for ongoing references. This is
// reduced as a preference to deletion from the backing database.
// deletes: Ignored (they were never inserted).
2016-02-07 21:18:51 +01:00
//
2016-01-18 12:41:31 +01:00
// record new commit's details.
2016-03-07 14:33:00 +01:00
trace! ( " commit: #{} ({}), end era: {:?} " , now , id , end ) ;
2016-03-06 22:39:04 +01:00
let mut counters = self . counters . as_ref ( ) . unwrap ( ) . write ( ) . unwrap ( ) ;
2016-03-06 22:43:21 +01:00
let batch = DBTransaction ::new ( ) ;
2016-01-18 12:41:31 +01:00
{
let mut index = 0 usize ;
let mut last ;
2016-03-06 21:57:55 +01:00
while try ! ( self . backing . get ( {
2016-03-06 22:43:21 +01:00
let mut r = RlpStream ::new_list ( 3 ) ;
2016-03-06 21:57:55 +01:00
r . append ( & now ) ;
r . append ( & index ) ;
2016-03-06 22:39:04 +01:00
r . append ( & & PADDING [ .. ] ) ;
2016-03-06 21:57:55 +01:00
last = r . drain ( ) ;
& last
} ) ) . is_some ( ) {
2016-01-18 12:41:31 +01:00
index + = 1 ;
}
2016-03-06 21:57:55 +01:00
let drained = self . overlay . drain ( ) ;
let removes : Vec < H256 > = drained
. iter ( )
2016-03-07 14:33:00 +01:00
. filter_map ( | ( k , & ( _ , c ) ) | if c < 0 { Some ( k . clone ( ) ) } else { None } )
2016-03-06 21:57:55 +01:00
. collect ( ) ;
let inserts : Vec < ( H256 , Bytes ) > = drained
. into_iter ( )
. filter_map ( | ( k , ( v , r ) ) | if r > 0 { assert! ( r = = 1 ) ; Some ( ( k , v ) ) } else { assert! ( r > = - 1 ) ; None } )
. collect ( ) ;
2016-01-18 12:41:31 +01:00
let mut r = RlpStream ::new_list ( 3 ) ;
r . append ( id ) ;
2016-03-06 21:57:55 +01:00
// Process the new inserts.
// We use the inserts for three things. For each:
// - we place into the backing DB or increment the counter if already in;
// - we note in the backing db that it was already in;
// - we write the key into our journal for this block;
r . begin_list ( inserts . len ( ) ) ;
inserts . iter ( ) . foreach ( | & ( k , _ ) | { r . append ( & k ) ; } ) ;
2016-02-04 02:40:35 +01:00
r . append ( & removes ) ;
2016-03-06 21:57:55 +01:00
Self ::insert_keys ( & inserts , & self . backing , & mut counters , & batch ) ;
2016-02-04 21:33:30 +01:00
try ! ( batch . put ( & last , r . as_raw ( ) ) ) ;
2016-03-06 22:43:21 +01:00
try ! ( batch . put ( & LATEST_ERA_KEY , & encode ( & now ) ) ) ;
2016-01-18 12:41:31 +01:00
}
// apply old commits' details
2016-01-18 13:30:01 +01:00
if let Some ( ( end_era , canon_id ) ) = end {
let mut index = 0 usize ;
let mut last ;
while let Some ( rlp_data ) = try ! ( self . backing . get ( {
2016-03-06 22:43:21 +01:00
let mut r = RlpStream ::new_list ( 3 ) ;
2016-01-18 13:30:01 +01:00
r . append ( & end_era ) ;
r . append ( & index ) ;
2016-03-06 22:39:04 +01:00
r . append ( & & PADDING [ .. ] ) ;
2016-01-18 15:47:50 +01:00
last = r . drain ( ) ;
2016-01-18 13:30:01 +01:00
& last
} ) ) {
2016-02-07 21:18:51 +01:00
let rlp = Rlp ::new ( & rlp_data ) ;
2016-03-06 21:57:55 +01:00
let inserts : Vec < H256 > = rlp . val_at ( 1 ) ;
let deletes : Vec < H256 > = rlp . val_at ( 2 ) ;
2016-02-07 21:18:51 +01:00
// Collect keys to be removed. These are removed keys for canonical block, inserted for non-canonical
2016-03-06 21:57:55 +01:00
Self ::kill_keys ( if canon_id = = rlp . val_at ( 0 ) { deletes } else { inserts } , & mut counters , & batch ) ;
2016-02-04 21:33:30 +01:00
try ! ( batch . delete ( & last ) ) ;
2016-01-18 13:30:01 +01:00
index + = 1 ;
2016-01-18 12:41:31 +01:00
}
2016-03-11 12:54:48 +01:00
trace! ( " OptionOneDB: delete journal for time #{}.{}, (canon was {}) " , end_era , index , canon_id ) ;
2016-01-18 12:41:31 +01:00
}
2016-02-04 21:33:30 +01:00
try ! ( self . backing . write ( batch ) ) ;
2016-03-11 12:54:48 +01:00
// trace!("OptionOneDB::commit() deleted {} nodes", deletes);
2016-03-06 21:57:55 +01:00
Ok ( 0 )
2016-02-05 22:54:33 +01:00
}
2016-02-04 02:40:35 +01:00
fn payload ( & self , key : & H256 ) -> Option < Bytes > {
self . backing . get ( & key . bytes ( ) ) . expect ( " Low-level database error. Some issue with your hard disk? " ) . map ( | v | v . to_vec ( ) )
2016-01-18 12:41:31 +01:00
}
2016-02-04 21:33:30 +01:00
2016-03-06 22:39:04 +01:00
fn read_counters ( db : & Database ) -> HashMap < H256 , i32 > {
2016-03-06 21:57:55 +01:00
let mut counters = HashMap ::new ( ) ;
2016-03-06 22:39:04 +01:00
if let Some ( val ) = db . get ( & LATEST_ERA_KEY ) . expect ( " Low-level database error. " ) {
2016-03-07 08:01:14 +01:00
let mut era = decode ::< u64 > ( & val ) ;
2016-02-04 21:33:30 +01:00
loop {
let mut index = 0 usize ;
while let Some ( rlp_data ) = db . get ( {
2016-03-06 22:43:21 +01:00
let mut r = RlpStream ::new_list ( 3 ) ;
2016-02-04 21:33:30 +01:00
r . append ( & era ) ;
r . append ( & index ) ;
2016-03-06 22:39:04 +01:00
r . append ( & & PADDING [ .. ] ) ;
2016-02-04 21:33:30 +01:00
& r . drain ( )
} ) . expect ( " Low-level database error. " ) {
2016-03-07 10:56:39 +01:00
trace! ( " read_counters: era={}, index={} " , era , index ) ;
2016-02-04 21:33:30 +01:00
let rlp = Rlp ::new ( & rlp_data ) ;
2016-03-06 21:57:55 +01:00
let inserts : Vec < H256 > = rlp . val_at ( 1 ) ;
Self ::replay_keys ( & inserts , db , & mut counters ) ;
2016-02-04 21:33:30 +01:00
index + = 1 ;
} ;
2016-03-07 08:01:14 +01:00
if index = = 0 | | era = = 0 {
2016-02-04 21:33:30 +01:00
break ;
}
2016-03-07 08:01:14 +01:00
era - = 1 ;
2016-02-04 21:33:30 +01:00
}
}
2016-03-06 21:57:55 +01:00
trace! ( " Recovered {} counters " , counters . len ( ) ) ;
counters
2016-02-04 21:33:30 +01:00
}
2016-03-11 12:54:48 +01:00
}
2016-03-06 22:39:04 +01:00
2016-03-11 12:54:48 +01:00
impl HashDB for OptionOneDB {
2016-03-07 14:33:00 +01:00
fn keys ( & self ) -> HashMap < H256 , i32 > {
2016-02-04 02:40:35 +01:00
let mut ret : HashMap < H256 , i32 > = HashMap ::new ( ) ;
2016-03-06 22:39:04 +01:00
for ( key , _ ) in self . backing . iter ( ) {
2016-02-04 02:40:35 +01:00
let h = H256 ::from_slice ( key . deref ( ) ) ;
ret . insert ( h , 1 ) ;
}
for ( key , refs ) in self . overlay . keys ( ) . into_iter ( ) {
let refs = * ret . get ( & key ) . unwrap_or ( & 0 ) + refs ;
ret . insert ( key , refs ) ;
}
ret
}
2016-03-07 14:33:00 +01:00
fn lookup ( & self , key : & H256 ) -> Option < & [ u8 ] > {
2016-02-04 02:40:35 +01:00
let k = self . overlay . raw ( key ) ;
match k {
Some ( & ( ref d , rc ) ) if rc > 0 = > Some ( d ) ,
_ = > {
if let Some ( x ) = self . payload ( key ) {
Some ( & self . overlay . denote ( key , x ) . 0 )
}
else {
None
}
}
}
}
2016-03-07 14:33:00 +01:00
fn exists ( & self , key : & H256 ) -> bool {
2016-02-04 02:40:35 +01:00
self . lookup ( key ) . is_some ( )
}
2016-03-07 14:33:00 +01:00
fn insert ( & mut self , value : & [ u8 ] ) -> H256 {
2016-02-04 21:33:30 +01:00
self . overlay . insert ( value )
2016-02-04 02:40:35 +01:00
}
fn emplace ( & mut self , key : H256 , value : Bytes ) {
2016-03-07 14:33:00 +01:00
self . overlay . emplace ( key , value ) ;
2016-02-04 02:40:35 +01:00
}
2016-03-07 14:33:00 +01:00
fn kill ( & mut self , key : & H256 ) {
self . overlay . kill ( key ) ;
2016-02-04 21:33:30 +01:00
}
2016-01-18 12:41:31 +01:00
}
2016-01-18 13:30:01 +01:00
2016-03-11 12:54:48 +01:00
impl JournalDB for OptionOneDB {
2016-03-11 13:19:10 +01:00
fn spawn ( & self ) -> Box < Box < JournalDB > > {
2016-03-11 12:54:48 +01:00
Box ::new ( OptionOneDB {
overlay : MemoryDB ::new ( ) ,
backing : self . backing . clone ( ) ,
counters : self . counters . clone ( ) ,
} )
}
fn mem_used ( & self ) -> usize {
self . overlay . mem_used ( ) + match self . counters {
Some ( ref c ) = > c . read ( ) . unwrap ( ) . heap_size_of_children ( ) ,
None = > 0
}
}
fn is_empty ( & self ) -> bool {
self . backing . get ( & LATEST_ERA_KEY ) . expect ( " Low level database error " ) . is_none ( )
}
fn commit ( & mut self , now : u64 , id : & H256 , end : Option < ( u64 , H256 ) > ) -> Result < u32 , UtilError > {
let have_counters = self . counters . is_some ( ) ;
if have_counters {
self . commit_with_counters ( now , id , end )
} else {
self . commit_without_counters ( )
}
}
}
2016-01-18 13:30:01 +01:00
#[ cfg(test) ]
mod tests {
use common ::* ;
use super ::* ;
use hashdb ::* ;
2016-03-06 21:57:55 +01:00
#[ test ]
fn insert_same_in_fork ( ) {
// history is 1
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new_temp ( ) ;
2016-03-06 21:57:55 +01:00
let x = jdb . insert ( b " X " ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , None ) . unwrap ( ) ;
jdb . commit ( 3 , & b " 1002a " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . commit ( 4 , & b " 1003a " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . remove ( & x ) ;
jdb . commit ( 3 , & b " 1002b " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
let x = jdb . insert ( b " X " ) ;
jdb . commit ( 4 , & b " 1003b " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . commit ( 5 , & b " 1004a " . sha3 ( ) , Some ( ( 3 , b " 1002a " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . commit ( 6 , & b " 1005a " . sha3 ( ) , Some ( ( 4 , b " 1003a " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . exists ( & x ) ) ;
}
2016-01-18 13:30:01 +01:00
#[ test ]
fn long_history ( ) {
// history is 3
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new_temp ( ) ;
2016-01-18 13:30:01 +01:00
let h = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . exists ( & h ) ) ;
jdb . remove ( & h ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . exists ( & h ) ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . exists ( & h ) ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( jdb . exists ( & h ) ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( ! jdb . exists ( & h ) ) ;
}
#[ test ]
fn complex ( ) {
// history is 1
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new_temp ( ) ;
2016-01-18 13:30:01 +01:00
let foo = jdb . insert ( b " foo " ) ;
let bar = jdb . insert ( b " bar " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
assert! ( jdb . exists ( & bar ) ) ;
jdb . remove ( & foo ) ;
jdb . remove ( & bar ) ;
let baz = jdb . insert ( b " baz " ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( jdb . exists ( & foo ) ) ;
assert! ( jdb . exists ( & bar ) ) ;
assert! ( jdb . exists ( & baz ) ) ;
let foo = jdb . insert ( b " foo " ) ;
jdb . remove ( & baz ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( jdb . exists ( & foo ) ) ;
assert! ( ! jdb . exists ( & bar ) ) ;
assert! ( jdb . exists ( & baz ) ) ;
jdb . remove ( & foo ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( jdb . exists ( & foo ) ) ;
assert! ( ! jdb . exists ( & bar ) ) ;
assert! ( ! jdb . exists ( & baz ) ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 3 , b " 3 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( ! jdb . exists ( & foo ) ) ;
assert! ( ! jdb . exists ( & bar ) ) ;
assert! ( ! jdb . exists ( & baz ) ) ;
}
#[ test ]
fn fork ( ) {
// history is 1
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new_temp ( ) ;
2016-01-18 13:30:01 +01:00
let foo = jdb . insert ( b " foo " ) ;
let bar = jdb . insert ( b " bar " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
assert! ( jdb . exists ( & bar ) ) ;
jdb . remove ( & foo ) ;
let baz = jdb . insert ( b " baz " ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
jdb . remove ( & bar ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( jdb . exists ( & foo ) ) ;
assert! ( jdb . exists ( & bar ) ) ;
assert! ( jdb . exists ( & baz ) ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , Some ( ( 1 , b " 1b " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-01-18 13:30:01 +01:00
assert! ( jdb . exists ( & foo ) ) ;
assert! ( ! jdb . exists ( & baz ) ) ;
assert! ( ! jdb . exists ( & bar ) ) ;
}
2016-02-04 21:33:30 +01:00
#[ test ]
fn overwrite ( ) {
// history is 1
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new_temp ( ) ;
2016-02-04 21:33:30 +01:00
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . insert ( b " foo " ) ;
assert! ( jdb . exists ( & foo ) ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
jdb . commit ( 3 , & b " 2 " . sha3 ( ) , Some ( ( 0 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
}
2016-02-05 22:54:33 +01:00
#[ test ]
fn fork_same_key ( ) {
// history is 1
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new_temp ( ) ;
2016-02-05 22:54:33 +01:00
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
jdb . commit ( 2 , & b " 2a " . sha3 ( ) , Some ( ( 1 , b " 1a " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
}
2016-03-06 22:39:04 +01:00
#[ test ]
fn reopen ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
let bar = H256 ::random ( ) ;
let foo = {
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
// history is 1
let foo = jdb . insert ( b " foo " ) ;
jdb . emplace ( bar . clone ( ) , b " bar " . to_vec ( ) ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
foo
} ;
{
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
}
{
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
assert! ( jdb . exists ( & foo ) ) ;
assert! ( jdb . exists ( & bar ) ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( ! jdb . exists ( & foo ) ) ;
}
}
#[ test ]
fn reopen_remove ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
let foo = {
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
// history is 1
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-07 09:10:02 +01:00
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
// foo is ancient history.
2016-03-06 22:39:04 +01:00
jdb . insert ( b " foo " ) ;
2016-03-07 09:10:02 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-06 22:39:04 +01:00
foo
} ;
{
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
jdb . remove ( & foo ) ;
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-07 09:10:02 +01:00
assert! ( jdb . exists ( & foo ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 3 , b " 3 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . commit ( 5 , & b " 5 " . sha3 ( ) , Some ( ( 4 , b " 4 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-06 22:39:04 +01:00
assert! ( ! jdb . exists ( & foo ) ) ;
}
}
#[ test ]
fn reopen_fork ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
let ( foo , bar , baz ) = {
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
// history is 1
let foo = jdb . insert ( b " foo " ) ;
let bar = jdb . insert ( b " bar " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
jdb . remove ( & foo ) ;
let baz = jdb . insert ( b " baz " ) ;
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
jdb . remove ( & bar ) ;
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
( foo , bar , baz )
} ;
{
2016-03-11 12:54:48 +01:00
let mut jdb = OptionOneDB ::new ( dir . to_str ( ) . unwrap ( ) ) ;
2016-03-06 22:39:04 +01:00
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , Some ( ( 1 , b " 1b " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . exists ( & foo ) ) ;
assert! ( ! jdb . exists ( & baz ) ) ;
assert! ( ! jdb . exists ( & bar ) ) ;
}
}
2016-01-18 13:30:01 +01:00
}