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

[storage] Add resize_value() to Storage #156 #157

Merged
merged 3 commits into from
Sep 6, 2022
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
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