Skip to main content

tempest_core/journal/
config.rs

1use std::io;
2
3use serde::{Serialize, de::DeserializeOwned};
4use smart_default::SmartDefault;
5
6use crate::utils::ByteSize;
7
8#[derive(Debug, Display, Error, From)]
9pub enum JournalError {
10    #[display("i/o error: {_0}")]
11    Io(io::Error),
12
13    #[display("encode error: {_0}")]
14    Encode(bincode::Error),
15
16    #[display("journal checksum mismatch: potential corruption")]
17    Checksum,
18
19    #[display("invalid magic number")]
20    InvalidMagic,
21
22    #[display("journal worker died")]
23    WorkerDied,
24}
25
26#[derive(Debug, Clone, SmartDefault)]
27#[debug("JournalConfig({:?} => {}x)", ByteSize(*growth_baseline), growth_factor)]
28pub struct JournalConfig {
29    #[default(2)]
30    pub growth_factor: u64,
31    #[default(64 * 1024 * 1024)]
32    pub growth_baseline: u64,
33}
34
35pub trait Replayable: 'static {
36    /// This atomic edit is serialized into the journal for later
37    /// reconstruction via sequential replay.
38    type Edit: Serialize + DeserializeOwned;
39
40    /// Apply another [`Self::Edit`] to the current state.
41    fn apply(&mut self, edit: Self::Edit);
42
43    /// Calculate a new edit that can be used to reconstruct the current state.
44    /// This is required to create the initial edit for file rotations.
45    fn snapshot(&self) -> Self::Edit;
46
47    /// This prefix is used to create new files for rotation internally.
48    fn filename_prefix() -> &'static str;
49
50    /// Separated from [`Default`], but usually has the same implementation,
51    /// just as a semantic distinction.
52    fn initial() -> Self;
53}