1use crate::ids::{ClientId, LogIndex, Term};
7use crate::membership::ClusterConfig;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum EntryKind {
12 NoOp,
15 Command {
19 client_id: ClientId,
21 seq: u64,
23 payload: Vec<u8>,
25 },
26 Config {
28 config: ClusterConfig,
30 },
31}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct LogEntry {
36 pub term: Term,
38 pub kind: EntryKind,
40}
41
42impl LogEntry {
43 #[must_use]
46 pub fn approx_len(&self) -> usize {
47 match &self.kind {
48 EntryKind::NoOp => 16,
49 EntryKind::Command { payload, .. } => 32 + payload.len(),
50 EntryKind::Config { config } => 32 + 8 * (config.voters.len() + config.learners.len()),
51 }
52 }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, Default)]
57pub struct Log {
58 base_index: LogIndex,
59 base_term: Term,
60 entries: Vec<LogEntry>,
61}
62
63impl Log {
64 #[must_use]
66 pub fn new() -> Self {
67 Self::default()
68 }
69
70 #[must_use]
72 pub const fn base_index(&self) -> LogIndex {
73 self.base_index
74 }
75
76 #[must_use]
78 pub const fn last_index(&self) -> LogIndex {
79 LogIndex(self.base_index.0 + self.entries.len() as u64)
80 }
81
82 #[must_use]
84 pub fn last_term(&self) -> Term {
85 self.entries.last().map_or(self.base_term, |e| e.term)
86 }
87
88 #[must_use]
91 pub fn term_at(&self, index: LogIndex) -> Option<Term> {
92 if index == LogIndex::ZERO && self.base_index == LogIndex::ZERO {
93 return Some(Term::ZERO);
94 }
95 if index == self.base_index {
96 return Some(self.base_term);
97 }
98 if index < self.base_index || index > self.last_index() {
99 return None;
100 }
101 let pos = self.pos_of(index);
102 self.entries.get(pos).map(|e| e.term)
103 }
104
105 #[must_use]
108 pub fn entry_at(&self, index: LogIndex) -> Option<&LogEntry> {
109 if index <= self.base_index || index > self.last_index() {
110 return None;
111 }
112 let pos = self.pos_of(index);
113 self.entries.get(pos)
114 }
115
116 fn pos_of(&self, index: LogIndex) -> usize {
120 usize::try_from(index.0 - self.base_index.0 - 1)
121 .expect("a bounds-checked log offset always fits in usize")
122 }
123
124 pub fn append(&mut self, new_entries: &[LogEntry]) -> LogIndex {
127 let first = self.last_index().next();
128 self.entries.extend_from_slice(new_entries);
129 first
130 }
131
132 #[must_use]
134 pub const fn base_term(&self) -> Term {
135 self.base_term
136 }
137
138 pub fn advance_base(&mut self, index: LogIndex, term: Term) {
144 if index <= self.base_index {
145 return;
146 }
147 let drop = usize::try_from(index.0 - self.base_index.0)
148 .unwrap_or(usize::MAX)
149 .min(self.entries.len());
150 self.entries.drain(..drop);
151 self.base_index = index;
152 self.base_term = term;
153 }
154
155 pub fn clear_entries(&mut self) {
159 self.entries.clear();
160 }
161
162 pub fn truncate_from(&mut self, index: LogIndex) {
165 if index <= self.base_index {
166 return;
167 }
168 let keep = self.pos_of(index);
169 self.entries.truncate(keep);
170 }
171
172 #[must_use]
174 pub fn first_index_of_term(&self, term: Term) -> Option<LogIndex> {
175 if term == self.base_term && self.base_index != LogIndex::ZERO {
178 return Some(self.base_index);
179 }
180 let start = self.entries.partition_point(|e| e.term < term);
181 match self.entries.get(start) {
182 Some(entry) if entry.term == term => {
183 Some(LogIndex(self.base_index.0 + start as u64 + 1))
184 }
185 _ => None,
186 }
187 }
188
189 #[must_use]
191 pub fn last_index_of_term(&self, term: Term) -> Option<LogIndex> {
192 let end = self.entries.partition_point(|e| e.term <= term);
193 if end == 0 {
194 if term == self.base_term && self.base_index != LogIndex::ZERO {
195 return Some(self.base_index);
196 }
197 return None;
198 }
199 match self.entries.get(end - 1) {
200 Some(entry) if entry.term == term => Some(LogIndex(self.base_index.0 + end as u64)),
201 _ => None,
202 }
203 }
204
205 #[must_use]
209 pub fn entries_from(
210 &self,
211 index: LogIndex,
212 max_count: usize,
213 max_bytes: usize,
214 ) -> Vec<LogEntry> {
215 if index <= self.base_index || index > self.last_index() {
216 return Vec::new();
217 }
218 let start = self.pos_of(index);
219 let mut out = Vec::new();
220 let mut bytes = 0_usize;
221 for entry in self.entries.iter().skip(start) {
222 if out.len() >= max_count {
223 break;
224 }
225 bytes += entry.approx_len();
226 if !out.is_empty() && bytes > max_bytes {
227 break;
228 }
229 out.push(entry.clone());
230 }
231 out
232 }
233
234 #[must_use]
236 pub fn entries(&self) -> &[LogEntry] {
237 &self.entries
238 }
239}