use std::path::PathBuf;
pub use crate::enums::{AppendFsync, EvictionPolicy, LogLevel, LogOutput};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerSection {
pub bind: [u8; 4],
pub port: u16,
pub threads: usize,
pub accept_shards: Option<usize>,
pub max_clients: usize,
pub data_dir: PathBuf,
}
impl Default for ServerSection {
fn default() -> Self {
Self {
bind: [127, 0, 0, 1],
port: 6004,
threads: 0,
accept_shards: None,
max_clients: 10_000,
data_dir: PathBuf::from("."),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersistenceSection {
pub aof: bool,
pub appendfsync: AppendFsync,
pub auto_aof_rewrite_percentage: u32,
pub auto_aof_rewrite_min_size: u64,
}
impl Default for PersistenceSection {
fn default() -> Self {
Self {
aof: true,
appendfsync: AppendFsync::EverySec,
auto_aof_rewrite_percentage: 100,
auto_aof_rewrite_min_size: 64 * 1024 * 1024,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemorySection {
pub maxmemory: u64,
pub maxmemory_policy: EvictionPolicy,
}
impl Default for MemorySection {
fn default() -> Self {
Self {
maxmemory: 0,
maxmemory_policy: EvictionPolicy::NoEviction,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default)]
pub struct MetricsSection {
pub listen_port: u16,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuditSection {
pub log_path: PathBuf,
}
impl Default for AuditSection {
fn default() -> Self {
Self { log_path: PathBuf::new() }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExpirySection {
pub hz: u32,
pub sample: u32,
}
impl Default for ExpirySection {
fn default() -> Self {
Self { hz: 10, sample: 20 }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogSection {
pub level: LogLevel,
pub output: LogOutput,
}
impl Default for LogSection {
fn default() -> Self {
Self {
level: LogLevel::Info,
output: LogOutput::Stderr,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AdvancedSection {
pub spin_limit: u32,
pub park_timeout_ms: u32,
pub tick_check_every: u32,
pub ring_capacity: usize,
}
impl Default for AdvancedSection {
fn default() -> Self {
Self {
spin_limit: 256,
park_timeout_ms: 50,
tick_check_every: 256,
ring_capacity: 1024,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NotificationSection {
pub notify_keyspace_events: String,
}
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct NotificationFlags {
pub keyspace: bool,
pub keyevent: bool,
pub generic: bool,
pub string: bool,
pub list: bool,
pub set: bool,
pub hash: bool,
pub zset: bool,
pub stream: bool,
}
impl NotificationFlags {
pub fn is_empty(&self) -> bool {
!(self.keyspace || self.keyevent)
|| !(self.generic
|| self.string
|| self.list
|| self.set
|| self.hash
|| self.zset
|| self.stream)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LuaSection {
pub time_limit_ms: u64,
pub allow_dialects: Vec<String>,
}
impl Default for LuaSection {
fn default() -> Self {
Self {
time_limit_ms: 5000,
allow_dialects: Vec::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlowlogSection {
pub slower_than_micros: i64,
pub max_len: u32,
}
impl Default for SlowlogSection {
fn default() -> Self {
Self {
slower_than_micros: -1,
max_len: 128,
}
}
}
pub fn parse_notification_flags(s: &str) -> NotificationFlags {
let mut f = NotificationFlags::default();
for c in s.chars() {
match c {
'K' => f.keyspace = true,
'E' => f.keyevent = true,
'g' => f.generic = true,
'$' => f.string = true,
'l' => f.list = true,
's' => f.set = true,
'h' => f.hash = true,
'z' => f.zset = true,
't' => f.stream = true,
'A' => {
f.generic = true;
f.string = true;
f.list = true;
f.set = true;
f.hash = true;
f.zset = true;
f.stream = true;
}
_ => {} }
}
f
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Config {
pub server: ServerSection,
pub persistence: PersistenceSection,
pub memory: MemorySection,
pub metrics: MetricsSection,
pub audit: AuditSection,
pub expiry: ExpirySection,
pub log: LogSection,
pub notification: NotificationSection,
pub advanced: AdvancedSection,
pub slowlog: SlowlogSection,
pub cluster: crate::cluster::ClusterSection,
pub lua: LuaSection,
pub replication: crate::replication::ReplicationSection,
pub feed: FeedSection,
pub source_path: Option<PathBuf>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FeedSection {
pub enabled: bool,
pub feed_buffer_size: u64,
}
impl Default for FeedSection {
fn default() -> Self {
Self { enabled: false, feed_buffer_size: 64 * 1024 * 1024 }
}
}
pub use crate::error::ConfigError;