Skip to content

Commit

Permalink
feat: expose compact
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Sep 1, 2024
1 parent a0b2e8c commit fe01697
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ impl<'a> Database<'a> {
&self.metadata
}

/// Compact the database.
///
/// Similar to [redb::Database::compact()](https://docs.rs/redb/latest/redb/struct.Database.html#method.compact).
pub fn compact(&mut self) -> Result<bool> {
self.instance.redb_database_mut()?.compact()?;
Ok(true)
}

/// Returns true if the database is upgrading from the given version selector.
///
/// - If the database is the old version, not matching the selector the function will return `false.
Expand Down
11 changes: 11 additions & 0 deletions src/database_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl DatabaseInstance {
pub(crate) fn redb_database(&self) -> Result<&redb::Database> {
self.kind.redb_database()
}

pub(crate) fn redb_database_mut(&mut self) -> Result<&mut redb::Database> {
self.kind.redb_database_mut()
}
}

enum DatabaseInstanceKind {
Expand All @@ -59,4 +63,11 @@ impl DatabaseInstanceKind {
DatabaseInstanceKind::OnDisk { redb_database, .. } => Ok(redb_database),
}
}

pub(crate) fn redb_database_mut(&mut self) -> Result<&mut redb::Database> {
match self {
DatabaseInstanceKind::InMemory { redb_database } => Ok(redb_database),
DatabaseInstanceKind::OnDisk { redb_database, .. } => Ok(redb_database),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! - [`create`](crate::Builder::create) - Create a database in a file.
//! - [`open`](crate::Builder::open) - Open a database.
//! - [`Database`] - Database instance.
//! - [`compact`](crate::Database::compact) - Compact the database.
//! - [`rw_transaction`](crate::Database::rw_transaction) - Create a read-write transaction.
//! - [`insert`](crate::transaction::RwTransaction::insert) - Insert a item, fail if the item already exists.
//! - [`upsert`](crate::transaction::RwTransaction::upsert) - Upsert a item, update if the item already exists.
Expand Down
48 changes: 48 additions & 0 deletions tests/compact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use native_db::*;
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
use shortcut_assert_fs::TmpFs;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
#[native_model(id = 1, version = 1)]
#[native_db]
struct Item {
#[primary_key]
id: u32,
name: String,
}

#[test]
fn test_compact() {
let tf = TmpFs::new().unwrap();
let db_path = tf.path("test");

let mut models = Models::new();
models.define::<Item>().unwrap();
let mut db = Builder::new().create(&models, db_path.clone()).unwrap();

// Insert 1000 items
let rw = db.rw_transaction().unwrap();
for i in 0..999 {
rw.insert(Item {
id: i,
name: format!("test_{}", i),
})
.unwrap();
}
rw.commit().unwrap();

// Check the size of the database
let metadata = std::fs::metadata(db_path.clone()).unwrap();
let file_size = metadata.len();
assert_eq!(file_size, 1589248);
dbg!(file_size);

let out = db.compact().unwrap();
assert!(out);

// Check the size of the compacted database
let metadata = std::fs::metadata(db_path.clone()).unwrap();
let file_size = metadata.len();
assert_eq!(file_size, 700416);
}

0 comments on commit fe01697

Please sign in to comment.