pub mod common; pub mod error; #[allow(dead_code)] pub mod heal; pub mod storageclass; use crate::error::Result; use crate::store::ECStore; use common::{lookup_configs, read_config_without_migrate, STORAGE_CLASS_SUB_SYS}; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::OnceLock; lazy_static! { pub static ref GLOBAL_StorageClass: OnceLock = OnceLock::new(); pub static ref DefaultKVS: OnceLock> = OnceLock::new(); pub static ref GLOBAL_ServerConfig: OnceLock = OnceLock::new(); pub static ref GLOBAL_ConfigSys: ConfigSys = ConfigSys::new(); } pub static RUSTFS_CONFIG_PREFIX: &str = "config"; pub struct ConfigSys {} impl Default for ConfigSys { fn default() -> Self { Self::new() } } impl ConfigSys { pub fn new() -> Self { Self {} } pub async fn init(&self, api: &ECStore) -> Result<()> { let mut cfg = read_config_without_migrate(api).await?; lookup_configs(&mut cfg, api).await; let _ = GLOBAL_ServerConfig.set(cfg); Ok(()) } } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct KV { pub key: String, pub value: String, pub hidden_if_empty: bool, } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct KVS(Vec); impl Default for KVS { fn default() -> Self { Self::new() } } impl KVS { pub fn new() -> Self { KVS(Vec::new()) } pub fn get(&self, key: &str) -> String { if let Some(v) = self.lookup(key) { v } else { "".to_owned() } } pub fn lookup(&self, key: &str) -> Option { for kv in self.0.iter() { if kv.key.as_str() == key { return Some(kv.value.clone()); } } None } } #[derive(Debug, Clone)] pub struct Config(HashMap>); impl Default for Config { fn default() -> Self { Self::new() } } impl Config { pub fn new() -> Self { let mut cfg = Config(HashMap::new()); cfg.set_defaults(); cfg } pub fn get_value(&self, subsys: &str, key: &str) -> Option { if let Some(m) = self.0.get(subsys) { m.get(key).cloned() } else { None } } pub fn set_defaults(&mut self) { if let Some(defaults) = DefaultKVS.get() { for (k, v) in defaults.iter() { if !self.0.contains_key(k) { let mut default = HashMap::new(); default.insert("_".to_owned(), v.clone()); self.0.insert(k.clone(), default); } else if !self.0[k].contains_key("_") { if let Some(m) = self.0.get_mut(k) { m.insert("_".to_owned(), v.clone()); } } } } } pub fn unmarshal(data: &[u8]) -> Result { let m: HashMap> = serde_json::from_slice(data)?; let mut cfg = Config(m); cfg.set_defaults(); Ok(cfg) } pub fn marshal(&self) -> Result> { let data = serde_json::to_vec(&self.0)?; Ok(data) } pub fn merge(&self) -> Config { // TODO: merge defauls self.clone() } } pub fn register_default_kvs(kvs: HashMap) { let mut p = HashMap::new(); for (k, v) in kvs { p.insert(k, v); } let _ = DefaultKVS.set(p); } pub fn init() { let mut kvs = HashMap::new(); kvs.insert(STORAGE_CLASS_SUB_SYS.to_owned(), storageclass::DefaultKVS.clone()); // TODO: other defauls register_default_kvs(kvs) }