diem_config/config/
storage_config.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use serde::{Deserialize, Serialize};
9use std::path::PathBuf;
10
11/// Port selected RocksDB options for tuning underlying rocksdb instance of
12/// DiemDB. see <https://github.com/facebook/rocksdb/blob/master/include/rocksdb/options.h>
13/// for detailed explanations.
14#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
15#[serde(default, deny_unknown_fields)]
16pub struct RocksdbConfig {
17    pub max_open_files: i32,
18    pub max_total_wal_size: u64,
19}
20
21impl Default for RocksdbConfig {
22    fn default() -> Self {
23        Self {
24            // Set max_open_files to 10k instead of -1 to avoid keep-growing memory in accordance
25            // with the number of files.
26            max_open_files: 10_000,
27            // For now we set the max total WAL size to be 1G. This config can be useful when column
28            // families are updated at non-uniform frequencies.
29            #[allow(clippy::integer_arithmetic)] // TODO: remove once clippy lint fixed
30            max_total_wal_size: 1u64 << 30,
31        }
32    }
33}
34
35#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
36#[serde(default, deny_unknown_fields)]
37pub struct StorageConfig {
38    pub dir: PathBuf,
39    #[serde(skip)]
40    data_dir: PathBuf,
41    /// Rocksdb-specific configurations
42    pub rocksdb_config: RocksdbConfig,
43}
44
45impl Default for StorageConfig {
46    fn default() -> StorageConfig {
47        StorageConfig {
48            dir: PathBuf::from("db"),
49            data_dir: PathBuf::from("./pos_db"),
50            rocksdb_config: RocksdbConfig::default(),
51        }
52    }
53}
54
55impl StorageConfig {
56    pub fn dir(&self) -> PathBuf {
57        if self.dir.is_relative() {
58            self.data_dir.join(&self.dir)
59        } else {
60            self.dir.clone()
61        }
62    }
63
64    pub fn set_data_dir(&mut self, data_dir: PathBuf) {
65        self.data_dir = data_dir;
66    }
67}