use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CompactionConfig {
pub threshold_tokens: usize,
pub retain_recent: usize,
pub min_messages_for_compaction: usize,
pub auto_compact: bool,
}
impl Default for CompactionConfig {
fn default() -> Self {
Self {
threshold_tokens: 80_000,
retain_recent: 10,
min_messages_for_compaction: 20,
auto_compact: true,
}
}
}
impl CompactionConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn with_threshold_tokens(mut self, threshold: usize) -> Self {
self.threshold_tokens = threshold;
self
}
#[must_use]
pub const fn with_retain_recent(mut self, count: usize) -> Self {
self.retain_recent = count;
self
}
#[must_use]
pub const fn with_min_messages(mut self, count: usize) -> Self {
self.min_messages_for_compaction = count;
self
}
#[must_use]
pub const fn with_auto_compact(mut self, auto: bool) -> Self {
self.auto_compact = auto;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = CompactionConfig::default();
assert_eq!(config.threshold_tokens, 80_000);
assert_eq!(config.retain_recent, 10);
assert_eq!(config.min_messages_for_compaction, 20);
assert!(config.auto_compact);
}
#[test]
fn test_builder_pattern() {
let config = CompactionConfig::new()
.with_threshold_tokens(50_000)
.with_retain_recent(5)
.with_min_messages(10)
.with_auto_compact(false);
assert_eq!(config.threshold_tokens, 50_000);
assert_eq!(config.retain_recent, 5);
assert_eq!(config.min_messages_for_compaction, 10);
assert!(!config.auto_compact);
}
}