Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] Add optimize storage to database object #700 #703

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions agdb/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
21 changes: 21 additions & 0 deletions agdb/tests/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}