2016-12-11 19:30:54 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-03-13 18:07:10 +01:00
|
|
|
// 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, ref-counted `JournalDB` implementation.
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
use common::*;
|
|
|
|
use rlp::*;
|
|
|
|
use hashdb::*;
|
2016-08-05 17:00:46 +02:00
|
|
|
use overlaydb::OverlayDB;
|
2016-08-25 14:28:45 +02:00
|
|
|
use memorydb::MemoryDB;
|
2016-07-28 23:46:24 +02:00
|
|
|
use super::{DB_PREFIX_LEN, LATEST_ERA_KEY};
|
2016-03-13 18:07:10 +01:00
|
|
|
use super::traits::JournalDB;
|
2016-07-28 23:46:24 +02:00
|
|
|
use kvdb::{Database, DBTransaction};
|
2016-03-13 18:07:10 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
use std::env;
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
|
2016-03-13 18:07:10 +01:00
|
|
|
/// and 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-03-13 18:07:10 +01:00
|
|
|
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
|
|
|
|
/// the removals actually take effect.
|
2016-09-26 17:14:44 +02:00
|
|
|
///
|
|
|
|
/// journal format:
|
2016-10-27 08:28:12 +02:00
|
|
|
/// ```
|
2016-09-26 17:14:44 +02:00
|
|
|
/// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
|
|
|
|
/// [era, 1] => [ id, [insert_0, ...], [remove_0, ...] ]
|
|
|
|
/// [era, n] => [ ... ]
|
2016-10-27 08:28:12 +02:00
|
|
|
/// ```
|
2016-09-26 17:14:44 +02:00
|
|
|
///
|
|
|
|
/// when we make a new commit, we journal the inserts and removes.
|
2016-10-27 08:28:12 +02:00
|
|
|
/// for each `end_era` that we journaled that we are no passing by,
|
2016-09-26 17:14:44 +02:00
|
|
|
/// we remove all of its removes assuming it is canonical and all
|
|
|
|
/// of its inserts otherwise.
|
|
|
|
// TODO: store last_era, reclaim_period.
|
2016-03-13 18:07:10 +01:00
|
|
|
pub struct RefCountedDB {
|
|
|
|
forward: OverlayDB,
|
|
|
|
backing: Arc<Database>,
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: Option<u64>,
|
2016-03-13 18:07:10 +01:00
|
|
|
inserts: Vec<H256>,
|
|
|
|
removes: Vec<H256>,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: Option<u32>,
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:21:30 +01:00
|
|
|
const PADDING : [u8; 10] = [ 0u8; 10 ];
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
impl RefCountedDB {
|
|
|
|
/// Create a new instance given a `backing` database.
|
2016-07-28 23:46:24 +02:00
|
|
|
pub fn new(backing: Arc<Database>, col: Option<u32>) -> RefCountedDB {
|
|
|
|
let latest_era = backing.get(col, &LATEST_ERA_KEY).expect("Low-level database error.").map(|val| decode::<u64>(&val));
|
2016-03-13 18:19:52 +01:00
|
|
|
|
2016-03-13 18:07:10 +01:00
|
|
|
RefCountedDB {
|
2016-07-28 23:46:24 +02:00
|
|
|
forward: OverlayDB::new(backing.clone(), col),
|
2016-03-13 18:07:10 +01:00
|
|
|
backing: backing,
|
|
|
|
inserts: vec![],
|
|
|
|
removes: vec![],
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: latest_era,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: col,
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new instance with an anonymous temporary database.
|
|
|
|
#[cfg(test)]
|
|
|
|
fn new_temp() -> RefCountedDB {
|
|
|
|
let mut dir = env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
2016-07-28 23:46:24 +02:00
|
|
|
let backing = Arc::new(Database::open_default(dir.to_str().unwrap()).unwrap());
|
|
|
|
Self::new(backing, None)
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HashDB for RefCountedDB {
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> { self.forward.keys() }
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> { self.forward.get(key) }
|
2016-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool { self.forward.contains(key) }
|
2016-03-13 18:07:10 +01:00
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 { let r = self.forward.insert(value); self.inserts.push(r.clone()); r }
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, key: H256, value: DBValue) { self.inserts.push(key.clone()); self.forward.emplace(key, value); }
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, key: &H256) { self.removes.push(key.clone()); }
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JournalDB for RefCountedDB {
|
2016-03-28 09:42:50 +02:00
|
|
|
fn boxed_clone(&self) -> Box<JournalDB> {
|
2016-03-13 18:07:10 +01:00
|
|
|
Box::new(RefCountedDB {
|
|
|
|
forward: self.forward.clone(),
|
|
|
|
backing: self.backing.clone(),
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: self.latest_era,
|
2016-03-13 18:07:10 +01:00
|
|
|
inserts: self.inserts.clone(),
|
|
|
|
removes: self.removes.clone(),
|
2016-07-28 23:46:24 +02:00
|
|
|
column: self.column.clone(),
|
2016-03-13 18:07:10 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mem_used(&self) -> usize {
|
|
|
|
self.inserts.heap_size_of_children() + self.removes.heap_size_of_children()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2016-03-13 18:19:52 +01:00
|
|
|
self.latest_era.is_none()
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
fn backing(&self) -> &Arc<Database> {
|
|
|
|
&self.backing
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:42:50 +02:00
|
|
|
fn latest_era(&self) -> Option<u64> { self.latest_era }
|
2016-04-12 00:51:14 +02:00
|
|
|
|
2016-07-11 12:34:29 +02:00
|
|
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
2016-07-28 23:46:24 +02:00
|
|
|
self.backing.get_by_prefix(self.column, &id[0..DB_PREFIX_LEN]).map(|b| b.to_vec())
|
2016-07-11 12:34:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
fn journal_under(&mut self, batch: &mut DBTransaction, now: u64, id: &H256) -> Result<u32, UtilError> {
|
2016-03-13 18:07:10 +01:00
|
|
|
// record new commit's details.
|
2016-09-26 17:14:44 +02:00
|
|
|
let mut index = 0usize;
|
|
|
|
let mut last;
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
while try!(self.backing.get(self.column, {
|
2016-03-13 18:07:10 +01:00
|
|
|
let mut r = RlpStream::new_list(3);
|
2016-09-26 17:14:44 +02:00
|
|
|
r.append(&now);
|
|
|
|
r.append(&index);
|
|
|
|
r.append(&&PADDING[..]);
|
|
|
|
last = r.drain();
|
|
|
|
&last
|
|
|
|
})).is_some() {
|
|
|
|
index += 1;
|
|
|
|
}
|
2016-03-18 10:14:19 +01:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
let mut r = RlpStream::new_list(3);
|
|
|
|
r.append(id);
|
|
|
|
r.append(&self.inserts);
|
|
|
|
r.append(&self.removes);
|
|
|
|
batch.put(self.column, &last, r.as_raw());
|
2016-03-13 21:21:30 +01:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
let ops = self.inserts.len() + self.removes.len();
|
2016-03-13 18:19:52 +01:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
trace!(target: "rcdb", "new journal for time #{}.{} => {}: inserts={:?}, removes={:?}", now, index, id, self.inserts, self.removes);
|
|
|
|
|
|
|
|
self.inserts.clear();
|
|
|
|
self.removes.clear();
|
|
|
|
|
|
|
|
if self.latest_era.map_or(true, |e| now > e) {
|
|
|
|
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
|
|
|
|
self.latest_era = Some(now);
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
Ok(ops as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mark_canonical(&mut self, batch: &mut DBTransaction, end_era: u64, canon_id: &H256) -> Result<u32, UtilError> {
|
2016-03-13 18:07:10 +01:00
|
|
|
// apply old commits' details
|
2016-09-26 17:14:44 +02:00
|
|
|
let mut index = 0usize;
|
|
|
|
let mut last;
|
|
|
|
while let Some(rlp_data) = {
|
|
|
|
try!(self.backing.get(self.column, {
|
|
|
|
let mut r = RlpStream::new_list(3);
|
|
|
|
r.append(&end_era);
|
|
|
|
r.append(&index);
|
|
|
|
r.append(&&PADDING[..]);
|
|
|
|
last = r.drain();
|
|
|
|
&last
|
|
|
|
}))
|
|
|
|
} {
|
|
|
|
let rlp = Rlp::new(&rlp_data);
|
|
|
|
let our_id: H256 = rlp.val_at(0);
|
|
|
|
let to_remove: Vec<H256> = rlp.val_at(if *canon_id == our_id {2} else {1});
|
|
|
|
trace!(target: "rcdb", "delete journal for time #{}.{}=>{}, (canon was {}): deleting {:?}", end_era, index, our_id, canon_id, to_remove);
|
|
|
|
for i in &to_remove {
|
|
|
|
self.forward.remove(i);
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
2016-09-26 17:14:44 +02:00
|
|
|
batch.delete(self.column, &last);
|
|
|
|
index += 1;
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-08-25 16:43:56 +02:00
|
|
|
let r = try!(self.forward.commit_to_batch(batch));
|
2016-03-13 18:07:10 +01:00
|
|
|
Ok(r)
|
|
|
|
}
|
2016-08-03 16:34:32 +02:00
|
|
|
|
2016-08-25 16:43:56 +02:00
|
|
|
fn inject(&mut self, batch: &mut DBTransaction) -> Result<u32, UtilError> {
|
2016-08-03 16:34:32 +02:00
|
|
|
self.inserts.clear();
|
|
|
|
for remove in self.removes.drain(..) {
|
|
|
|
self.forward.remove(&remove);
|
|
|
|
}
|
2016-08-10 16:29:40 +02:00
|
|
|
self.forward.commit_to_batch(batch)
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
|
|
|
fn consolidate(&mut self, mut with: MemoryDB) {
|
|
|
|
for (key, (value, rc)) in with.drain() {
|
|
|
|
for _ in 0..rc {
|
|
|
|
self.emplace(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in rc..0 {
|
|
|
|
self.remove(&key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 18:07:10 +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-03-13 18:07:10 +01:00
|
|
|
use common::*;
|
2016-11-28 17:05:37 +01:00
|
|
|
use hashdb::{HashDB, DBValue};
|
2016-03-13 18:07:10 +01:00
|
|
|
use super::*;
|
|
|
|
use super::super::traits::JournalDB;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_history() {
|
|
|
|
// history is 3
|
|
|
|
let mut jdb = RefCountedDB::new_temp();
|
|
|
|
let h = jdb.insert(b"foo");
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-13 18:07:10 +01:00
|
|
|
jdb.remove(&h);
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(1, &b"1".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(2, &b"2".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&h));
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-12 00:51:14 +02:00
|
|
|
#[test]
|
|
|
|
fn latest_era_should_work() {
|
|
|
|
// history is 3
|
|
|
|
let mut jdb = RefCountedDB::new_temp();
|
|
|
|
assert_eq!(jdb.latest_era(), None);
|
|
|
|
let h = jdb.insert(b"foo");
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(0));
|
|
|
|
jdb.remove(&h);
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(1, &b"1".sha3(), None).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(1));
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(2, &b"2".sha3(), None).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(2));
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(3));
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(4));
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:07:10 +01:00
|
|
|
#[test]
|
|
|
|
fn complex() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = RefCountedDB::new_temp();
|
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.remove(&bar);
|
|
|
|
let baz = jdb.insert(b"baz");
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
jdb.remove(&baz);
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&bar));
|
|
|
|
assert!(!jdb.contains(&baz));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&bar));
|
|
|
|
assert!(!jdb.contains(&baz));
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fork() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = RefCountedDB::new_temp();
|
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
|
|
|
let baz = jdb.insert(b"baz");
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap();
|
2016-03-13 18:07:10 +01:00
|
|
|
|
|
|
|
jdb.remove(&bar);
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap();
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&baz));
|
|
|
|
assert!(!jdb.contains(&bar));
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
2016-08-03 16:34:32 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inject() {
|
|
|
|
let mut jdb = RefCountedDB::new_temp();
|
|
|
|
let key = jdb.insert(b"dog");
|
|
|
|
jdb.inject_batch().unwrap();
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(jdb.get(&key).unwrap(), DBValue::from_slice(b"dog"));
|
2016-08-03 16:34:32 +02:00
|
|
|
jdb.remove(&key);
|
|
|
|
jdb.inject_batch().unwrap();
|
|
|
|
|
|
|
|
assert!(jdb.get(&key).is_none());
|
|
|
|
}
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|