-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
RaftLogReader::get_key_log_ids()
Key log IDs represent the first log IDs proposed by each Leader. These IDs enable Openraft to efficiently access log IDs at each index with a succinct storage. Previously, key log IDs were obtained using a binary-search-like algorithm through `RaftLogReader`. This commit introduces the `RaftLogReader::get_key_log_ids()` method, allowing implementations to directly return a list of key log IDs if the `RaftLogStorage` can provide them. For backward compatibility, a default implementation using the original binary-search method is provided. No application changes are required when upgrading to this version. Tests verifying the implementation are included in `openraft::testing::log::suite::Suite`. - Fixes: databendlabs#1261
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::fmt; | ||
|
||
use crate::alias::LogIdOf; | ||
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-test (stable, 0)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / rt-monoio
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-feature-test (nightly, single-term-leader)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-test (nightly, 0, single-term-leader)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-test (nightly, 30)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-feature-test (nightly, single-term-leader,serde,singlethreaded)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-feature-test (nightly)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / Build (nightly, bench,serde,bt,singlethreaded)
Check failure on line 3 in openraft/src/engine/leader_log_ids.rs GitHub Actions / openraft-feature-test (nightly, serde)
|
||
use crate::RaftTypeConfig; | ||
|
||
/// The first and the last log id belonging to a Leader. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub(crate) struct LeaderLogIds<C: RaftTypeConfig> { | ||
first_last: Option<(LogIdOf<C>, LogIdOf<C>)>, | ||
} | ||
|
||
impl<C> fmt::Display for LeaderLogIds<C> | ||
where C: RaftTypeConfig | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.first_last { | ||
None => write!(f, "None"), | ||
Some((first, last)) => write!(f, "({}, {})", first, last), | ||
} | ||
} | ||
} | ||
|
||
impl<C> LeaderLogIds<C> | ||
where C: RaftTypeConfig | ||
{ | ||
pub(crate) fn new(log_ids: Option<(LogIdOf<C>, LogIdOf<C>)>) -> Self { | ||
Self { first_last: log_ids } | ||
} | ||
|
||
/// Used only in tests | ||
#[allow(dead_code)] | ||
pub(crate) fn new1(log_id: LogIdOf<C>) -> Self { | ||
Self { | ||
first_last: Some((log_id.clone(), log_id)), | ||
} | ||
} | ||
|
||
/// Used only in tests | ||
#[allow(dead_code)] | ||
pub(crate) fn new2(first: LogIdOf<C>, last: LogIdOf<C>) -> Self { | ||
Self { | ||
first_last: Some((first, last)), | ||
} | ||
} | ||
|
||
pub(crate) fn first(&self) -> Option<&LogIdOf<C>> { | ||
self.first_last.as_ref().map(|x| &x.0) | ||
} | ||
|
||
pub(crate) fn last(&self) -> Option<&LogIdOf<C>> { | ||
self.first_last.as_ref().map(|x| &x.1) | ||
} | ||
} |