diff --git a/crates/curp/src/server/raw_curp/log.rs b/crates/curp/src/server/raw_curp/log.rs index b8c0750b8..ff528630d 100644 --- a/crates/curp/src/server/raw_curp/log.rs +++ b/crates/curp/src/server/raw_curp/log.rs @@ -129,10 +129,15 @@ impl LogEntryVecDeque { self.batch_index.push_back(0); // placeholder self.last_batch_size += entry_size; - while self.last_batch_size > self.batch_limit { + while self.last_batch_size >= self.batch_limit { if let Some(cur_batch_index) = self.batch_index.get_mut(self.first_entry_at_last_batch) { *cur_batch_index = self.entries.len() - 1 - self.first_entry_at_last_batch; + + if self.last_batch_size == self.batch_limit { + *cur_batch_index += 1; + } + if let Some(cur_entry_size) = self.entry_size.get(self.first_entry_at_last_batch) { self.last_batch_size -= *cur_entry_size; } @@ -160,11 +165,11 @@ impl LogEntryVecDeque { let _ = self .batch_index .pop_front() - .unwrap_or_else(|| unreachable!()); + .unwrap_or_else(|| unreachable!("The batch_index cannot be empty")); let _ = self .entry_size .pop_front() - .unwrap_or_else(|| unreachable!()); + .unwrap_or_else(|| unreachable!("The pop_front cannot be empty")); self.entries.pop_front() } else { None @@ -198,9 +203,7 @@ impl LogEntryVecDeque { #[allow(clippy::range_plus_one)] fn get_range_by_batch(&self, left: usize) -> Range { #![allow(clippy::indexing_slicing)] - if left >= self.batch_index.len() { - left..self.entries.len() - } else if self.entry_size[left] > self.batch_limit { + if self.entry_size[left] > self.batch_limit { left..left + 1 } else if self.batch_index[left] == 0 { left..self.entries.len() @@ -235,6 +238,7 @@ impl LogEntryVecDeque { self.last_batch_size = 0; self.first_entry_at_last_batch = 0; self.batch_index.clear(); + self.entry_size.clear(); let prev_entries = self.entries.clone(); self.entries.clear();