use std::collections::BTreeMap;
use std::sync::Arc;
use relate_core::{Error, Result, Value};
use crate::eval::eval;
use crate::overlay::ExecGraph;
use crate::row::Row;
use crate::exec::catalog::{catalog_row, project_catalog};
#[derive(Clone, Copy)]
struct Setting<'a> {
name: &'a str,
value: &'a str,
is_dynamic: bool,
default_value: &'a str,
startup_value: &'a str,
valid_values: &'a str,
is_explicitly_set: bool,
is_deprecated: bool,
description: &'a str,
}
const SETTINGS: &[Setting<'static>] = &[
Setting {
name: "browser.allow_outgoing_connections",
value: "true",
is_dynamic: false,
default_value: "true",
startup_value: "true",
valid_values: "A boolean.",
is_explicitly_set: false,
is_deprecated: false,
description: "Configure the policy for outgoing Neo4j Browser connections.",
},
Setting {
name: "db.checkpoint",
value: "PERIODIC",
is_dynamic: false,
default_value: "PERIODIC",
startup_value: "PERIODIC",
valid_values: "One of [PERIODIC, CONTINUOUS, VOLUME, VOLUMETRIC].",
is_explicitly_set: false,
is_deprecated: false,
description: "Configures the general policy for when checkpoints should occur.",
},
Setting {
name: "db.checkpoint.interval.time",
value: "15m",
is_dynamic: false,
default_value: "15m",
startup_value: "15m",
valid_values: "A duration (Valid units are: `ns`, `μs`, `ms`, `s`, `m`, `h` and `d`; default unit is `s`).",
is_explicitly_set: false,
is_deprecated: false,
description: "Configures the time interval between checkpoints.",
},
Setting {
name: "db.checkpoint.interval.tx",
value: "100000",
is_dynamic: false,
default_value: "100000",
startup_value: "100000",
valid_values: "An integer that is minimum `1`.",
is_explicitly_set: false,
is_deprecated: false,
description: "Configures the transaction interval between checkpoints.",
},
Setting {
name: "db.checkpoint.interval.volume",
value: "250.00MiB",
is_dynamic: false,
default_value: "250.00MiB",
startup_value: "250.00MiB",
valid_values: "A byte size that is minimum `1.00KiB`.",
is_explicitly_set: false,
is_deprecated: false,
description: "Configures the volume of transaction logs between checkpoints.",
},
Setting {
name: "db.format",
value: "block",
is_dynamic: true,
default_value: "block",
startup_value: "block",
valid_values: "A string.",
is_explicitly_set: false,
is_deprecated: false,
description: "Database format. This is the format that will be used for new databases.",
},
Setting {
name: "db.query.default_language",
value: "CYPHER_5",
is_dynamic: false,
default_value: "CYPHER_5",
startup_value: "CYPHER_5",
valid_values: "One of [CYPHER_5, CYPHER_25].",
is_explicitly_set: false,
is_deprecated: false,
description: "The default language of a database determines which language is used to evaluate queries that do not explicitly select a language. This setting determines the default language used for new (and initial) databases where not specified as part of CREATE or ALTER database.",
},
Setting {
name: "dbms.cluster.network.supported_compression_algos",
value: "",
is_dynamic: false,
default_value: "",
startup_value: "",
valid_values: "A comma-separated list where each element is a string.",
is_explicitly_set: false,
is_deprecated: false,
description: "Network compression algorithms that this instance allows in negotiation as a comma-separated list.",
},
Setting {
name: "dbms.routing.load_balancing.plugin",
value: "server_policies",
is_dynamic: false,
default_value: "server_policies",
startup_value: "server_policies",
valid_values: "A string.",
is_explicitly_set: false,
is_deprecated: true,
description: "The load balancing plugin to use.",
},
Setting {
name: "dbms.routing_ttl",
value: "5m",
is_dynamic: false,
default_value: "5m",
startup_value: "5m",
valid_values: "A duration that is minimum `1s`.",
is_explicitly_set: false,
is_deprecated: false,
description: "How long callers should cache the response of the routing procedure `dbms.routing.getRoutingTable()`.",
},
Setting {
name: "initial.server.mode_constraint",
value: "NONE",
is_dynamic: false,
default_value: "NONE",
startup_value: "NONE",
valid_values: "One of [PRIMARY, SECONDARY, NONE].",
is_explicitly_set: false,
is_deprecated: false,
description: "Determines whether the server is configured to host primary databases only, secondary databases only, or both.",
},
Setting {
name: "server.bolt.advertised_address",
value: "localhost:7687",
is_dynamic: false,
default_value: ":7687",
startup_value: ":7687",
valid_values: "A socket address in the format of `hostname:port`, `hostname`, or `:port`.",
is_explicitly_set: false,
is_deprecated: false,
description: "Advertised address for this connector.",
},
Setting {
name: "server.bolt.connection_keep_alive_for_requests",
value: "ALL",
is_dynamic: false,
default_value: "ALL",
startup_value: "ALL",
valid_values: "One of [ALL, STREAMING, OFF].",
is_explicitly_set: false,
is_deprecated: false,
description: "The type of messages to enable keep-alive messages for `ALL`, `STREAMING`, or `OFF`.",
},
Setting {
name: "server.bolt.enabled",
value: "true",
is_dynamic: false,
default_value: "true",
startup_value: "true",
valid_values: "A boolean.",
is_explicitly_set: true,
is_deprecated: false,
description: "Enable the Bolt connector.",
},
Setting {
name: "server.db.query_cache_size",
value: "1000",
is_dynamic: false,
default_value: "1000",
startup_value: "1000",
valid_values: "An integer that is minimum `0`.",
is_explicitly_set: false,
is_deprecated: true,
description: "The number of cached queries per database.",
},
];
fn setting_row(setting: &Setting<'_>) -> Row {
catalog_row([
("name", Value::String(setting.name.into())),
("value", Value::String(setting.value.into())),
("isDynamic", Value::Bool(setting.is_dynamic)),
("defaultValue", Value::String(setting.default_value.into())),
("description", Value::String(setting.description.into())),
("startupValue", Value::String(setting.startup_value.into())),
("isExplicitlySet", Value::Bool(setting.is_explicitly_set)),
("validValues", Value::String(setting.valid_values.into())),
("isDeprecated", Value::Bool(setting.is_deprecated)),
])
}
enum SettingFilter {
All,
Names(Vec<Arc<str>>),
}
impl SettingFilter {
fn admits(&self, name: &str) -> bool {
match self {
SettingFilter::All => true,
SettingFilter::Names(names) => names.iter().any(|wanted| &**wanted == name),
}
}
}
fn setting_filter(value: Value) -> Result<SettingFilter> {
match value {
Value::Null => Ok(SettingFilter::Names(Vec::new())),
Value::String(name) => Ok(SettingFilter::Names(vec![name])),
Value::List(values) if values.is_empty() => Ok(SettingFilter::All),
Value::List(values) => {
let mut names = Vec::with_capacity(values.len());
for value in values.iter() {
match value {
Value::String(name) => names.push(name.clone()),
Value::Null => {}
_ => return Err(Error::type_error("Expected a string")),
}
}
Ok(SettingFilter::Names(names))
}
_ => Err(Error::type_error("Expected a string or a list of strings")),
}
}
pub(crate) fn show_settings_rows(
graph: &ExecGraph<'_>,
c: &relate_parser::ShowSettingsClause,
rows: Vec<Row>,
clause: &relate_parser::Clause,
params: &BTreeMap<String, Value>,
) -> Result<Vec<Row>> {
let preview_text = preview_setting_text(&graph.preview_sources());
let mut catalog: Vec<Setting<'_>> = SETTINGS
.iter()
.copied()
.chain(preview_text.iter().map(PreviewSettingText::setting))
.collect();
catalog.sort_by(|a, b| a.name.cmp(b.name));
let Some(names_expr) = &c.names else {
return project_catalog(
graph,
rows,
catalog.iter().map(setting_row).collect(),
clause,
params,
);
};
let lowered = relate_front::lower_expr(names_expr)?;
crate::exec::catalog::project_catalog_by_row(graph, rows, clause, params, |row| {
let value = eval(graph, row, &lowered, params)?;
let filter = setting_filter(value)?;
Ok(catalog
.iter()
.filter(|setting| filter.admits(setting.name))
.map(setting_row)
.collect())
})
}
fn preview_setting_text(sources: &relate_core::PreviewSources) -> Vec<PreviewSettingText> {
let union = sources.union();
relate_core::PREVIEW_FEATURES
.iter()
.filter(|feature| union.enabled(**feature))
.map(|feature| {
let enabled_by = sources
.carriers(*feature)
.map(provenance_label)
.collect::<Vec<_>>()
.join(" + ");
PreviewSettingText {
name: format!("relate.preview.{}", feature.kebab_name()),
description: format!(
"The preview feature opt-in resolved at open; enabled by {enabled_by}."
),
}
})
.collect()
}
struct PreviewSettingText {
name: String,
description: String,
}
impl PreviewSettingText {
fn setting(&self) -> Setting<'_> {
Setting {
name: &self.name,
value: "true",
is_dynamic: false,
default_value: "false",
startup_value: "false",
valid_values: "A boolean.",
is_explicitly_set: true,
is_deprecated: false,
description: &self.description,
}
}
}
fn provenance_label(source: relate_core::PreviewSource) -> String {
match source {
relate_core::PreviewSource::Env => relate_core::PREVIEW_ENV_VAR.to_string(),
relate_core::PreviewSource::Config => {
format!("the store's {}", relate_core::STORE_CONFIG_FILE)
}
relate_core::PreviewSource::Option => "the typed opt-in".to_string(),
}
}
#[cfg(test)]
mod settings_tests {
use super::*;
use crate::graph::Graph;
fn show(query: &str) -> Vec<Row> {
crate::exec::test_support::show_catalog_rows(&Graph::default(), query)
}
#[test]
fn yield_where_rejects_a_non_boolean_predicate() {
let err = crate::exec::test_support::try_show_catalog_rows(
&Graph::default(),
"SHOW SETTINGS YIELD name WHERE name",
)
.expect_err("a string predicate is not a boolean");
assert!(
matches!(err, relate_core::Error::InvalidArgumentType(_)),
"typed rejection, got: {err}"
);
}
#[test]
fn settings_catalog_carries_neo4j_metadata() {
let rows = show("SHOW SETTINGS YIELD name, value, isDynamic, defaultValue, startupValue, description ORDER BY name LIMIT 1");
assert_eq!(rows.len(), 1);
assert_eq!(
rows[0].get("name"),
Some(&Value::String("browser.allow_outgoing_connections".into()))
);
assert_eq!(rows[0].get("value"), Some(&Value::String("true".into())));
assert_eq!(rows[0].get("isDynamic"), Some(&Value::Bool(false)));
assert_eq!(
rows[0].get("defaultValue"),
Some(&Value::String("true".into()))
);
assert_eq!(
rows[0].get("startupValue"),
Some(&Value::String("true".into()))
);
let dynamic = show("SHOW SETTINGS YIELD name, isDynamic WHERE isDynamic RETURN name");
assert_eq!(
dynamic
.iter()
.map(|row| row.get("name").cloned())
.collect::<Vec<_>>(),
vec![Some(Value::String("db.format".into()))]
);
let deprecated =
show("SHOW SETTINGS YIELD name, isDeprecated WHERE name = 'server.db.query_cache_size' RETURN isDeprecated");
assert_eq!(deprecated[0].get("isDeprecated"), Some(&Value::Bool(true)));
}
#[test]
fn setting_name_expressions_reject_wrong_types() {
let graph = Graph::default();
for query in ["SHOW SETTINGS 123", "SHOW SETTINGS ['db.format', 123]"] {
let err = crate::exec::test_support::error_of(
crate::exec::test_support::try_exec_cypher(&graph, query),
);
assert!(err.contains("type error"), "{query}: {err}");
}
assert!(crate::exec::test_support::show_catalog_rows(
&graph,
"SHOW SETTINGS [null] YIELD name"
)
.is_empty());
}
#[test]
fn show_settings_row_key_order_matches_front_column_table() {
crate::exec::test_support::assert_row_keys_match(
None,
"SHOW SETTINGS YIELD *",
&relate_front::show_columns::full_setting_columns(),
);
}
#[test]
fn composed_show_settings_pages_the_flattened_result_once() {
use crate::exec::test_support::{column, exec_cypher, string_cell};
let graph = Graph::default();
let result = exec_cypher(
&graph,
"UNWIND [['db.format', 'dbms.connector.bolt.listen_address'], \
['browser.allow_outgoing_connections']] AS names \
SHOW SETTINGS names YIELD name ORDER BY name LIMIT 1 RETURN name",
);
assert_eq!(
column(&result, "name", string_cell),
vec!["browser.allow_outgoing_connections".to_string()]
);
}
fn ontology_by_env() -> Graph {
let mut enabled = Graph::default();
enabled.configure_preview(relate_core::PreviewSources {
env: relate_core::PreviewFeatures::none()
.with_feature(relate_core::PreviewFeature::Ontology),
..relate_core::PreviewSources::default()
});
enabled
}
#[test]
fn no_preview_row_while_off() {
assert!(
show("SHOW SETTINGS YIELD name WHERE name STARTS WITH 'relate.preview.'").is_empty()
);
}
#[test]
fn an_enabled_feature_has_one_row_naming_its_source() {
let enabled = ontology_by_env();
let rows = crate::exec::test_support::show_catalog_rows(
&enabled,
"SHOW SETTINGS YIELD name, value, isDynamic, isExplicitlySet, description \
WHERE name STARTS WITH 'relate.preview.'",
);
assert_eq!(rows.len(), 1, "exactly the enabled feature");
assert_eq!(
rows[0].get("name"),
Some(&Value::String("relate.preview.ontology".into()))
);
assert_eq!(rows[0].get("value"), Some(&Value::String("true".into())));
assert_eq!(rows[0].get("isDynamic"), Some(&Value::Bool(false)));
assert_eq!(rows[0].get("isExplicitlySet"), Some(&Value::Bool(true)));
let description = rows[0]
.get("description")
.and_then(Value::as_string)
.expect("the description column");
assert_eq!(
description,
"The preview feature opt-in resolved at open; \
enabled by RELATE_PREVIEW."
);
}
#[test]
fn a_stacked_feature_names_every_carrier_in_order() {
let mut stacked = Graph::default();
stacked.configure_preview(relate_core::PreviewSources {
env: relate_core::PreviewFeatures::parse("ontology").unwrap(),
config: relate_core::PreviewFeatures::parse("path-construction").unwrap(),
option: relate_core::PreviewFeatures::parse("ontology").unwrap(),
});
let rows = crate::exec::test_support::show_catalog_rows(
&stacked,
"SHOW SETTINGS YIELD name, description \
WHERE name STARTS WITH 'relate.preview.' RETURN name, description",
);
let by_name: std::collections::BTreeMap<String, String> = rows
.iter()
.map(|row| {
(
row.get("name")
.and_then(Value::as_string)
.map(str::to_string)
.expect("name"),
row.get("description")
.and_then(Value::as_string)
.map(str::to_string)
.expect("description"),
)
})
.collect();
assert_eq!(
by_name
.get("relate.preview.path-construction")
.map(String::as_str),
Some(
"The preview feature opt-in resolved at open; \
enabled by the store's relate.yaml."
),
"the single-source feature names its one source"
);
assert_eq!(
by_name.get("relate.preview.ontology").map(String::as_str),
Some(
"The preview feature opt-in resolved at open; \
enabled by RELATE_PREVIEW + the typed opt-in."
),
"the stacked feature names both its sources, in order"
);
}
#[test]
fn the_combined_catalog_stays_name_sorted() {
let enabled = ontology_by_env();
let names: Vec<String> =
crate::exec::test_support::show_catalog_rows(&enabled, "SHOW SETTINGS YIELD name")
.iter()
.map(|row| {
row.get("name")
.and_then(Value::as_string)
.map(str::to_string)
})
.collect::<Option<_>>()
.expect("every row carries a name");
assert!(names.contains(&"relate.preview.ontology".to_string()));
assert!(
names.windows(2).all(|pair| pair[0] <= pair[1]),
"the catalog stays name-sorted: {names:?}"
);
}
#[test]
fn the_names_form_selects_preview_rows() {
let enabled = ontology_by_env();
let rows = crate::exec::test_support::show_catalog_rows(
&enabled,
"SHOW SETTINGS 'relate.preview.ontology' YIELD name, value",
);
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get("value"), Some(&Value::String("true".into())));
assert!(show("SHOW SETTINGS 'relate.preview.ontology' YIELD name").is_empty());
}
}