From 1029f8438cafc21d8a08883a8852f4b3df9b0b50 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Sat, 1 Oct 2016 16:33:19 +0400 Subject: [PATCH] using arc (#2420) --- ethcore/src/migrations/state/v7.rs | 9 +++++---- ethcore/src/migrations/v9.rs | 3 ++- parity/migration.rs | 5 +++-- util/src/migration/mod.rs | 13 +++++++------ util/src/migration/tests.rs | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/ethcore/src/migrations/state/v7.rs b/ethcore/src/migrations/state/v7.rs index 9327decef..9af75a8ed 100644 --- a/ethcore/src/migrations/state/v7.rs +++ b/ethcore/src/migrations/state/v7.rs @@ -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}; @@ -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) -> Result<(), Error> { if let Some(val) = try!(source.get(None, V7_LATEST_ERA_KEY).map_err(Error::Custom)) { let mut era = decode::(&val); loop { @@ -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, 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)); @@ -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) -> Result<(), Error> { + fn migrate(&mut self, source: Arc, config: &Config, dest: &mut Database, col: Option) -> Result<(), Error> { let mut batch = Batch::new(config, col); // check version metadata. @@ -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())); self.migrate_journal(source, batch, dest) } } diff --git a/ethcore/src/migrations/v9.rs b/ethcore/src/migrations/v9.rs index d4070d0c0..83729dc55 100644 --- a/ethcore/src/migrations/v9.rs +++ b/ethcore/src/migrations/v9.rs @@ -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 { @@ -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) -> Result<(), Error> { + fn migrate(&mut self, source: Arc, config: &Config, dest: &mut Database, col: Option) -> Result<(), Error> { let mut batch = Batch::new(config, self.column); for (key, value) in source.iter(col) { diff --git a/parity/migration.rs b/parity/migration.rs index 66e1d8010..084ade676 100644 --- a/parity/migration.rs +++ b/parity/migration.rs @@ -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}; @@ -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(()) } diff --git a/util/src/migration/mod.rs b/util/src/migration/mod.rs index 0d34f4198..cd2b7fae1 100644 --- a/util/src/migration/mod.rs +++ b/util/src/migration/mod.rs @@ -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}; @@ -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) -> Result<(), Error>; + fn migrate(&mut self, source: Arc, config: &Config, destination: &mut Database, col: Option) -> Result<(), Error>; } /// A simple migration over key-value pairs. @@ -142,7 +143,7 @@ impl Migration for T { fn version(&self) -> u32 { SimpleMigration::version(self) } - fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option) -> Result<(), Error> { + fn migrate(&mut self, source: Arc, config: &Config, dest: &mut Database, col: Option) -> Result<(), Error> { let mut batch = Batch::new(config, col); for (key, value) in source.iter(col) { @@ -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 @@ -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. diff --git a/util/src/migration/tests.rs b/util/src/migration/tests.rs index 05229bee5..57a5a9e32 100644 --- a/util/src/migration/tests.rs +++ b/util/src/migration/tests.rs @@ -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) -> Result<(), Error> { + fn migrate(&mut self, source: Arc, config: &Config, dest: &mut Database, col: Option) -> Result<(), Error> { let mut batch = Batch::new(config, col); for (key, value) in source.iter(col) {