Skip to content

Commit

Permalink
[storage] Add resize_value() to Storage #156 (#157)
Browse files Browse the repository at this point in the history
* Update file_storage.rs

* Update storage.rs

* Update pr.yaml
  • Loading branch information
michaelvlach authored Sep 6, 2022
1 parent 9878ff1 commit b322061
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- uses: actions/checkout@v3
- uses: taiki-e/install-action@cargo-llvm-cov
- run: rustup component add llvm-tools-preview
- run: cargo llvm-cov --fail-uncovered-regions 81 --fail-uncovered-functions 0 --fail-uncovered-lines 0
- run: cargo llvm-cov --fail-uncovered-regions 83 --fail-uncovered-functions 0 --fail-uncovered-lines 0

test:
runs-on: ubuntu-latest
Expand Down
16 changes: 16 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ pub(crate) trait Storage<T: StorageImpl = Self>: StorageImpl<T> {
self.commit()
}

fn resize_value(&mut self, index: i64, new_size: u64) -> Result<(), DbError> {
if new_size == 0 {
return Err(DbError::Storage("value size cannot be 0".to_string()));
}

let mut record = self.record(index)?;

if record.size != new_size {
self.transaction();
self.move_record_to_end(index, new_size, new_size, &mut record)?;
self.commit()?;
}

Ok(())
}

fn shrink_to_fit(&mut self) -> Result<(), DbError> {
self.transaction();
let indexes = self.indexes_by_position();
Expand Down
72 changes: 72 additions & 0 deletions src/storage/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,78 @@ mod tests {
);
}

#[test]
fn resize_value_greater() {
let test_file = TestFile::from("./file_storage-resize_value_greater.agdb");
let mut storage = FileStorage::try_from(test_file.file_name().clone()).unwrap();

let index = storage.insert(&10_i64).unwrap();
let expected_size = std::mem::size_of::<i64>() as u64;

assert_eq!(storage.value_size(index), Ok(expected_size));

storage.resize_value(index, expected_size * 2).unwrap();

assert_eq!(storage.value_size(index), Ok(expected_size * 2));
}

#[test]
fn resize_value_missing_index() {
let test_file = TestFile::from("./file_storage-resize_value_missing_index.agdb");
let mut storage = FileStorage::try_from(test_file.file_name().clone()).unwrap();

assert_eq!(
storage.resize_value(1, 1),
Err(DbError::Storage("index '1' not found".to_string()))
);
}

#[test]
fn resize_value_same() {
let test_file = TestFile::from("./file_storage-resize_value_same.agdb");
let mut storage = FileStorage::try_from(test_file.file_name().clone()).unwrap();

let index = storage.insert(&10_i64).unwrap();
let expected_size = std::mem::size_of::<i64>() as u64;

assert_eq!(storage.value_size(index), Ok(expected_size));

storage.resize_value(index, expected_size).unwrap();

assert_eq!(storage.value_size(index), Ok(expected_size));
}

#[test]
fn resize_value_smaller() {
let test_file = TestFile::from("./file_storage-resize_value_smaller.agdb");
let mut storage = FileStorage::try_from(test_file.file_name().clone()).unwrap();

let index = storage.insert(&10_i64).unwrap();
let expected_size = std::mem::size_of::<i64>() as u64;

assert_eq!(storage.value_size(index), Ok(expected_size));

storage.resize_value(index, expected_size / 2).unwrap();

assert_eq!(storage.value_size(index), Ok(expected_size / 2));
}

#[test]
fn resize_value_zero() {
let test_file = TestFile::from("./file_storage-resize_value_zero.agdb");
let mut storage = FileStorage::try_from(test_file.file_name().clone()).unwrap();

let index = storage.insert(&10_i64).unwrap();
let expected_size = std::mem::size_of::<i64>() as u64;

assert_eq!(storage.value_size(index), Ok(expected_size));

assert_eq!(
storage.resize_value(index, 0),
Err(DbError::Storage("value size cannot be 0".to_string()))
);
}

#[test]
fn restore_from_open_file() {
let test_file = TestFile::from("./file_storage-restore_from_open_file.agdb");
Expand Down

0 comments on commit b322061

Please sign in to comment.