Enable state queries for overlayrecent db (#1575)
This commit is contained in:
parent
bdf4446173
commit
da4b1c36cb
@ -202,7 +202,7 @@ impl JournalDB for ArchiveDB {
|
|||||||
fn latest_era(&self) -> Option<u64> { self.latest_era }
|
fn latest_era(&self) -> Option<u64> { self.latest_era }
|
||||||
|
|
||||||
fn state(&self, id: &H256) -> Option<Bytes> {
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
||||||
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 }
|
fn is_pruned(&self) -> bool { false }
|
||||||
|
@ -339,6 +339,10 @@ impl JournalDB for EarlyMergeDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
||||||
|
self.backing.get_by_prefix(&id[0..DB_PREFIX_LEN]).map(|b| b.to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
|
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
|
||||||
fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
|
fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
|
||||||
// journal format:
|
// journal format:
|
||||||
|
@ -171,7 +171,7 @@ impl OverlayRecentDB {
|
|||||||
for r in insertions.iter() {
|
for r in insertions.iter() {
|
||||||
let k: H256 = r.val_at(0);
|
let k: H256 = r.val_at(0);
|
||||||
let v: Bytes = r.val_at(1);
|
let v: Bytes = r.val_at(1);
|
||||||
overlay.emplace(k.clone(), v);
|
overlay.emplace(OverlayRecentDB::to_short_key(&k), v);
|
||||||
inserted_keys.push(k);
|
inserted_keys.push(k);
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
@ -191,6 +191,13 @@ impl OverlayRecentDB {
|
|||||||
trace!("Recovered {} overlay entries, {} journal entries", count, journal.len());
|
trace!("Recovered {} overlay entries, {} journal entries", count, journal.len());
|
||||||
JournalOverlay { backing_overlay: overlay, journal: journal, latest_era: latest_era }
|
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 {
|
impl JournalDB for OverlayRecentDB {
|
||||||
@ -212,6 +219,11 @@ impl JournalDB for OverlayRecentDB {
|
|||||||
|
|
||||||
fn latest_era(&self) -> Option<u64> { self.journal_overlay.unwrapped_read().latest_era }
|
fn latest_era(&self) -> Option<u64> { self.journal_overlay.unwrapped_read().latest_era }
|
||||||
|
|
||||||
|
fn state(&self, key: &H256) -> Option<Bytes> {
|
||||||
|
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<u32, UtilError> {
|
fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
|
||||||
// record new commit's details.
|
// record new commit's details.
|
||||||
trace!("commit: #{} ({}), end era: {:?}", now, id, end);
|
trace!("commit: #{} ({}), end era: {:?}", now, id, end);
|
||||||
@ -230,7 +242,7 @@ impl JournalDB for OverlayRecentDB {
|
|||||||
r.begin_list(2);
|
r.begin_list(2);
|
||||||
r.append(&k);
|
r.append(&k);
|
||||||
r.append(&v);
|
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);
|
r.append(&removed_keys);
|
||||||
|
|
||||||
@ -266,7 +278,7 @@ impl JournalDB for OverlayRecentDB {
|
|||||||
{
|
{
|
||||||
if canon_id == journal.id {
|
if canon_id == journal.id {
|
||||||
for h in &journal.insertions {
|
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 {
|
if rc > 0 {
|
||||||
canon_insertions.push((h.clone(), d.clone())); //TODO: optimize this to avoid data copy
|
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
|
// update the overlay
|
||||||
for k in overlay_deletions {
|
for k in overlay_deletions {
|
||||||
journal_overlay.backing_overlay.remove(&k);
|
journal_overlay.backing_overlay.remove(&OverlayRecentDB::to_short_key(&k));
|
||||||
}
|
}
|
||||||
// apply canon deletions
|
// apply canon deletions
|
||||||
for k in 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));
|
try!(batch.delete(&k));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -322,7 +334,7 @@ impl HashDB for OverlayRecentDB {
|
|||||||
match k {
|
match k {
|
||||||
Some(&(ref d, rc)) if rc > 0 => Some(d),
|
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 {
|
match v {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
Some(&self.transaction_overlay.denote(key, x).0)
|
Some(&self.transaction_overlay.denote(key, x).0)
|
||||||
|
@ -111,6 +111,10 @@ impl JournalDB for RefCountedDB {
|
|||||||
|
|
||||||
fn latest_era(&self) -> Option<u64> { self.latest_era }
|
fn latest_era(&self) -> Option<u64> { self.latest_era }
|
||||||
|
|
||||||
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
||||||
|
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<u32, UtilError> {
|
fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
|
||||||
// journal format:
|
// journal format:
|
||||||
// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
|
// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
|
||||||
|
@ -39,9 +39,7 @@ pub trait JournalDB : HashDB + Send + Sync {
|
|||||||
fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError>;
|
fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError>;
|
||||||
|
|
||||||
/// State data query
|
/// State data query
|
||||||
fn state(&self, _id: &H256) -> Option<Bytes> {
|
fn state(&self, _id: &H256) -> Option<Bytes>;
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this database is pruned.
|
/// Whether this database is pruned.
|
||||||
fn is_pruned(&self) -> bool { true }
|
fn is_pruned(&self) -> bool { true }
|
||||||
|
Loading…
Reference in New Issue
Block a user