Skip to content

Commit

Permalink
[storage] Add clear() to WriteAheadLog #143 (#144)
Browse files Browse the repository at this point in the history
* Update write_ahead_log.rs

* Update write_ahead_log.rs
  • Loading branch information
michaelvlach authored Sep 3, 2022
1 parent 70fc368 commit e3ee096
Show file tree
Hide file tree
Showing 2 changed files with 21 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 62 --fail-uncovered-functions 0 --fail-uncovered-lines 1
- run: cargo llvm-cov --fail-uncovered-regions 63 --fail-uncovered-functions 0 --fail-uncovered-lines 1

test:
runs-on: ubuntu-latest
Expand Down
20 changes: 20 additions & 0 deletions src/storage/write_ahead_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub(crate) struct WriteAheadLog {

#[allow(dead_code)]
impl WriteAheadLog {
pub(crate) fn clear(&mut self) -> Result<(), DbError> {
Ok(self.file.set_len(0)?)
}

pub(crate) fn insert(&mut self, record: WriteAheadLogRecord) -> Result<(), DbError> {
self.file.write_all(&record.position.serialize())?;
self.file
Expand Down Expand Up @@ -75,6 +79,22 @@ mod tests {
WriteAheadLog::try_from(test_file.file_name().clone()).unwrap();
}

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

let mut wal = WriteAheadLog::try_from(test_file.file_name().clone()).unwrap();
let record = WriteAheadLogRecord {
position: 1,
bytes: vec![1_u8; 5],
};

wal.insert(record).unwrap();
wal.clear().unwrap();

assert_eq!(wal.records(), Ok(vec![]));
}

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

0 comments on commit e3ee096

Please sign in to comment.