Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for different SQLite journal modes #193

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion mls-rs-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ x509 = ["mls-rs-identity-x509"]
mls-rs = { path = "../mls-rs", version = "0.41.2", features = ["ffi"] }
mls-rs-crypto-openssl = { path = "../mls-rs-crypto-openssl", version = "0.10.0", optional = true }
mls-rs-identity-x509 = { path = "../mls-rs-identity-x509", version = "0.12.0", optional = true }
mls-rs-provider-sqlite = { path = "../mls-rs-provider-sqlite", version = "0.13.0", default-features = false, optional = true }
mls-rs-provider-sqlite = { path = "../mls-rs-provider-sqlite", version = "0.14.0", default-features = false, optional = true }
safer-ffi = { version = "0.1.7", default-features = false }
safer-ffi-gen = { version = "0.9.2", default-features = false }
2 changes: 1 addition & 1 deletion mls-rs-provider-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mls-rs-provider-sqlite"
version = "0.13.0"
version = "0.14.0"
stefunctional marked this conversation as resolved.
Show resolved Hide resolved
edition = "2021"
description = "SQLite based state storage for mls-rs"
homepage = "https://github.com/awslabs/mls-rs"
Expand Down
2 changes: 1 addition & 1 deletion mls-rs-provider-sqlite/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod tests {
}

fn test_storage() -> SqLiteApplicationStorage {
SqLiteDataStorageEngine::new(MemoryStrategy)
SqLiteDataStorageEngine::new(MemoryStrategy, None)
.unwrap()
.application_data_storage()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion mls-rs-provider-sqlite/src/group_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ mod tests {
use super::*;

fn get_test_storage() -> SqLiteGroupStateStorage {
SqLiteDataStorageEngine::new(MemoryStrategy)
SqLiteDataStorageEngine::new(MemoryStrategy, None)
.unwrap()
.group_state_storage()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion mls-rs-provider-sqlite/src/key_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod tests {
use mls_rs_core::{crypto::HpkeSecretKey, key_package::KeyPackageData};

fn test_storage() -> SqLiteKeyPackageStorage {
SqLiteDataStorageEngine::new(MemoryStrategy)
SqLiteDataStorageEngine::new(MemoryStrategy, None)
.unwrap()
.key_package_storage()
.unwrap()
Expand Down
50 changes: 49 additions & 1 deletion mls-rs-provider-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,39 @@ impl mls_rs_core::error::IntoAnyError for SqLiteDataStorageError {
}
}

#[derive(Clone, Debug)]
pub enum JournalMode {
DELETE,
TRUNCATE,
PERSIST,
MEMORY,
WAL,
OFF
stefunctional marked this conversation as resolved.
Show resolved Hide resolved
}

// Note: for in-memory dbs (such as what the tests use), the only available options are MEMORY or OFF
// Invalid modes do not error, only no-op
stefunctional marked this conversation as resolved.
Show resolved Hide resolved
impl JournalMode {
fn as_str(&self) -> &'static str {
match self {
JournalMode::DELETE => "DELETE",
JournalMode::TRUNCATE => "TRUNCATE",
JournalMode::PERSIST => "PERSIST",
JournalMode::MEMORY => "MEMORY",
JournalMode::WAL => "WAL",
JournalMode::OFF => "OFF",
}
}
}

#[derive(Clone, Debug)]
/// SQLite data storage engine.
pub struct SqLiteDataStorageEngine<CS>
where
CS: ConnectionStrategy,
{
connection_strategy: CS,
journal_mode: Option<JournalMode>,
}

impl<CS> SqLiteDataStorageEngine<CS>
Expand All @@ -69,9 +95,11 @@ where
{
pub fn new(
connection_strategy: CS,
journal_mode: Option<JournalMode>,
stefunctional marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<SqLiteDataStorageEngine<CS>, SqLiteDataStorageError> {
Ok(SqLiteDataStorageEngine {
connection_strategy,
journal_mode,
})
}

Expand All @@ -83,6 +111,12 @@ where
.pragma_query_value(None, "user_version", |rows| rows.get::<_, u32>(0))
.map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))?;

if let Some(journal_mode) = &self.journal_mode {
connection
.pragma_update(None, "journal_mode", journal_mode.as_str())
.map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))?;
}

if current_schema != 1 {
create_tables_v1(&connection)?;
}
Expand Down Expand Up @@ -156,7 +190,7 @@ mod tests {

#[test]
pub fn user_version_test() {
let database = SqLiteDataStorageEngine::new(MemoryStrategy).unwrap();
let database = SqLiteDataStorageEngine::new(MemoryStrategy, None).unwrap();

let _connection = database.create_connection().unwrap();

Expand All @@ -170,4 +204,18 @@ mod tests {

assert_eq!(current_schema, 1);
}

#[test]
pub fn journal_mode_test() {
// Connect with journal_mode other than default (OFF)
let database = SqLiteDataStorageEngine::new(MemoryStrategy, Some(crate::JournalMode::OFF)).unwrap();

let connection = database.create_connection().unwrap();

let journal_mode = connection
.pragma_query_value(None, "journal_mode", |rows| rows.get::<_, String>(0))
.unwrap();

assert_eq!(journal_mode, "off");
}
}
2 changes: 1 addition & 1 deletion mls-rs-provider-sqlite/src/psk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod tests {
}

fn test_storage() -> SqLitePreSharedKeyStorage {
SqLiteDataStorageEngine::new(MemoryStrategy)
SqLiteDataStorageEngine::new(MemoryStrategy, None)
.unwrap()
.pre_shared_key_storage()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion mls-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ spin = { version = "0.9.8", default-features = false, features = ["mutex", "spin
maybe-async = { version = "0.2.10" }

# Optional dependencies
mls-rs-provider-sqlite = { path = "../mls-rs-provider-sqlite", version = "0.13.0", default-features = false, optional = true }
mls-rs-provider-sqlite = { path = "../mls-rs-provider-sqlite", version = "0.14.0", default-features = false, optional = true }
mls-rs-crypto-openssl = { path = "../mls-rs-crypto-openssl", optional = true, version = "0.10.0" }
# TODO: https://github.com/GoogleChromeLabs/wasm-bindgen-rayon
rayon = { version = "1", optional = true }
Expand Down
Loading