Skip to content

Commit

Permalink
fix LogBatch::is_empty (#212)
Browse files Browse the repository at this point in the history
Adding an empty list of entries to log batch should not change the output of `is_empty()`.

Signed-off-by: tabokie <xy.tao@outlook.com>
  • Loading branch information
tabokie authored Apr 12, 2022
1 parent 2fe0c36 commit 1e2a947
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/log_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,9 @@ impl LogBatch {
entries: &[M::Entry],
) -> Result<()> {
debug_assert!(self.buf_state == BufState::Open);
if entries.is_empty() {
return Ok(());
}

let mut entry_indexes = Vec::with_capacity(entries.len());
self.buf_state = BufState::Incomplete;
Expand Down Expand Up @@ -636,6 +639,9 @@ impl LogBatch {
) -> Result<()> {
debug_assert!(entry_indexes.len() == entries.len());
debug_assert!(self.buf_state == BufState::Open);
if entry_indexes.is_empty() {
return Ok(());
}

self.buf_state = BufState::Incomplete;
let old_buf_len = self.buf.len();
Expand Down Expand Up @@ -1236,6 +1242,16 @@ mod tests {
}
}

#[test]
fn test_empty_log_batch() {
let mut batch = LogBatch::default();
assert!(batch.is_empty());
batch.add_entries::<Entry>(0, &Vec::new()).unwrap();
assert!(batch.is_empty());
batch.add_raw_entries(0, Vec::new(), Vec::new()).unwrap();
assert!(batch.is_empty());
}

#[cfg(feature = "nightly")]
#[bench]
fn bench_log_batch_add_entry_and_encode(b: &mut test::Bencher) {
Expand Down

0 comments on commit 1e2a947

Please sign in to comment.