From 9a1f77321441756522dead9d94bda7d51e89a627 Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Thu, 24 Aug 2023 21:12:45 +0200 Subject: [PATCH 1/2] Update db.rs --- agdb/src/db.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/agdb/src/db.rs b/agdb/src/db.rs index b6a087484..af70de6d2 100644 --- a/agdb/src/db.rs +++ b/agdb/src/db.rs @@ -196,17 +196,14 @@ impl Db { } /// Flushes the underlying file and copies it - /// to `filename` path. + /// to `filename` path. Consider calling `optimize_storage()` + /// prior to this function to reduce the size of the storage + /// file. If speed is of the essence you may omit that operation + /// at expense of the file size. pub fn backup(&mut self, filename: &str) -> Result<(), DbError> { self.storage.backup(filename) } - /// Returns the filename that was used to - /// construct the database. - pub fn filename(&self) -> &str { - self.storage.filename() - } - /// Executes immutable query: /// /// - Select elements @@ -241,6 +238,21 @@ impl Db { self.transaction_mut(|transaction| transaction.exec_mut(query)) } + /// Returns the filename that was used to + /// construct the database. + pub fn filename(&self) -> &str { + self.storage.filename() + } + + /// Reclaims no longer used segments of the database file by packing all + /// used storage segments together. This operation is done automatically + /// when the database goes out of scope. In long running programs it might + /// be desired to perform the storage file optimization without fully shutting + /// down. + pub fn optimize_storage(&mut self) -> Result<(), DbError> { + self.storage.shrink_to_fit() + } + /// Executes immutable transaction. The transaction is running a closure `f` /// that will receive `&Transaction` object to run `exec` queries as if run /// on the main database object. You shall specify the return type `T` From 5444dc152ddda9ae457e6ce08214aa781ec99f93 Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Thu, 24 Aug 2023 21:12:47 +0200 Subject: [PATCH 2/2] Update db_test.rs --- agdb/tests/db_test.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/agdb/tests/db_test.rs b/agdb/tests/db_test.rs index f586c2d18..eefc1d094 100644 --- a/agdb/tests/db_test.rs +++ b/agdb/tests/db_test.rs @@ -8,6 +8,7 @@ use agdb::QueryId; use std::sync::Arc; use std::sync::RwLock; use test_db::test_file::TestFile; +use test_db::TestDb; #[allow(unused_imports)] #[test] @@ -330,3 +331,23 @@ fn filename() { let db = Db::new(test_file.file_name()).unwrap(); assert_eq!(db.filename(), test_file.file_name()); } + +#[test] +fn optimize_storage() { + let mut db = TestDb::new(); + + db.exec_mut( + QueryBuilder::insert() + .nodes() + .count(100) + .values_uniform(vec![("key", 123).into()]) + .query(), + 100, + ); + + let size = std::fs::metadata(db.db.filename()).unwrap().len(); + db.db.optimize_storage().unwrap(); + let optimized_size = std::fs::metadata(db.db.filename()).unwrap().len(); + + assert!(optimized_size < size); +}