ckb_app_config/configs/db.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
/// Database config options.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// Database directory path.
///
/// By default, it is a subdirectory inside the data directory.
#[serde(default)]
pub path: PathBuf,
/// The capacity of RocksDB cache, which caches uncompressed data blocks, indexes and filters, default is 128MB
#[serde(default)]
pub cache_size: Option<usize>,
/// Provide RocksDB options.
///
/// More details can be found in [the official tuning guide](https://github.com/facebook/rocksdb/wiki/RocksDB-Tuning-Guide).
#[serde(default)]
pub options: HashMap<String, String>,
/// Provide an options file to tune RocksDB for your workload and your system configuration.
///
/// More details can be found in [the official tuning guide](https://github.com/facebook/rocksdb/wiki/RocksDB-Tuning-Guide).
pub options_file: Option<PathBuf>,
}
impl Config {
/// Canonicalizes paths in the config options.
///
/// If `self.path` is not set, set it to `data_dir / name`.
///
/// If `self.path` or `self.options_file` is relative, convert them to absolute path using
/// `root_dir` as current working directory.
pub fn adjust<P: AsRef<Path>>(&mut self, root_dir: &Path, data_dir: P, name: &str) {
// If path is not set, use the default path
if self.path.to_str().is_none() || self.path.to_str() == Some("") {
self.path = data_dir.as_ref().to_path_buf().join(name);
} else if self.path.is_relative() {
// If the path is relative, set the base path to `ckb.toml`
self.path = root_dir.to_path_buf().join(&self.path)
}
// If options file is a relative path, set the base path to `ckb.toml`
if let Some(file) = self.options_file.iter_mut().next() {
if file.is_relative() {
let file_new = root_dir.to_path_buf().join(&file);
*file = file_new;
}
}
}
}