Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 4 additions & 81 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ itertools = "0.14.0"
rmp-serde = "1.3.0"
security-framework-sys = { version = "2.14.0", optional = true }
rusqlite_migration = "2.3.0"
r2d2 = "0.8.10"
r2d2_sqlite = { version = "0.31.0", features = ["bundled"] }
rusqlite = { version = "0.37.0", features = ["bundled"] }

[dev-dependencies]
color-eyre = "0.6.3"
Expand Down
70 changes: 30 additions & 40 deletions src/storage/storage_backend/sqlite_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ use core::fmt;
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
sync::Arc,
sync::{Arc, Mutex},
};

use itertools::Itertools;
use r2d2::{Builder, Pool};
use r2d2_sqlite::{
rusqlite::{self, named_params},
SqliteConnectionManager,
};
use rusqlite::{named_params, Connection};
use rusqlite_migration::{Migrations, M};
use thiserror::Error;

Expand All @@ -25,8 +21,8 @@ pub enum SqliteBackendError {
SqlError(#[from] rusqlite::Error),
#[error("Key not found.")]
NoKeyError,
#[error("Failed to acquire SQLite pooled connection.")]
Acquire { source: r2d2::Error },
#[error("Failed to acquire lock to SQLite connection.")]
Acquire,
}

#[derive(Error, Debug)]
Expand All @@ -41,14 +37,15 @@ pub enum SqliteBackendInitializationError {
#[error("Failed to set SQLite pragmas: {source}")]
Pragma { source: rusqlite::Error },
#[error("Failed opening sqlite database with path '{path:?}' and error: {source}")]
Open { source: r2d2::Error, path: PathBuf },
#[error("Failed to acquire SQLite pooled connection.")]
Acquire { source: r2d2::Error },
Open {
source: rusqlite::Error,
path: PathBuf,
},
}

#[derive(Clone)]
pub(in crate::storage) struct SqliteBackend {
pool: Arc<Pool<SqliteConnectionManager>>,
connection: Arc<Mutex<Connection>>,
}

const MIGRATIONS_SLICE: &[M<'_>] = &[
Expand All @@ -67,25 +64,18 @@ impl SqliteBackend {

tracing::trace!("opening sql db: {:?}", path);

let manager = SqliteConnectionManager::file(&path).with_init(|conn| {
conn.pragma_update(None, "journal_mode", "WAL")?;
conn.pragma_update(None, "synchronous", "NORMAL")?;
conn.pragma_update(None, "busy_timeout", "15000")?;

Ok(())
});
let pool = Builder::new().build(manager).map_err(|err| {
SqliteBackendInitializationError::Open {
let mut conn =
Connection::open(&path).map_err(|err| SqliteBackendInitializationError::Open {
source: err,
path: path,
}
})?;
})?;

// This is ok, as conn only is mut to hinder nested transactions on one connection.
// See `rusqlite::Transaction` for more info.
let mut conn = pool
.get()
.map_err(|err| SqliteBackendInitializationError::Acquire { source: err })?;
conn.pragma_update(None, "journal_mode", "WAL")
.map_err(|err| SqliteBackendInitializationError::Pragma { source: err })?;
conn.pragma_update(None, "synchronous", "NORMAL")
.map_err(|err| SqliteBackendInitializationError::Pragma { source: err })?;
conn.pragma_update(None, "busy_timeout", "15000")
.map_err(|err| SqliteBackendInitializationError::Pragma { source: err })?;

match MIGRATIONS.to_latest(&mut conn) {
Ok(_) => (),
Expand All @@ -104,7 +94,7 @@ impl SqliteBackend {
}

Ok(Self {
pool: Arc::new(pool),
connection: Arc::new(Mutex::new(conn)),
})
}
}
Expand All @@ -117,9 +107,9 @@ impl fmt::Debug for SqliteBackend {

impl StorageBackend for SqliteBackend {
fn store(&self, key: ScopedKey, data: &[u8]) -> Result<(), StorageBackendError> {
self.pool
.get()
.map_err(|err| SqliteBackendError::Acquire { source: err })?
self.connection
.lock()
.map_err(|_| SqliteBackendError::Acquire)?
.execute(
"INSERT INTO keys (id, provider, encryption_key_id, signature_key_id, data_blob)
VALUES (:id, :provider, :encryption_key_id, :signature_key_id, :data_blob)
Expand All @@ -142,9 +132,9 @@ impl StorageBackend for SqliteBackend {
"SELECT data_blob FROM keys WHERE id = :id AND provider = :provider AND encryption_key_id = :encryption_key_id AND signature_key_id = :signature_key_id;";

let result = self
.pool
.get()
.map_err(|err| SqliteBackendError::Acquire { source: err })?
.connection
.lock()
.map_err(|_| SqliteBackendError::Acquire)?
.query_one(
query,
named_params! {
Expand All @@ -169,9 +159,9 @@ impl StorageBackend for SqliteBackend {
let query =
"DELETE FROM keys WHERE id = :id AND provider = :provider AND encryption_key_id = :encryption_key_id AND signature_key_id = :signature_key_id;";

self.pool
.get()
.map_err(|err| SqliteBackendError::Acquire { source: err })?
self.connection
.lock()
.map_err(|_| SqliteBackendError::Acquire)?
.execute(
query,
named_params! {
Expand All @@ -186,9 +176,9 @@ impl StorageBackend for SqliteBackend {
}

fn keys(&self) -> Vec<Result<ScopedKey, StorageBackendError>> {
let conn = match self.pool.get() {
let conn = match self.connection.lock() {
Ok(c) => c,
Err(err) => return vec![Err(SqliteBackendError::Acquire { source: err }.into())],
Err(_) => return vec![Err(SqliteBackendError::Acquire.into())],
};

let query = "SELECT id, provider, encryption_key_id, signature_key_id FROM keys;";
Expand Down
Loading