1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::prelude::*;
use holochain_serialized_bytes::prelude::*;
use once_cell::sync::Lazy;
use rusqlite::*;
use scheduled_thread_pool::ScheduledThreadPool;
use std::{
any::Any,
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
const SQLITE_BUSY_TIMEOUT: Duration = Duration::from_secs(30);
pub(crate) struct Databases {
dbs: parking_lot::RwLock<HashMap<PathBuf, Box<dyn Any + Send + Sync>>>,
}
pub(crate) static DATABASE_HANDLES: Lazy<Databases> = Lazy::new(|| {
let orig_handler = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
eprintln!("FATAL PANIC {:#?}", panic_info);
orig_handler(panic_info);
}));
Databases::new()
});
static R2D2_THREADPOOL: Lazy<Arc<ScheduledThreadPool>> = Lazy::new(|| {
let t = ScheduledThreadPool::new(1);
Arc::new(t)
});
pub type ConnectionPool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
pub type PConnInner = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;
impl Databases {
pub fn new() -> Self {
Databases {
dbs: parking_lot::RwLock::new(HashMap::new()),
}
}
pub fn get_or_insert<Kind, F>(
&self,
kind: &Kind,
path_prefix: &Path,
insert: F,
) -> DatabaseResult<DbWrite<Kind>>
where
Kind: DbKindT + Send + Sync + 'static,
F: FnOnce(Kind) -> DatabaseResult<DbWrite<Kind>>,
{
let path = path_prefix.join(kind.filename());
let ret = self
.dbs
.read()
.get(&path)
.and_then(|d| d.downcast_ref::<DbWrite<Kind>>().cloned());
match ret {
Some(ret) => Ok(ret),
None => match self.dbs.write().entry(path) {
std::collections::hash_map::Entry::Occupied(o) => Ok(o
.get()
.downcast_ref::<DbWrite<Kind>>()
.expect("Downcast to db kind failed. This is a bug")
.clone()),
std::collections::hash_map::Entry::Vacant(v) => {
let db = insert(kind.clone())?;
v.insert(Box::new(db.clone()));
Ok(db)
}
},
}
}
}
pub(crate) fn new_connection_pool(
path: Option<&Path>,
synchronous_level: DbSyncLevel,
) -> ConnectionPool {
use r2d2_sqlite::SqliteConnectionManager;
let manager = match path {
Some(path) => SqliteConnectionManager::file(path),
None => SqliteConnectionManager::memory(),
};
let customizer = Box::new(ConnCustomizer { synchronous_level });
let max_cons = num_read_threads() + 1;
r2d2::Pool::builder()
.max_size(max_cons as u32)
.min_idle(Some(0))
.idle_timeout(Some(Duration::from_secs(30)))
.thread_pool(R2D2_THREADPOOL.clone())
.connection_customizer(customizer)
.build(manager)
.unwrap()
}
#[derive(Debug)]
struct ConnCustomizer {
synchronous_level: DbSyncLevel,
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq)]
pub enum DbSyncLevel {
Full,
Normal,
Off,
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq)]
pub enum DbSyncStrategy {
Fast,
Resilient,
}
impl Default for DbSyncLevel {
fn default() -> Self {
DbSyncLevel::Normal
}
}
impl Default for DbSyncStrategy {
fn default() -> Self {
DbSyncStrategy::Fast
}
}
impl r2d2::CustomizeConnection<Connection, rusqlite::Error> for ConnCustomizer {
fn on_acquire(&self, conn: &mut Connection) -> Result<(), rusqlite::Error> {
initialize_connection(conn, self.synchronous_level)?;
Ok(())
}
}
pub(crate) fn initialize_connection(
conn: &mut Connection,
synchronous_level: DbSyncLevel,
) -> rusqlite::Result<()> {
conn.busy_timeout(SQLITE_BUSY_TIMEOUT)?;
#[cfg(feature = "db-encryption")]
{
use std::io::Write;
let key = get_encryption_key_shim();
let mut hex = *br#"0000000000000000000000000000000000000000000000000000000000000000"#;
let mut c = std::io::Cursor::new(&mut hex[..]);
for b in &key {
write!(c, "{:02X}", b)
.map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;
}
let _keyval = std::str::from_utf8(&hex).unwrap();
const FAKE_KEY: &str = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648101010101010101010101010101010101'";
conn.pragma_update(None, "key", &FAKE_KEY)?;
}
conn.pragma_update(None, "trusted_schema", &false)?;
conn.pragma_update(None, "foreign_keys", &"ON".to_string())?;
match synchronous_level {
DbSyncLevel::Full => conn.pragma_update(None, "synchronous", &"2".to_string())?,
DbSyncLevel::Normal => conn.pragma_update(None, "synchronous", &"1".to_string())?,
DbSyncLevel::Off => conn.pragma_update(None, "synchronous", &"0".to_string())?,
}
Ok(())
}
#[cfg(feature = "db-encryption")]
fn get_encryption_key_shim() -> [u8; 32] {
[
26, 111, 7, 31, 52, 204, 156, 103, 203, 171, 156, 89, 98, 51, 158, 143, 57, 134, 93, 56,
199, 225, 53, 141, 39, 77, 145, 130, 136, 108, 96, 201,
]
}
#[derive(shrinkwraprs::Shrinkwrap)]
#[shrinkwrap(mutable, unsafe_ignore_visibility)]
pub struct PConn {
#[shrinkwrap(main_field)]
inner: PConnInner,
}
impl PConn {
pub(crate) fn new(inner: PConnInner) -> Self {
Self { inner }
}
}