Skip to content

Commit

Permalink
proto scaffolding for just using a Mutex on SqliteConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Nov 10, 2023
1 parent 6706885 commit 1fc724e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xmtp_mls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ native = ["libsqlite3-sys/bundled-sqlcipher-vendored-openssl"]
[dependencies]
anyhow = "1.0.71"
async-trait = "0.1.68"
diesel = { version = "2.1.3", features = ["sqlite", "r2d2", "returning_clauses_for_sqlite_3_35"] }
diesel = { version = "2.1.3", features = ["sqlite", "r2d2", "returning_clauses_for_sqlite_3_35", "i-implement-a-third-party-backend-and-opt-into-breaking-changes"] }
diesel_migrations = { version = "2.1.0", features = ["sqlite"] }
ethers = "2.0.4"
ethers-core = "2.0.4"
Expand Down
108 changes: 108 additions & 0 deletions xmtp_mls/src/storage/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//! An SqliteConnection wrapped in a Arc/Mutex to make it Sync
use std::sync::{Arc, Mutex};

use diesel::{
associations::HasTable,
connection::{
AnsiTransactionManager, ConnectionSealed, DefaultLoadingMode, LoadConnection,

Check warning on line 8 in xmtp_mls/src/storage/connection.rs

View workflow job for this annotation

GitHub Actions / Test

unused imports: `DefaultLoadingMode`, `LoadConnection`
SimpleConnection, TransactionManager,
},
expression::{is_aggregate, MixedAggregates, ValidGrouping},
helper_types::{Find, Update},
prelude::{Connection, Identifiable, SqliteConnection},
query_builder::{AsChangeset, IntoUpdateTarget, QueryFragment, QueryId},
query_dsl::{
methods::{ExecuteDsl, FindDsl, LoadQuery},
UpdateAndFetchResults,
},
r2d2::R2D2Connection,
sqlite::Sqlite,
ConnectionResult, QueryResult, Table,
};

struct SyncSqliteConnection {
inner: Arc<Mutex<SqliteConnection>>,
}

/// This is safe because all operations happen through Arc<Mutex<T>>
unsafe impl Sync for SyncSqliteConnection {}

impl Connection for SyncSqliteConnection {
type Backend = Sqlite;
type TransactionManager = AnsiTransactionManager;

fn establish(database_url: &str) -> ConnectionResult<Self> {

Check warning on line 35 in xmtp_mls/src/storage/connection.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `database_url`
todo!()
}

fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>

Check warning on line 39 in xmtp_mls/src/storage/connection.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `source`
where
T: QueryFragment<Self::Backend> + QueryId,
{
todo!()
}

fn transaction_state(
&mut self,
) -> &mut <Self::TransactionManager as TransactionManager<Self>>::TransactionStateData {
todo!()
}
}

impl ConnectionSealed for SyncSqliteConnection {}

impl SimpleConnection for SyncSqliteConnection {
fn batch_execute(&mut self, query: &str) -> QueryResult<()> {

Check warning on line 56 in xmtp_mls/src/storage/connection.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `query`
todo!()
}
}

impl From<SqliteConnection> for SyncSqliteConnection {
fn from(connection: SqliteConnection) -> Self {
Self {
inner: Arc::new(Mutex::new(connection)),
}
}
}

impl R2D2Connection for SyncSqliteConnection {
fn ping(&mut self) -> QueryResult<()> {
let mut conn = self.inner.lock().unwrap();
(*conn).ping()
}

fn is_broken(&mut self) -> bool {
let mut conn = self.inner.lock().unwrap();
(*conn).is_broken()
}
}

impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for SyncSqliteConnection
where
Changes: Copy + Identifiable,
Changes: AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Changes::Table: FindDsl<Changes::Id>,
Update<Changes, Changes>: ExecuteDsl<SqliteConnection>,
Find<Changes::Table, Changes::Id>: LoadQuery<'b, SqliteConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
let mut conn = self.inner.lock().unwrap();
(*conn).update_and_fetch(changeset)
}
}

/*
impl LoadConnection<DefaultLoadingMode> for SyncSqliteConnection {
type Cursor<'conn, 'query>
where
Self: 'conn;
type Row<'conn, 'query>
where
Self: 'conn;
}
*/
1 change: 1 addition & 0 deletions xmtp_mls/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod connection;
mod encrypted_store;
mod errors;
mod serialization;
Expand Down

0 comments on commit 1fc724e

Please sign in to comment.