use std::time::Duration;
use crate::error::{DbError, DbResult};
#[derive(Debug, Clone)]
pub struct FixedConfig {
pub shard_count: usize,
pub shard_prefix_bits: usize,
pub grow_step: u32,
pub sync_interval: Duration,
pub sync_batch_size: u32,
pub enable_fsync: bool,
}
impl Default for FixedConfig {
fn default() -> Self {
let shard_count = std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(4);
Self {
shard_count,
shard_prefix_bits: 0,
grow_step: 1_000_000,
sync_interval: Duration::from_millis(50),
sync_batch_size: 1000,
enable_fsync: false,
}
}
}
impl FixedConfig {
pub fn test() -> Self {
Self {
shard_count: 3,
grow_step: 1_000,
sync_interval: Duration::from_millis(10),
sync_batch_size: 100,
..Default::default()
}
}
pub fn validate(&self) -> DbResult<()> {
if self.shard_count == 0 || self.shard_count > 255 {
return Err(DbError::Config("shard_count must be 1..=255"));
}
if self.grow_step == 0 {
return Err(DbError::Config("grow_step must be > 0"));
}
if self.sync_batch_size == 0 {
return Err(DbError::Config("sync_batch_size must be > 0"));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config_is_valid() {
FixedConfig::default().validate().unwrap();
}
#[test]
fn test_test_config_is_valid() {
FixedConfig::test().validate().unwrap();
}
#[test]
fn test_invalid_shard_count() {
let mut c = FixedConfig::test();
c.shard_count = 0;
assert!(c.validate().is_err());
c.shard_count = 256;
assert!(c.validate().is_err());
}
#[test]
fn test_invalid_grow_step() {
let mut c = FixedConfig::test();
c.grow_step = 0;
assert!(c.validate().is_err());
}
#[test]
fn test_invalid_sync_batch_size() {
let mut c = FixedConfig::test();
c.sync_batch_size = 0;
assert!(c.validate().is_err());
}
}