Skip to content

Commit

Permalink
[db] Add size() to public API #730 (#733)
Browse files Browse the repository at this point in the history
add size()
  • Loading branch information
michaelvlach authored Sep 14, 2023
1 parent e14a942 commit 440b5d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 11 additions & 0 deletions agdb/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ impl<Store: StorageData> DbImpl<Store> {
result
}

/// Returns the database size in bytes. Depending on the underlying storage
/// the physical size in hardware might be higher. For example for in-memory
/// storage this function reports used storage but actually allocated memory
/// will likely be higher (expecting database growth in order to prevent too
/// frequent allocations). For file based storages this number will be accurate
/// but the actually used space on disk will be higher up to the next file system
/// block size.
pub fn size(&self) -> u64 {
self.storage.len()
}

pub(crate) fn commit(&mut self) -> Result<(), QueryError> {
self.undo_stack.clear();
Ok(())
Expand Down
4 changes: 1 addition & 3 deletions agdb/src/storage/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ impl FileStorage {

impl StorageData for FileStorage {
fn backup(&mut self, name: &str) -> Result<(), DbError> {
self.flush()?;
std::fs::copy(&self.filename, name)?;
Ok(())
}

fn flush(&mut self) -> Result<(), DbError> {
self.wal.clear()?;
Ok(self.file.flush()?)
self.wal.clear()
}

fn len(&self) -> u64 {
Expand Down
4 changes: 2 additions & 2 deletions agdb/tests/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ fn optimize_storage() {
100,
);

let size = std::fs::metadata(db.db.filename()).unwrap().len();
let size = db.db.size();
db.db.optimize_storage().unwrap();
let optimized_size = std::fs::metadata(db.db.filename()).unwrap().len();
let optimized_size = db.db.size();

assert!(optimized_size < size);
}

0 comments on commit 440b5d6

Please sign in to comment.