delta_domain_sdk/storage/options.rs
1//! # Database Configuration Options
2//!
3//! This module provides configuration options for the database used by delta domains.
4//! These options control aspects like storage location and persistence behavior.
5//!
6//! The primary use case is for the [RocksDB](https://rocksdb.org/) storage backend, which requires
7//! filesystem paths and other configuration settings.
8//!
9//! ## Example
10//!
11//! ```rust,no_run
12//! use delta_domain_sdk::storage::options::DbOptions;
13//! use std::path::PathBuf;
14//!
15//! // Create default options (uses system temp directory)
16//! let default_options = DbOptions::default();
17//!
18//! // Create custom options with specific path
19//! let custom_options = DbOptions::default()
20//! .with_db_prefix_path(PathBuf::from("/data/domain"));
21//!
22//! // Append to the path
23//! let mut options = DbOptions::default();
24//! options.append_to_prefix_path("shard1");
25//! ```
26
27use std::path::PathBuf;
28
29/// Configuration options for the domain database
30///
31/// These options control where and how the database stores data on disk.
32/// They are primarily used with the [RocksDB](https://rocksdb.org/) storage backend.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct DbOptions {
35 /// Database path prefix, excluding the database name
36 ///
37 /// This is the directory where database files will be stored.
38 /// The actual database name will be appended to this path.
39 db_prefix_path: PathBuf,
40}
41
42impl DbOptions {
43 /// Set the database path prefix
44 ///
45 /// # Parameters
46 ///
47 /// * `prefix_path` - The directory path where database files will be stored
48 ///
49 /// # Returns
50 ///
51 /// Updated options with the new path setting
52 pub fn with_db_prefix_path(mut self, prefix_path: PathBuf) -> Self {
53 self.db_prefix_path = prefix_path;
54 self
55 }
56
57 /// Append a path component to the database's existing prefix path
58 ///
59 /// This is useful for creating subdirectories for different shards or
60 /// domain instances.
61 ///
62 /// # Parameters
63 ///
64 /// * `path` - Path component to append
65 pub fn append_to_prefix_path(&mut self, path: &str) {
66 self.db_prefix_path.push(path);
67 }
68}
69
70impl Default for DbOptions {
71 fn default() -> Self {
72 Self {
73 // Default to the system temporary directory
74 db_prefix_path: std::env::temp_dir(),
75 }
76 }
77}
78
79/// Convert our options to the storage crate's options format
80impl From<DbOptions> for storage::database::spec::DbUserOptions {
81 fn from(value: DbOptions) -> Self {
82 Self::new(value.db_prefix_path)
83 }
84}