Skip to content

Commit

Permalink
parse_db_options: avoid string copy for integer options (#984)
Browse files Browse the repository at this point in the history
Small refactoring noticed while reviewing #983
  • Loading branch information
serprex authored Jan 4, 2024
1 parent 69202ec commit 708dd92
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
6 changes: 2 additions & 4 deletions nexus/analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,16 +673,14 @@ fn parse_db_options(
// partition_count default to 3 if not set, parse as int
let partition_count = opts
.get("partition_count")
.map(|s| s.to_string())
.unwrap_or_else(|| "3".to_string())
.unwrap_or(&"3")
.parse::<u32>()
.context("unable to parse partition_count as valid int")?;

// message_retention_in_days default to 7 if not set, parse as int
let message_retention_in_days = opts
.get("message_retention_in_days")
.map(|s| s.to_string())
.unwrap_or_else(|| "7".to_string())
.unwrap_or(&"7")
.parse::<u32>()
.context("unable to parse message_retention_in_days as valid int")?;

Expand Down
14 changes: 7 additions & 7 deletions nexus/analyzer/src/qrep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ pub fn process_options(
anyhow::bail!("{} must be one of {:?}", name, values);
}
}
opts.insert((*name).to_string(), Value::String(str.clone()));
opts.insert(name.to_string(), Value::String(str.clone()));
} else {
anyhow::bail!("Invalid value for {}", name);
}
} else if *required {
anyhow::bail!("{} is required", name);
} else if let Some(default) = default_val {
opts.insert((*name).to_string(), Value::String(default.to_string()));
opts.insert(name.to_string(), Value::String(default.to_string()));
}
}
QRepOptionType::Int {
Expand All @@ -139,15 +139,15 @@ pub fn process_options(
anyhow::bail!("{} must be greater than {}", name, min);
}
}
opts.insert((*name).to_string(), Value::Number(num.into()));
opts.insert(name.to_string(), Value::Number(num.into()));
} else {
anyhow::bail!("Invalid value for {}", name);
}
} else if *required {
anyhow::bail!("{} is required", name);
} else {
let v = *default_value;
opts.insert((*name).to_string(), Value::Number(v.into()));
opts.insert(name.to_string(), Value::Number(v.into()));
}
}
QRepOptionType::StringArray { name } => {
Expand All @@ -158,7 +158,7 @@ pub fn process_options(
.split(',')
.map(|s| Value::String(s.trim().to_string()))
.collect();
opts.insert((*name).to_string(), Value::Array(values));
opts.insert(name.to_string(), Value::Array(values));
} else {
anyhow::bail!("Invalid value for {}", name);
}
Expand All @@ -171,15 +171,15 @@ pub fn process_options(
} => {
if let Some(raw_value) = raw_opts.remove(*name) {
if let SqlValue::Boolean(b) = raw_value {
opts.insert((*name).to_string(), Value::Bool(*b));
opts.insert(name.to_string(), Value::Bool(*b));
} else {
anyhow::bail!("Invalid value for {}", name);
}
} else if *required {
anyhow::bail!("{} is required", name);
} else {
let v = *default_value;
opts.insert((*name).to_string(), Value::Bool(v));
opts.insert(name.to_string(), Value::Bool(v));
}
}
}
Expand Down

0 comments on commit 708dd92

Please sign in to comment.