delta_domain_sdk/
storage.rs1use domain_runtime::{
16 ports::{
17 DbCommandsPort,
18 DbQueriesPort,
19 },
20 storage::ColumnFamilies,
21};
22use std::sync::Arc;
23use storage::{
24 database::GenericDatabase,
25 key_value::KeyValueStorage,
26};
27
28pub use storage::{
30 database::StorageError,
31 key_value::KeyValueStorageWithColumnFamilies,
32};
33
34pub mod options;
36
37#[derive(Debug)]
47pub struct Database<Storage: KeyValueStorage> {
48 storage: Arc<Storage>,
50}
51
52impl<Storage: KeyValueStorageWithColumnFamilies> Clone for Database<Storage> {
53 fn clone(&self) -> Self {
54 Self {
55 storage: self.storage.clone(),
56 }
57 }
58}
59
60impl<Storage: KeyValueStorageWithColumnFamilies> Database<Storage> {
61 pub fn new(db: Storage) -> Self {
63 let db = Arc::new(db);
64 Self { storage: db }
65 }
66}
67
68impl<Storage: KeyValueStorageWithColumnFamilies> GenericDatabase for Database<Storage> {
69 type Storage = Storage;
70
71 fn storage(&self) -> &Self::Storage {
72 &self.storage
73 }
74}
75
76impl<S> DbQueriesPort for Database<S> where
77 S: KeyValueStorageWithColumnFamilies<
78 ColumnFamilyIdentifier = ColumnFamilies,
79 Error = StorageError,
80 > + Send
81 + Sync
82 + 'static
83{
84}
85
86impl<S> DbCommandsPort for Database<S> where
87 S: KeyValueStorageWithColumnFamilies<
88 ColumnFamilyIdentifier = ColumnFamilies,
89 Error = StorageError,
90 > + Send
91 + Sync
92 + 'static
93{
94}
95
96#[cfg(not(feature = "rocksdb"))]
100pub(crate) type DefaultStorage = in_memory::Storage;
101#[cfg(feature = "rocksdb")]
105pub(crate) type DefaultStorage = rocksdb::Storage;
106
107pub mod in_memory {
109 use super::*;
110 use storage::in_memory::StaticInMemoryStorage;
111
112 pub type Storage = StaticInMemoryStorage<ColumnFamilies>;
114}
115
116#[cfg(feature = "rocksdb")]
118pub mod rocksdb {
119 use super::*;
120 use storage::database::spec::{
121 DbSpec as DbSpecTrait,
122 StaticDbSpec,
123 };
124 use storage_rocksdb::RocksDb;
125
126 #[derive(Debug, Clone, Copy)]
128 pub struct DbSpec;
129
130 impl DbSpecTrait for DbSpec {
131 const NAME: &'static str = "DomainDb";
132 }
133
134 impl StaticDbSpec for DbSpec {
135 type ColumnFamilyIdentifier = ColumnFamilies;
136 }
137
138 pub type Storage = RocksDb<DbSpec>;
140}