Skip to main content

kaspa_database/
db.rs

1use rocksdb::{DBWithThreadMode, MultiThreaded};
2use std::ops::{Deref, DerefMut};
3use std::path::PathBuf;
4
5pub use conn_builder::ConnBuilder;
6use kaspa_utils::fd_budget::FDGuard;
7pub use rocksdb_preset::RocksDbPreset;
8
9mod conn_builder;
10mod rocksdb_preset;
11
12/// The DB type used for Kaspad stores
13pub struct DB {
14    inner: DBWithThreadMode<MultiThreaded>,
15    _fd_guard: FDGuard,
16}
17
18impl DB {
19    pub fn new(inner: DBWithThreadMode<MultiThreaded>, fd_guard: FDGuard) -> Self {
20        Self { inner, _fd_guard: fd_guard }
21    }
22}
23
24impl DerefMut for DB {
25    fn deref_mut(&mut self) -> &mut Self::Target {
26        &mut self.inner
27    }
28}
29
30impl Deref for DB {
31    type Target = DBWithThreadMode<MultiThreaded>;
32
33    fn deref(&self) -> &Self::Target {
34        &self.inner
35    }
36}
37
38/// Deletes an existing DB if it exists
39pub fn delete_db(db_dir: PathBuf) {
40    if !db_dir.exists() {
41        return;
42    }
43    let options = rocksdb::Options::default();
44    let path = db_dir.to_str().unwrap();
45    <DBWithThreadMode<MultiThreaded>>::destroy(&options, path).expect("DB is expected to be deletable");
46}