Skip to content

Commit

Permalink
Use Option<> for 'db_log_dir' config
Browse files Browse the repository at this point in the history
Following #959 (comment)
  • Loading branch information
romanz committed Nov 28, 2023
1 parent be2026f commit 4f76c63
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
1 change: 0 additions & 1 deletion internal/config_specification.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
pub daemon_dir: PathBuf,
pub daemon_auth: SensitiveAuth,
pub daemon_rpc_addr: SocketAddr,
Expand Down
25 changes: 13 additions & 12 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ impl DBStore {
.collect()
}

fn open_internal(path: &Path, log_dir: &Path) -> Result<Self> {
fn open_internal(path: &Path, log_dir: Option<&Path>) -> Result<Self> {
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()))?;
Expand Down Expand Up @@ -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<Self> {
pub fn open(path: &Path, log_dir: Option<&Path>, auto_reindex: bool) -> Result<Self> {
let mut store = Self::open_internal(path, log_dir)?;
let config = store.get_config();
debug!("DB {:?}", config);
Expand Down Expand Up @@ -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(),
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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",
Expand Down Expand Up @@ -452,16 +454,15 @@ 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());
assert_eq!(dir_files, vec![OsStr::new("LOG")]);

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());
Expand Down
6 changes: 5 additions & 1 deletion src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub(crate) enum Error {

impl Tracker {
pub fn new(config: &Config, metrics: Metrics) -> Result<Self> {
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(
Expand Down

0 comments on commit 4f76c63

Please sign in to comment.