Usage of LATEST_ERA fixes for archive and ref-counted DBs.

This commit is contained in:
Gav Wood 2016-03-13 18:19:52 +01:00
parent 5107fc5897
commit 706c56f56a
2 changed files with 17 additions and 2 deletions

View File

@ -35,6 +35,7 @@ use std::env;
pub struct ArchiveDB { pub struct ArchiveDB {
overlay: MemoryDB, overlay: MemoryDB,
backing: Arc<Database>, backing: Arc<Database>,
latest_era: Option<u64>,
} }
// all keys must be at least 12 bytes // 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"); 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::<u64>(&val));
ArchiveDB { ArchiveDB {
overlay: MemoryDB::new(), overlay: MemoryDB::new(),
backing: Arc::new(backing), backing: Arc::new(backing),
latest_era: latest_era,
} }
} }
@ -129,6 +132,7 @@ impl JournalDB for ArchiveDB {
Box::new(ArchiveDB { Box::new(ArchiveDB {
overlay: MemoryDB::new(), overlay: MemoryDB::new(),
backing: self.backing.clone(), backing: self.backing.clone(),
latest_era: None,
}) })
} }
@ -137,7 +141,7 @@ impl JournalDB for ArchiveDB {
} }
fn is_empty(&self) -> bool { 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<u32, UtilError> { fn commit(&mut self, _: u64, _: &H256, _: Option<(u64, H256)>) -> Result<u32, UtilError> {

View File

@ -35,10 +35,12 @@ use std::env;
pub struct RefCountedDB { pub struct RefCountedDB {
forward: OverlayDB, forward: OverlayDB,
backing: Arc<Database>, backing: Arc<Database>,
latest_era: Option<u64>,
inserts: Vec<H256>, inserts: Vec<H256>,
removes: Vec<H256>, removes: Vec<H256>,
} }
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 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; const DB_VERSION : u32 = 512;
@ -61,11 +63,14 @@ impl RefCountedDB {
} }
let backing = Arc::new(backing); let backing = Arc::new(backing);
let latest_era = backing.get(&LATEST_ERA_KEY).expect("Low-level database error.").map(|val| decode::<u64>(&val));
RefCountedDB { RefCountedDB {
forward: OverlayDB::new_with_arc(backing.clone()), forward: OverlayDB::new_with_arc(backing.clone()),
backing: backing, backing: backing,
inserts: vec![], inserts: vec![],
removes: vec![], removes: vec![],
latest_era: latest_era,
} }
} }
@ -92,6 +97,7 @@ impl JournalDB for RefCountedDB {
Box::new(RefCountedDB { Box::new(RefCountedDB {
forward: self.forward.clone(), forward: self.forward.clone(),
backing: self.backing.clone(), backing: self.backing.clone(),
latest_era: self.latest_era,
inserts: self.inserts.clone(), inserts: self.inserts.clone(),
removes: self.removes.clone(), removes: self.removes.clone(),
}) })
@ -102,7 +108,7 @@ impl JournalDB for RefCountedDB {
} }
fn is_empty(&self) -> bool { 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<u32, UtilError> { fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
@ -141,6 +147,11 @@ impl JournalDB for RefCountedDB {
try!(self.backing.put(&last, r.as_raw())); try!(self.backing.put(&last, r.as_raw()));
self.inserts.clear(); self.inserts.clear();
self.removes.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 // apply old commits' details