const DEFAULT_MAX_RECORD_SIZE: u32 = 64 * 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RecoveryPolicy {
StopAtFirstError,
SkipBadRecords,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WalConfig {
max_record_size: u32,
recovery_policy: RecoveryPolicy,
}
impl WalConfig {
#[must_use]
pub const fn new() -> Self {
WalConfig {
max_record_size: DEFAULT_MAX_RECORD_SIZE,
recovery_policy: RecoveryPolicy::StopAtFirstError,
}
}
#[must_use]
pub const fn with_max_record_size(mut self, bytes: u32) -> Self {
self.max_record_size = bytes;
self
}
#[must_use]
pub const fn max_record_size(self) -> u32 {
self.max_record_size
}
#[must_use]
pub const fn with_recovery_policy(mut self, policy: RecoveryPolicy) -> Self {
self.recovery_policy = policy;
self
}
#[must_use]
pub const fn recovery_policy(self) -> RecoveryPolicy {
self.recovery_policy
}
}
impl Default for WalConfig {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_max_record_size_is_64_mib() {
assert_eq!(WalConfig::new().max_record_size(), 64 * 1024 * 1024);
assert_eq!(WalConfig::default().max_record_size(), 64 * 1024 * 1024);
}
#[test]
fn test_with_max_record_size_overrides_default() {
let config = WalConfig::new().with_max_record_size(123);
assert_eq!(config.max_record_size(), 123);
}
#[test]
fn test_config_is_copy_and_eq() {
let a = WalConfig::new().with_max_record_size(10);
let b = a;
assert_eq!(a, b);
}
}