Skip to main content

reifydb_codec/log/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4pub mod index;
5pub mod meta;
6pub mod reader;
7pub mod record;
8pub mod vote;
9
10use std::fmt::{self, Display, Formatter};
11
12use reifydb_value::reifydb_assertions;
13use serde::{Deserialize, Serialize};
14
15#[repr(transparent)]
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
17pub struct LogVersion(u64);
18
19impl LogVersion {
20	pub const ZERO: Self = Self(0);
21
22	pub const fn new(version: u64) -> Self {
23		Self(version)
24	}
25
26	pub const fn as_u64(self) -> u64 {
27		self.0
28	}
29}
30
31impl Display for LogVersion {
32	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
33		write!(f, "{}", self.0)
34	}
35}
36
37impl From<LogVersion> for u64 {
38	fn from(version: LogVersion) -> Self {
39		version.0
40	}
41}
42
43#[repr(transparent)]
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
45pub struct LogIndex(u64);
46
47impl LogIndex {
48	pub const ZERO: Self = Self(0);
49
50	pub const fn new(index: u64) -> Self {
51		Self(index)
52	}
53
54	pub const fn as_u64(self) -> u64 {
55		self.0
56	}
57}
58
59impl Display for LogIndex {
60	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
61		write!(f, "{}", self.0)
62	}
63}
64
65impl From<LogIndex> for u64 {
66	fn from(index: LogIndex) -> Self {
67		index.0
68	}
69}
70
71#[repr(transparent)]
72#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
73pub struct Term(u64);
74
75impl Term {
76	pub const ZERO: Self = Self(0);
77
78	pub const fn new(term: u64) -> Self {
79		Self(term)
80	}
81
82	pub const fn as_u64(self) -> u64 {
83		self.0
84	}
85}
86
87impl Display for Term {
88	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
89		write!(f, "{}", self.0)
90	}
91}
92
93impl From<Term> for u64 {
94	fn from(term: Term) -> Self {
95		term.0
96	}
97}
98
99#[repr(transparent)]
100#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
101pub struct NodeId(u64);
102
103impl NodeId {
104	pub const fn new(id: u64) -> Self {
105		reifydb_assertions! {
106			assert!(
107				id != u64::MAX,
108				"u64::MAX is the on disk encoding of no vote, so a node carrying it is indistinguishable \
109				 from a node that has never voted and can vote twice in one term"
110			);
111		}
112		Self(id)
113	}
114
115	pub const fn as_u64(self) -> u64 {
116		self.0
117	}
118}
119
120impl Display for NodeId {
121	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
122		write!(f, "{}", self.0)
123	}
124}
125
126impl From<NodeId> for u64 {
127	fn from(id: NodeId) -> Self {
128		id.0
129	}
130}
131
132#[repr(transparent)]
133#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
134pub struct RecordKind(u32);
135
136impl RecordKind {
137	pub const fn new(kind: u32) -> Self {
138		Self(kind)
139	}
140
141	pub const fn as_u32(self) -> u32 {
142		self.0
143	}
144}
145
146impl Display for RecordKind {
147	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
148		write!(f, "{}", self.0)
149	}
150}
151
152impl From<RecordKind> for u32 {
153	fn from(kind: RecordKind) -> Self {
154		kind.0
155	}
156}
157
158#[repr(transparent)]
159#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
160pub struct Position(u64);
161
162impl Position {
163	pub const ZERO: Self = Self(0);
164
165	pub const fn new(position: u64) -> Self {
166		Self(position)
167	}
168
169	pub const fn as_u64(self) -> u64 {
170		self.0
171	}
172
173	pub const fn advance(self, bytes: u64) -> Self {
174		Self(self.0 + bytes)
175	}
176
177	pub const fn distance_from(self, earlier: Self) -> u64 {
178		self.0.saturating_sub(earlier.0)
179	}
180}
181
182impl Display for Position {
183	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
184		write!(f, "{}", self.0)
185	}
186}
187
188impl From<Position> for u64 {
189	fn from(position: Position) -> Self {
190		position.0
191	}
192}
193
194#[repr(transparent)]
195#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
196pub struct VoteSeq(u64);
197
198impl VoteSeq {
199	pub const FIRST: Self = Self(1);
200
201	pub const fn new(seq: u64) -> Self {
202		Self(seq)
203	}
204
205	pub const fn as_u64(self) -> u64 {
206		self.0
207	}
208
209	pub const fn next(self) -> Self {
210		Self(self.0 + 1)
211	}
212}
213
214impl Display for VoteSeq {
215	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
216		write!(f, "{}", self.0)
217	}
218}
219
220impl From<VoteSeq> for u64 {
221	fn from(seq: VoteSeq) -> Self {
222		seq.0
223	}
224}