Skip to content

Commit 610f82a

Browse files
committed
chore(sqlite): Remove connection::Config.
This patch removes the `connection::Config` type. It was “inspired” from `deadpool_sqlite`, but we can clearly remove it by using our own `SqliteStoreConfig` type. It simplifies the way we open a database.
1 parent 60490f4 commit 610f82a

File tree

6 files changed

+46
-77
lines changed

6 files changed

+46
-77
lines changed

crates/matrix-sdk-sqlite/src/connection.rs

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,44 +67,11 @@
6767
use std::{convert::Infallible, path::PathBuf};
6868

6969
pub use deadpool::managed::reexports::*;
70-
use deadpool::managed::{self, Metrics, PoolConfig, RecycleError};
70+
use deadpool::managed::{self, Metrics, RecycleError};
7171
use deadpool_sync::SyncWrapper;
7272

7373
/// The default runtime used by `matrix-sdk-sqlite` for `deadpool`.
74-
const RUNTIME: Runtime = Runtime::Tokio1;
75-
76-
/// The configuration of a connection.
77-
#[derive(Clone, Debug)]
78-
pub struct Config {
79-
path: PathBuf,
80-
pool_config: PoolConfig,
81-
}
82-
83-
impl Config {
84-
/// Create a new [`Config`].
85-
///
86-
/// `path` represents the path to the database. `pool_config` represents the
87-
/// [`PoolConfig`].
88-
#[must_use]
89-
pub fn new(path: impl Into<PathBuf>, pool_config: PoolConfig) -> Self {
90-
Self { path: path.into(), pool_config }
91-
}
92-
93-
/// Creates a new [`Pool`].
94-
///
95-
/// # Errors
96-
///
97-
/// See [`CreatePoolError`] for details.
98-
pub fn create_pool(&self) -> Result<Pool, managed::CreatePoolError<Infallible>> {
99-
let manager = Manager::from_config(self);
100-
101-
Pool::builder(manager)
102-
.config(self.pool_config)
103-
.runtime(RUNTIME)
104-
.build()
105-
.map_err(CreatePoolError::Build)
106-
}
107-
}
74+
pub const RUNTIME: Runtime = Runtime::Tokio1;
10875

10976
deadpool::managed_reexports!(
11077
"matrix-sdk-sqlite",
@@ -121,15 +88,14 @@ pub type Connection = Object;
12188
/// [`Connection`]s.
12289
#[derive(Debug)]
12390
pub struct Manager {
124-
config: Config,
91+
database_path: PathBuf,
12592
}
12693

12794
impl Manager {
128-
/// Creates a new [`Manager`] using the given [`Config`] backed by the
129-
/// specified [`Runtime`].
95+
/// Creates a new [`Manager`] for a database.
13096
#[must_use]
131-
fn from_config(config: &Config) -> Self {
132-
Self { config: config.clone() }
97+
pub fn new(database_path: PathBuf) -> Self {
98+
Self { database_path }
13399
}
134100
}
135101

@@ -138,7 +104,7 @@ impl managed::Manager for Manager {
138104
type Error = rusqlite::Error;
139105

140106
async fn create(&self) -> Result<Self::Type, Self::Error> {
141-
let path = self.config.path.clone();
107+
let path = self.database_path.clone();
142108
SyncWrapper::new(RUNTIME, move || rusqlite::Connection::open(path)).await
143109
}
144110

crates/matrix-sdk-sqlite/src/crypto_store.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use tracing::{debug, instrument, warn};
4646
use vodozemac::Curve25519PublicKey;
4747

4848
use crate::{
49-
connection::{self, Connection as SqliteAsyncConn, Pool as SqlitePool},
49+
connection::{Connection as SqliteAsyncConn, Pool as SqlitePool},
5050
error::{Error, Result},
5151
utils::{
5252
repeat_vars, EncryptableStore, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
@@ -103,15 +103,12 @@ impl SqliteCryptoStore {
103103

104104
/// Open the SQLite-based crypto store with the config open config.
105105
pub async fn open_with_config(config: SqliteStoreConfig) -> Result<Self, OpenStoreError> {
106-
let SqliteStoreConfig { path, pool_config, runtime_config, secret } = config;
106+
fs::create_dir_all(&config.path).await.map_err(OpenStoreError::CreateDir)?;
107107

108-
fs::create_dir_all(&path).await.map_err(OpenStoreError::CreateDir)?;
108+
let pool = config.build_pool_of_connections(DATABASE_NAME)?;
109109

110-
let config = connection::Config::new(path.join(DATABASE_NAME), pool_config);
111-
let pool = config.create_pool()?;
112-
113-
let this = Self::open_with_pool(pool, secret).await?;
114-
this.pool.get().await?.apply_runtime_config(runtime_config).await?;
110+
let this = Self::open_with_pool(pool, config.secret).await?;
111+
this.pool.get().await?.apply_runtime_config(config.runtime_config).await?;
115112

116113
Ok(this)
117114
}

crates/matrix-sdk-sqlite/src/event_cache_store.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,12 @@ impl SqliteEventCacheStore {
130130

131131
let _timer = timer!("open_with_config");
132132

133-
let SqliteStoreConfig { path, pool_config, runtime_config, secret } = config;
133+
fs::create_dir_all(&config.path).await.map_err(OpenStoreError::CreateDir)?;
134134

135-
fs::create_dir_all(&path).await.map_err(OpenStoreError::CreateDir)?;
135+
let pool = config.build_pool_of_connections(DATABASE_NAME)?;
136136

137-
let config = crate::connection::Config::new(path.join(DATABASE_NAME), pool_config);
138-
let pool = config.create_pool()?;
139-
140-
let this = Self::open_with_pool(pool, secret).await?;
141-
this.write().await?.apply_runtime_config(runtime_config).await?;
137+
let this = Self::open_with_pool(pool, config.secret).await?;
138+
this.write().await?.apply_runtime_config(config.runtime_config).await?;
142139

143140
Ok(this)
144141
}

crates/matrix-sdk-sqlite/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@ impl SqliteStoreConfig {
207207
self.runtime_config.journal_size_limit = limit;
208208
self
209209
}
210+
211+
/// Build a pool of active connections to a particular database.
212+
pub fn build_pool_of_connections(
213+
&self,
214+
database_name: &str,
215+
) -> Result<connection::Pool, connection::CreatePoolError> {
216+
let path = self.path.join(database_name);
217+
let manager = connection::Manager::new(path);
218+
219+
connection::Pool::builder(manager)
220+
.config(self.pool_config)
221+
.runtime(connection::RUNTIME)
222+
.build()
223+
.map_err(connection::CreatePoolError::Build)
224+
}
210225
}
211226

212227
/// This type represents values to set at runtime when a database is opened.

crates/matrix-sdk-sqlite/src/media_store.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use tokio::{
3838
use tracing::{debug, instrument, trace};
3939

4040
use crate::{
41-
connection::{self, Connection as SqliteAsyncConn, Pool as SqlitePool},
41+
connection::{Connection as SqliteAsyncConn, Pool as SqlitePool},
4242
error::{Error, Result},
4343
utils::{
4444
repeat_vars, time_to_timestamp, EncryptableStore, SqliteAsyncConnExt,
@@ -122,15 +122,12 @@ impl SqliteMediaStore {
122122

123123
let _timer = timer!("open_with_config");
124124

125-
let SqliteStoreConfig { path, secret, pool_config, runtime_config } = config;
125+
fs::create_dir_all(&config.path).await.map_err(OpenStoreError::CreateDir)?;
126126

127-
fs::create_dir_all(&path).await.map_err(OpenStoreError::CreateDir)?;
127+
let pool = config.build_pool_of_connections(DATABASE_NAME)?;
128128

129-
let config = connection::Config::new(path.join(DATABASE_NAME), pool_config);
130-
let pool = config.create_pool()?;
131-
132-
let this = Self::open_with_pool(pool, secret).await?;
133-
this.write().await?.apply_runtime_config(runtime_config).await?;
129+
let this = Self::open_with_pool(pool, config.secret).await?;
130+
this.write().await?.apply_runtime_config(config.runtime_config).await?;
134131

135132
Ok(this)
136133
}

crates/matrix-sdk-sqlite/src/state_store.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use tokio::{
4545
use tracing::{debug, instrument, trace, warn};
4646

4747
use crate::{
48-
connection::{self, Connection as SqliteAsyncConn, Pool as SqlitePool},
48+
connection::{Connection as SqliteAsyncConn, Pool as SqlitePool},
4949
error::{Error, Result},
5050
utils::{
5151
repeat_vars, EncryptableStore, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
@@ -123,15 +123,12 @@ impl SqliteStateStore {
123123

124124
/// Open the SQLite-based state store with the config open config.
125125
pub async fn open_with_config(config: SqliteStoreConfig) -> Result<Self, OpenStoreError> {
126-
let SqliteStoreConfig { path, pool_config, runtime_config, secret } = config;
126+
fs::create_dir_all(&config.path).await.map_err(OpenStoreError::CreateDir)?;
127127

128-
fs::create_dir_all(&path).await.map_err(OpenStoreError::CreateDir)?;
128+
let pool = config.build_pool_of_connections(DATABASE_NAME)?;
129129

130-
let config = connection::Config::new(path.join(DATABASE_NAME), pool_config);
131-
let pool = config.create_pool()?;
132-
133-
let this = Self::open_with_pool(pool, secret).await?;
134-
this.pool.get().await?.apply_runtime_config(runtime_config).await?;
130+
let this = Self::open_with_pool(pool, config.secret).await?;
131+
this.pool.get().await?.apply_runtime_config(config.runtime_config).await?;
135132

136133
Ok(this)
137134
}
@@ -2407,10 +2404,9 @@ mod migration_tests {
24072404

24082405
use super::{init, keys, SqliteStateStore, DATABASE_NAME};
24092406
use crate::{
2410-
connection,
24112407
error::{Error, Result},
24122408
utils::{EncryptableStore as _, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt},
2413-
OpenStoreError, PoolConfig, Secret,
2409+
OpenStoreError, Secret, SqliteStoreConfig,
24142410
};
24152411

24162412
static TMP_DIR: Lazy<TempDir> = Lazy::new(|| tempdir().unwrap());
@@ -2423,10 +2419,11 @@ mod migration_tests {
24232419
}
24242420

24252421
async fn create_fake_db(path: &Path, version: u8) -> Result<SqliteStateStore> {
2426-
fs::create_dir_all(&path).await.map_err(OpenStoreError::CreateDir).unwrap();
2422+
let config = SqliteStoreConfig::new(path);
2423+
2424+
fs::create_dir_all(&config.path).await.map_err(OpenStoreError::CreateDir).unwrap();
24272425

2428-
let config = connection::Config::new(path.join(DATABASE_NAME), PoolConfig::default());
2429-
let pool = config.create_pool().unwrap();
2426+
let pool = config.build_pool_of_connections(DATABASE_NAME).unwrap();
24302427
let conn = pool.get().await?;
24312428

24322429
init(&conn).await?;

0 commit comments

Comments
 (0)