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 rlp::*;
|
|
|
|
use hashdb::*;
|
|
|
|
use memorydb::*;
|
2016-06-18 17:58:28 +02:00
|
|
|
use super::{DB_PREFIX_LEN, LATEST_ERA_KEY, VERSION_KEY};
|
2016-03-11 13:50:39 +01:00
|
|
|
use super::traits::JournalDB;
|
|
|
|
use kvdb::{Database, DBTransaction, DatabaseConfig};
|
|
|
|
#[cfg(test)]
|
|
|
|
use std::env;
|
|
|
|
|
2016-06-27 13:37:22 +02:00
|
|
|
/// Suffix appended to auxiliary keys to distinguish them from normal keys.
|
|
|
|
/// Would be nich to use rocksdb columns for this eventually.
|
2016-06-27 09:16:34 +02:00
|
|
|
const AUX_FLAG: u8 = 255;
|
|
|
|
|
2016-06-27 13:37:22 +02:00
|
|
|
/// Database version.
|
|
|
|
const DB_VERSION : u32 = 0x103;
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
|
2016-03-11 13:50:39 +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-11 13:50:39 +01:00
|
|
|
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
|
|
|
|
/// the removals actually take effect.
|
|
|
|
pub struct ArchiveDB {
|
|
|
|
overlay: MemoryDB,
|
|
|
|
backing: Arc<Database>,
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: Option<u64>,
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArchiveDB {
|
|
|
|
/// Create a new instance from file
|
2016-06-27 13:23:50 +02:00
|
|
|
pub fn new(path: &str, config: DatabaseConfig) -> ArchiveDB {
|
2016-07-19 09:23:53 +02:00
|
|
|
let backing = Database::open(&config, path).unwrap_or_else(|e| {
|
2016-03-11 13:50:39 +01:00
|
|
|
panic!("Error opening state db: {}", e);
|
|
|
|
});
|
|
|
|
if !backing.is_empty() {
|
|
|
|
match backing.get(&VERSION_KEY).map(|d| d.map(|v| decode::<u32>(&v))) {
|
|
|
|
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-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
backing.put(&VERSION_KEY, &encode(&DB_VERSION)).expect("Error writing version to database");
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:19:52 +01:00
|
|
|
let latest_era = backing.get(&LATEST_ERA_KEY).expect("Low-level database error.").map(|val| decode::<u64>(&val));
|
2016-03-11 13:50:39 +01:00
|
|
|
ArchiveDB {
|
|
|
|
overlay: MemoryDB::new(),
|
|
|
|
backing: Arc::new(backing),
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: latest_era,
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new instance with an anonymous temporary database.
|
|
|
|
#[cfg(test)]
|
|
|
|
fn new_temp() -> ArchiveDB {
|
|
|
|
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-03-11 13:50:39 +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-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HashDB for ArchiveDB {
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
|
|
|
let mut ret: HashMap<H256, i32> = HashMap::new();
|
|
|
|
for (key, _) in self.backing.iter() {
|
|
|
|
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-06-23 11:16:11 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<&[u8]> {
|
2016-03-11 13:50:39 +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-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool {
|
|
|
|
self.get(key).is_some()
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
|
|
|
self.overlay.insert(value)
|
|
|
|
}
|
2016-06-27 13:37:22 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
fn emplace(&mut self, key: H256, value: Bytes) {
|
|
|
|
self.overlay.emplace(key, value);
|
|
|
|
}
|
2016-06-27 13:37:22 +02:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, key: &H256) {
|
|
|
|
self.overlay.remove(key);
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2016-06-27 09:16:34 +02:00
|
|
|
|
|
|
|
fn insert_aux(&mut self, hash: Vec<u8>, value: Vec<u8>) {
|
|
|
|
self.overlay.insert_aux(hash, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_aux(&self, hash: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
if let Some(res) = self.overlay.get_aux(hash) {
|
|
|
|
return Some(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut db_hash = hash.to_vec();
|
|
|
|
db_hash.push(AUX_FLAG);
|
|
|
|
|
|
|
|
self.backing.get(&db_hash)
|
|
|
|
.expect("Low-level database error. Some issue with your hard disk?")
|
|
|
|
.map(|v| v.to_vec())
|
|
|
|
}
|
2016-06-27 13:37:22 +02:00
|
|
|
|
|
|
|
fn remove_aux(&mut self, hash: &[u8]) {
|
|
|
|
self.overlay.remove_aux(hash);
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JournalDB for ArchiveDB {
|
2016-03-28 09:42:50 +02:00
|
|
|
fn boxed_clone(&self) -> Box<JournalDB> {
|
2016-03-11 13:50:39 +01:00
|
|
|
Box::new(ArchiveDB {
|
2016-03-27 14:35:27 +02:00
|
|
|
overlay: self.overlay.clone(),
|
2016-03-11 13:50:39 +01:00
|
|
|
backing: self.backing.clone(),
|
2016-03-13 19:22:42 +01:00
|
|
|
latest_era: self.latest_era,
|
2016-03-11 13:50:39 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mem_used(&self) -> usize {
|
|
|
|
self.overlay.mem_used()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2016-03-13 18:19:52 +01:00
|
|
|
self.latest_era.is_none()
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 19:22:42 +01:00
|
|
|
fn commit(&mut self, now: u64, _: &H256, _: Option<(u64, H256)>) -> Result<u32, UtilError> {
|
2016-03-11 13:50:39 +01:00
|
|
|
let batch = DBTransaction::new();
|
|
|
|
let mut inserts = 0usize;
|
|
|
|
let mut deletes = 0usize;
|
2016-06-27 09:16:34 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
for i in self.overlay.drain().into_iter() {
|
|
|
|
let (key, (value, rc)) = i;
|
|
|
|
if rc > 0 {
|
|
|
|
assert!(rc == 1);
|
2016-07-06 11:23:29 +02:00
|
|
|
batch.put(&key, &value).expect("Low-level database error. Some issue with your hard disk?");
|
2016-03-11 13:50:39 +01:00
|
|
|
inserts += 1;
|
|
|
|
}
|
|
|
|
if rc < 0 {
|
|
|
|
assert!(rc == -1);
|
|
|
|
deletes += 1;
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 09:16:34 +02:00
|
|
|
|
|
|
|
for (mut key, value) in self.overlay.drain_aux().into_iter() {
|
|
|
|
key.push(AUX_FLAG);
|
|
|
|
batch.put(&key, &value).expect("Low-level database error. Some issue with your hard disk?");
|
|
|
|
}
|
|
|
|
|
2016-03-13 19:22:42 +01:00
|
|
|
if self.latest_era.map_or(true, |e| now > e) {
|
|
|
|
try!(batch.put(&LATEST_ERA_KEY, &encode(&now)));
|
|
|
|
self.latest_era = Some(now);
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
try!(self.backing.write(batch));
|
|
|
|
Ok((inserts + deletes) as u32)
|
|
|
|
}
|
2016-03-11 19:15:56 +01:00
|
|
|
|
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-03-11 19:15:56 +01:00
|
|
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
2016-07-11 12:34:29 +02:00
|
|
|
self.backing.get_by_prefix(&id[0..DB_PREFIX_LEN]).map(|b| b.to_vec())
|
2016-03-11 19:15:56 +01:00
|
|
|
}
|
2016-06-02 20:34:38 +02:00
|
|
|
|
2016-06-03 12:10:10 +02:00
|
|
|
fn is_pruned(&self) -> bool { false }
|
2016-03-11 13:50:39 +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-11 13:50:39 +01:00
|
|
|
use common::*;
|
|
|
|
use super::*;
|
|
|
|
use hashdb::*;
|
2016-03-11 15:54:28 +01:00
|
|
|
use journaldb::traits::JournalDB;
|
2016-06-27 18:47:50 +02:00
|
|
|
use kvdb::DatabaseConfig;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_same_in_fork() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = ArchiveDB::new_temp();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&x));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_history() {
|
|
|
|
// history is 3
|
|
|
|
let mut jdb = ArchiveDB::new_temp();
|
|
|
|
let h = jdb.insert(b"foo");
|
|
|
|
jdb.commit(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&h);
|
|
|
|
jdb.commit(1, &b"1".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.commit(2, &b"2".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.commit(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.commit(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = ArchiveDB::new_temp();
|
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
|
|
|
jdb.commit(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.remove(&bar);
|
|
|
|
let baz = jdb.insert(b"baz");
|
|
|
|
jdb.commit(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-11 13:50:39 +01:00
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
jdb.remove(&baz);
|
|
|
|
jdb.commit(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(&baz));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.commit(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
jdb.commit(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fork() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = ArchiveDB::new_temp();
|
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
|
|
|
jdb.commit(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
jdb.commit(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overwrite() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = ArchiveDB::new_temp();
|
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
jdb.commit(0, &b"0".sha3(), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.commit(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap();
|
|
|
|
jdb.insert(b"foo");
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.commit(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.commit(3, &b"2".sha3(), Some((0, b"2".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fork_same_key() {
|
|
|
|
// history is 1
|
|
|
|
let mut jdb = ArchiveDB::new_temp();
|
|
|
|
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();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
jdb.commit(2, &b"2a".sha3(), Some((1, b"1a".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reopen() {
|
|
|
|
let mut dir = ::std::env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
|
|
|
let bar = H256::random();
|
|
|
|
|
|
|
|
let foo = {
|
2016-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(dir.to_str().unwrap(), DatabaseConfig::default());
|
2016-03-11 13:50:39 +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-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(dir.to_str().unwrap(), DatabaseConfig::default());
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.commit(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::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-11 13:50:39 +01:00
|
|
|
jdb.commit(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reopen_remove() {
|
|
|
|
let mut dir = ::std::env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
|
|
|
|
|
|
|
let foo = {
|
2016-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(dir.to_str().unwrap(), DatabaseConfig::default());
|
2016-03-11 13:50:39 +01:00
|
|
|
// history is 1
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
jdb.commit(0, &b"0".sha3(), None).unwrap();
|
|
|
|
jdb.commit(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap();
|
|
|
|
|
|
|
|
// foo is ancient history.
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
|
|
|
jdb.commit(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap();
|
|
|
|
foo
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2016-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(dir.to_str().unwrap(), DatabaseConfig::default());
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.commit(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
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-18 10:14:19 +01:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen_fork() {
|
|
|
|
let mut dir = ::std::env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
2016-03-11 22:43:59 +01:00
|
|
|
let (foo, _, _) = {
|
2016-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(dir.to_str().unwrap(), DatabaseConfig::default());
|
2016-03-11 13:50:39 +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-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(dir.to_str().unwrap(), DatabaseConfig::default());
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.commit(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:15:56 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_state() {
|
|
|
|
let temp = ::devtools::RandomTempPath::new();
|
|
|
|
|
|
|
|
let key = {
|
2016-06-27 18:47:50 +02:00
|
|
|
let mut jdb = ArchiveDB::new(temp.as_str(), DatabaseConfig::default());
|
2016-03-11 19:15:56 +01:00
|
|
|
let key = jdb.insert(b"foo");
|
|
|
|
jdb.commit(0, &b"0".sha3(), None).unwrap();
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2016-06-27 18:47:50 +02:00
|
|
|
let jdb = ArchiveDB::new(temp.as_str(), DatabaseConfig::default());
|
2016-03-11 19:15:56 +01:00
|
|
|
let state = jdb.state(&key);
|
|
|
|
assert!(state.is_some());
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|