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