Bloomchain (#1014)
* use bloomchain crate in blockchain module. remove obsole chainfilter submodule * update database version to 6.0 * removed redundant line * simple db migration * make migration slightly more functional * bloomchain migration * migration version is just a single unsigned integer * updated migration v6 * parity migration * db migration * removed hardcoded migration dir * replace ptr::copy with clone_from_slice, removed potential endianess problem from trace/db.rs * removed superfluous line * blockchains log blooms config is not exposed any more
This commit is contained in:
@@ -17,9 +17,30 @@
|
||||
//! `kvdb::Database` as `migration::Destination`
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use kvdb::{Database, DBTransaction};
|
||||
use kvdb::{Database, DatabaseIterator, DBTransaction};
|
||||
use migration::{Destination, Error};
|
||||
|
||||
/// Database iterator with `Item` complient with migration `Manager` interface.
|
||||
pub struct MigrationIterator {
|
||||
iter: DatabaseIterator,
|
||||
}
|
||||
|
||||
impl From<DatabaseIterator> for MigrationIterator {
|
||||
fn from(iter: DatabaseIterator) -> Self {
|
||||
MigrationIterator {
|
||||
iter: iter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for MigrationIterator {
|
||||
type Item = (Vec<u8>, Vec<u8>);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.iter.next().map(|(k, v)| (k.to_vec(), v.to_vec()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Destination for Database {
|
||||
fn commit(&mut self, batch: BTreeMap<Vec<u8>, Vec<u8>>) -> Result<(), Error> {
|
||||
let transaction = DBTransaction::new();
|
||||
|
||||
@@ -76,10 +76,6 @@ impl Manager {
|
||||
pub fn execute<D>(&self, db_iter: D, version: u32, destination: &mut Destination) -> Result<(), Error> where
|
||||
D: Iterator<Item = (Vec<u8>, Vec<u8>)> {
|
||||
|
||||
if self.is_latest_version(version) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let migrations = try!(self.migrations_from(version).ok_or(Error::MigrationImpossible));
|
||||
|
||||
let mut batch: BTreeMap<Vec<u8>, Vec<u8>> = BTreeMap::new();
|
||||
@@ -104,11 +100,11 @@ impl Manager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns true if given string is equal to latest known version.
|
||||
pub fn is_latest_version(&self, version: u32) -> bool {
|
||||
/// Returns true if migration is needed.
|
||||
pub fn is_needed(&self, version: u32) -> bool {
|
||||
match self.migrations.last() {
|
||||
Some(last) => version == last.version(),
|
||||
None => true
|
||||
Some(last) => version < last.version(),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ mod manager;
|
||||
mod tests;
|
||||
|
||||
pub use self::manager::{Error, Config, Manager};
|
||||
pub use self::db_impl::MigrationIterator;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Single migration.
|
||||
|
||||
@@ -70,6 +70,7 @@ fn one_simple_migration() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn no_migration_needed() {
|
||||
let mut manager = Manager::new(Config::default());
|
||||
let keys = vec![vec![], vec![1u8]];
|
||||
@@ -79,7 +80,6 @@ fn no_migration_needed() {
|
||||
let mut result = BTreeMap::new();
|
||||
manager.add_migration(Migration0).unwrap();
|
||||
manager.execute(db, 1, &mut result).unwrap();
|
||||
assert!(result.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -117,3 +117,14 @@ fn second_migration() {
|
||||
manager.execute(db, 1, &mut result).unwrap();
|
||||
assert_eq!(expected_db, result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_migration_needed() {
|
||||
let mut manager = Manager::new(Config::default());
|
||||
manager.add_migration(Migration0).unwrap();
|
||||
manager.add_migration(Migration1).unwrap();
|
||||
|
||||
assert!(manager.is_needed(0));
|
||||
assert!(manager.is_needed(1));
|
||||
assert!(!manager.is_needed(2));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user