Skip to content

Commit 2d245af

Browse files
committed
refactor(sqlite): Add trait method for the VACUUM operation
Signed-off-by: Kévin Commaille <[email protected]>
1 parent 69588d5 commit 2d245af

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

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

+3-14
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ use matrix_sdk_store_encryption::StoreCipher;
3636
use ruma::{time::SystemTime, MilliSecondsSinceUnixEpoch, MxcUri, RoomId};
3737
use rusqlite::{params_from_iter, OptionalExtension, Transaction, TransactionBehavior};
3838
use tokio::fs;
39-
#[cfg(not(test))]
40-
use tracing::warn;
4139
use tracing::{debug, trace};
4240

4341
use crate::{
@@ -959,19 +957,10 @@ impl EventCacheStoreMedia for SqliteEventCacheStore {
959957
})
960958
.await?;
961959

962-
// If we removed media, use the VACUUM command to defragment the
963-
// database and free space on the filesystem.
960+
// If we removed media, defragment the database and free space on the
961+
// filesystem.
964962
if removed {
965-
if let Err(error) = conn.execute("VACUUM", ()).await {
966-
// Since this is an optimisation step, do not propagate the error
967-
// but log it.
968-
#[cfg(not(test))]
969-
warn!("Failed to vacuum database: {error}");
970-
971-
// We want to know if there is an error with this step during tests.
972-
#[cfg(test)]
973-
return Err(error.into());
974-
}
963+
conn.vacuum().await?;
975964
}
976965

977966
Ok(())

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl SqliteStateStore {
336336
// Defragment the DB and optimize its size on the filesystem.
337337
// This should have been run in the migration for version 7, to reduce the size
338338
// of the DB as we removed the media cache.
339-
conn.execute_batch("VACUUM").await?;
339+
conn.vacuum().await?;
340340
conn.set_kv("version", vec![12]).await?;
341341
}
342342

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

+20
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use matrix_sdk_store_encryption::StoreCipher;
2222
use ruma::time::SystemTime;
2323
use rusqlite::{limits::Limit, OptionalExtension, Params, Row, Statement, Transaction};
2424
use serde::{de::DeserializeOwned, Serialize};
25+
#[cfg(not(test))]
26+
use tracing::warn;
2527

2628
use crate::{
2729
error::{Error, Result},
@@ -136,6 +138,24 @@ pub(crate) trait SqliteAsyncConnExt {
136138
self.execute_batch("PRAGMA journal_size_limit = 10000000;").await.map_err(Error::from)?;
137139
Ok(())
138140
}
141+
142+
/// Defragment the database and free space on the filesystem.
143+
///
144+
/// Only returns an error in tests, otherwise the error is only logged.
145+
async fn vacuum(&self) -> Result<()> {
146+
if let Err(error) = self.execute_batch("VACUUM").await {
147+
// Since this is an optimisation step, do not propagate the error
148+
// but log it.
149+
#[cfg(not(test))]
150+
warn!("Failed to vacuum database: {error}");
151+
152+
// We want to know if there is an error with this step during tests.
153+
#[cfg(test)]
154+
return Err(error.into());
155+
}
156+
157+
Ok(())
158+
}
139159
}
140160

141161
#[async_trait]

0 commit comments

Comments
 (0)