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-04-06 10:07:24 +02:00
//! `JournalDB` over in-memory overlay
2016-01-18 12:41:31 +01:00
use common ::* ;
use rlp ::* ;
use hashdb ::* ;
2016-02-04 02:40:35 +01:00
use memorydb ::* ;
2016-07-07 09:37:31 +02:00
use misc ::RwLockable ;
2016-06-18 17:58:28 +02:00
use super ::{ DB_PREFIX_LEN , LATEST_ERA_KEY , VERSION_KEY } ;
2016-02-18 03:46:24 +01:00
use kvdb ::{ Database , DBTransaction , DatabaseConfig } ;
2016-01-31 17:01:36 +01:00
#[ cfg(test) ]
use std ::env ;
2016-03-11 22:43:59 +01:00
use super ::JournalDB ;
2016-01-18 12:41:31 +01:00
2016-04-06 10:07:24 +02:00
/// Implementation of the `JournalDB` trait for a disk-backed database with a memory overlay
2016-03-04 20:19:36 +01:00
/// and, possibly, latent-removal semantics.
///
2016-04-06 10:07:24 +02:00
/// Like `OverlayDB`, there is a memory overlay; `commit()` must be called in order to
/// write operations out to disk. Unlike `OverlayDB`, `remove()` operations do not take effect
2016-01-18 12:41:31 +01:00
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
/// the removals actually take effect.
2016-03-07 07:06:55 +01:00
///
2016-03-12 10:41:35 +01:00
/// There are two memory overlays:
/// - Transaction overlay contains current transaction data. It is merged with with history
2016-03-07 07:06:55 +01:00
/// overlay on each `commit()`
2016-03-12 10:41:35 +01:00
/// - History overlay contains all data inserted during the history period. When the node
2016-03-07 07:06:55 +01:00
/// in the overlay becomes ancient it is written to disk on `commit()`
///
2016-03-12 10:41:35 +01:00
/// There is also a journal maintained in memory and on the disk as well which lists insertions
/// and removals for each commit during the history period. This is used to track
2016-03-07 07:06:55 +01:00
/// data nodes that go out of history scope and must be written to disk.
///
/// Commit workflow:
2016-03-08 18:35:25 +01:00
/// 1. Create a new journal record from the transaction overlay.
/// 2. Inseart each node from the transaction overlay into the History overlay increasing reference
2016-03-07 07:06:55 +01:00
/// count if it is already there. Note that the reference counting is managed by `MemoryDB`
2016-03-08 18:35:25 +01:00
/// 3. Clear the transaction overlay.
/// 4. For a canonical journal record that becomes ancient inserts its insertions into the disk DB
/// 5. For each journal record that goes out of the history scope (becomes ancient) remove its
2016-03-12 10:41:35 +01:00
/// insertions from the history overlay, decreasing the reference counter and removing entry if
2016-03-07 07:06:55 +01:00
/// if reaches zero.
2016-03-12 10:41:35 +01:00
/// 6. For a canonical journal record that becomes ancient delete its removals from the disk only if
2016-03-07 07:06:55 +01:00
/// the removed key is not present in the history overlay.
2016-03-08 18:35:25 +01:00
/// 7. Delete ancient record from memory and disk.
2016-03-13 00:20:31 +01:00
2016-03-12 11:19:42 +01:00
pub struct OverlayRecentDB {
2016-03-06 17:28:50 +01:00
transaction_overlay : MemoryDB ,
2016-02-18 03:46:24 +01:00
backing : Arc < Database > ,
2016-03-11 22:43:59 +01:00
journal_overlay : Arc < RwLock < JournalOverlay > > ,
2016-03-06 17:28:50 +01:00
}
2016-03-11 11:52:11 +01:00
#[ derive(PartialEq) ]
2016-03-06 17:28:50 +01:00
struct JournalOverlay {
backing_overlay : MemoryDB ,
2016-03-11 11:52:11 +01:00
journal : HashMap < u64 , Vec < JournalEntry > > ,
2016-03-13 11:50:09 +01:00
latest_era : Option < u64 > ,
2016-03-06 17:28:50 +01:00
}
2016-03-11 11:52:11 +01:00
#[ derive(PartialEq) ]
2016-03-06 17:28:50 +01:00
struct JournalEntry {
id : H256 ,
insertions : Vec < H256 > ,
deletions : Vec < H256 > ,
}
impl HeapSizeOf for JournalEntry {
fn heap_size_of_children ( & self ) -> usize {
self . insertions . heap_size_of_children ( ) + self . deletions . heap_size_of_children ( )
}
2016-01-18 12:41:31 +01:00
}
2016-03-12 11:19:42 +01:00
impl Clone for OverlayRecentDB {
fn clone ( & self ) -> OverlayRecentDB {
OverlayRecentDB {
2016-03-27 14:35:27 +02:00
transaction_overlay : self . transaction_overlay . clone ( ) ,
2016-02-04 21:33:30 +01:00
backing : self . backing . clone ( ) ,
2016-03-06 17:28:50 +01:00
journal_overlay : self . journal_overlay . clone ( ) ,
2016-02-04 21:33:30 +01:00
}
}
}
2016-04-12 03:42:50 +02:00
const DB_VERSION : u32 = 0x203 ;
2016-02-18 03:46:24 +01:00
const PADDING : [ u8 ; 10 ] = [ 0 u8 ; 10 ] ;
2016-02-04 21:33:30 +01:00
2016-03-12 11:19:42 +01:00
impl OverlayRecentDB {
2016-02-18 03:46:24 +01:00
/// Create a new instance from file
2016-06-27 13:23:50 +02:00
pub fn new ( path : & str , config : DatabaseConfig ) -> OverlayRecentDB {
Self ::from_prefs ( path , config )
2016-03-04 20:19:36 +01:00
}
/// Create a new instance from file
2016-06-27 13:23:50 +02:00
pub fn from_prefs ( path : & str , config : DatabaseConfig ) -> OverlayRecentDB {
let opts = config . prefix ( DB_PREFIX_LEN ) ;
2016-02-18 21:15:56 +01:00
let backing = Database ::open ( & opts , path ) . unwrap_or_else ( | e | {
2016-02-18 03:46:24 +01:00
panic! ( " Error opening state db: {} " , e ) ;
} ) ;
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-11 22:43:59 +01:00
Ok ( Some ( DB_VERSION ) ) = > { }
2016-04-12 03:42:50 +02:00
v = > panic! ( " Incompatible DB version, expected {} , got {:?} ; to resolve, remove {} and restart. " , DB_VERSION , v , path )
2016-02-05 01:49:06 +01:00
}
} else {
2016-03-11 22:43:59 +01:00
backing . put ( & VERSION_KEY , & encode ( & DB_VERSION ) ) . expect ( " Error writing version to database " ) ;
2016-02-05 01:49:06 +01:00
}
2016-03-04 20:19:36 +01:00
2016-03-12 11:19:42 +01:00
let journal_overlay = Arc ::new ( RwLock ::new ( OverlayRecentDB ::read_overlay ( & backing ) ) ) ;
OverlayRecentDB {
2016-03-06 17:28:50 +01:00
transaction_overlay : MemoryDB ::new ( ) ,
2016-02-18 03:46:24 +01:00
backing : Arc ::new ( backing ) ,
2016-03-06 17:28:50 +01:00
journal_overlay : journal_overlay ,
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-12 11:19:42 +01:00
pub fn new_temp ( ) -> OverlayRecentDB {
2016-01-18 12:41:31 +01:00
let mut dir = env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
Self ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) )
2016-01-18 12:41:31 +01:00
}
2016-03-11 22:43:59 +01:00
#[ cfg(test) ]
fn can_reconstruct_refs ( & self ) -> bool {
let reconstructed = Self ::read_overlay ( & self . backing ) ;
2016-07-07 09:37:31 +02:00
let journal_overlay = self . journal_overlay . unwrapped_read ( ) ;
2016-03-11 22:43:59 +01:00
* journal_overlay = = reconstructed
2016-02-05 01:49:06 +01:00
}
2016-01-18 12:41:31 +01:00
2016-03-11 22:43:59 +01:00
fn payload ( & self , key : & H256 ) -> Option < Bytes > {
2016-07-06 11:23:29 +02:00
self . backing . get ( key ) . expect ( " Low-level database error. Some issue with your hard disk? " ) . map ( | v | v . to_vec ( ) )
2016-03-04 20:19:36 +01:00
}
2016-03-11 22:43:59 +01:00
fn read_overlay ( db : & Database ) -> JournalOverlay {
let mut journal = HashMap ::new ( ) ;
let mut overlay = MemoryDB ::new ( ) ;
let mut count = 0 ;
2016-03-13 11:50:09 +01:00
let mut latest_era = None ;
2016-03-11 22:43:59 +01:00
if let Some ( val ) = db . get ( & LATEST_ERA_KEY ) . expect ( " Low-level database error. " ) {
2016-03-13 11:50:09 +01:00
let mut era = decode ::< u64 > ( & val ) ;
latest_era = Some ( era ) ;
2016-03-11 22:43:59 +01:00
loop {
let mut index = 0 usize ;
while let Some ( rlp_data ) = db . get ( {
let mut r = RlpStream ::new_list ( 3 ) ;
r . append ( & era ) ;
r . append ( & index ) ;
r . append ( & & PADDING [ .. ] ) ;
& r . drain ( )
} ) . expect ( " Low-level database error. " ) {
trace! ( " read_overlay: era={}, index={} " , era , index ) ;
let rlp = Rlp ::new ( & rlp_data ) ;
let id : H256 = rlp . val_at ( 0 ) ;
let insertions = rlp . at ( 1 ) ;
let deletions : Vec < H256 > = rlp . val_at ( 2 ) ;
let mut inserted_keys = Vec ::new ( ) ;
for r in insertions . iter ( ) {
let k : H256 = r . val_at ( 0 ) ;
let v : Bytes = r . val_at ( 1 ) ;
overlay . emplace ( k . clone ( ) , v ) ;
inserted_keys . push ( k ) ;
count + = 1 ;
}
journal . entry ( era ) . or_insert_with ( Vec ::new ) . push ( JournalEntry {
id : id ,
insertions : inserted_keys ,
deletions : deletions ,
} ) ;
index + = 1 ;
} ;
if index = = 0 | | era = = 0 {
break ;
}
era - = 1 ;
2016-03-04 20:19:36 +01:00
}
}
2016-03-11 22:43:59 +01:00
trace! ( " Recovered {} overlay entries, {} journal entries " , count , journal . len ( ) ) ;
JournalOverlay { backing_overlay : overlay , journal : journal , latest_era : latest_era }
2016-03-04 20:19:36 +01:00
}
2016-03-11 22:43:59 +01:00
}
2016-03-04 20:19:36 +01:00
2016-03-12 11:19:42 +01:00
impl JournalDB for OverlayRecentDB {
2016-03-28 09:42:50 +02:00
fn boxed_clone ( & self ) -> Box < JournalDB > {
2016-03-11 22:43:59 +01:00
Box ::new ( self . clone ( ) )
}
fn mem_used ( & self ) -> usize {
let mut mem = self . transaction_overlay . mem_used ( ) ;
2016-07-07 09:37:31 +02:00
let overlay = self . journal_overlay . unwrapped_read ( ) ;
2016-03-11 22:43:59 +01:00
mem + = overlay . backing_overlay . mem_used ( ) ;
mem + = overlay . journal . heap_size_of_children ( ) ;
mem
2016-03-04 20:19:36 +01:00
}
2016-03-11 22:43:59 +01:00
fn is_empty ( & self ) -> bool {
self . backing . get ( & LATEST_ERA_KEY ) . expect ( " Low level database error " ) . is_none ( )
}
2016-07-07 09:37:31 +02:00
fn latest_era ( & self ) -> Option < u64 > { self . journal_overlay . unwrapped_read ( ) . latest_era }
2016-04-12 00:51:14 +02:00
2016-03-11 22:43:59 +01:00
fn commit ( & mut self , now : u64 , id : & H256 , end : Option < ( u64 , H256 ) > ) -> Result < u32 , UtilError > {
2016-01-18 12:41:31 +01:00
// record new commit's details.
2016-03-04 22:54:59 +01:00
trace! ( " commit: #{} ({}), end era: {:?} " , now , id , end ) ;
2016-07-07 09:37:31 +02:00
let mut journal_overlay = self . journal_overlay . unwrapped_write ( ) ;
2016-02-18 03:46:24 +01:00
let batch = DBTransaction ::new ( ) ;
2016-01-18 12:41:31 +01:00
{
let mut r = RlpStream ::new_list ( 3 ) ;
2016-03-06 17:28:50 +01:00
let mut tx = self . transaction_overlay . drain ( ) ;
let inserted_keys : Vec < _ > = tx . iter ( ) . filter_map ( | ( k , & ( _ , c ) ) | if c > 0 { Some ( k . clone ( ) ) } else { None } ) . collect ( ) ;
let removed_keys : Vec < _ > = tx . iter ( ) . filter_map ( | ( k , & ( _ , c ) ) | if c < 0 { Some ( k . clone ( ) ) } else { None } ) . collect ( ) ;
2016-03-12 10:41:35 +01:00
// Increase counter for each inserted key no matter if the block is canonical or not.
2016-03-06 17:28:50 +01:00
let insertions = tx . drain ( ) . filter_map ( | ( k , ( v , c ) ) | if c > 0 { Some ( ( k , v ) ) } else { None } ) ;
2016-01-18 12:41:31 +01:00
r . append ( id ) ;
2016-03-06 17:28:50 +01:00
r . begin_list ( inserted_keys . len ( ) ) ;
for ( k , v ) in insertions {
r . begin_list ( 2 ) ;
r . append ( & k ) ;
r . append ( & v ) ;
journal_overlay . backing_overlay . emplace ( k , v ) ;
}
r . append ( & removed_keys ) ;
2016-03-08 18:35:25 +01:00
let mut k = RlpStream ::new_list ( 3 ) ;
2016-03-12 10:41:35 +01:00
let index = journal_overlay . journal . get ( & now ) . map_or ( 0 , | j | j . len ( ) ) ;
2016-03-08 18:35:25 +01:00
k . append ( & now ) ;
k . append ( & index ) ;
k . append ( & & PADDING [ .. ] ) ;
try ! ( batch . put ( & k . drain ( ) , r . as_raw ( ) ) ) ;
2016-03-13 11:50:09 +01:00
if journal_overlay . latest_era . map_or ( true , | e | now > e ) {
2016-03-11 11:52:11 +01:00
try ! ( batch . put ( & LATEST_ERA_KEY , & encode ( & now ) ) ) ;
2016-03-13 11:50:09 +01:00
journal_overlay . latest_era = Some ( now ) ;
2016-03-11 11:52:11 +01:00
}
2016-03-08 18:35:25 +01:00
journal_overlay . journal . entry ( now ) . or_insert_with ( Vec ::new ) . push ( JournalEntry { id : id . clone ( ) , insertions : inserted_keys , deletions : removed_keys } ) ;
2016-01-18 12:41:31 +01:00
}
2016-03-08 18:35:25 +01:00
let journal_overlay = journal_overlay . deref_mut ( ) ;
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 {
2016-03-08 18:35:25 +01:00
if let Some ( ref mut records ) = journal_overlay . journal . get_mut ( & end_era ) {
let mut canon_insertions : Vec < ( H256 , Bytes ) > = Vec ::new ( ) ;
let mut canon_deletions : Vec < H256 > = Vec ::new ( ) ;
let mut overlay_deletions : Vec < H256 > = Vec ::new ( ) ;
let mut index = 0 usize ;
for mut journal in records . drain ( .. ) {
//delete the record from the db
let mut r = RlpStream ::new_list ( 3 ) ;
r . append ( & end_era ) ;
r . append ( & index ) ;
r . append ( & & PADDING [ .. ] ) ;
try ! ( batch . delete ( & r . drain ( ) ) ) ;
trace! ( " commit: Delete journal for time #{}.{}: {}, (canon was {}): +{} -{} entries " , end_era , index , journal . id , canon_id , journal . insertions . len ( ) , journal . deletions . len ( ) ) ;
{
if canon_id = = journal . id {
for h in & journal . insertions {
2016-03-11 11:52:11 +01:00
if let Some ( & ( ref d , rc ) ) = journal_overlay . backing_overlay . raw ( h ) {
2016-03-09 18:37:44 +01:00
if rc > 0 {
canon_insertions . push ( ( h . clone ( ) , d . clone ( ) ) ) ; //TODO: optimize this to avoid data copy
}
2016-03-08 18:35:25 +01:00
}
2016-03-06 17:28:50 +01:00
}
2016-03-08 18:35:25 +01:00
canon_deletions = journal . deletions ;
2016-03-06 17:28:50 +01:00
}
2016-03-08 18:35:25 +01:00
overlay_deletions . append ( & mut journal . insertions ) ;
2016-03-06 17:28:50 +01:00
}
2016-03-11 11:52:11 +01:00
index + = 1 ;
2016-02-07 21:18:51 +01:00
}
2016-03-08 18:35:25 +01:00
// apply canon inserts first
for ( k , v ) in canon_insertions {
try ! ( batch . put ( & k , & v ) ) ;
2016-01-18 13:30:01 +01:00
}
2016-03-11 11:52:11 +01:00
// update the overlay
2016-03-08 18:35:25 +01:00
for k in overlay_deletions {
2016-06-23 11:16:11 +02:00
journal_overlay . backing_overlay . remove ( & k ) ;
2016-03-06 17:28:50 +01:00
}
2016-03-11 11:52:11 +01:00
// apply canon deletions
2016-03-08 18:35:25 +01:00
for k in canon_deletions {
2016-06-23 11:16:11 +02:00
if ! journal_overlay . backing_overlay . contains ( & k ) {
2016-03-08 18:35:25 +01:00
try ! ( batch . delete ( & k ) ) ;
}
}
journal_overlay . backing_overlay . purge ( ) ;
2016-02-05 22:54:33 +01:00
}
2016-03-08 18:35:25 +01:00
journal_overlay . journal . remove ( & end_era ) ;
2016-02-05 22:54:33 +01:00
}
2016-03-06 17:28:50 +01:00
try ! ( self . backing . write ( batch ) ) ;
2016-03-11 22:43:59 +01:00
Ok ( 0 )
2016-02-05 22:54:33 +01:00
}
2016-01-18 12:41:31 +01:00
}
2016-03-12 11:19:42 +01:00
impl HashDB for OverlayRecentDB {
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-02-18 03:46:24 +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 ) ;
}
2016-03-06 17:28:50 +01:00
for ( key , refs ) in self . transaction_overlay . keys ( ) . into_iter ( ) {
2016-02-04 02:40:35 +01:00
let refs = * ret . get ( & key ) . unwrap_or ( & 0 ) + refs ;
ret . insert ( key , refs ) ;
}
ret
}
2016-06-23 11:16:11 +02:00
fn get ( & self , key : & H256 ) -> Option < & [ u8 ] > {
2016-03-06 17:28:50 +01:00
let k = self . transaction_overlay . raw ( key ) ;
2016-02-04 02:40:35 +01:00
match k {
Some ( & ( ref d , rc ) ) if rc > 0 = > Some ( d ) ,
_ = > {
2016-07-07 09:37:31 +02:00
let v = self . journal_overlay . unwrapped_read ( ) . backing_overlay . get ( key ) . map ( | v | v . to_vec ( ) ) ;
2016-03-06 17:28:50 +01:00
match v {
Some ( x ) = > {
Some ( & self . transaction_overlay . denote ( key , x ) . 0 )
}
_ = > {
if let Some ( x ) = self . payload ( key ) {
Some ( & self . transaction_overlay . denote ( key , x ) . 0 )
}
else {
None
}
}
2016-02-04 02:40:35 +01:00
}
}
}
}
2016-06-23 11:16:11 +02:00
fn contains ( & self , key : & H256 ) -> bool {
self . get ( key ) . is_some ( )
2016-02-04 02:40:35 +01:00
}
2016-03-12 10:41:35 +01:00
fn insert ( & mut self , value : & [ u8 ] ) -> H256 {
2016-03-06 17:28:50 +01:00
self . transaction_overlay . insert ( value )
2016-02-04 02:40:35 +01:00
}
fn emplace ( & mut self , key : H256 , value : Bytes ) {
2016-03-12 10:41:35 +01:00
self . transaction_overlay . emplace ( key , value ) ;
2016-02-04 02:40:35 +01:00
}
2016-06-23 11:16:11 +02:00
fn remove ( & mut self , key : & H256 ) {
self . transaction_overlay . remove ( 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
#[ cfg(test) ]
mod tests {
2016-03-18 10:14:19 +01:00
#![ cfg_attr(feature= " dev " , allow(blacklisted_name)) ]
2016-04-06 10:07:24 +02:00
#![ cfg_attr(feature= " dev " , allow(similar_names)) ]
2016-03-18 10:14:19 +01:00
2016-01-18 13:30:01 +01:00
use common ::* ;
use super ::* ;
use hashdb ::* ;
2016-03-11 11:52:11 +01:00
use log ::init_log ;
2016-03-11 22:43:59 +01:00
use journaldb ::JournalDB ;
2016-06-27 18:47:50 +02:00
use kvdb ::DatabaseConfig ;
2016-01-18 13:30:01 +01:00
2016-03-06 21:57:55 +01:00
#[ test ]
fn insert_same_in_fork ( ) {
// history is 1
2016-03-12 11:19:42 +01:00
let mut jdb = OverlayRecentDB ::new_temp ( ) ;
2016-03-06 21:57:55 +01:00
let x = jdb . insert ( b " X " ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
jdb . commit ( 3 , & b " 1002a " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
jdb . commit ( 4 , & b " 1003a " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
jdb . remove ( & x ) ;
jdb . commit ( 3 , & b " 1002b " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
let x = jdb . insert ( b " X " ) ;
jdb . commit ( 4 , & b " 1003b " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
jdb . commit ( 5 , & b " 1004a " . sha3 ( ) , Some ( ( 3 , b " 1002a " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
jdb . commit ( 6 , & b " 1005a " . sha3 ( ) , Some ( ( 4 , b " 1003a " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 21:57:55 +01:00
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & x ) ) ;
2016-03-06 21:57:55 +01:00
}
2016-01-18 13:30:01 +01:00
#[ test ]
fn long_history ( ) {
// history is 3
2016-03-12 11:19:42 +01:00
let mut jdb = OverlayRecentDB ::new_temp ( ) ;
2016-01-18 13:30:01 +01:00
let h = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & h ) ) ;
2016-01-18 13:30:01 +01:00
jdb . remove ( & h ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & h ) ) ;
2016-01-18 13:30:01 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & h ) ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & h ) ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( ! jdb . contains ( & h ) ) ;
2016-01-18 13:30:01 +01:00
}
#[ test ]
fn complex ( ) {
// history is 1
2016-03-12 11:19:42 +01:00
let mut jdb = OverlayRecentDB ::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 ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( jdb . contains ( & bar ) ) ;
2016-01-18 13:30:01 +01:00
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-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( jdb . contains ( & bar ) ) ;
assert! ( jdb . contains ( & baz ) ) ;
2016-01-18 13:30:01 +01:00
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-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( ! jdb . contains ( & bar ) ) ;
assert! ( jdb . contains ( & baz ) ) ;
2016-01-18 13:30:01 +01:00
jdb . remove ( & foo ) ;
2016-01-18 23:50:40 +01:00
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( ! jdb . contains ( & bar ) ) ;
assert! ( ! jdb . contains ( & baz ) ) ;
2016-01-18 13:30:01 +01:00
2016-01-18 23:50:40 +01:00
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 3 , b " 3 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( ! jdb . contains ( & foo ) ) ;
assert! ( ! jdb . contains ( & bar ) ) ;
assert! ( ! jdb . contains ( & baz ) ) ;
2016-01-18 13:30:01 +01:00
}
#[ test ]
fn fork ( ) {
// history is 1
2016-03-12 11:19:42 +01:00
let mut jdb = OverlayRecentDB ::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 ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( jdb . contains ( & bar ) ) ;
2016-01-18 13:30:01 +01:00
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-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
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-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-01-18 13:30:01 +01:00
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( jdb . contains ( & bar ) ) ;
assert! ( jdb . contains ( & baz ) ) ;
2016-01-18 13:30:01 +01:00
2016-01-18 23:50:40 +01:00
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , Some ( ( 1 , b " 1b " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( ! jdb . contains ( & baz ) ) ;
assert! ( ! jdb . contains ( & bar ) ) ;
2016-01-18 13:30:01 +01:00
}
2016-02-04 21:33:30 +01:00
#[ test ]
fn overwrite ( ) {
// history is 1
2016-03-12 11:19:42 +01:00
let mut jdb = OverlayRecentDB ::new_temp ( ) ;
2016-02-04 21:33:30 +01:00
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-02-04 21:33:30 +01:00
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-02-04 21:33:30 +01:00
jdb . insert ( b " foo " ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-02-04 21:33:30 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-02-04 21:33:30 +01:00
jdb . commit ( 3 , & b " 2 " . sha3 ( ) , Some ( ( 0 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-02-04 21:33:30 +01:00
}
2016-02-05 22:54:33 +01:00
#[ test ]
2016-03-11 11:52:11 +01:00
fn fork_same_key_one ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-02-05 22:54:33 +01:00
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-02-05 22:54:33 +01:00
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-02-05 22:54:33 +01:00
jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1c " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-02-05 22:54:33 +01:00
jdb . commit ( 2 , & b " 2a " . sha3 ( ) , Some ( ( 1 , b " 1a " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-02-05 22:54:33 +01:00
}
2016-02-11 13:32:27 +01:00
2016-03-11 11:52:11 +01:00
#[ test ]
fn fork_same_key_other ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1c " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , Some ( ( 1 , b " 1b " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
}
#[ test ]
fn fork_ins_del_ins ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 2 , & b " 2a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 3 , & b " 3a " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 3 , & b " 3b " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 4 , & b " 4a " . sha3 ( ) , Some ( ( 2 , b " 2a " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 5 , & b " 5a " . sha3 ( ) , Some ( ( 3 , b " 3a " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
}
2016-03-06 22:39:04 +01:00
2016-02-11 13:32:27 +01:00
#[ test ]
fn reopen ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-03-06 17:28:50 +01:00
let bar = H256 ::random ( ) ;
2016-02-11 13:32:27 +01:00
let foo = {
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-02-11 13:32:27 +01:00
// history is 1
let foo = jdb . insert ( b " foo " ) ;
2016-03-06 17:28:50 +01:00
jdb . emplace ( bar . clone ( ) , b " bar " . to_vec ( ) ) ;
2016-02-11 13:32:27 +01:00
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-02-11 13:32:27 +01:00
foo
} ;
{
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-02-11 13:32:27 +01:00
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-02-11 13:32:27 +01:00
}
{
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( jdb . contains ( & bar ) ) ;
2016-03-06 17:28:50 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( ! jdb . contains ( & foo ) ) ;
2016-03-06 17:28:50 +01:00
}
}
#[ test ]
2016-03-11 11:52:11 +01:00
fn insert_delete_insert_delete_insert_expunge ( ) {
init_log ( ) ;
2016-03-06 17:28:50 +01:00
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
// history is 4
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
// expunge foo
jdb . commit ( 5 , & b " 5 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
}
#[ test ]
fn forked_insert_delete_insert_delete_insert_expunge ( ) {
init_log ( ) ;
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
// history is 4
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 2 , & b " 2a " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 3 , & b " 3a " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . commit ( 3 , & b " 3b " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 4 , & b " 4a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 4 , & b " 4b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
// expunge foo
jdb . commit ( 5 , & b " 5 " . sha3 ( ) , Some ( ( 1 , b " 1a " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
}
#[ test ]
fn broken_assert ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
// history is 1
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
// foo is ancient history.
jdb . remove ( & foo ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ; // BROKEN
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
jdb . remove ( & foo ) ;
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 3 , b " 3 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 5 , & b " 5 " . sha3 ( ) , Some ( ( 4 , b " 4 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( ! jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
}
2016-03-12 10:41:35 +01:00
2016-03-11 11:52:11 +01:00
#[ test ]
fn reopen_test ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
// history is 4
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
// foo is ancient history.
jdb . insert ( b " foo " ) ;
let bar = jdb . insert ( b " bar " ) ;
jdb . commit ( 5 , & b " 5 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & foo ) ;
jdb . remove ( & bar ) ;
jdb . commit ( 6 , & b " 6 " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . insert ( b " foo " ) ;
jdb . insert ( b " bar " ) ;
jdb . commit ( 7 , & b " 7 " . sha3 ( ) , Some ( ( 3 , b " 3 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
}
2016-03-12 10:41:35 +01:00
2016-03-11 11:52:11 +01:00
#[ test ]
fn reopen_remove_three ( ) {
init_log ( ) ;
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
let foo = b " foo " . sha3 ( ) ;
{
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-06 17:28:50 +01:00
// history is 1
2016-03-11 11:52:11 +01:00
jdb . insert ( b " foo " ) ;
2016-03-06 17:28:50 +01:00
jdb . commit ( 0 , & b " 0 " . sha3 ( ) , None ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-07 09:10:02 +01:00
// foo is ancient history.
2016-03-11 11:52:11 +01:00
jdb . remove ( & foo ) ;
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
2016-03-06 17:28:50 +01:00
jdb . insert ( b " foo " ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 3 , & b " 3 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
// incantation to reopen the db
2016-06-27 18:47:50 +02:00
} ; { let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-06 17:28:50 +01:00
jdb . remove ( & foo ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 4 , & b " 4 " . sha3 ( ) , Some ( ( 2 , b " 2 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
// incantation to reopen the db
2016-06-27 18:47:50 +02:00
} ; { let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 5 , & b " 5 " . sha3 ( ) , Some ( ( 3 , b " 3 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
2016-03-11 11:52:11 +01:00
// incantation to reopen the db
2016-06-27 18:47:50 +02:00
} ; { let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-11 11:52:11 +01:00
jdb . commit ( 6 , & b " 6 " . sha3 ( ) , Some ( ( 4 , b " 4 " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( ! jdb . contains ( & foo ) ) ;
2016-02-11 13:32:27 +01:00
}
}
2016-03-12 10:41:35 +01:00
2016-03-06 17:28:50 +01:00
#[ test ]
fn reopen_fork ( ) {
let mut dir = ::std ::env ::temp_dir ( ) ;
dir . push ( H32 ::random ( ) . hex ( ) ) ;
let ( foo , bar , baz ) = {
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-06 17:28:50 +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 ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 17:28:50 +01:00
jdb . remove ( & foo ) ;
let baz = jdb . insert ( b " baz " ) ;
jdb . commit ( 1 , & b " 1a " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 17:28:50 +01:00
jdb . remove ( & bar ) ;
jdb . commit ( 1 , & b " 1b " . sha3 ( ) , Some ( ( 0 , b " 0 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-06 17:28:50 +01:00
( foo , bar , baz )
} ;
{
2016-06-27 18:47:50 +02:00
let mut jdb = OverlayRecentDB ::new ( dir . to_str ( ) . unwrap ( ) , DatabaseConfig ::default ( ) ) ;
2016-03-06 17:28:50 +01:00
jdb . commit ( 2 , & b " 2b " . sha3 ( ) , Some ( ( 1 , b " 1b " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-11 11:52:11 +01:00
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( ! jdb . contains ( & baz ) ) ;
assert! ( ! jdb . contains ( & bar ) ) ;
2016-03-06 17:28:50 +01:00
}
}
2016-03-13 11:50:09 +01:00
#[ test ]
fn insert_older_era ( ) {
let mut jdb = OverlayRecentDB ::new_temp ( ) ;
let foo = jdb . insert ( b " foo " ) ;
jdb . commit ( 0 , & b " 0a " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
let bar = jdb . insert ( b " bar " ) ;
jdb . commit ( 1 , & b " 1 " . sha3 ( ) , Some ( ( 0 , b " 0a " . sha3 ( ) ) ) ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
jdb . remove ( & bar ) ;
jdb . commit ( 0 , & b " 0b " . sha3 ( ) , None ) . unwrap ( ) ;
assert! ( jdb . can_reconstruct_refs ( ) ) ;
2016-03-13 11:55:48 +01:00
jdb . commit ( 2 , & b " 2 " . sha3 ( ) , Some ( ( 1 , b " 1 " . sha3 ( ) ) ) ) . unwrap ( ) ;
2016-03-13 11:50:09 +01:00
2016-06-23 11:16:11 +02:00
assert! ( jdb . contains ( & foo ) ) ;
assert! ( jdb . contains ( & bar ) ) ;
2016-03-13 11:50:09 +01:00
}
2016-01-18 13:30:01 +01:00
}