From 4f76c630e9f448dcf8099e4bd68dacf0ef9e40e4 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Mon, 27 Nov 2023 21:52:23 +0200 Subject: [PATCH] Use Option<> for 'db_log_dir' config Following https://github.com/romanz/electrs/pull/959#discussion_r1405387423 --- internal/config_specification.toml | 1 - src/config.rs | 2 +- src/db.rs | 25 +++++++++++++------------ src/tracker.rs | 6 +++++- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/internal/config_specification.toml b/internal/config_specification.toml index e539fdca7..75aad9bca 100644 --- a/internal/config_specification.toml +++ b/internal/config_specification.toml @@ -33,7 +33,6 @@ default = "\"./db\".into()" name = "db_log_dir" type = "std::path::PathBuf" doc = "Directory to store index database internal log (default: same as specified by `db_dir`)" -default = "\"\".into()" [[param]] name = "daemon_dir" diff --git a/src/config.rs b/src/config.rs index d8d12a3ab..915ab9add 100644 --- a/src/config.rs +++ b/src/config.rs @@ -127,7 +127,7 @@ pub struct Config { // See below for the documentation of each field: pub network: Network, pub db_path: PathBuf, - pub db_log_dir: PathBuf, + pub db_log_dir: Option, pub daemon_dir: PathBuf, pub daemon_auth: SensitiveAuth, pub daemon_rpc_addr: SocketAddr, diff --git a/src/db.rs b/src/db.rs index 5aa20026d..8d6c7583e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -121,11 +121,13 @@ impl DBStore { .collect() } - fn open_internal(path: &Path, log_dir: &Path) -> Result { + fn open_internal(path: &Path, log_dir: Option<&Path>) -> Result { let mut db_opts = default_opts(); db_opts.create_if_missing(true); db_opts.create_missing_column_families(true); - db_opts.set_db_log_dir(log_dir); + if let Some(d) = log_dir { + db_opts.set_db_log_dir(d); + } let db = rocksdb::DB::open_cf_descriptors(&db_opts, path, Self::create_cf_descriptors()) .with_context(|| format!("failed to open DB: {}", path.display()))?; @@ -153,7 +155,7 @@ impl DBStore { } /// Opens a new RocksDB at the specified location. - pub fn open(path: &Path, log_dir: &Path, auto_reindex: bool) -> Result { + pub fn open(path: &Path, log_dir: Option<&Path>, auto_reindex: bool) -> Result { let mut store = Self::open_internal(path, log_dir)?; let config = store.get_config(); debug!("DB {:?}", config); @@ -368,13 +370,13 @@ mod tests { fn test_reindex_new_format() { let dir = tempfile::tempdir().unwrap(); { - let store = DBStore::open(dir.path(), dir.path(), false).unwrap(); + let store = DBStore::open(dir.path(), None, false).unwrap(); let mut config = store.get_config().unwrap(); config.format += 1; store.set_config(config); }; assert_eq!( - DBStore::open(dir.path(), dir.path(), false) + DBStore::open(dir.path(), None, false) .err() .unwrap() .to_string(), @@ -385,7 +387,7 @@ mod tests { ) ); { - let store = DBStore::open(dir.path(), dir.path(), true).unwrap(); + let store = DBStore::open(dir.path(), None, true).unwrap(); store.flush(); let config = store.get_config().unwrap(); assert_eq!(config.format, CURRENT_FORMAT); @@ -403,14 +405,14 @@ mod tests { db.put(b"F", b"").unwrap(); // insert legacy DB compaction marker (in 'default' column family) }; assert_eq!( - DBStore::open(dir.path(), dir.path(), false) + DBStore::open(dir.path(), None, false) .err() .unwrap() .to_string(), format!("re-index required due to legacy format",) ); { - let store = DBStore::open(dir.path(), dir.path(), true).unwrap(); + let store = DBStore::open(dir.path(), None, true).unwrap(); store.flush(); let config = store.get_config().unwrap(); assert_eq!(config.format, CURRENT_FORMAT); @@ -420,7 +422,7 @@ mod tests { #[test] fn test_db_prefix_scan() { let dir = tempfile::tempdir().unwrap(); - let store = DBStore::open(dir.path(), dir.path(), true).unwrap(); + let store = DBStore::open(dir.path(), None, true).unwrap(); let items: &[&[u8]] = &[ b"ab", @@ -452,8 +454,7 @@ mod tests { #[test] fn test_db_log_in_same_dir() { let dir1 = tempfile::tempdir().unwrap(); - let empty = Path::new(""); - let _store = DBStore::open(dir1.path(), &empty, true).unwrap(); + let _store = DBStore::open(dir1.path(), None, true).unwrap(); // LOG file is created in dir1 let dir_files = list_log_files(dir1.path()); @@ -461,7 +462,7 @@ mod tests { let dir2 = tempfile::tempdir().unwrap(); let dir3 = tempfile::tempdir().unwrap(); - let _store = DBStore::open(dir2.path(), dir3.path(), true).unwrap(); + let _store = DBStore::open(dir2.path(), Some(dir3.path()), true).unwrap(); // *_LOG file is not created in dir2, but in dir3 let dir_files = list_log_files(dir2.path()); diff --git a/src/tracker.rs b/src/tracker.rs index 0abe7fc4e..9ed97f355 100644 --- a/src/tracker.rs +++ b/src/tracker.rs @@ -33,7 +33,11 @@ pub(crate) enum Error { impl Tracker { pub fn new(config: &Config, metrics: Metrics) -> Result { - let store = DBStore::open(&config.db_path, &config.db_log_dir, config.auto_reindex)?; + let store = DBStore::open( + &config.db_path, + config.db_log_dir.as_deref(), + config.auto_reindex, + )?; let chain = Chain::new(config.network); Ok(Self { index: Index::load(