1use crate::ids::{ClientId, LogIndex, Term};
9use crate::membership::ClusterConfig;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum EntryKind {
14 NoOp,
17 Command {
21 client_id: ClientId,
23 seq: u64,
25 payload: Vec<u8>,
27 },
28 Config {
30 config: ClusterConfig,
32 },
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct LogEntry {
38 pub term: Term,
40 pub kind: EntryKind,
42}
43
44impl LogEntry {
45 #[must_use]
48 pub fn approx_len(&self) -> usize {
49 match &self.kind {
50 EntryKind::NoOp => 16,
51 EntryKind::Command { payload, .. } => 32 + payload.len(),
52 EntryKind::Config { config } => 32 + 8 * (config.voters.len() + config.learners.len()),
53 }
54 }
55}
56
57#[derive(Debug, Clone, PartialEq, Eq, Default)]
59pub struct Log {
60 base_index: LogIndex,
61 base_term: Term,
62 entries: Vec<LogEntry>,
63}
64
65impl Log {
66 #[must_use]
68 pub fn new() -> Self {
69 Self::default()
70 }
71
72 #[must_use]
74 pub const fn base_index(&self) -> LogIndex {
75 self.base_index
76 }
77
78 #[must_use]
80 pub fn last_index(&self) -> LogIndex {
81 LogIndex(self.base_index.0 + self.entries.len() as u64)
82 }
83
84 #[must_use]
86 pub fn last_term(&self) -> Term {
87 self.entries.last().map_or(self.base_term, |e| e.term)
88 }
89
90 #[must_use]
93 pub fn term_at(&self, index: LogIndex) -> Option<Term> {
94 if index == LogIndex::ZERO && self.base_index == LogIndex::ZERO {
95 return Some(Term::ZERO);
96 }
97 if index == self.base_index {
98 return Some(self.base_term);
99 }
100 if index < self.base_index || index > self.last_index() {
101 return None;
102 }
103 let pos = self.pos_of(index);
104 self.entries.get(pos).map(|e| e.term)
105 }
106
107 #[must_use]
110 pub fn entry_at(&self, index: LogIndex) -> Option<&LogEntry> {
111 if index <= self.base_index || index > self.last_index() {
112 return None;
113 }
114 let pos = self.pos_of(index);
115 self.entries.get(pos)
116 }
117
118 fn pos_of(&self, index: LogIndex) -> usize {
122 usize::try_from(index.0 - self.base_index.0 - 1)
123 .expect("a bounds-checked log offset always fits in usize")
124 }
125
126 pub fn append(&mut self, new_entries: &[LogEntry]) -> LogIndex {
129 let first = self.last_index().next();
130 self.entries.extend_from_slice(new_entries);
131 first
132 }
133
134 #[must_use]
136 pub const fn base_term(&self) -> Term {
137 self.base_term
138 }
139
140 pub fn advance_base(&mut self, index: LogIndex, term: Term) {
146 if index <= self.base_index {
147 return;
148 }
149 let drop = usize::try_from(index.0 - self.base_index.0)
150 .unwrap_or(usize::MAX)
151 .min(self.entries.len());
152 self.entries.drain(..drop);
153 self.base_index = index;
154 self.base_term = term;
155 }
156
157 pub fn clear_entries(&mut self) {
161 self.entries.clear();
162 }
163
164 pub fn truncate_from(&mut self, index: LogIndex) {
167 if index <= self.base_index {
168 return;
169 }
170 let keep = self.pos_of(index);
171 self.entries.truncate(keep);
172 }
173
174 #[must_use]
176 pub fn first_index_of_term(&self, term: Term) -> Option<LogIndex> {
177 if term == self.base_term && self.base_index != LogIndex::ZERO {
180 return Some(self.base_index);
181 }
182 let start = self.entries.partition_point(|e| e.term < term);
183 match self.entries.get(start) {
184 Some(entry) if entry.term == term => {
185 Some(LogIndex(self.base_index.0 + start as u64 + 1))
186 }
187 _ => None,
188 }
189 }
190
191 #[must_use]
193 pub fn last_index_of_term(&self, term: Term) -> Option<LogIndex> {
194 let end = self.entries.partition_point(|e| e.term <= term);
195 if end == 0 {
196 if term == self.base_term && self.base_index != LogIndex::ZERO {
197 return Some(self.base_index);
198 }
199 return None;
200 }
201 match self.entries.get(end - 1) {
202 Some(entry) if entry.term == term => Some(LogIndex(self.base_index.0 + end as u64)),
203 _ => None,
204 }
205 }
206
207 #[must_use]
211 pub fn entries_from(
212 &self,
213 index: LogIndex,
214 max_count: usize,
215 max_bytes: usize,
216 ) -> Vec<LogEntry> {
217 if index <= self.base_index || index > self.last_index() {
218 return Vec::new();
219 }
220 let start = self.pos_of(index);
221 let mut out = Vec::new();
222 let mut bytes = 0_usize;
223 for entry in self.entries.iter().skip(start) {
224 if out.len() >= max_count {
225 break;
226 }
227 bytes += entry.approx_len();
228 if !out.is_empty() && bytes > max_bytes {
229 break;
230 }
231 out.push(entry.clone());
232 }
233 out
234 }
235
236 #[must_use]
238 pub fn entries(&self) -> &[LogEntry] {
239 &self.entries
240 }
241}