commit final batch and migrate journaled insertions

This commit is contained in:
Robert Habermeier 2016-07-09 15:20:06 +02:00
parent 274e7767b4
commit 1f885254e1
2 changed files with 41 additions and 30 deletions

View File

@ -19,18 +19,19 @@ use util::*;
static NULL_RLP_STATIC: [u8; 1] = [0x80; 1]; static NULL_RLP_STATIC: [u8; 1] = [0x80; 1];
#[inline]
// combines a key with an address hash to ensure uniqueness. // combines a key with an address hash to ensure uniqueness.
// leaves the first 96 bits untouched in order to support partial key lookup. // leaves the first 96 bits untouched in order to support partial key lookup.
#[inline]
fn combine_key<'a>(address_hash: &'a H256, key: &'a H256) -> H256 { fn combine_key<'a>(address_hash: &'a H256, key: &'a H256) -> H256 {
let mut dst = key.clone(); let mut dst = key.clone();
{ {
let last_dst: &mut [u8] = &mut *dst;
let last_src: &[u8] = &*address_hash; let last_src: &[u8] = &*address_hash;
let last_dst: &mut [u8] = &mut *dst;
for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) { for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) {
*k ^= *a *k ^= *a
} }
} }
dst dst
} }

View File

@ -26,8 +26,8 @@ use util::migration::{Batch, Config, Error, Migration, SimpleMigration};
use util::rlp::{decode, Rlp, RlpStream, Stream, View}; use util::rlp::{decode, Rlp, RlpStream, Stream, View};
use util::sha3::Hashable; use util::sha3::Hashable;
// attempt to migrate a key, value pair. Err if migration not possible. // attempt to migrate a key, value pair. None if migration not possible.
fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Result<H256, H256> { fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Option<H256> {
let val_hash = val.sha3(); let val_hash = val.sha3();
if key_h != val_hash { if key_h != val_hash {
@ -38,7 +38,7 @@ fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Result<H256, H256> {
// check that the address is actually a 20-byte value. // check that the address is actually a 20-byte value.
// the leftmost 12 bytes should be zero. // the leftmost 12 bytes should be zero.
if &address[0..12] != &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] { if &address[0..12] != &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] {
return Err(key_h); return None;
} }
let address_hash = Address::from(address).sha3(); let address_hash = Address::from(address).sha3();
@ -47,6 +47,7 @@ fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Result<H256, H256> {
key_h.copy_from_slice(&*val_hash); key_h.copy_from_slice(&*val_hash);
assert_eq!(key_h, val_hash); assert_eq!(key_h, val_hash);
{
let last_src: &[u8] = &*address_hash; let last_src: &[u8] = &*address_hash;
let last_dst: &mut [u8] = &mut *key_h; let last_dst: &mut [u8] = &mut *key_h;
for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) { for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) {
@ -54,7 +55,10 @@ fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Result<H256, H256> {
} }
} }
Ok(key_h) Some(key_h)
} else {
None
}
} }
/// Version for ArchiveDB. /// Version for ArchiveDB.
@ -72,10 +76,11 @@ impl SimpleMigration for ArchiveV7 {
} }
let key_h = H256::from_slice(&key[..]); let key_h = H256::from_slice(&key[..]);
let migrated = attempt_migrate(key_h, &value[..]) if let Some(new_key) = attempt_migrate(key_h, &value[..]) {
.expect("no 32-bit metadata keys in this version of archive; qed"); Some((new_key[..].to_owned(), value))
} else {
Some((migrated[..].to_owned(), value)) Some((key, value))
}
} }
} }
@ -118,13 +123,14 @@ impl OverlayRecentV7 {
// migrate all inserted keys. // migrate all inserted keys.
for r in rlp.at(1).iter() { for r in rlp.at(1).iter() {
let old_key: H256 = r.val_at(0); let mut key: H256 = r.val_at(0);
let v: Bytes = r.val_at(1); let v: Bytes = r.val_at(1);
let key = match self.migrated_keys.get(&old_key) { if let Some(new_key) = self.migrated_keys.get(&key) {
Some(new) => new.clone(), key = *new_key;
None => old_key.clone(), } else if let Some(new_key) = attempt_migrate(key, &v) {
}; key = new_key;
}
inserted_keys.push((key, v)); inserted_keys.push((key, v));
} }
@ -149,6 +155,7 @@ impl OverlayRecentV7 {
// and insert it into the new database. // and insert it into the new database.
try!(batch.insert(entry_key, stream.out(), dest)); try!(batch.insert(entry_key, stream.out(), dest));
index += 1; index += 1;
} else { } else {
break; break;
@ -161,7 +168,7 @@ impl OverlayRecentV7 {
era -= 1; era -= 1;
} }
} }
Ok(()) batch.commit(dest)
} }
} }
@ -174,22 +181,25 @@ impl Migration for OverlayRecentV7 {
fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database) -> Result<(), Error> { fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database) -> Result<(), Error> {
let mut batch = Batch::new(config); let mut batch = Batch::new(config);
// migrate version metadata. // check version metadata.
match try!(source.get(V7_VERSION_KEY).map_err(Error::Custom)) { match try!(source.get(V7_VERSION_KEY).map_err(Error::Custom)) {
Some(ref version) if decode::<u32>(&*version) == DB_VERSION => { Some(ref version) if decode::<u32>(&*version) == DB_VERSION => {}
try!(batch.insert(V7_VERSION_KEY.into(), version[..].to_owned(), dest));
}
_ => return Err(Error::MigrationImpossible), // missing or wrong version _ => return Err(Error::MigrationImpossible), // missing or wrong version
} }
for (key, value) in source.iter().filter(|&(ref k, _)| k.len() == 32) { for (key, value) in source.iter() {
let mut key = key.into_vec();
if key.len() == 32 {
let key_h = H256::from_slice(&key[..]); let key_h = H256::from_slice(&key[..]);
if let Ok(new_key) = attempt_migrate(key_h.clone(), &value) { if let Some(new_key) = attempt_migrate(key_h.clone(), &value) {
self.migrated_keys.insert(key_h, new_key); self.migrated_keys.insert(key_h, new_key);
try!(batch.insert(new_key[..].to_owned(), value.into_vec(), dest)); key.copy_from_slice(&new_key[..]);
} }
} }
try!(batch.insert(key, value.into_vec(), dest));
}
self.migrate_journal(source, batch, dest) self.migrate_journal(source, batch, dest)
} }
} }