tempest-kv 0.0.2

Key-Value storage layer for TempestDB
Documentation
use smart_default::SmartDefault;
use tempest_core::journal::JournalConfig;

#[derive(Debug, Clone, SmartDefault)]
pub struct MemtableConfig {
    /// Approximate byte size before the active memtable is frozen.
    #[default(64 * 1024 * 1024)]
    pub size_threshold: usize,
    /// Max number of immutable memtables before writes stall.
    #[default(4)]
    pub max_immutable_count: usize,
}

#[derive(Debug, Clone, SmartDefault)]
pub struct WalConfig {
    #[default(64 * 1024 * 1024)]
    pub rotate_file_size_threshold: u64,
    #[default(10_000)]
    pub rotate_record_count_threshold: u32,
    #[default(32)]
    pub max_concurrent_writes: usize,
    pub journal: JournalConfig,
}

#[derive(Debug, Clone, Default)]
pub struct ManifestConfig {
    pub journal: JournalConfig,
}

#[derive(Debug, Clone, SmartDefault)]
pub struct SstWriteConfig {
    /// Target size for SST blocks.
    // TODO: update when refactoring for O_DIRECT:
    /// Blocks will always have at least the specified size, so this is more of a threshold for
    /// flushing the block, than a maximum/limit.
    #[default(4096)]
    pub block_target_size: usize,
    /// Restart interval of delta compression ranges.
    #[default(32)]
    pub block_restart_interval: u32,
    /// False-positive-rate (FPR) of SST bloom filters.
    #[default(0.01)]
    pub bloom_false_positive_rate: f64,
}

#[derive(Debug, Clone, SmartDefault)]
pub struct SstConfig {
    /// Configuration options for SST writers during flushing and compaction.
    pub write: SstWriteConfig,
}

#[derive(Debug, Clone, SmartDefault)]
pub struct CompactionConfig {
    /// The number of SST files that may accumulate before a compaction is triggered.
    #[default(4)]
    pub l0_file_threshold: usize,
}

/// Configuration for this storage and all its sub-components.
#[derive(Debug, Clone, Default)]
pub struct StorageConfig {
    pub memtable: MemtableConfig,
    pub wal: WalConfig,
    pub manifest: ManifestConfig,
    pub sst: SstConfig,
    pub compaction: CompactionConfig,
}

impl StorageConfig {
    /// A config tuned for fast testing.
    ///
    /// - **memtable:** Tiny thresholds force frequent flushes/compactions.
    #[cfg(test)]
    pub fn for_testing() -> Self {
        Self {
            memtable: MemtableConfig {
                size_threshold: 512,
                max_immutable_count: 2,
            },
            wal: WalConfig {
                rotate_file_size_threshold: 4 * 1024,
                rotate_record_count_threshold: 32,
                max_concurrent_writes: 4,
                journal: JournalConfig::default(),
            },
            sst: SstConfig {
                write: SstWriteConfig {
                    // frequent delta encoding restarts within block writer
                    block_restart_interval: 4,
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        }
    }
}