Fixes and traces for refcountdb.

This commit is contained in:
Gav Wood 2016-03-13 21:21:30 +01:00
parent 81291622eb
commit 1be92ea8ef
2 changed files with 28 additions and 13 deletions

View File

@ -43,6 +43,7 @@ pub struct RefCountedDB {
const LATEST_ERA_KEY : [u8; 12] = [ b'l', b'a', b's', b't', 0, 0, 0, 0, 0, 0, 0, 0 ]; 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;
const PADDING : [u8; 10] = [ 0u8; 10 ];
impl RefCountedDB { impl RefCountedDB {
/// Create a new instance given a `backing` database. /// Create a new instance given a `backing` database.
@ -125,15 +126,15 @@ impl JournalDB for RefCountedDB {
// of its inserts otherwise. // of its inserts otherwise.
// record new commit's details. // record new commit's details.
let batch = DBTransaction::new();
{ {
let mut index = 0usize; let mut index = 0usize;
let mut last; let mut last;
while try!(self.backing.get({ while try!(self.backing.get({
let mut r = RlpStream::new_list(2); let mut r = RlpStream::new_list(3);
r.append(&now); r.append(&now);
r.append(&index); r.append(&index);
r.append(&&PADDING[..]);
last = r.drain(); last = r.drain();
&last &last
})).is_some() { })).is_some() {
@ -145,11 +146,14 @@ impl JournalDB for RefCountedDB {
r.append(&self.inserts); r.append(&self.inserts);
r.append(&self.removes); r.append(&self.removes);
try!(self.backing.put(&last, r.as_raw())); try!(self.backing.put(&last, r.as_raw()));
trace!(target: "rcdb", "new journal for time #{}.{} => {}: inserts={:?}, removes={:?}", now, index, id, self.inserts, self.removes);
self.inserts.clear(); self.inserts.clear();
self.removes.clear(); self.removes.clear();
if self.latest_era.map_or(true, |e| now > e) { if self.latest_era.map_or(true, |e| now > e) {
try!(batch.put(&LATEST_ERA_KEY, &encode(&now))); try!(self.backing.put(&LATEST_ERA_KEY, &encode(&now)));
self.latest_era = Some(now); self.latest_era = Some(now);
} }
} }
@ -158,26 +162,30 @@ impl JournalDB for RefCountedDB {
if let Some((end_era, canon_id)) = end { if let Some((end_era, canon_id)) = end {
let mut index = 0usize; let mut index = 0usize;
let mut last; let mut last;
while let Some(rlp_data) = try!(self.backing.get({ while let Some(rlp_data) = {
let mut r = RlpStream::new_list(2); // trace!(target: "rcdb", "checking for journal #{}.{}", end_era, index);
r.append(&end_era); try!(self.backing.get({
r.append(&index); let mut r = RlpStream::new_list(3);
last = r.drain(); r.append(&end_era);
&last r.append(&index);
})) { r.append(&&PADDING[..]);
last = r.drain();
&last
}))
} {
let rlp = Rlp::new(&rlp_data); let rlp = Rlp::new(&rlp_data);
let to_remove: Vec<H256> = rlp.val_at(if canon_id == rlp.val_at(0) {2} else {1}); let our_id: H256 = rlp.val_at(0);
let to_remove: Vec<H256> = rlp.val_at(if canon_id == our_id {2} else {1});
trace!(target: "rcdb", "delete journal for time #{}.{}=>{}, (canon was {}): deleting {:?}", end_era, index, our_id, canon_id, to_remove);
for i in &to_remove { for i in &to_remove {
self.forward.remove(i); self.forward.remove(i);
} }
try!(self.backing.delete(&last)); try!(self.backing.delete(&last));
trace!("RefCountedDB: delete journal for time #{}.{}, (canon was {}): {} entries", end_era, index, canon_id, to_remove.len());
index += 1; index += 1;
} }
} }
let r = try!(self.forward.commit()); let r = try!(self.forward.commit());
try!(self.backing.write(batch));
Ok(r) Ok(r)
} }
} }

View File

@ -70,12 +70,14 @@ impl OverlayDB {
let (back_value, back_rc) = x; let (back_value, back_rc) = x;
let total_rc: i32 = back_rc as i32 + rc; let total_rc: i32 = back_rc as i32 + rc;
if total_rc < 0 { if total_rc < 0 {
warn!("NEGATIVELY REFERENCED HASH {:?}", key);
return Err(From::from(BaseDataError::NegativelyReferencedHash)); return Err(From::from(BaseDataError::NegativelyReferencedHash));
} }
deletes += if self.put_payload_in_batch(batch, &key, (back_value, total_rc as u32)) {1} else {0}; deletes += if self.put_payload_in_batch(batch, &key, (back_value, total_rc as u32)) {1} else {0};
} }
None => { None => {
if rc < 0 { if rc < 0 {
warn!("NEGATIVELY REFERENCED HASH {:?}", key);
return Err(From::from(BaseDataError::NegativelyReferencedHash)); return Err(From::from(BaseDataError::NegativelyReferencedHash));
} }
self.put_payload_in_batch(batch, &key, (value, rc as u32)); self.put_payload_in_batch(batch, &key, (value, rc as u32));
@ -126,12 +128,14 @@ impl OverlayDB {
let (back_value, back_rc) = x; let (back_value, back_rc) = x;
let total_rc: i32 = back_rc as i32 + rc; let total_rc: i32 = back_rc as i32 + rc;
if total_rc < 0 { if total_rc < 0 {
warn!("NEGATIVELY REFERENCED HASH {:?}", key);
return Err(From::from(BaseDataError::NegativelyReferencedHash)); return Err(From::from(BaseDataError::NegativelyReferencedHash));
} }
deletes += if self.put_payload(&key, (back_value, total_rc as u32)) {1} else {0}; deletes += if self.put_payload(&key, (back_value, total_rc as u32)) {1} else {0};
} }
None => { None => {
if rc < 0 { if rc < 0 {
warn!("NEGATIVELY REFERENCED HASH {:?}", key);
return Err(From::from(BaseDataError::NegativelyReferencedHash)); return Err(From::from(BaseDataError::NegativelyReferencedHash));
} }
self.put_payload(&key, (value, rc as u32)); self.put_payload(&key, (value, rc as u32));
@ -167,6 +171,9 @@ impl OverlayDB {
/// ``` /// ```
pub fn revert(&mut self) { self.overlay.clear(); } pub fn revert(&mut self) { self.overlay.clear(); }
/// Get the number of references that would be committed.
pub fn commit_refs(&self, key: &H256) -> i32 { self.overlay.raw(&key).map_or(0, |&(_, refs)| refs) }
/// Get the refs and value of the given key. /// Get the refs and value of the given key.
fn payload(&self, key: &H256) -> Option<(Bytes, u32)> { fn payload(&self, key: &H256) -> Option<(Bytes, u32)> {
self.backing.get(&key.bytes()) self.backing.get(&key.bytes())