forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Retrieve Key Log IDs via
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
1 parent
2c84eb4
commit 2e02fc1
Showing
10 changed files
with
264 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::fmt; | ||
|
||
use crate::type_config::alias::LogIdOf; | ||
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) | ||
} | ||
} |
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
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
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
Oops, something went wrong.