-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsqlitepool.rs
396 lines (329 loc) · 12.8 KB
/
sqlitepool.rs
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use std::{sync::Arc, time::Duration};
use async_trait::async_trait;
use snafu::{prelude::*, ResultExt};
use tokio_rusqlite::{Connection, ToSql};
use super::{DbConnectionPool, Result};
use crate::sql::db_connection_pool::{
dbconnection::{sqliteconn::SqliteConnection, AsyncDbConnection, DbConnection},
JoinPushDown, Mode,
};
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("ConnectionPoolError: {source}"))]
ConnectionPoolError { source: tokio_rusqlite::Error },
#[snafu(display("No path provided for SQLite connection"))]
NoPathError {},
#[snafu(display("Database to attach does not exist: {path}"))]
DatabaseDoesNotExist { path: String },
}
pub struct SqliteConnectionPoolFactory {
path: Arc<str>,
mode: Mode,
attach_databases: Option<Vec<Arc<str>>>,
busy_timeout: Duration,
}
impl SqliteConnectionPoolFactory {
pub fn new(path: &str, mode: Mode, busy_timeout: Duration) -> Self {
SqliteConnectionPoolFactory {
path: path.into(),
mode,
attach_databases: None,
busy_timeout,
}
}
#[must_use]
pub fn with_databases(mut self, attach_databases: Option<Vec<Arc<str>>>) -> Self {
self.attach_databases = attach_databases;
self
}
pub async fn build(&self) -> Result<SqliteConnectionPool> {
let join_push_down = match (self.mode, &self.attach_databases) {
(Mode::File, Some(attach_databases)) => {
if attach_databases.is_empty() {
JoinPushDown::AllowedFor(self.path.to_string())
} else {
let mut attach_databases = attach_databases.clone();
for database in &attach_databases {
// check if the database file exists
if std::fs::metadata(database.as_ref()).is_err() {
return Err(Error::DatabaseDoesNotExist {
path: database.to_string(),
}
.into());
}
}
if !attach_databases.contains(&self.path) {
attach_databases.push(Arc::clone(&self.path));
}
attach_databases.sort();
JoinPushDown::AllowedFor(attach_databases.join(";")) // push down is allowed cross-database when they're attached together
}
}
(Mode::File, None) => JoinPushDown::AllowedFor(self.path.to_string()),
_ => JoinPushDown::Disallow,
};
let attach_databases = if let Some(attach_databases) = &self.attach_databases {
attach_databases.clone()
} else {
vec![]
};
let pool = SqliteConnectionPool::new(
&self.path,
self.mode,
join_push_down,
attach_databases,
self.busy_timeout,
)
.await?;
pool.setup().await?;
Ok(pool)
}
}
#[derive(Debug)]
pub struct SqliteConnectionPool {
conn: Connection,
join_push_down: JoinPushDown,
mode: Mode,
path: Arc<str>,
attach_databases: Vec<Arc<str>>,
busy_timeout: Duration,
}
impl SqliteConnectionPool {
/// Creates a new instance of `SqliteConnectionPool`.
///
/// NOTE: The `SqliteConnectionPool` currently does no connection pooling, it simply creates a new connection
/// and clones it on each call to `connect()`.
///
/// # Errors
///
/// Returns an error if there is a problem creating the connection pool.
#[allow(clippy::needless_pass_by_value)]
pub async fn new(
path: &str,
mode: Mode,
join_push_down: JoinPushDown,
attach_databases: Vec<Arc<str>>,
busy_timeout: Duration,
) -> Result<Self> {
let conn = match mode {
Mode::Memory => Connection::open_in_memory()
.await
.context(ConnectionPoolSnafu)?,
Mode::File => Connection::open(path.to_string())
.await
.context(ConnectionPoolSnafu)?,
};
Ok(SqliteConnectionPool {
conn,
join_push_down,
mode,
attach_databases,
path: path.into(),
busy_timeout,
})
}
/// Initializes an SQLite database on-disk without creating a connection pool.
/// No-op if the database is in-memory.
pub async fn init(path: &str, mode: Mode) -> Result<()> {
if mode == Mode::File {
Connection::open(path.to_string())
.await
.context(ConnectionPoolSnafu)?;
}
Ok(())
}
pub async fn setup(&self) -> Result<()> {
let conn = self.conn.clone();
let busy_timeout = self.busy_timeout;
// these configuration options are only applicable for file-mode databases
if self.mode == Mode::File {
// change transaction mode to Write-Ahead log instead of default atomic rollback journal: https://www.sqlite.org/wal.html
// NOTE: This is a no-op if the database is in-memory, as only MEMORY or OFF are supported: https://www.sqlite.org/pragma.html#pragma_journal_mode
conn.call(move |conn| {
conn.pragma_update(None, "journal_mode", "WAL")?;
conn.pragma_update(None, "synchronous", "NORMAL")?;
conn.pragma_update(None, "cache_size", "-20000")?;
conn.pragma_update(None, "foreign_keys", "true")?;
conn.pragma_update(None, "temp_store", "memory")?;
// conn.set_transaction_behavior(TransactionBehavior::Immediate); introduced in rustqlite 0.32.1, but tokio-rusqlite is still on 0.31.0
// Set user configurable connection timeout
conn.busy_timeout(busy_timeout)?;
Ok(())
})
.await
.context(ConnectionPoolSnafu)?;
// database attachments are only supported for file-mode databases
#[cfg(feature = "sqlite-federation")]
{
let attach_databases = self
.attach_databases
.iter()
.enumerate()
.map(|(i, db)| format!("ATTACH DATABASE '{db}' AS attachment_{i}"));
for attachment in attach_databases {
if attachment == *self.path {
continue;
}
conn.call(move |conn| {
conn.execute(&attachment, [])?;
Ok(())
})
.await
.context(ConnectionPoolSnafu)?;
}
Ok::<(), super::Error>(())
}?;
}
Ok(())
}
#[must_use]
pub fn connect_sync(&self) -> Box<dyn DbConnection<Connection, &'static (dyn ToSql + Sync)>> {
Box::new(SqliteConnection::new(self.conn.clone()))
}
/// Will attempt to clone the connection pool. This will always succeed for in-memory mode.
/// For file-mode, it will attempt to create a new connection pool with the same configuration.
///
/// Due to the way the connection pool is implemented, it doesn't allow multiple concurrent reads/writes
/// using the same connection pool instance.
pub async fn try_clone(&self) -> Result<Self> {
match self.mode {
Mode::Memory => Ok(SqliteConnectionPool {
conn: self.conn.clone(),
join_push_down: self.join_push_down.clone(),
mode: self.mode,
path: Arc::clone(&self.path),
attach_databases: self.attach_databases.clone(),
busy_timeout: self.busy_timeout,
}),
Mode::File => {
let attach_databases = if self.attach_databases.is_empty() {
None
} else {
Some(self.attach_databases.clone())
};
SqliteConnectionPoolFactory::new(&self.path, self.mode, self.busy_timeout)
.with_databases(attach_databases)
.build()
.await
}
}
}
}
#[async_trait]
impl DbConnectionPool<Connection, &'static (dyn ToSql + Sync)> for SqliteConnectionPool {
async fn connect(
&self,
) -> Result<Box<dyn DbConnection<Connection, &'static (dyn ToSql + Sync)>>> {
let conn = self.conn.clone();
Ok(Box::new(SqliteConnection::new(conn)))
}
fn join_push_down(&self) -> JoinPushDown {
self.join_push_down.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sql::db_connection_pool::Mode;
use rand::Rng;
use rstest::rstest;
use std::time::Duration;
fn random_db_name() -> String {
let mut rng = rand::thread_rng();
let mut name = String::new();
for _ in 0..10 {
name.push(rng.gen_range(b'a'..=b'z') as char);
}
format!("./{name}.sqlite")
}
#[rstest]
#[tokio::test]
async fn test_sqlite_connection_pool_factory() {
let db_name = random_db_name();
let factory = SqliteConnectionPoolFactory::new(&db_name, Mode::File, Duration::default());
let pool = factory.build().await.unwrap();
assert!(pool.join_push_down == JoinPushDown::AllowedFor(db_name.clone()));
assert!(pool.mode == Mode::File);
assert_eq!(pool.path, db_name.clone().into());
drop(pool);
// cleanup
std::fs::remove_file(&db_name).unwrap();
}
#[tokio::test]
async fn test_sqlite_connection_pool_factory_with_attachments() {
let mut db_names = [random_db_name(), random_db_name(), random_db_name()];
db_names.sort();
let factory =
SqliteConnectionPoolFactory::new(&db_names[0], Mode::File, Duration::from_millis(5000))
.with_databases(Some(vec![
db_names[1].clone().into(),
db_names[2].clone().into(),
]));
SqliteConnectionPool::init(&db_names[1], Mode::File)
.await
.unwrap();
SqliteConnectionPool::init(&db_names[2], Mode::File)
.await
.unwrap();
let pool = factory.build().await.unwrap();
let push_down = db_names.join(";");
assert!(pool.join_push_down == JoinPushDown::AllowedFor(push_down));
assert!(pool.mode == Mode::File);
assert_eq!(pool.path, db_names[0].clone().into());
drop(pool);
// cleanup
for db in &db_names {
std::fs::remove_file(db).unwrap();
}
}
#[tokio::test]
async fn test_sqlite_connection_pool_factory_with_empty_attachments() {
let db_name = random_db_name();
let factory =
SqliteConnectionPoolFactory::new(&db_name, Mode::File, Duration::from_millis(5000))
.with_databases(Some(vec![]));
let pool = factory.build().await.unwrap();
assert!(pool.join_push_down == JoinPushDown::AllowedFor(db_name.clone()));
assert!(pool.mode == Mode::File);
assert_eq!(pool.path, db_name.clone().into());
drop(pool);
// cleanup
std::fs::remove_file(&db_name).unwrap();
}
#[tokio::test]
async fn test_sqlite_connection_pool_factory_memory_with_attachments() {
let factory = SqliteConnectionPoolFactory::new(
"./test.sqlite",
Mode::Memory,
Duration::from_millis(5000),
)
.with_databases(Some(vec!["./test1.sqlite".into(), "./test2.sqlite".into()]));
let pool = factory.build().await.unwrap();
assert!(pool.join_push_down == JoinPushDown::Disallow);
assert!(pool.mode == Mode::Memory);
assert_eq!(pool.path, "./test.sqlite".into());
drop(pool);
// in memory mode, attachments are not created and nothing happens
assert!(std::fs::metadata("./test.sqlite").is_err());
assert!(std::fs::metadata("./test1.sqlite").is_err());
assert!(std::fs::metadata("./test2.sqlite").is_err());
}
#[tokio::test]
async fn test_sqlite_connection_pool_factory_errors_with_missing_attachments() {
let mut db_names = [random_db_name(), random_db_name(), random_db_name()];
db_names.sort();
let factory =
SqliteConnectionPoolFactory::new(&db_names[0], Mode::File, Duration::from_millis(5000))
.with_databases(Some(vec![
db_names[1].clone().into(),
db_names[2].clone().into(),
]));
let pool = factory.build().await;
assert!(pool.is_err());
let err = pool.err().unwrap();
assert!(err.to_string().contains(&format!(
"Database to attach does not exist: {}",
db_names[1]
)));
}
}