tempest-core 0.0.2

Core utilities and primitives for TempestDB
Documentation
use std::io;

use serde::{Serialize, de::DeserializeOwned};
use smart_default::SmartDefault;

use crate::utils::ByteSize;

#[derive(Debug, Display, Error, From)]
pub enum JournalError {
    #[display("i/o error: {_0}")]
    Io(io::Error),

    #[display("encode error: {_0}")]
    Encode(bincode::Error),

    #[display("journal checksum mismatch: potential corruption")]
    Checksum,

    #[display("invalid magic number")]
    InvalidMagic,

    #[display("journal worker died")]
    WorkerDied,
}

#[derive(Debug, Clone, SmartDefault)]
#[debug("JournalConfig({:?} => {}x)", ByteSize(*growth_baseline), growth_factor)]
pub struct JournalConfig {
    #[default(2)]
    pub growth_factor: u64,
    #[default(64 * 1024 * 1024)]
    pub growth_baseline: u64,
}

pub trait Replayable: 'static {
    /// This atomic edit is serialized into the journal for later
    /// reconstruction via sequential replay.
    type Edit: Serialize + DeserializeOwned;

    /// Apply another [`Self::Edit`] to the current state.
    fn apply(&mut self, edit: Self::Edit);

    /// Calculate a new edit that can be used to reconstruct the current state.
    /// This is required to create the initial edit for file rotations.
    fn snapshot(&self) -> Self::Edit;

    /// This prefix is used to create new files for rotation internally.
    fn filename_prefix() -> &'static str;

    /// Separated from [`Default`], but usually has the same implementation,
    /// just as a semantic distinction.
    fn initial() -> Self;
}