From 706c56f56a8758413d722fd97338cea37bf1a059 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 13 Mar 2016 18:19:52 +0100 Subject: [PATCH] Usage of LATEST_ERA fixes for archive and ref-counted DBs. --- util/src/journaldb/archivedb.rs | 6 +++++- util/src/journaldb/refcounteddb.rs | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/util/src/journaldb/archivedb.rs b/util/src/journaldb/archivedb.rs index 2e4e966c1..19570b281 100644 --- a/util/src/journaldb/archivedb.rs +++ b/util/src/journaldb/archivedb.rs @@ -35,6 +35,7 @@ use std::env; pub struct ArchiveDB { overlay: MemoryDB, backing: Arc, + latest_era: Option, } // all keys must be at least 12 bytes @@ -60,9 +61,11 @@ impl ArchiveDB { backing.put(&VERSION_KEY, &encode(&DB_VERSION)).expect("Error writing version to database"); } + let latest_era = backing.get(&LATEST_ERA_KEY).expect("Low-level database error.").map(|val| decode::(&val)); ArchiveDB { overlay: MemoryDB::new(), backing: Arc::new(backing), + latest_era: latest_era, } } @@ -129,6 +132,7 @@ impl JournalDB for ArchiveDB { Box::new(ArchiveDB { overlay: MemoryDB::new(), backing: self.backing.clone(), + latest_era: None, }) } @@ -137,7 +141,7 @@ impl JournalDB for ArchiveDB { } fn is_empty(&self) -> bool { - self.backing.get(&LATEST_ERA_KEY).expect("Low level database error").is_none() + self.latest_era.is_none() } fn commit(&mut self, _: u64, _: &H256, _: Option<(u64, H256)>) -> Result { diff --git a/util/src/journaldb/refcounteddb.rs b/util/src/journaldb/refcounteddb.rs index 32471b36c..09362676b 100644 --- a/util/src/journaldb/refcounteddb.rs +++ b/util/src/journaldb/refcounteddb.rs @@ -35,10 +35,12 @@ use std::env; pub struct RefCountedDB { forward: OverlayDB, backing: Arc, + latest_era: Option, inserts: Vec, removes: Vec, } +const LATEST_ERA_KEY : [u8; 12] = [ b'l', b'a', b's', b't', 0, 0, 0, 0, 0, 0, 0, 0 ]; const VERSION_KEY : [u8; 12] = [ b'j', b'v', b'e', b'r', 0, 0, 0, 0, 0, 0, 0, 0 ]; const DB_VERSION : u32 = 512; @@ -61,11 +63,14 @@ impl RefCountedDB { } let backing = Arc::new(backing); + let latest_era = backing.get(&LATEST_ERA_KEY).expect("Low-level database error.").map(|val| decode::(&val)); + RefCountedDB { forward: OverlayDB::new_with_arc(backing.clone()), backing: backing, inserts: vec![], removes: vec![], + latest_era: latest_era, } } @@ -92,6 +97,7 @@ impl JournalDB for RefCountedDB { Box::new(RefCountedDB { forward: self.forward.clone(), backing: self.backing.clone(), + latest_era: self.latest_era, inserts: self.inserts.clone(), removes: self.removes.clone(), }) @@ -102,7 +108,7 @@ impl JournalDB for RefCountedDB { } fn is_empty(&self) -> bool { - self.backing.get(&VERSION_KEY).expect("Low level database error").is_none() + self.latest_era.is_none() } fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result { @@ -141,6 +147,11 @@ impl JournalDB for RefCountedDB { try!(self.backing.put(&last, r.as_raw())); self.inserts.clear(); self.removes.clear(); + + if self.latest_era.map_or(true, |e| now > e) { + try!(batch.put(&LATEST_ERA_KEY, &encode(&now))); + self.latest_era = Some(now); + } } // apply old commits' details