use crate::serde_json::Value as JsonValue;
use crate::storage::UnifiedStore;
#[inline]
fn num(v: f64) -> JsonValue {
JsonValue::Number(v)
}
#[inline]
fn text(s: &str) -> JsonValue {
JsonValue::String(s.to_string())
}
#[derive(Debug, Clone)]
pub struct ConfigDefault {
pub key: &'static str,
pub tier: Tier,
pub default: fn() -> JsonValue,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tier {
Critical,
Optional,
}
pub const MATRIX: &[ConfigDefault] = &[
ConfigDefault {
key: "cache.blob.l1_bytes_max",
tier: Tier::Critical,
default: || num(crate::storage::cache::DEFAULT_BLOB_L1_BYTES_MAX as f64),
},
ConfigDefault {
key: "cache.blob.l2_bytes_max",
tier: Tier::Critical,
default: || num(crate::storage::cache::DEFAULT_BLOB_L2_BYTES_MAX as f64),
},
ConfigDefault {
key: "cache.blob.max_namespaces",
tier: Tier::Critical,
default: || num(crate::storage::cache::DEFAULT_BLOB_MAX_NAMESPACES as f64),
},
ConfigDefault {
key: "durability.mode",
tier: Tier::Critical,
default: || text("sync"),
},
ConfigDefault {
key: "runtime.result_cache.backend",
tier: Tier::Critical,
default: || text("legacy"),
},
ConfigDefault {
key: "concurrency.locking.enabled",
tier: Tier::Critical,
default: || JsonValue::Bool(true),
},
ConfigDefault {
key: "concurrency.locking.deadlock_timeout_ms",
tier: Tier::Optional,
default: || num(5000.0),
},
ConfigDefault {
key: "storage.wal.max_interval_ms",
tier: Tier::Critical,
default: || num(10.0),
},
ConfigDefault {
key: "storage.wal.min_batch_size",
tier: Tier::Optional,
default: || num(4.0),
},
ConfigDefault {
key: "storage.bgwriter.delay_ms",
tier: Tier::Critical,
default: || num(200.0),
},
ConfigDefault {
key: "storage.bgwriter.max_pages_per_round",
tier: Tier::Optional,
default: || num(100.0),
},
ConfigDefault {
key: "storage.bgwriter.lru_multiplier",
tier: Tier::Optional,
default: || num(2.0),
},
ConfigDefault {
key: "storage.bulk_insert.max_buffered_rows",
tier: Tier::Optional,
default: || num(1000.0),
},
ConfigDefault {
key: "storage.bulk_insert.max_buffered_bytes",
tier: Tier::Optional,
default: || num(65536.0),
},
ConfigDefault {
key: "storage.hot_update.max_chain_hops",
tier: Tier::Optional,
default: || num(32.0),
},
ConfigDefault {
key: "storage.btree.lehman_yao",
tier: Tier::Critical,
default: || JsonValue::Bool(true),
},
ConfigDefault {
key: "ai.ner.backend",
tier: Tier::Optional,
default: || text("heuristic"),
},
ConfigDefault {
key: "ai.ner.endpoint",
tier: Tier::Optional,
default: || text(""),
},
ConfigDefault {
key: "ai.ner.model",
tier: Tier::Optional,
default: || text(""),
},
ConfigDefault {
key: "ai.ner.timeout_ms",
tier: Tier::Optional,
default: || num(5000.0),
},
ConfigDefault {
key: "ai.ner.fallback",
tier: Tier::Optional,
default: || text("use_heuristic"),
},
ConfigDefault {
key: "runtime.ai.transport_pool_size",
tier: Tier::Optional,
default: || num(16.0),
},
ConfigDefault {
key: "runtime.ai.transport_timeout_ms",
tier: Tier::Optional,
default: || num(30000.0),
},
ConfigDefault {
key: "runtime.ai.transport_retry_max_attempts",
tier: Tier::Optional,
default: || num(3.0),
},
ConfigDefault {
key: "runtime.ai.transport_retry_base_ms",
tier: Tier::Optional,
default: || num(500.0),
},
ConfigDefault {
key: "cache.blob.policy.extended",
tier: Tier::Optional,
default: || text("off"),
},
ConfigDefault {
key: "cache.blob.async_promotion",
tier: Tier::Optional,
default: || text("off"),
},
];
pub fn default_for(key: &str) -> Option<JsonValue> {
MATRIX
.iter()
.find(|entry| entry.key == key)
.map(|entry| (entry.default)())
}
pub fn tier_for(key: &str) -> Option<Tier> {
MATRIX
.iter()
.find(|entry| entry.key == key)
.map(|entry| entry.tier)
}
pub fn heal_critical_keys(store: &UnifiedStore) {
for entry in MATRIX {
if entry.tier != Tier::Critical {
continue;
}
if is_key_present(store, entry.key) {
continue;
}
store.set_config_tree(entry.key, &(entry.default)());
}
}
fn is_key_present(store: &UnifiedStore, key: &str) -> bool {
let Some(manager) = store.get_collection("red_config") else {
return false;
};
let mut found = false;
manager.for_each_entity(|entity| {
if let Some(row) = entity.data.as_row() {
let entry_key = row.get_field("key").and_then(|v| match v {
crate::storage::schema::Value::Text(s) => Some(s.as_ref()),
_ => None,
});
if entry_key == Some(key) {
found = true;
return false; }
}
true
});
found
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_matrix_entry_has_a_default_that_resolves() {
for entry in MATRIX {
let value = (entry.default)();
assert!(
!matches!(value, JsonValue::Null),
"matrix key {} has a null default, defeats self-heal",
entry.key
);
}
}
#[test]
fn critical_keys_cover_the_core_guarantees() {
let required_critical = [
"cache.blob.l1_bytes_max",
"cache.blob.l2_bytes_max",
"cache.blob.max_namespaces",
"durability.mode",
"runtime.result_cache.backend",
"concurrency.locking.enabled",
"storage.wal.max_interval_ms",
"storage.bgwriter.delay_ms",
"storage.btree.lehman_yao",
];
for key in required_critical {
assert_eq!(
tier_for(key),
Some(Tier::Critical),
"{key} must be a Tier A (Critical) key",
);
}
}
#[test]
fn optional_keys_are_not_self_healed() {
let must_be_optional = [
"concurrency.locking.deadlock_timeout_ms",
"storage.wal.min_batch_size",
"storage.bgwriter.max_pages_per_round",
"storage.bgwriter.lru_multiplier",
"storage.bulk_insert.max_buffered_rows",
"storage.bulk_insert.max_buffered_bytes",
"storage.hot_update.max_chain_hops",
];
for key in must_be_optional {
assert_eq!(tier_for(key), Some(Tier::Optional), "{key} tier mismatch");
}
}
#[test]
fn unknown_key_returns_none() {
assert!(default_for("nonexistent.key").is_none());
assert!(tier_for("nonexistent.key").is_none());
}
}