refactor(sled): Remove derive_builder

… and use owned Builder pattern like for Client.
This commit is contained in:
Jonas Platte
2023-02-20 15:35:06 +01:00
committed by Jonas Platte
parent b0e7f3d031
commit cb06feea22
4 changed files with 38 additions and 32 deletions

1
Cargo.lock generated
View File

@@ -3034,7 +3034,6 @@ dependencies = [
"async-stream",
"async-trait",
"dashmap",
"derive_builder",
"fs_extra",
"futures-core",
"futures-util",

View File

@@ -27,7 +27,6 @@ crypto-store = [
async-stream = { workspace = true }
async-trait = { workspace = true }
dashmap = { workspace = true }
derive_builder = "0.11.2"
fs_extra = "1.2.0"
futures-core = "0.3.21"
futures-util = { version = "0.3.21", default-features = false }

View File

@@ -63,12 +63,11 @@ pub async fn make_store_config(
#[cfg(not(feature = "crypto-store"))]
{
let mut store_builder = SledStateStore::builder();
store_builder.path(path.as_ref().to_path_buf());
let mut store_builder = SledStateStore::builder().path(path.as_ref().to_path_buf());
if let Some(passphrase) = passphrase {
store_builder.passphrase(passphrase.to_owned());
};
store_builder = store_builder.passphrase(passphrase.to_owned());
}
let state_store = store_builder.build().map_err(StoreError::backend)?;
Ok(StoreConfig::new().state_store(state_store))
@@ -82,11 +81,9 @@ async fn open_stores_with_path(
path: impl AsRef<std::path::Path>,
passphrase: Option<&str>,
) -> Result<(SledStateStore, SledCryptoStore), OpenStoreError> {
let mut store_builder = SledStateStore::builder();
store_builder.path(path.as_ref().to_path_buf());
let mut store_builder = SledStateStore::builder().path(path.as_ref().to_path_buf());
if let Some(passphrase) = passphrase {
store_builder.passphrase(passphrase.to_owned());
store_builder = store_builder.passphrase(passphrase.to_owned());
}
let state_store = store_builder.build().map_err(StoreError::backend)?;

View File

@@ -20,7 +20,6 @@ use std::{
};
use async_trait::async_trait;
use derive_builder::Builder;
use futures_core::stream::Stream;
use futures_util::stream::{self, StreamExt, TryStreamExt};
use matrix_sdk_base::{
@@ -177,25 +176,27 @@ enum DbOrPath {
Path(PathBuf),
}
#[derive(Builder, Debug)]
#[builder(name = "SledStateStoreBuilder", build_fn(skip))]
#[allow(dead_code)]
pub struct SledStateStoreBuilderConfig {
#[builder(setter(custom))]
db_or_path: DbOrPath,
/// Set the password the sled store is encrypted with (if any)
passphrase: String,
/// The strategy to use when a merge conflict is found, see
/// [`MigrationConflictStrategy`] for details
#[builder(default = "MigrationConflictStrategy::BackupAndDrop")]
/// Builder for [`SledStateStore`].
#[derive(Debug)]
pub struct SledStateStoreBuilder {
db_or_path: Option<DbOrPath>,
passphrase: Option<String>,
migration_conflict_strategy: MigrationConflictStrategy,
}
impl SledStateStoreBuilder {
fn new() -> Self {
Self {
db_or_path: None,
passphrase: None,
migration_conflict_strategy: MigrationConflictStrategy::BackupAndDrop,
}
}
/// Path to the sled store files, created if not it doesn't exist yet.
///
/// Mutually exclusive with [`db`][Self::db], whichever is called last wins.
pub fn path(&mut self, path: PathBuf) -> &mut SledStateStoreBuilder {
pub fn path(mut self, path: PathBuf) -> Self {
self.db_or_path = Some(DbOrPath::Path(path));
self
}
@@ -204,11 +205,25 @@ impl SledStateStoreBuilder {
///
/// Mutually exclusive with [`path`][Self::path], whichever is called last
/// wins.
pub fn db(&mut self, db: Db) -> &mut SledStateStoreBuilder {
pub fn db(mut self, db: Db) -> Self {
self.db_or_path = Some(DbOrPath::Db(db));
self
}
/// Set the password the sled store is encrypted with (if any).
pub fn passphrase(mut self, value: String) -> Self {
self.passphrase = Some(value);
self
}
/// Set the strategy to use when a merge conflict is found.
///
/// See [`MigrationConflictStrategy`] for details.
pub fn migration_conflict_strategy(mut self, value: MigrationConflictStrategy) -> Self {
self.migration_conflict_strategy = value;
self
}
/// Create a [`SledStateStore`] with the options set on this builder.
///
/// # Errors
@@ -219,7 +234,7 @@ impl SledStateStoreBuilder {
/// path.
/// * Migration error: The migration to a newer version of the schema
/// failed, see `SledStoreError::MigrationConflict`.
pub fn build(&mut self) -> Result<SledStateStore> {
pub fn build(self) -> Result<SledStateStore> {
let (db, path) = match &self.db_or_path {
None => {
let db = Config::new().temporary(true).open().map_err(StoreError::backend)?;
@@ -254,11 +269,7 @@ impl SledStateStoreBuilder {
let migration_res = store.upgrade();
if let Err(SledStoreError::MigrationConflict { path, .. }) = &migration_res {
// how are supposed to react about this?
match self
.migration_conflict_strategy
.as_ref()
.unwrap_or(&MigrationConflictStrategy::BackupAndDrop)
{
match self.migration_conflict_strategy {
MigrationConflictStrategy::BackupAndDrop => {
let mut new_path = path.clone();
new_path.set_extension(format!(
@@ -397,9 +408,9 @@ impl SledStateStore {
})
}
/// Generate a SledStateStoreBuilder with default parameters
/// Create a [`SledStateStoreBuilder`] with default parameters.
pub fn builder() -> SledStateStoreBuilder {
SledStateStoreBuilder::default()
SledStateStoreBuilder::new()
}
fn drop_tables(self) -> StoreResult<()> {