2016-03-11 13:50:39 +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` interface and implementation.
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
use common::*;
|
|
|
|
|
|
|
|
/// Export the journaldb module.
|
|
|
|
pub mod traits;
|
|
|
|
mod archivedb;
|
2016-03-12 11:19:42 +01:00
|
|
|
mod earlymergedb;
|
|
|
|
mod overlayrecentdb;
|
2016-03-13 18:09:44 +01:00
|
|
|
mod refcounteddb;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Export the `JournalDB` trait.
|
2016-03-11 13:50:39 +01:00
|
|
|
pub use self::traits::JournalDB;
|
|
|
|
|
2016-03-11 14:45:19 +01:00
|
|
|
/// A journal database algorithm.
|
2016-04-12 03:42:50 +02:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2016-03-11 14:45:19 +01:00
|
|
|
pub enum Algorithm {
|
|
|
|
/// Keep all keys forever.
|
|
|
|
Archive,
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-03-11 14:45:19 +01:00
|
|
|
/// Ancient and recent history maintained separately; recent history lasts for particular
|
|
|
|
/// number of blocks.
|
|
|
|
///
|
|
|
|
/// Inserts go into backing database, journal retains knowledge of whether backing DB key is
|
|
|
|
/// ancient or recent. Non-canon inserts get explicitly reverted and removed from backing DB.
|
|
|
|
EarlyMerge,
|
|
|
|
|
|
|
|
/// Ancient and recent history maintained separately; recent history lasts for particular
|
|
|
|
/// number of blocks.
|
|
|
|
///
|
|
|
|
/// Inserts go into memory overlay, which is tried for key fetches. Memory overlay gets
|
|
|
|
/// flushed in backing only at end of recent history.
|
|
|
|
OverlayRecent,
|
|
|
|
|
|
|
|
/// Ancient and recent history maintained separately; recent history lasts for particular
|
|
|
|
/// number of blocks.
|
|
|
|
///
|
|
|
|
/// References are counted in disk-backed DB.
|
|
|
|
RefCounted,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Algorithm {
|
|
|
|
fn default() -> Algorithm { Algorithm::Archive }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Algorithm {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", match self {
|
|
|
|
&Algorithm::Archive => "archive",
|
|
|
|
&Algorithm::EarlyMerge => "earlymerge",
|
|
|
|
&Algorithm::OverlayRecent => "overlayrecent",
|
|
|
|
&Algorithm::RefCounted => "refcounted",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Create a new `JournalDB` trait object.
|
2016-06-20 12:42:04 +02:00
|
|
|
pub fn new(path: &str, algorithm: Algorithm, cache_size: Option<usize>) -> Box<JournalDB> {
|
2016-03-11 14:45:19 +01:00
|
|
|
match algorithm {
|
2016-06-20 12:42:04 +02:00
|
|
|
Algorithm::Archive => Box::new(archivedb::ArchiveDB::new(path, cache_size)),
|
|
|
|
Algorithm::EarlyMerge => Box::new(earlymergedb::EarlyMergeDB::new(path, cache_size)),
|
|
|
|
Algorithm::OverlayRecent => Box::new(overlayrecentdb::OverlayRecentDB::new(path, cache_size)),
|
|
|
|
Algorithm::RefCounted => Box::new(refcounteddb::RefCountedDB::new(path, cache_size)),
|
2016-03-11 14:45:19 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:58:28 +02:00
|
|
|
|
|
|
|
// all keys must be at least 12 bytes
|
|
|
|
const DB_PREFIX_LEN : usize = 12;
|
|
|
|
const LATEST_ERA_KEY : [u8; DB_PREFIX_LEN] = [ b'l', b'a', b's', b't', 0, 0, 0, 0, 0, 0, 0, 0 ];
|
|
|
|
const VERSION_KEY : [u8; DB_PREFIX_LEN] = [ b'j', b'v', b'e', b'r', 0, 0, 0, 0, 0, 0, 0, 0 ];
|