Update to latest kvdb-*: no default column, DBValue is Vec (#11312)
* Only use kvdb "column families" This PR contains the changes necessary to use the `kvdb-*` crates from https://github.com/paritytech/parity-common/pull/278 (so a synchronized merge is required) which drops support for the old-style rocksdb "default" column to get a smaller and less complex API. As it stands this PR is working correctly except for secret-store; we need to migrate it to use a new column family. * Fix secretstore build * Fix secretstore build: include ethkey when building with the "accounts" feature * typos * Restore state test commit * Override all of parity-common from git * Be precise about version requirement to migrate secretstore code * Update ethcore/db/src/db.rs Co-Authored-By: Niklas Adolfsson <niklasadolfsson1@gmail.com> * Address review grumbles * Review grumbles * Cleanup Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
@@ -29,7 +29,7 @@ use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use tempdir::TempDir;
|
||||
use kvdb_rocksdb::Database;
|
||||
use kvdb_rocksdb::{Database, DatabaseConfig};
|
||||
use migration::{Batch, Config, SimpleMigration, Migration, Manager, ChangeColumns};
|
||||
|
||||
#[inline]
|
||||
@@ -39,11 +39,11 @@ fn db_path(path: &Path) -> PathBuf {
|
||||
|
||||
// initialize a database at the given directory with the given values.
|
||||
fn make_db(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
||||
let db = Database::open_default(path.to_str().unwrap()).expect("failed to open temp database");
|
||||
let db = Database::open(&DatabaseConfig::default(), path.to_str().unwrap()).expect("failed to open temp database");
|
||||
{
|
||||
let mut transaction = db.transaction();
|
||||
for (k, v) in pairs {
|
||||
transaction.put(None, &k, &v);
|
||||
transaction.put(0, &k, &v);
|
||||
}
|
||||
|
||||
db.write(transaction).expect("failed to write db transaction");
|
||||
@@ -52,10 +52,12 @@ fn make_db(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
||||
|
||||
// helper for verifying a migrated database.
|
||||
fn verify_migration(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
||||
let db = Database::open_default(path.to_str().unwrap()).unwrap();
|
||||
let db = Database::open(&DatabaseConfig::default(), path.to_str().expect("valid path")).expect("database should be there");
|
||||
|
||||
for (k, v) in pairs {
|
||||
let x = db.get(None, &k).unwrap().unwrap();
|
||||
let x = db.get(0, &k)
|
||||
.expect("database IO should work")
|
||||
.expect(&format!("key={:?} should be in column 0 in the db", &k));
|
||||
|
||||
assert_eq!(&x[..], &v[..]);
|
||||
}
|
||||
@@ -64,18 +66,9 @@ fn verify_migration(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
||||
struct Migration0;
|
||||
|
||||
impl SimpleMigration for Migration0 {
|
||||
fn columns(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
fn version(&self) -> u32 {
|
||||
1
|
||||
}
|
||||
|
||||
fn migrated_column_index(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
fn columns(&self) -> u32 { 1 }
|
||||
fn version(&self) -> u32 { 1 }
|
||||
fn migrated_column_index(&self) -> u32 { 0 }
|
||||
fn simple_migrate(&mut self, mut key: Vec<u8>, mut value: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> {
|
||||
key.push(0x11);
|
||||
value.push(0x22);
|
||||
@@ -87,18 +80,9 @@ impl SimpleMigration for Migration0 {
|
||||
struct Migration1;
|
||||
|
||||
impl SimpleMigration for Migration1 {
|
||||
fn columns(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
fn version(&self) -> u32 {
|
||||
2
|
||||
}
|
||||
|
||||
fn migrated_column_index(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
fn columns(&self) -> u32 { 1 }
|
||||
fn version(&self) -> u32 { 2 }
|
||||
fn migrated_column_index(&self) -> u32 { 0 }
|
||||
fn simple_migrate(&mut self, key: Vec<u8>, _value: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> {
|
||||
Some((key, vec![]))
|
||||
}
|
||||
@@ -107,20 +91,17 @@ impl SimpleMigration for Migration1 {
|
||||
struct AddsColumn;
|
||||
|
||||
impl Migration for AddsColumn {
|
||||
fn pre_columns(&self) -> Option<u32> { None }
|
||||
|
||||
fn columns(&self) -> Option<u32> { Some(1) }
|
||||
|
||||
fn pre_columns(&self) -> u32 { 1 }
|
||||
fn columns(&self) -> u32 { 1 }
|
||||
fn version(&self) -> u32 { 1 }
|
||||
|
||||
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: Option<u32>) -> io::Result<()> {
|
||||
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: u32) -> io::Result<()> {
|
||||
let mut batch = Batch::new(config, col);
|
||||
|
||||
for (key, value) in source.iter(col) {
|
||||
batch.insert(key.into_vec(), value.into_vec(), dest)?;
|
||||
}
|
||||
|
||||
if col == Some(1) {
|
||||
if col == 1 {
|
||||
batch.insert(vec![1, 2, 3], vec![4, 5, 6], dest)?;
|
||||
}
|
||||
|
||||
@@ -204,8 +185,8 @@ fn first_and_noop_migration() {
|
||||
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
||||
let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]];
|
||||
|
||||
manager.add_migration(Migration0).unwrap();
|
||||
let end_path = manager.execute(&db_path, 0).unwrap();
|
||||
manager.add_migration(Migration0).expect("Migration0 can be added");
|
||||
let end_path = manager.execute(&db_path, 0).expect("Migration0 runs clean");
|
||||
|
||||
verify_migration(&end_path, expected);
|
||||
}
|
||||
@@ -254,8 +235,8 @@ fn change_columns() {
|
||||
|
||||
let mut manager = Manager::new(Config::default());
|
||||
manager.add_migration(ChangeColumns {
|
||||
pre_columns: None,
|
||||
post_columns: Some(4),
|
||||
pre_columns: 1,
|
||||
post_columns: 4,
|
||||
version: 1,
|
||||
}).unwrap();
|
||||
|
||||
@@ -266,7 +247,7 @@ fn change_columns() {
|
||||
|
||||
assert_eq!(db_path, new_path, "Changing columns is an in-place migration.");
|
||||
|
||||
let config = DatabaseConfig::with_columns(Some(4));
|
||||
let config = DatabaseConfig::with_columns(4);
|
||||
let db = Database::open(&config, new_path.to_str().unwrap()).unwrap();
|
||||
assert_eq!(db.num_columns(), 4);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user