openethereum/util/src/journaldb/traits.rs

59 lines
2.1 KiB
Rust
Raw Normal View History

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
//! Disk-backed `HashDB` implementation.
2016-03-11 13:50:39 +01:00
use common::*;
use hashdb::*;
use kvdb::{Database, DBTransaction};
2016-03-11 13:50:39 +01:00
2016-04-06 10:07:24 +02:00
/// A `HashDB` which can manage a short-term journal potentially containing many forks of mutually
2016-03-11 13:50:39 +01:00
/// exclusive actions.
pub trait JournalDB : HashDB + Send + Sync {
/// Return a copy of ourself, in a box.
2016-03-28 09:42:50 +02:00
fn boxed_clone(&self) -> Box<JournalDB>;
2016-03-11 13:50:39 +01:00
/// Returns heap memory size used
fn mem_used(&self) -> usize;
/// Check if this database has any commits
fn is_empty(&self) -> bool;
2016-04-12 00:51:14 +02:00
/// Get the latest era in the DB. None if there isn't yet any data in there.
2016-04-12 03:42:50 +02:00
fn latest_era(&self) -> Option<u64>;
2016-04-12 00:51:14 +02:00
2016-03-11 13:50:39 +01:00
/// Commit all recent insert operations and canonical historical commits' removals from the
/// old era to the backing database, reverting any non-canonical historical commit's inserts.
fn commit(&mut self, batch: &DBTransaction, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError>;
2016-03-11 19:15:56 +01:00
/// State data query
fn state(&self, _id: &H256) -> Option<Bytes>;
2016-06-03 12:10:10 +02:00
/// Whether this database is pruned.
fn is_pruned(&self) -> bool { true }
/// Get backing database.
fn backing(&self) -> &Arc<Database>;
#[cfg(test)]
/// Commit all changes in a single batch
fn commit_batch(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
let batch = self.backing().transaction();
let res = try!(self.commit(&batch, now, id, end));
self.backing().write(batch).map(|_| res).map_err(Into::into)
}
2016-03-11 13:50:39 +01:00
}