2022-06-08 08:01:44 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-10-10 10:06:27 +00:00
|
|
|
use structopt::StructOpt;
|
2022-06-08 08:01:44 +00:00
|
|
|
|
2023-10-10 10:06:27 +00:00
|
|
|
use garage_db::*;
|
2022-06-08 08:01:44 +00:00
|
|
|
|
|
|
|
/// K2V command line interface
|
2023-10-10 10:06:27 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct ConvertDbOpt {
|
|
|
|
/// Input database path (not the same as metadata_dir, see
|
|
|
|
/// https://garagehq.deuxfleurs.fr/documentation/reference-manual/configuration/#db-engine-since-v0-8-0)
|
|
|
|
#[structopt(short = "i")]
|
2022-06-08 08:01:44 +00:00
|
|
|
input_path: PathBuf,
|
2023-10-10 10:06:27 +00:00
|
|
|
/// Input database engine (sled, lmdb or sqlite; limited by db engines
|
|
|
|
/// enabled in this build)
|
|
|
|
#[structopt(short = "a")]
|
2022-06-08 08:01:44 +00:00
|
|
|
input_engine: String,
|
|
|
|
|
2023-10-10 10:06:27 +00:00
|
|
|
/// Output database path
|
|
|
|
#[structopt(short = "o")]
|
2022-06-08 08:01:44 +00:00
|
|
|
output_path: PathBuf,
|
2023-10-10 10:06:27 +00:00
|
|
|
/// Output database engine
|
|
|
|
#[structopt(short = "b")]
|
2022-06-08 08:01:44 +00:00
|
|
|
output_engine: String,
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:06:27 +00:00
|
|
|
pub(crate) fn do_conversion(args: ConvertDbOpt) -> Result<()> {
|
2022-06-08 08:01:44 +00:00
|
|
|
let input = open_db(args.input_path, args.input_engine)?;
|
|
|
|
let output = open_db(args.output_path, args.output_engine)?;
|
|
|
|
output.import(&input)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open_db(path: PathBuf, engine: String) -> Result<Db> {
|
|
|
|
match engine.as_str() {
|
2023-10-10 10:06:27 +00:00
|
|
|
#[cfg(feature = "sled")]
|
2022-06-08 08:01:44 +00:00
|
|
|
"sled" => {
|
|
|
|
let db = sled_adapter::sled::Config::default().path(&path).open()?;
|
|
|
|
Ok(sled_adapter::SledDb::init(db))
|
|
|
|
}
|
2023-10-10 10:06:27 +00:00
|
|
|
#[cfg(feature = "sqlite")]
|
2022-06-08 08:01:44 +00:00
|
|
|
"sqlite" | "sqlite3" | "rusqlite" => {
|
|
|
|
let db = sqlite_adapter::rusqlite::Connection::open(&path)?;
|
2023-05-17 12:30:53 +00:00
|
|
|
db.pragma_update(None, "journal_mode", &"WAL")?;
|
|
|
|
db.pragma_update(None, "synchronous", &"NORMAL")?;
|
2022-06-08 08:01:44 +00:00
|
|
|
Ok(sqlite_adapter::SqliteDb::init(db))
|
|
|
|
}
|
2023-10-10 10:06:27 +00:00
|
|
|
#[cfg(feature = "lmdb")]
|
2022-06-08 08:01:44 +00:00
|
|
|
"lmdb" | "heed" => {
|
|
|
|
std::fs::create_dir_all(&path).map_err(|e| {
|
|
|
|
Error(format!("Unable to create LMDB data directory: {}", e).into())
|
|
|
|
})?;
|
|
|
|
|
2022-06-09 12:55:20 +00:00
|
|
|
let map_size = lmdb_adapter::recommended_map_size();
|
2022-06-08 08:01:44 +00:00
|
|
|
|
2023-05-17 12:30:53 +00:00
|
|
|
let mut env_builder = lmdb_adapter::heed::EnvOpenOptions::new();
|
|
|
|
env_builder.max_dbs(100);
|
|
|
|
env_builder.map_size(map_size);
|
|
|
|
unsafe {
|
2023-10-10 12:06:25 +00:00
|
|
|
env_builder.flag(lmdb_adapter::heed::flags::Flags::MdbNoMetaSync);
|
2023-05-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
let db = env_builder.open(&path)?;
|
2022-06-08 08:01:44 +00:00
|
|
|
Ok(lmdb_adapter::LmdbDb::init(db))
|
|
|
|
}
|
2023-10-10 10:06:27 +00:00
|
|
|
e => Err(Error(
|
|
|
|
format!("Invalid or unsupported DB engine: {}", e).into(),
|
|
|
|
)),
|
2022-06-08 08:01:44 +00:00
|
|
|
}
|
|
|
|
}
|