Skip to content

Commit

Permalink
refactor: add more ino in unreachable && fix redundant codes
Browse files Browse the repository at this point in the history
Signed-off-by: GFX9 <[email protected]>
  • Loading branch information
GFX9 committed Apr 26, 2024
1 parent 4df3234 commit 72f76c2
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions crates/curp/src/server/raw_curp/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,16 @@ impl<C: Command> LogEntryVecDeque<C> {
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 {
// self.last_batch_size == self.batch_limit means the last index must be included in `get_range`
*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;
}
Expand Down Expand Up @@ -160,11 +166,11 @@ impl<C: Command> LogEntryVecDeque<C> {
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
Expand Down Expand Up @@ -218,10 +224,7 @@ impl<C: Command> LogEntryVecDeque<C> {
/// check whether the log entry range [li,..) exceeds the batch limit or not
fn has_next_batch(&self, left: usize) -> bool {
if let Some(&offset) = self.batch_index.get(left) {
offset != 0
|| (self.first_entry_at_last_batch == left
&& self.batch_limit == self.last_batch_size)
|| left == self.batch_index.len() - 1
offset != 0 || self.first_entry_at_last_batch > left
} else {
false
}
Expand All @@ -235,6 +238,7 @@ impl<C: Command> LogEntryVecDeque<C> {
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();
Expand Down

0 comments on commit 72f76c2

Please sign in to comment.