mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
Revert "Revert "feat: add geometry to database args"" (#12165)
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
//! clap [Args](clap::Args) for database configuration
|
||||
|
||||
use std::time::Duration;
|
||||
use std::{fmt, str::FromStr, time::Duration};
|
||||
|
||||
use crate::version::default_client_version;
|
||||
use clap::{
|
||||
@ -22,6 +22,12 @@ pub struct DatabaseArgs {
|
||||
/// NFS volume.
|
||||
#[arg(long = "db.exclusive")]
|
||||
pub exclusive: Option<bool>,
|
||||
/// Maximum database size (e.g., 4TB, 8MB)
|
||||
#[arg(long = "db.max-size", value_parser = parse_byte_size)]
|
||||
pub max_size: Option<usize>,
|
||||
/// Database growth step (e.g., 4GB, 4KB)
|
||||
#[arg(long = "db.growth-step", value_parser = parse_byte_size)]
|
||||
pub growth_step: Option<usize>,
|
||||
/// Read transaction timeout in seconds, 0 means no timeout.
|
||||
#[arg(long = "db.read-transaction-timeout")]
|
||||
pub read_transaction_timeout: Option<u64>,
|
||||
@ -33,8 +39,9 @@ impl DatabaseArgs {
|
||||
self.get_database_args(default_client_version())
|
||||
}
|
||||
|
||||
/// Returns the database arguments with configured log level and given client version.
|
||||
pub const fn get_database_args(
|
||||
/// Returns the database arguments with configured log level, client version,
|
||||
/// max read transaction duration, and geometry.
|
||||
pub fn get_database_args(
|
||||
&self,
|
||||
client_version: ClientVersion,
|
||||
) -> reth_db::mdbx::DatabaseArguments {
|
||||
@ -48,6 +55,8 @@ impl DatabaseArgs {
|
||||
.with_log_level(self.log_level)
|
||||
.with_exclusive(self.exclusive)
|
||||
.with_max_read_transaction_duration(max_read_transaction_duration)
|
||||
.with_geometry_max_size(self.max_size)
|
||||
.with_growth_step(self.growth_step)
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,10 +98,84 @@ impl TypedValueParser for LogLevelValueParser {
|
||||
Some(Box::new(values))
|
||||
}
|
||||
}
|
||||
|
||||
/// Size in bytes.
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ByteSize(pub usize);
|
||||
|
||||
impl From<ByteSize> for usize {
|
||||
fn from(s: ByteSize) -> Self {
|
||||
s.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ByteSize {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let s = s.trim().to_uppercase();
|
||||
let parts: Vec<&str> = s.split_whitespace().collect();
|
||||
|
||||
let (num_str, unit) = match parts.len() {
|
||||
1 => {
|
||||
let (num, unit) =
|
||||
s.split_at(s.find(|c: char| c.is_alphabetic()).unwrap_or(s.len()));
|
||||
(num, unit)
|
||||
}
|
||||
2 => (parts[0], parts[1]),
|
||||
_ => {
|
||||
return Err("Invalid format. Use '<number><unit>' or '<number> <unit>'.".to_string())
|
||||
}
|
||||
};
|
||||
|
||||
let num: usize = num_str.parse().map_err(|_| "Invalid number".to_string())?;
|
||||
|
||||
let multiplier = match unit {
|
||||
"B" | "" => 1, // Assume bytes if no unit is specified
|
||||
"KB" => 1024,
|
||||
"MB" => 1024 * 1024,
|
||||
"GB" => 1024 * 1024 * 1024,
|
||||
"TB" => 1024 * 1024 * 1024 * 1024,
|
||||
_ => return Err(format!("Invalid unit: {}. Use B, KB, MB, GB, or TB.", unit)),
|
||||
};
|
||||
|
||||
Ok(Self(num * multiplier))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ByteSize {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
const KB: usize = 1024;
|
||||
const MB: usize = KB * 1024;
|
||||
const GB: usize = MB * 1024;
|
||||
const TB: usize = GB * 1024;
|
||||
|
||||
let (size, unit) = if self.0 >= TB {
|
||||
(self.0 as f64 / TB as f64, "TB")
|
||||
} else if self.0 >= GB {
|
||||
(self.0 as f64 / GB as f64, "GB")
|
||||
} else if self.0 >= MB {
|
||||
(self.0 as f64 / MB as f64, "MB")
|
||||
} else if self.0 >= KB {
|
||||
(self.0 as f64 / KB as f64, "KB")
|
||||
} else {
|
||||
(self.0 as f64, "B")
|
||||
};
|
||||
|
||||
write!(f, "{:.2}{}", size, unit)
|
||||
}
|
||||
}
|
||||
|
||||
/// Value parser function that supports various formats.
|
||||
fn parse_byte_size(s: &str) -> Result<usize, String> {
|
||||
s.parse::<ByteSize>().map(Into::into)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use clap::Parser;
|
||||
use reth_db::mdbx::{GIGABYTE, KILOBYTE, MEGABYTE, TERABYTE};
|
||||
|
||||
/// A helper type to parse Args more easily
|
||||
#[derive(Parser)]
|
||||
@ -108,6 +191,101 @@ mod tests {
|
||||
assert_eq!(args, default_args);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_command_parser_with_valid_max_size() {
|
||||
let cmd = CommandParser::<DatabaseArgs>::try_parse_from([
|
||||
"reth",
|
||||
"--db.max-size",
|
||||
"4398046511104",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(cmd.args.max_size, Some(TERABYTE * 4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_command_parser_with_invalid_max_size() {
|
||||
let result =
|
||||
CommandParser::<DatabaseArgs>::try_parse_from(["reth", "--db.max-size", "invalid"]);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_command_parser_with_valid_growth_step() {
|
||||
let cmd = CommandParser::<DatabaseArgs>::try_parse_from([
|
||||
"reth",
|
||||
"--db.growth-step",
|
||||
"4294967296",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(cmd.args.growth_step, Some(GIGABYTE * 4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_command_parser_with_invalid_growth_step() {
|
||||
let result =
|
||||
CommandParser::<DatabaseArgs>::try_parse_from(["reth", "--db.growth-step", "invalid"]);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_command_parser_with_valid_max_size_and_growth_step_from_str() {
|
||||
let cmd = CommandParser::<DatabaseArgs>::try_parse_from([
|
||||
"reth",
|
||||
"--db.max-size",
|
||||
"2TB",
|
||||
"--db.growth-step",
|
||||
"1GB",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(cmd.args.max_size, Some(TERABYTE * 2));
|
||||
assert_eq!(cmd.args.growth_step, Some(GIGABYTE));
|
||||
|
||||
let cmd = CommandParser::<DatabaseArgs>::try_parse_from([
|
||||
"reth",
|
||||
"--db.max-size",
|
||||
"12MB",
|
||||
"--db.growth-step",
|
||||
"2KB",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(cmd.args.max_size, Some(MEGABYTE * 12));
|
||||
assert_eq!(cmd.args.growth_step, Some(KILOBYTE * 2));
|
||||
|
||||
// with spaces
|
||||
let cmd = CommandParser::<DatabaseArgs>::try_parse_from([
|
||||
"reth",
|
||||
"--db.max-size",
|
||||
"12 MB",
|
||||
"--db.growth-step",
|
||||
"2 KB",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(cmd.args.max_size, Some(MEGABYTE * 12));
|
||||
assert_eq!(cmd.args.growth_step, Some(KILOBYTE * 2));
|
||||
|
||||
let cmd = CommandParser::<DatabaseArgs>::try_parse_from([
|
||||
"reth",
|
||||
"--db.max-size",
|
||||
"1073741824",
|
||||
"--db.growth-step",
|
||||
"1048576",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(cmd.args.max_size, Some(GIGABYTE));
|
||||
assert_eq!(cmd.args.growth_step, Some(MEGABYTE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_command_parser_max_size_and_growth_step_from_str_invalid_unit() {
|
||||
let result =
|
||||
CommandParser::<DatabaseArgs>::try_parse_from(["reth", "--db.growth-step", "1 PB"]);
|
||||
assert!(result.is_err());
|
||||
|
||||
let result =
|
||||
CommandParser::<DatabaseArgs>::try_parse_from(["reth", "--db.max-size", "2PB"]);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_possible_values() {
|
||||
// Initialize the LogLevelValueParser
|
||||
|
||||
Reference in New Issue
Block a user