use crate::{arg_of, child_block, first_word, has_leaf, named_children};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DatabaseResize {
pub database_size_diff: Option<String>,
pub database_size_on_disk: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EphemeralInstance {
pub name: String,
pub cyclic_version: Option<String>,
pub resize: Option<DatabaseResize>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EphemeralDatabase {
pub allow_commit_synchronize_with_gres: bool,
pub commit_synchronize_model: Option<String>,
pub cyclic_version_for_ephemeral_default: Option<String>,
pub delete_ephemeral_default: bool,
pub ignore_ephemeral_default: bool,
pub instances: Vec<EphemeralInstance>,
pub resize_ephemeral_default: Option<DatabaseResize>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VirtualMemoryProcess {
pub name: String,
pub fixed_size: Option<String>,
pub page_pooling_size: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConfigurationDatabase {
pub ephemeral: Option<EphemeralDatabase>,
pub extend_size: bool,
pub max_db_size: Option<String>,
pub optimize_db_size: bool,
pub resize: Option<DatabaseResize>,
pub virtual_memory_mapping: Vec<VirtualMemoryProcess>,
}
fn resize_values(resize: &ConfigLine) -> DatabaseResize {
DatabaseResize {
database_size_diff: arg_of(resize, "database-size-diff").map(str::to_string),
database_size_on_disk: arg_of(resize, "database-size-on-disk").map(str::to_string),
}
}
fn parse_resize(parent: &ConfigLine) -> Option<DatabaseResize> {
child_block(parent, "resize").map(resize_values)
}
fn parse_ephemeral(ephemeral: &ConfigLine) -> EphemeralDatabase {
EphemeralDatabase {
allow_commit_synchronize_with_gres: has_leaf(ephemeral, "allow-commit-synchronize-with-gres"),
commit_synchronize_model: arg_of(ephemeral, "commit-synchronize-model").map(str::to_string),
cyclic_version_for_ephemeral_default: arg_of(ephemeral, "cyclic-version-for-ephemeral-default")
.map(str::to_string),
delete_ephemeral_default: has_leaf(ephemeral, "delete-ephemeral-default"),
ignore_ephemeral_default: has_leaf(ephemeral, "ignore-ephemeral-default"),
instances: named_children(ephemeral, "instance")
.map(|(name, instance)| EphemeralInstance {
name: name.to_string(),
cyclic_version: arg_of(instance, "cyclic-version").map(str::to_string),
resize: parse_resize(instance),
})
.collect(),
resize_ephemeral_default: child_block(ephemeral, "resize-ephemeral-default").map(resize_values),
}
}
pub(super) fn parse_configuration_database(database: &ConfigLine) -> ConfigurationDatabase {
ConfigurationDatabase {
ephemeral: child_block(database, "ephemeral").map(parse_ephemeral),
extend_size: has_leaf(database, "extend-size"),
max_db_size: arg_of(database, "max-db-size").map(str::to_string),
optimize_db_size: has_leaf(database, "optimize-db-size"),
resize: parse_resize(database),
virtual_memory_mapping: child_block(database, "virtual-memory-mapping")
.map(|mapping| {
named_children(mapping, "process")
.map(|(name, process)| VirtualMemoryProcess {
name: name.to_string(),
fixed_size: arg_of(process, "fixed-size").and_then(first_word).map(str::to_string),
page_pooling_size: arg_of(process, "page-pooling-size")
.and_then(first_word)
.map(str::to_string),
})
.collect()
})
.unwrap_or_default(),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_configuration_database() {
let database = system(&juniper::parse_braces(
r#"
system {
configuration-database {
extend-size;
max-db-size 1024;
optimize-db-size;
resize {
database-size-diff 100;
database-size-on-disk 200;
}
virtual-memory-mapping {
process rpd {
fixed-size 512 KB;
page-pooling-size 64;
}
}
ephemeral {
allow-commit-synchronize-with-gres;
commit-synchronize-model asynchronous;
cyclic-version-for-ephemeral-default 4;
delete-ephemeral-default;
ignore-ephemeral-default;
instance high {
cyclic-version 8;
resize {
database-size-diff 50;
database-size-on-disk 80;
}
}
instance low;
resize-ephemeral-default {
database-size-diff 10;
}
}
}
}
"#,
))
.unwrap()
.configuration_database
.unwrap();
assert!(database.extend_size);
assert!(database.optimize_db_size);
assert_eq!(database.max_db_size.as_deref(), Some("1024"));
let resize = database.resize.unwrap();
assert_eq!(resize.database_size_diff.as_deref(), Some("100"));
assert_eq!(resize.database_size_on_disk.as_deref(), Some("200"));
assert_eq!(database.virtual_memory_mapping[0].name, "rpd");
assert_eq!(database.virtual_memory_mapping[0].fixed_size.as_deref(), Some("512"));
assert_eq!(
database.virtual_memory_mapping[0].page_pooling_size.as_deref(),
Some("64")
);
let ephemeral = database.ephemeral.unwrap();
assert!(ephemeral.allow_commit_synchronize_with_gres);
assert_eq!(ephemeral.commit_synchronize_model.as_deref(), Some("asynchronous"));
assert_eq!(ephemeral.cyclic_version_for_ephemeral_default.as_deref(), Some("4"));
assert!(ephemeral.delete_ephemeral_default);
assert!(ephemeral.ignore_ephemeral_default);
let names: Vec<_> = ephemeral.instances.iter().map(|i| i.name.as_str()).collect();
assert_eq!(names, ["high", "low"]);
assert_eq!(ephemeral.instances[0].cyclic_version.as_deref(), Some("8"));
assert_eq!(
ephemeral.instances[0]
.resize
.as_ref()
.unwrap()
.database_size_on_disk
.as_deref(),
Some("80")
);
assert!(ephemeral.instances[1].resize.is_none());
assert_eq!(
ephemeral
.resize_ephemeral_default
.unwrap()
.database_size_diff
.as_deref(),
Some("10")
);
}
#[test]
fn no_configuration_database_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.configuration_database.is_none());
}
}