holochain_sqlite/db/
kind.rs

1use holo_hash::DnaHash;
2use holochain_zome_types::cell::CellId;
3use std::path::PathBuf;
4use std::sync::Arc;
5
6/// The various types of database, used to specify the list of databases to initialize
7#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
8pub enum DbKind {
9    /// Specifies the environment used for authoring data by all cells on the same [`DnaHash`].
10    #[display(fmt = "{:?}-{:?}", "_0.dna_hash()", "_0.agent_pubkey()")]
11    Authored(Arc<CellId>),
12    /// Specifies the environment used for dht data by all cells on the same [`DnaHash`].
13    #[display(fmt = "{:?}", "_0")]
14    Dht(Arc<DnaHash>),
15    /// Specifies the environment used by each Cache (one per dna).
16    #[display(fmt = "{:?}", "_0")]
17    Cache(Arc<DnaHash>),
18    /// Specifies the environment used by a Conductor
19    Conductor,
20    /// Specifies the environment used to save wasm
21    Wasm,
22    /// Metadata about peers, for tracking local state and observations about peers.
23    PeerMetaStore(Arc<DnaHash>),
24    #[cfg(feature = "test_utils")]
25    Test(String),
26}
27
28pub trait DbKindT: Clone + std::fmt::Debug + Send + Sync + 'static {
29    fn kind(&self) -> DbKind;
30
31    /// Construct a partial Path based on the kind
32    fn filename(&self) -> PathBuf {
33        self.filename_inner()
34    }
35
36    /// The above provided `filename` method attaches the .sqlite3 extension.
37    /// Implement this to provide the front part of the database filename.
38    fn filename_inner(&self) -> PathBuf;
39
40    /// Whether to wipe the database if it is corrupt.
41    /// Some database it's safe to wipe them if they are corrupt because
42    /// they can be refilled from the network. Other databases cannot
43    /// be refilled and some manual intervention is required.
44    fn if_corrupt_wipe(&self) -> bool;
45}
46
47pub trait DbKindOp {}
48
49#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
50/// Specifies the environment used for authoring data by all cells on the same [`DnaHash`].
51pub struct DbKindAuthored(pub Arc<CellId>);
52
53#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
54/// Specifies the environment used for dht data by all cells on the same [`DnaHash`].
55pub struct DbKindDht(pub Arc<DnaHash>);
56
57#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
58/// Specifies the environment used by each Cache (one per dna).
59pub struct DbKindCache(pub Arc<DnaHash>);
60
61#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
62/// Specifies the environment used by a Conductor
63pub struct DbKindConductor;
64
65#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
66/// Specifies the environment used to save wasm
67pub struct DbKindWasm;
68
69/// Database kind for [DbKind::PeerMetaStore]
70#[derive(Clone, Debug, PartialEq, Eq, Hash, derive_more::Display)]
71pub struct DbKindPeerMetaStore(pub Arc<DnaHash>);
72
73impl DbKindT for DbKindAuthored {
74    fn kind(&self) -> DbKind {
75        DbKind::Authored(self.0.clone())
76    }
77
78    fn filename_inner(&self) -> PathBuf {
79        [
80            "authored",
81            &format!("{}-{}", self.0.dna_hash(), self.0.agent_pubkey()),
82        ]
83        .iter()
84        .collect()
85    }
86
87    fn if_corrupt_wipe(&self) -> bool {
88        false
89    }
90}
91
92impl DbKindOp for DbKindAuthored {}
93
94impl DbKindAuthored {
95    pub fn dna_hash(&self) -> &DnaHash {
96        self.0.dna_hash()
97    }
98}
99
100impl DbKindT for DbKindDht {
101    fn kind(&self) -> DbKind {
102        DbKind::Dht(self.0.clone())
103    }
104
105    fn filename_inner(&self) -> PathBuf {
106        ["dht", &self.0.to_string()].iter().collect()
107    }
108
109    fn if_corrupt_wipe(&self) -> bool {
110        true
111    }
112}
113
114impl DbKindOp for DbKindDht {}
115
116impl DbKindDht {
117    pub fn dna_hash(&self) -> &DnaHash {
118        &self.0
119    }
120    pub fn to_dna_hash(&self) -> Arc<DnaHash> {
121        self.0.clone()
122    }
123}
124
125impl DbKindT for DbKindCache {
126    fn kind(&self) -> DbKind {
127        DbKind::Cache(self.0.clone())
128    }
129
130    fn filename_inner(&self) -> PathBuf {
131        ["cache", &self.0.to_string()].iter().collect()
132    }
133
134    fn if_corrupt_wipe(&self) -> bool {
135        true
136    }
137}
138
139impl DbKindCache {
140    pub fn dna_hash(&self) -> &DnaHash {
141        &self.0
142    }
143    pub fn to_dna_hash(&self) -> Arc<DnaHash> {
144        self.0.clone()
145    }
146}
147
148impl DbKindOp for DbKindCache {}
149
150impl DbKindT for DbKindConductor {
151    fn kind(&self) -> DbKind {
152        DbKind::Conductor
153    }
154
155    fn filename_inner(&self) -> PathBuf {
156        ["conductor", "conductor"].iter().collect()
157    }
158
159    fn if_corrupt_wipe(&self) -> bool {
160        false
161    }
162}
163
164impl DbKindT for DbKindWasm {
165    fn kind(&self) -> DbKind {
166        DbKind::Wasm
167    }
168
169    fn filename_inner(&self) -> PathBuf {
170        ["wasm", "wasm"].iter().collect()
171    }
172
173    fn if_corrupt_wipe(&self) -> bool {
174        false
175    }
176}
177
178impl DbKindT for DbKindPeerMetaStore {
179    fn kind(&self) -> DbKind {
180        DbKind::PeerMetaStore(self.0.clone())
181    }
182
183    fn filename_inner(&self) -> PathBuf {
184        ["p2p", &format!("peer-meta-{}", self.0)].iter().collect()
185    }
186
187    fn if_corrupt_wipe(&self) -> bool {
188        true
189    }
190}