diff --git a/util/src/hashdb.rs b/util/src/hashdb.rs index 35606e1ba..51b9d445e 100644 --- a/util/src/hashdb.rs +++ b/util/src/hashdb.rs @@ -114,6 +114,11 @@ pub trait HashDB: AsHashDB { fn get_aux(&self, _hash: &[u8]) -> Option> { unimplemented!(); } + + /// Removes auxiliary data from hashdb. + fn remove_aux(&mut self, _hash: &[u8]) { + unimplemented!(); + } } /// Upcast trait. diff --git a/util/src/journaldb/archivedb.rs b/util/src/journaldb/archivedb.rs index 40b322802..3713fc79e 100644 --- a/util/src/journaldb/archivedb.rs +++ b/util/src/journaldb/archivedb.rs @@ -26,8 +26,13 @@ use kvdb::{Database, DBTransaction, DatabaseConfig}; #[cfg(test)] 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; +/// Database version. +const DB_VERSION : u32 = 0x103; + /// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay /// and latent-removal semantics. /// @@ -41,8 +46,6 @@ pub struct ArchiveDB { latest_era: Option, } -const DB_VERSION : u32 = 0x103; - impl ArchiveDB { /// Create a new instance from file pub fn new(path: &str, cache_size: Option) -> ArchiveDB { @@ -122,9 +125,11 @@ impl HashDB for ArchiveDB { fn insert(&mut self, value: &[u8]) -> H256 { self.overlay.insert(value) } + fn emplace(&mut self, key: H256, value: Bytes) { self.overlay.emplace(key, value); } + fn remove(&mut self, key: &H256) { self.overlay.remove(key); } @@ -145,6 +150,10 @@ impl HashDB for ArchiveDB { .expect("Low-level database error. Some issue with your hard disk?") .map(|v| v.to_vec()) } + + fn remove_aux(&mut self, hash: &[u8]) { + self.overlay.remove_aux(hash); + } } impl JournalDB for ArchiveDB { diff --git a/util/src/memorydb.rs b/util/src/memorydb.rs index f62c6b9b2..f63dfd992 100644 --- a/util/src/memorydb.rs +++ b/util/src/memorydb.rs @@ -136,16 +136,12 @@ impl MemoryDB { /// Return the internal map of hashes to data, clearing the current state. pub fn drain(&mut self) -> HashMap { - let mut data = HashMap::new(); - mem::swap(&mut self.data, &mut data); - data + mem::replace(&mut self.data, HashMap::new()) } /// Return the internal map of auxiliary data, clearing the current state. pub fn drain_aux(&mut self) -> HashMap { - let mut aux = HashMap::new(); - mem::swap(&mut self.aux, &mut aux); - aux + mem::replace(&mut self.aux, HashMap::new()) } /// 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> { self.aux.get(hash).cloned() } + + fn remove_aux(&mut self, hash: &[u8]) { + self.aux.remove(hash); + } } #[test]