xline-kv/Xline

Bug: batch_index in raw_curp will overflow eventually

Closed this issue · 1 comments

Now batch_index[i] is the sum of serialized size of entries[0..i-1].

The log.compact() cannot reset the batch_index, so it will overflow eventually.

A better way to calculate the batch of log entries is calculate it at push_back().

struct LogEntryVecDeque<C: Command> {
    entries: VecDeque<LogEntry<C>>,
    entry_size: VecDeque<u64>,
    /// the right index of the batch
    /// batch_range: [i, batch_index[i]]
    batch_index: VecDeque<usize>,
    first_entry_at_last_batch: usize,
    last_batch_size: u64,
    batch_limit: u64,
}
...
    fn push_back(&mut self, entry: LogEntry<C>) -> Result<(), bincode::Error> {
        let entry_size = serialized_size(&entry)?;

        self.entries.push_back(entry);
        self.entry_size.push_back(entry_size);
        self.batch_index.push_back(0)

        while self.last_batch_size + entry_size > self.batch_limit {
            self.batch_index[self.first_entry_at_last_batch] = entries.len() - 1;
            self.last_batch_size = self.last_batch_size - self.entry_size[self.first_entry_at_last_batch];
            self.first_entry_at_last_batch++;
        }
        Ok(())
    }
...
    /// `binary_search` can be optimized in `get_range_by_batch`
    fn get_range_by_batch(&self, left: usize) -> Range<usize> {
        Range<usize>{
            start: left,
            end: self.batch_index[left],
        }
    }

Let me try.