Skip to content

Commit

Permalink
[storage] Fix values not invalidated after move #158 (#159)
Browse files Browse the repository at this point in the history
* Update file_storage.rs

* Update file_storage.rs

* Update storage_impl.rs

* Update storage_impl.rs

* Update storage.rs

* Update pr.yaml
  • Loading branch information
michaelvlach authored Sep 6, 2022
1 parent b322061 commit e0b61be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
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 83 --fail-uncovered-functions 0 --fail-uncovered-lines 0
- run: cargo llvm-cov --fail-uncovered-regions 84 --fail-uncovered-functions 0 --fail-uncovered-lines 0

test:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) trait Storage<T: StorageImpl = Self>: StorageImpl<T> {
fn remove(&mut self, index: i64) -> Result<(), DbError> {
self.transaction();
let position = self.record(index)?.position;
self.write(std::io::SeekFrom::Start(position), (-index).serialize())?;
self.invalidate_record(index, position)?;
self.remove_index(index);
self.commit()
}
Expand Down
23 changes: 23 additions & 0 deletions src/storage/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,29 @@ mod tests {
);
}

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

let index;

{
let mut storage = FileStorage::try_from(test_file.file_name().as_str()).unwrap();
index = storage.insert(&10_i64).unwrap();
storage.insert(&5_i64).unwrap();
storage.resize_value(index, 1).unwrap();
storage.remove(index).unwrap();
}

let mut storage = FileStorage::try_from(test_file.file_name().as_str()).unwrap();

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

#[test]
fn value_size() {
let test_file = TestFile::from("./file_storage-value_size.agdb");
Expand Down
6 changes: 6 additions & 0 deletions src/storage/storage_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,25 @@ pub(crate) trait StorageImpl<T = Self> {
fn indexes_by_position(&self) -> Vec<i64>;
fn insert_wal_record(&mut self, record: WriteAheadLogRecord) -> Result<(), DbError>;

fn invalidate_record(&mut self, index: i64, position: u64) -> Result<(), DbError> {
self.write(std::io::SeekFrom::Start(position), (-index).serialize())
}

fn move_record_to_end(
&mut self,
index: i64,
new_size: u64,
offset: u64,
record: &mut StorageRecord,
) -> Result<(), DbError> {
let old_position = record.position;
*record = self.copy_record_to_end(
record.position + std::mem::size_of::<StorageRecord>() as u64,
core::cmp::min(record.size, offset),
index,
new_size,
)?;
self.invalidate_record(index, old_position)?;
*self.record_mut(index) = record.clone();

Ok(())
Expand Down

0 comments on commit e0b61be

Please sign in to comment.