1mod db;
2pub mod errors;
3mod peers;
4mod tickets;
5
6use std::path::PathBuf;
7
8pub use db::{HoprNodeDb, HoprNodeDbConfig};
9pub use hopr_api::{chain::RedeemableTicket, db::*};
10
11pub async fn init_hopr_node_db(
13 db_data_path: &str,
14 initialize: bool,
15 force_initialize: bool,
16) -> anyhow::Result<HoprNodeDb> {
17 let db_path: PathBuf = [db_data_path, "node_db"].iter().collect();
18 tracing::info!(path = ?db_path, "initiating DB");
19
20 let mut create_if_missing = initialize;
21
22 if force_initialize {
23 tracing::info!("Force cleaning up existing database");
24 std::fs::remove_dir_all(db_path.as_path())?;
25 create_if_missing = true;
26 }
27
28 if let Some(parent_dir_path) = db_path.as_path().parent() {
30 if !parent_dir_path.is_dir() {
31 std::fs::create_dir_all(parent_dir_path)
32 .map_err(|e| anyhow::anyhow!("Failed to create DB parent directory at '{parent_dir_path:?}': {e}"))?
33 }
34 }
35
36 let db_cfg = HoprNodeDbConfig {
37 create_if_missing,
38 force_create: force_initialize,
39 log_slow_queries: std::time::Duration::from_millis(150),
40 };
41 let node_db = HoprNodeDb::new(db_path.as_path(), db_cfg).await?;
42
43 Ok(node_db)
44}