-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use bytes::BytesMut; | ||
use curp_external_api::{cmd::ProposeId, LogIndex}; | ||
use curp_test_utils::test_cmd::TestCommand; | ||
use parking_lot::Mutex; | ||
use tokio_util::codec::Encoder; | ||
|
||
use crate::log_entry::{EntryData, LogEntry}; | ||
|
||
use super::codec::{DataFrame, WAL}; | ||
|
||
pub(super) struct EntryGenerator { | ||
inner: Mutex<Inner>, | ||
} | ||
|
||
struct Inner { | ||
next_index: u64, | ||
segment_size: u64, | ||
logs_sent: Vec<LogEntry<TestCommand>>, | ||
} | ||
|
||
impl EntryGenerator { | ||
pub(super) fn new(segment_size: u64) -> Self { | ||
Self { | ||
inner: Mutex::new(Inner { | ||
next_index: 1, | ||
segment_size, | ||
logs_sent: Vec::new(), | ||
}), | ||
} | ||
} | ||
|
||
pub(super) fn skip(&self, num_index: usize) { | ||
let mut this = self.inner.lock(); | ||
this.next_index += num_index as u64; | ||
} | ||
|
||
pub(super) fn next(&self) -> LogEntry<TestCommand> { | ||
let mut this = self.inner.lock(); | ||
let entry = | ||
LogEntry::<TestCommand>::new(this.next_index, 1, EntryData::Empty(ProposeId(1, 2))); | ||
this.logs_sent.push(entry.clone()); | ||
this.next_index += 1; | ||
entry | ||
} | ||
|
||
pub(super) fn take(&self, num: usize) -> Vec<LogEntry<TestCommand>> { | ||
(0..num).map(|_| self.next()).collect() | ||
} | ||
|
||
pub(super) fn reset_next_index_to(&self, index: LogIndex) { | ||
let mut this = self.inner.lock(); | ||
this.next_index = index; | ||
this.logs_sent.truncate(index as usize - 1); | ||
} | ||
|
||
pub(super) fn current_index(&self) -> LogIndex { | ||
let this = self.inner.lock(); | ||
this.next_index - 1 | ||
} | ||
|
||
pub(super) fn all_logs(&self) -> Vec<LogEntry<TestCommand>> { | ||
self.inner.lock().logs_sent.clone() | ||
} | ||
|
||
pub(super) fn num_entries_per_page(&self) -> usize { | ||
let page_size = 4096; | ||
self.cal_num(page_size) | ||
} | ||
|
||
pub(super) fn num_entries_per_segment(&self) -> usize { | ||
let this = self.inner.lock(); | ||
self.cal_num(this.segment_size as usize) | ||
} | ||
|
||
fn cal_num(&self, size: usize) -> usize { | ||
let header_size = 32; | ||
let entry_size = self.entry_size(); | ||
(size - header_size + entry_size - 1) / entry_size | ||
} | ||
|
||
fn entry_size(&self) -> usize { | ||
let sample_entry = LogEntry::<TestCommand>::new(1, 1, EntryData::Empty(ProposeId(1, 2))); | ||
let mut wal_codec = WAL::<TestCommand>::new(); | ||
let mut buf = BytesMut::new(); | ||
wal_codec.encode(vec![DataFrame::Entry(sample_entry)], &mut buf); | ||
buf.len() | ||
} | ||
} |