openethereum/parity/db/rocksdb/mod.rs

112 lines
3.3 KiB
Rust
Raw Normal View History

2020-09-22 14:53:52 +02:00
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.
2020-09-22 14:53:52 +02:00
// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-09-22 14:53:52 +02:00
// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
2020-09-22 14:53:52 +02:00
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
2020-08-05 06:08:03 +02:00
extern crate ethcore_blockchain;
extern crate kvdb_rocksdb;
extern crate migration_rocksdb;
2020-08-05 06:08:03 +02:00
use self::{
ethcore_blockchain::{BlockChainDB, BlockChainDBHandler},
kvdb_rocksdb::{Database, DatabaseConfig},
};
use blooms_db;
2020-08-13 18:25:19 +02:00
use ethcore::client::ClientConfig;
use kvdb::KeyValueDB;
2020-08-05 06:08:03 +02:00
use std::{fs, io, path::Path, sync::Arc};
mod blooms;
mod helpers;
2020-08-05 06:08:03 +02:00
mod migration;
pub use self::migration::migrate;
struct AppDB {
2020-07-29 10:36:15 +02:00
key_value: Arc<dyn KeyValueDB>,
2020-08-05 06:08:03 +02:00
blooms: blooms_db::Database,
trace_blooms: blooms_db::Database,
}
impl BlockChainDB for AppDB {
2020-07-29 10:36:15 +02:00
fn key_value(&self) -> &Arc<dyn KeyValueDB> {
2020-08-05 06:08:03 +02:00
&self.key_value
}
2020-08-05 06:08:03 +02:00
fn blooms(&self) -> &blooms_db::Database {
&self.blooms
}
2020-08-05 06:08:03 +02:00
fn trace_blooms(&self) -> &blooms_db::Database {
&self.trace_blooms
}
}
/// Open a secret store DB using the given secret store data path. The DB path is one level beneath the data path.
#[cfg(feature = "secretstore")]
2020-07-29 10:36:15 +02:00
pub fn open_secretstore_db(data_path: &str) -> Result<Arc<dyn KeyValueDB>, String> {
2020-08-05 06:08:03 +02:00
use std::path::PathBuf;
let mut db_path = PathBuf::from(data_path);
db_path.push("db");
let db_path = db_path
.to_str()
.ok_or_else(|| "Invalid secretstore path".to_string())?;
Ok(Arc::new(
Database::open_default(&db_path).map_err(|e| format!("Error opening database: {:?}", e))?,
))
}
/// Create a restoration db handler using the config generated by `client_path` and `client_config`.
2020-08-05 06:08:03 +02:00
pub fn restoration_db_handler(
client_path: &Path,
client_config: &ClientConfig,
2020-07-29 10:36:15 +02:00
) -> Box<dyn BlockChainDBHandler> {
2020-08-05 06:08:03 +02:00
let client_db_config = helpers::client_db_config(client_path, client_config);
struct RestorationDBHandler {
config: DatabaseConfig,
}
impl BlockChainDBHandler for RestorationDBHandler {
2020-07-29 10:36:15 +02:00
fn open(&self, db_path: &Path) -> io::Result<Arc<dyn BlockChainDB>> {
2020-08-05 06:08:03 +02:00
open_database(&db_path.to_string_lossy(), &self.config)
}
}
Box::new(RestorationDBHandler {
config: client_db_config,
})
}
2020-07-29 10:36:15 +02:00
pub fn open_database(
client_path: &str,
config: &DatabaseConfig,
) -> io::Result<Arc<dyn BlockChainDB>> {
2020-08-05 06:08:03 +02:00
let path = Path::new(client_path);
2020-08-05 06:08:03 +02:00
let blooms_path = path.join("blooms");
let trace_blooms_path = path.join("trace_blooms");
fs::create_dir_all(&blooms_path)?;
fs::create_dir_all(&trace_blooms_path)?;
2020-08-05 06:08:03 +02:00
let db = AppDB {
key_value: Arc::new(Database::open(&config, client_path)?),
blooms: blooms_db::Database::open(blooms_path)?,
trace_blooms: blooms_db::Database::open(trace_blooms_path)?,
};
2020-08-05 06:08:03 +02:00
Ok(Arc::new(db))
}