MCPcopy Create free account
hub / github.com/apache/datafusion / parse_capacity_limit

Function parse_capacity_limit

benchmarks/src/util/options.rs:154–174  ·  view source on GitHub ↗

Parse capacity limit from string to number of bytes by allowing units: K, M and G. Supports formats like '1.5G' -> 1610612736, '100M' -> 104857600

(limit: &str)

Source from the content-addressed store, hash-verified

152/// Parse capacity limit from string to number of bytes by allowing units: K, M and G.
153/// Supports formats like '1.5G' -> 1610612736, '100M' -> 104857600
154fn parse_capacity_limit(limit: &str) -> Result<usize, String> {
155 if limit.trim().is_empty() {
156 return Err("Capacity limit cannot be empty".to_string());
157 }
158 let (number, unit) = limit.split_at(limit.len() - 1);
159 let number: f64 = number
160 .parse()
161 .map_err(|_| format!("Failed to parse number from capacity limit '{limit}'"))?;
162 if number.is_sign_negative() || number.is_infinite() {
163 return Err("Limit value should be positive finite number".to_string());
164 }
165
166 match unit {
167 "K" => Ok((number * 1024.0) as usize),
168 "M" => Ok((number * 1024.0 * 1024.0) as usize),
169 "G" => Ok((number * 1024.0 * 1024.0 * 1024.0) as usize),
170 _ => Err(format!(
171 "Unsupported unit '{unit}' in capacity limit '{limit}'. Unit must be one of: 'K', 'M', 'G'"
172 )),
173 }
174}
175
176#[cfg(test)]
177mod tests {

Callers 3

set_runtime_variableMethod · 0.85
runtime_env_builderMethod · 0.85

Calls 6

trimMethod · 0.80
is_infiniteMethod · 0.80
is_emptyMethod · 0.45
to_stringMethod · 0.45
lenMethod · 0.45
parseMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…