Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Make migration api more friendly #2420

Merged
merged 2 commits into from
Oct 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ethcore/src/migrations/state/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use util::{Address, FixedHash, H256};
use util::kvdb::Database;
use util::migration::{Batch, Config, Error, Migration, SimpleMigration, Progress};
use util::sha3::Hashable;
use std::sync::Arc;

use rlp::{decode, Rlp, RlpStream, Stream, View};

Expand Down Expand Up @@ -107,7 +108,7 @@ pub struct OverlayRecentV7 {
impl OverlayRecentV7 {
// walk all journal entries in the database backwards.
// find migrations for any possible inserted keys.
fn walk_journal(&mut self, source: &Database) -> Result<(), Error> {
fn walk_journal(&mut self, source: Arc<Database>) -> Result<(), Error> {
if let Some(val) = try!(source.get(None, V7_LATEST_ERA_KEY).map_err(Error::Custom)) {
let mut era = decode::<u64>(&val);
loop {
Expand Down Expand Up @@ -151,7 +152,7 @@ impl OverlayRecentV7 {
// walk all journal entries in the database backwards.
// replace all possible inserted/deleted keys with their migrated counterparts
// and commit the altered entries.
fn migrate_journal(&self, source: &Database, mut batch: Batch, dest: &mut Database) -> Result<(), Error> {
fn migrate_journal(&self, source: Arc<Database>, mut batch: Batch, dest: &mut Database) -> Result<(), Error> {
if let Some(val) = try!(source.get(None, V7_LATEST_ERA_KEY).map_err(Error::Custom)) {
try!(batch.insert(V7_LATEST_ERA_KEY.into(), val.to_owned(), dest));

Expand Down Expand Up @@ -228,7 +229,7 @@ impl Migration for OverlayRecentV7 {
// walk all records in the database, attempting to migrate any possible and
// keeping records of those that we do. then migrate the journal using
// this information.
fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
let mut batch = Batch::new(config, col);

// check version metadata.
Expand Down Expand Up @@ -257,7 +258,7 @@ impl Migration for OverlayRecentV7 {
try!(batch.insert(key, value.into_vec(), dest));
}

try!(self.walk_journal(source));
try!(self.walk_journal(source.clone()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't see why a clone is necessary here. works just fine with a reference.

self.migrate_journal(source, batch, dest)
}
}
3 changes: 2 additions & 1 deletion ethcore/src/migrations/v9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use rlp::{Rlp, RlpStream, View, Stream};
use util::kvdb::Database;
use util::migration::{Batch, Config, Error, Migration, Progress};
use std::sync::Arc;

/// Which part of block to preserve
pub enum Extract {
Expand Down Expand Up @@ -55,7 +56,7 @@ impl Migration for ToV9 {

fn version(&self) -> u32 { 9 }

fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
let mut batch = Batch::new(config, self.column);

for (key, value) in source.iter(col) {
Expand Down
5 changes: 3 additions & 2 deletions parity/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::fs::File;
use std::io::{Read, Write, Error as IoError, ErrorKind};
use std::path::{Path, PathBuf};
use std::fmt::{Display, Formatter, Error as FmtError};
use std::sync::Arc;
use util::journaldb::Algorithm;
use util::migration::{Manager as MigrationManager, Config as MigrationConfig, Error as MigrationError, Migration};
use util::kvdb::{CompactionProfile, Database, DatabaseConfig};
Expand Down Expand Up @@ -172,13 +173,13 @@ fn consolidate_database(
let old_path_str = try!(old_db_path.to_str().ok_or(Error::MigrationImpossible));
let new_path_str = try!(new_db_path.to_str().ok_or(Error::MigrationImpossible));

let cur_db = try!(Database::open(&db_config, old_path_str).map_err(db_error));
let cur_db = Arc::new(try!(Database::open(&db_config, old_path_str).map_err(db_error)));
// open new DB with proper number of columns
db_config.columns = migration.columns();
let mut new_db = try!(Database::open(&db_config, new_path_str).map_err(db_error));

// Migrate to new database (default column only)
try!(migration.migrate(&cur_db, &config, &mut new_db, None));
try!(migration.migrate(cur_db, &config, &mut new_db, None));

Ok(())
}
Expand Down
13 changes: 7 additions & 6 deletions util/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::collections::BTreeMap;
use std::fs;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use ::kvdb::{CompactionProfile, Database, DatabaseConfig, DBTransaction};

Expand Down Expand Up @@ -123,7 +124,7 @@ pub trait Migration: 'static {
/// Version of the database after the migration.
fn version(&self) -> u32;
/// Migrate a source to a destination.
fn migrate(&mut self, source: &Database, config: &Config, destination: &mut Database, col: Option<u32>) -> Result<(), Error>;
fn migrate(&mut self, source: Arc<Database>, config: &Config, destination: &mut Database, col: Option<u32>) -> Result<(), Error>;
}

/// A simple migration over key-value pairs.
Expand All @@ -142,7 +143,7 @@ impl<T: SimpleMigration> Migration for T {

fn version(&self) -> u32 { SimpleMigration::version(self) }

fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
let mut batch = Batch::new(config, col);

for (key, value) in source.iter(col) {
Expand Down Expand Up @@ -239,7 +240,7 @@ impl Manager {

// start with the old db.
let old_path_str = try!(old_path.to_str().ok_or(Error::MigrationImpossible));
let mut cur_db = try!(Database::open(&db_config, old_path_str).map_err(Error::Custom));
let mut cur_db = Arc::new(try!(Database::open(&db_config, old_path_str).map_err(Error::Custom)));

for migration in migrations {
// Change number of columns in new db
Expand All @@ -254,16 +255,16 @@ impl Manager {
// perform the migration from cur_db to new_db.
match current_columns {
// migrate only default column
None => try!(migration.migrate(&cur_db, &config, &mut new_db, None)),
None => try!(migration.migrate(cur_db.clone(), &config, &mut new_db, None)),
Some(v) => {
// Migrate all columns in previous DB
for col in 0..v {
try!(migration.migrate(&cur_db, &config, &mut new_db, Some(col)))
try!(migration.migrate(cur_db.clone(), &config, &mut new_db, Some(col)))
}
}
}
// next iteration, we will migrate from this db into the other temp.
cur_db = new_db;
cur_db = Arc::new(new_db);
temp_idx.swap();

// remove the other temporary migration database.
Expand Down
2 changes: 1 addition & 1 deletion util/src/migration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Migration for AddsColumn {

fn version(&self) -> u32 { 1 }

fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
let mut batch = Batch::new(config, col);

for (key, value) in source.iter(col) {
Expand Down