openraft/log_id/
mod.rs

1//! This mod defines the identity of a raft log and provides supporting utilities to work with log
2//! id related types.
3
4mod log_id_option_ext;
5mod log_index_option_ext;
6mod raft_log_id;
7
8use std::fmt::Display;
9use std::fmt::Formatter;
10
11pub use log_id_option_ext::LogIdOptionExt;
12pub use log_index_option_ext::LogIndexOptionExt;
13pub use raft_log_id::RaftLogId;
14
15use crate::CommittedLeaderId;
16use crate::MessageSummary;
17use crate::NodeId;
18
19/// The identity of a raft log.
20///
21/// The log id serves as unique identifier for a log entry across the system. It is composed of two
22/// parts: a leader id, which refers to the leader that proposed this log, and an integer index.
23#[derive(Debug, Default, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
24#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
25pub struct LogId<NID: NodeId> {
26    /// The id of the leader that proposed this log
27    pub leader_id: CommittedLeaderId<NID>,
28    /// The index of a log in the storage.
29    ///
30    /// Log index is a consecutive integer.
31    pub index: u64,
32}
33
34impl<NID: NodeId> RaftLogId<NID> for LogId<NID> {
35    fn get_log_id(&self) -> &LogId<NID> {
36        self
37    }
38
39    fn set_log_id(&mut self, log_id: &LogId<NID>) {
40        *self = log_id.clone()
41    }
42}
43
44impl<NID: NodeId> Display for LogId<NID> {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        write!(f, "{}-{}", self.leader_id, self.index)
47    }
48}
49
50impl<NID: NodeId> MessageSummary<LogId<NID>> for LogId<NID> {
51    fn summary(&self) -> String {
52        format!("{}", self)
53    }
54}
55
56impl<NID: NodeId> LogId<NID> {
57    /// Creates a log id proposed by a committed leader with `leader_id` at the given index.
58    pub fn new(leader_id: CommittedLeaderId<NID>, index: u64) -> Self {
59        LogId { leader_id, index }
60    }
61
62    /// Returns the leader id that proposed this log.
63    pub fn committed_leader_id(&self) -> &CommittedLeaderId<NID> {
64        &self.leader_id
65    }
66}