use mem::replace instead of mem::swap in ArchiveDB, add aux_remove()

This commit is contained in:
debris 2016-06-27 13:37:22 +02:00
parent 36626f96a8
commit 7904464d24
3 changed files with 22 additions and 8 deletions

View File

@ -114,6 +114,11 @@ pub trait HashDB: AsHashDB {
fn get_aux(&self, _hash: &[u8]) -> Option<Vec<u8>> { fn get_aux(&self, _hash: &[u8]) -> Option<Vec<u8>> {
unimplemented!(); unimplemented!();
} }
/// Removes auxiliary data from hashdb.
fn remove_aux(&mut self, _hash: &[u8]) {
unimplemented!();
}
} }
/// Upcast trait. /// Upcast trait.

View File

@ -26,8 +26,13 @@ use kvdb::{Database, DBTransaction, DatabaseConfig};
#[cfg(test)] #[cfg(test)]
use std::env; use std::env;
/// Suffix appended to auxiliary keys to distinguish them from normal keys.
/// Would be nich to use rocksdb columns for this eventually.
const AUX_FLAG: u8 = 255; const AUX_FLAG: u8 = 255;
/// Database version.
const DB_VERSION : u32 = 0x103;
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay /// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
/// and latent-removal semantics. /// and latent-removal semantics.
/// ///
@ -41,8 +46,6 @@ pub struct ArchiveDB {
latest_era: Option<u64>, latest_era: Option<u64>,
} }
const DB_VERSION : u32 = 0x103;
impl ArchiveDB { impl ArchiveDB {
/// Create a new instance from file /// Create a new instance from file
pub fn new(path: &str, cache_size: Option<usize>) -> ArchiveDB { pub fn new(path: &str, cache_size: Option<usize>) -> ArchiveDB {
@ -122,9 +125,11 @@ impl HashDB for ArchiveDB {
fn insert(&mut self, value: &[u8]) -> H256 { fn insert(&mut self, value: &[u8]) -> H256 {
self.overlay.insert(value) self.overlay.insert(value)
} }
fn emplace(&mut self, key: H256, value: Bytes) { fn emplace(&mut self, key: H256, value: Bytes) {
self.overlay.emplace(key, value); self.overlay.emplace(key, value);
} }
fn remove(&mut self, key: &H256) { fn remove(&mut self, key: &H256) {
self.overlay.remove(key); self.overlay.remove(key);
} }
@ -145,6 +150,10 @@ impl HashDB for ArchiveDB {
.expect("Low-level database error. Some issue with your hard disk?") .expect("Low-level database error. Some issue with your hard disk?")
.map(|v| v.to_vec()) .map(|v| v.to_vec())
} }
fn remove_aux(&mut self, hash: &[u8]) {
self.overlay.remove_aux(hash);
}
} }
impl JournalDB for ArchiveDB { impl JournalDB for ArchiveDB {

View File

@ -136,16 +136,12 @@ impl MemoryDB {
/// Return the internal map of hashes to data, clearing the current state. /// Return the internal map of hashes to data, clearing the current state.
pub fn drain(&mut self) -> HashMap<H256, (Bytes, i32)> { pub fn drain(&mut self) -> HashMap<H256, (Bytes, i32)> {
let mut data = HashMap::new(); mem::replace(&mut self.data, HashMap::new())
mem::swap(&mut self.data, &mut data);
data
} }
/// Return the internal map of auxiliary data, clearing the current state. /// Return the internal map of auxiliary data, clearing the current state.
pub fn drain_aux(&mut self) -> HashMap<Bytes, Bytes> { pub fn drain_aux(&mut self) -> HashMap<Bytes, Bytes> {
let mut aux = HashMap::new(); mem::replace(&mut self.aux, HashMap::new())
mem::swap(&mut self.aux, &mut aux);
aux
} }
/// Denote than an existing value has the given key. Used when a key gets removed without /// Denote than an existing value has the given key. Used when a key gets removed without
@ -250,6 +246,10 @@ impl HashDB for MemoryDB {
fn get_aux(&self, hash: &[u8]) -> Option<Vec<u8>> { fn get_aux(&self, hash: &[u8]) -> Option<Vec<u8>> {
self.aux.get(hash).cloned() self.aux.get(hash).cloned()
} }
fn remove_aux(&mut self, hash: &[u8]) {
self.aux.remove(hash);
}
} }
#[test] #[test]