-
Notifications
You must be signed in to change notification settings - Fork 157
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: #1261
- Loading branch information
1 parent
2c84eb4
commit 9178ef8
Showing
10 changed files
with
271 additions
and
47 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,54 @@ | ||
use std::fmt; | ||
use std::ops::RangeInclusive; | ||
|
||
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> { | ||
log_id_range: Option<RangeInclusive<LogIdOf<C>>>, | ||
} | ||
|
||
impl<C> fmt::Display for LeaderLogIds<C> | ||
where C: RaftTypeConfig | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.log_id_range { | ||
None => write!(f, "None"), | ||
Some(rng) => write!(f, "({}, {})", rng.start(), rng.end()), | ||
} | ||
} | ||
} | ||
|
||
impl<C> LeaderLogIds<C> | ||
where C: RaftTypeConfig | ||
{ | ||
pub(crate) fn new(log_id_range: Option<RangeInclusive<LogIdOf<C>>>) -> Self { | ||
Self { log_id_range } | ||
} | ||
|
||
/// Used only in tests | ||
#[allow(dead_code)] | ||
pub(crate) fn new_single(log_id: LogIdOf<C>) -> Self { | ||
Self { | ||
log_id_range: Some(log_id.clone()..=log_id), | ||
} | ||
} | ||
|
||
/// Used only in tests | ||
#[allow(dead_code)] | ||
pub(crate) fn new_start_end(first: LogIdOf<C>, last: LogIdOf<C>) -> Self { | ||
Self { | ||
log_id_range: Some(first..=last), | ||
} | ||
} | ||
|
||
pub(crate) fn first(&self) -> Option<&LogIdOf<C>> { | ||
self.log_id_range.as_ref().map(|x| x.start()) | ||
} | ||
|
||
pub(crate) fn last(&self) -> Option<&LogIdOf<C>> { | ||
self.log_id_range.as_ref().map(|x| x.end()) | ||
} | ||
} |
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.