db/rocksdb/
mod.rs

1// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2// This file is part of Parity.
3
4// Parity is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity.  If not, see <http://www.gnu.org/licenses/>.
16
17// Copyright 2019 Conflux Foundation. All rights reserved.
18// Conflux is free software and distributed under GNU General Public License.
19// See http://www.gnu.org/licenses/
20
21use kvdb_rocksdb::{CompactionProfile, Database, DatabaseConfig};
22use std::{io, path::Path, str::FromStr, sync::Arc};
23
24pub struct SystemDB {
25    // This is the general db that will be shared and used by
26    // all the special db at upper layer.
27    key_value: Arc<Database>,
28}
29
30impl SystemDB {
31    pub fn key_value(&self) -> &Arc<Database> { &self.key_value }
32
33    pub fn new(kvdb: Arc<Database>) -> Self { Self { key_value: kvdb } }
34}
35
36/// db compaction profile
37#[derive(Debug, PartialEq, Clone)]
38pub enum DatabaseCompactionProfile {
39    /// Try to determine compaction profile automatically
40    Auto,
41    /// SSD compaction profile
42    SSD,
43    /// HDD or other slow storage io compaction profile
44    HDD,
45}
46
47impl Default for DatabaseCompactionProfile {
48    fn default() -> Self { DatabaseCompactionProfile::Auto }
49}
50
51impl FromStr for DatabaseCompactionProfile {
52    type Err = String;
53
54    fn from_str(s: &str) -> Result<Self, Self::Err> {
55        match s {
56            "auto" => Ok(DatabaseCompactionProfile::Auto),
57            "ssd" => Ok(DatabaseCompactionProfile::SSD),
58            "hdd" => Ok(DatabaseCompactionProfile::HDD),
59            _ => Err(
60                "Invalid compaction profile given. Expected default/hdd/ssd."
61                    .into(),
62            ),
63        }
64    }
65}
66
67pub fn compaction_profile(
68    profile: &DatabaseCompactionProfile, db_path: &Path,
69) -> CompactionProfile {
70    match profile {
71        &DatabaseCompactionProfile::Auto => CompactionProfile::auto(db_path),
72        &DatabaseCompactionProfile::SSD => CompactionProfile::ssd(),
73        &DatabaseCompactionProfile::HDD => CompactionProfile::hdd(),
74    }
75}
76
77pub fn db_config(
78    path: &Path, db_cache_size: Option<usize>,
79    db_compaction: DatabaseCompactionProfile, columns: u32, disable_wal: bool,
80) -> DatabaseConfig {
81    let mut db_config = DatabaseConfig::with_columns(columns);
82
83    db_config.memory_budget = db_cache_size;
84    db_config.compaction = compaction_profile(&db_compaction, &path);
85    db_config.disable_wal = disable_wal;
86
87    db_config
88}
89
90pub fn open_database(
91    path: &str, config: &DatabaseConfig,
92) -> io::Result<Arc<SystemDB>> {
93    let db = match Database::open(config, path) {
94        Ok(db) => {
95            info!("Open db successfully ({:?})", path);
96            db
97        }
98        Err(e) => {
99            warn!("Failed to open db ({:?})", path);
100            return Err(e);
101        }
102    };
103
104    let sys_db = SystemDB::new(Arc::new(db));
105
106    Ok(Arc::new(sys_db))
107}