From da4b1c36cbca256bcdb17e587925df40ce0f4c03 Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Mon, 11 Jul 2016 12:34:29 +0200 Subject: [PATCH] Enable state queries for overlayrecent db (#1575) --- util/src/journaldb/archivedb.rs | 2 +- util/src/journaldb/earlymergedb.rs | 4 ++++ util/src/journaldb/overlayrecentdb.rs | 24 ++++++++++++++++++------ util/src/journaldb/refcounteddb.rs | 4 ++++ util/src/journaldb/traits.rs | 4 +--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/util/src/journaldb/archivedb.rs b/util/src/journaldb/archivedb.rs index ca436d7ea..e70134c22 100644 --- a/util/src/journaldb/archivedb.rs +++ b/util/src/journaldb/archivedb.rs @@ -202,7 +202,7 @@ impl JournalDB for ArchiveDB { fn latest_era(&self) -> Option { self.latest_era } fn state(&self, id: &H256) -> Option { - self.backing.get_by_prefix(&id[0..12]).and_then(|b| Some(b.to_vec())) + self.backing.get_by_prefix(&id[0..DB_PREFIX_LEN]).map(|b| b.to_vec()) } fn is_pruned(&self) -> bool { false } diff --git a/util/src/journaldb/earlymergedb.rs b/util/src/journaldb/earlymergedb.rs index 15f6a28b5..abc6fb2e0 100644 --- a/util/src/journaldb/earlymergedb.rs +++ b/util/src/journaldb/earlymergedb.rs @@ -339,6 +339,10 @@ impl JournalDB for EarlyMergeDB { } } + fn state(&self, id: &H256) -> Option { + self.backing.get_by_prefix(&id[0..DB_PREFIX_LEN]).map(|b| b.to_vec()) + } + #[cfg_attr(feature="dev", allow(cyclomatic_complexity))] fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result { // journal format: diff --git a/util/src/journaldb/overlayrecentdb.rs b/util/src/journaldb/overlayrecentdb.rs index 5c60e22b5..269b7e457 100644 --- a/util/src/journaldb/overlayrecentdb.rs +++ b/util/src/journaldb/overlayrecentdb.rs @@ -171,7 +171,7 @@ impl OverlayRecentDB { for r in insertions.iter() { let k: H256 = r.val_at(0); let v: Bytes = r.val_at(1); - overlay.emplace(k.clone(), v); + overlay.emplace(OverlayRecentDB::to_short_key(&k), v); inserted_keys.push(k); count += 1; } @@ -191,6 +191,13 @@ impl OverlayRecentDB { trace!("Recovered {} overlay entries, {} journal entries", count, journal.len()); JournalOverlay { backing_overlay: overlay, journal: journal, latest_era: latest_era } } + + #[inline] + fn to_short_key(key: &H256) -> H256 { + let mut k = H256::new(); + &mut k[0..DB_PREFIX_LEN].copy_from_slice(&key[0..DB_PREFIX_LEN]); + k + } } impl JournalDB for OverlayRecentDB { @@ -212,6 +219,11 @@ impl JournalDB for OverlayRecentDB { fn latest_era(&self) -> Option { self.journal_overlay.unwrapped_read().latest_era } + fn state(&self, key: &H256) -> Option { + let v = self.journal_overlay.unwrapped_read().backing_overlay.get(&OverlayRecentDB::to_short_key(key)).map(|v| v.to_vec()); + v.or_else(|| self.backing.get_by_prefix(&key[0..DB_PREFIX_LEN]).map(|b| b.to_vec())) + } + fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result { // record new commit's details. trace!("commit: #{} ({}), end era: {:?}", now, id, end); @@ -230,7 +242,7 @@ impl JournalDB for OverlayRecentDB { r.begin_list(2); r.append(&k); r.append(&v); - journal_overlay.backing_overlay.emplace(k, v); + journal_overlay.backing_overlay.emplace(OverlayRecentDB::to_short_key(&k), v); } r.append(&removed_keys); @@ -266,7 +278,7 @@ impl JournalDB for OverlayRecentDB { { if canon_id == journal.id { for h in &journal.insertions { - if let Some(&(ref d, rc)) = journal_overlay.backing_overlay.raw(h) { + if let Some(&(ref d, rc)) = journal_overlay.backing_overlay.raw(&OverlayRecentDB::to_short_key(h)) { if rc > 0 { canon_insertions.push((h.clone(), d.clone())); //TODO: optimize this to avoid data copy } @@ -284,11 +296,11 @@ impl JournalDB for OverlayRecentDB { } // update the overlay for k in overlay_deletions { - journal_overlay.backing_overlay.remove(&k); + journal_overlay.backing_overlay.remove(&OverlayRecentDB::to_short_key(&k)); } // apply canon deletions for k in canon_deletions { - if !journal_overlay.backing_overlay.contains(&k) { + if !journal_overlay.backing_overlay.contains(&OverlayRecentDB::to_short_key(&k)) { try!(batch.delete(&k)); } } @@ -322,7 +334,7 @@ impl HashDB for OverlayRecentDB { match k { Some(&(ref d, rc)) if rc > 0 => Some(d), _ => { - let v = self.journal_overlay.unwrapped_read().backing_overlay.get(key).map(|v| v.to_vec()); + let v = self.journal_overlay.unwrapped_read().backing_overlay.get(&OverlayRecentDB::to_short_key(key)).map(|v| v.to_vec()); match v { Some(x) => { Some(&self.transaction_overlay.denote(key, x).0) diff --git a/util/src/journaldb/refcounteddb.rs b/util/src/journaldb/refcounteddb.rs index 4be6a1eda..aea6f16ad 100644 --- a/util/src/journaldb/refcounteddb.rs +++ b/util/src/journaldb/refcounteddb.rs @@ -111,6 +111,10 @@ impl JournalDB for RefCountedDB { fn latest_era(&self) -> Option { self.latest_era } + fn state(&self, id: &H256) -> Option { + self.backing.get_by_prefix(&id[0..DB_PREFIX_LEN]).map(|b| b.to_vec()) + } + fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result { // journal format: // [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ] diff --git a/util/src/journaldb/traits.rs b/util/src/journaldb/traits.rs index 74149b062..53fd17a62 100644 --- a/util/src/journaldb/traits.rs +++ b/util/src/journaldb/traits.rs @@ -39,9 +39,7 @@ pub trait JournalDB : HashDB + Send + Sync { fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result; /// State data query - fn state(&self, _id: &H256) -> Option { - None - } + fn state(&self, _id: &H256) -> Option; /// Whether this database is pruned. fn is_pruned(&self) -> bool { true }