use std::path::PathBuf;
use super::WriteEngineConfig;
use crate::config::Config;
use crate::schema::TableSchema;
fn clamp_threshold_bytes(bytes: u64, knob: &str) -> usize {
match usize::try_from(bytes) {
Ok(v) => v,
Err(_) => {
tracing::warn!(
"config {} = {} bytes exceeds usize::MAX ({}) on this target; clamping",
knob,
bytes,
usize::MAX
);
usize::MAX
}
}
}
impl WriteEngineConfig {
pub fn from_config(
config: &Config,
data_dir: PathBuf,
wal_dir: PathBuf,
schema: TableSchema,
) -> Self {
Self {
data_dir,
wal_dir,
memtable_flush_threshold: clamp_threshold_bytes(
config.storage.memtable_size_threshold,
"storage.memtable_size_threshold",
),
memtable_hard_limit: clamp_threshold_bytes(
config.storage.memtable_hard_limit,
"storage.memtable_hard_limit",
),
schema,
durability: super::Durability::default(),
udt_registry: None,
auto_compaction: config.storage.compaction.auto_compaction,
compaction_min_threshold: config.storage.compaction.min_threshold,
compaction_max_threshold: config.storage.compaction.max_threshold,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::CompactionConfig;
fn schema() -> TableSchema {
crate::storage::write_engine::test_support::create_test_schema()
}
#[test]
fn new_equals_from_config_of_default_config() {
let data = PathBuf::from("/tmp/cqlite-bridge-data");
let wal = PathBuf::from("/tmp/cqlite-bridge-wal");
let via_new = WriteEngineConfig::new(data.clone(), wal.clone(), schema());
let via_bridge = WriteEngineConfig::from_config(&Config::default(), data, wal, schema());
assert_eq!(
via_new.memtable_flush_threshold,
via_bridge.memtable_flush_threshold
);
assert_eq!(via_new.memtable_hard_limit, via_bridge.memtable_hard_limit);
assert_eq!(via_new.auto_compaction, via_bridge.auto_compaction);
assert_eq!(
via_new.compaction_min_threshold,
via_bridge.compaction_min_threshold
);
assert_eq!(
via_new.compaction_max_threshold,
via_bridge.compaction_max_threshold
);
}
#[test]
fn engine_defaults_originate_from_public_config_defaults() {
let defaults = Config::default();
let cfg =
WriteEngineConfig::new(PathBuf::from("/tmp/d"), PathBuf::from("/tmp/w"), schema());
assert_eq!(
cfg.memtable_flush_threshold as u64, defaults.storage.memtable_size_threshold,
"engine flush threshold must come from Config::default()"
);
assert_eq!(
cfg.compaction_min_threshold,
defaults.storage.compaction.min_threshold
);
assert_eq!(
cfg.compaction_max_threshold,
defaults.storage.compaction.max_threshold
);
assert_eq!(
cfg.memtable_hard_limit as u64, defaults.storage.memtable_hard_limit,
"engine hard limit must come from Config::default()"
);
assert_eq!(defaults.storage.memtable_size_threshold, 64 * 1024 * 1024);
assert_eq!(defaults.storage.memtable_hard_limit, 256 * 1024 * 1024);
}
#[test]
fn from_config_threads_every_public_knob() {
let mut config = Config::default();
config.storage.memtable_size_threshold = 4096;
config.storage.memtable_hard_limit = 8192;
config.storage.compaction.auto_compaction = false;
config.storage.compaction.min_threshold = 2;
config.storage.compaction.max_threshold = 3;
let cfg = WriteEngineConfig::from_config(
&config,
PathBuf::from("/tmp/d"),
PathBuf::from("/tmp/w"),
schema(),
);
assert_eq!(cfg.memtable_flush_threshold, 4096);
assert_eq!(cfg.memtable_hard_limit, 8192);
assert!(!cfg.auto_compaction);
assert_eq!(cfg.compaction_min_threshold, 2);
assert_eq!(cfg.compaction_max_threshold, 3);
}
#[test]
fn oversized_threshold_clamps_and_never_truncates() {
assert_eq!(clamp_threshold_bytes(u64::MAX, "test"), usize::MAX);
assert_eq!(clamp_threshold_bytes(0, "test"), 0);
assert_eq!(clamp_threshold_bytes(4096, "test"), 4096);
}
#[test]
fn from_config_threads_every_compaction_field() {
let mut config = Config::default();
config.storage.compaction = CompactionConfig {
auto_compaction: true,
min_threshold: 7,
max_threshold: 9,
};
let CompactionConfig {
auto_compaction,
min_threshold,
max_threshold,
} = config.storage.compaction.clone();
let cfg = WriteEngineConfig::from_config(
&config,
PathBuf::from("/tmp/d"),
PathBuf::from("/tmp/w"),
schema(),
);
assert_eq!(cfg.auto_compaction, auto_compaction);
assert_eq!(cfg.compaction_min_threshold, min_threshold);
assert_eq!(cfg.compaction_max_threshold, max_threshold);
}
}