Skip to content

Commit

Permalink
hash-cache-tool: Refactors width calculation (#2530)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Aug 9, 2024
1 parent 8fa9571 commit 0fda304
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions accounts-db/accounts-hash-cache-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn do_inspect(file: impl AsRef<Path>, force: bool) -> Result<(), String> {
file.as_ref().display(),
)
})?;
let count_width = (header.count as f64).log10().ceil() as usize;
let count_width = width10(header.count as u64);

let mut count = Saturating(0);
scan_file(reader, header.count, |entry| {
Expand Down Expand Up @@ -235,13 +235,13 @@ fn do_diff_files(file1: impl AsRef<Path>, file2: impl AsRef<Path>) -> Result<(),
let num_accounts1 = entries1.len();
let num_accounts2 = entries2.len();
let num_accounts_width = {
let width1 = (num_accounts1 as f64).log10().ceil() as usize;
let width2 = (num_accounts2 as f64).log10().ceil() as usize;
let width1 = width10(num_accounts1 as u64);
let width2 = width10(num_accounts2 as u64);
cmp::max(width1, width2)
};
let lamports_width = {
let width1 = (capitalization1 as f64).log10().ceil() as usize;
let width2 = (capitalization2 as f64).log10().ceil() as usize;
let width1 = width10(capitalization1);
let width2 = width10(capitalization2);
cmp::max(width1, width2)
};
println!("File 1: number of accounts: {num_accounts1:num_accounts_width$}, capitalization: {capitalization1:lamports_width$} lamports");
Expand Down Expand Up @@ -284,7 +284,7 @@ fn do_diff_files(file1: impl AsRef<Path>, file2: impl AsRef<Path>) -> Result<(),

// display the unique entries in each file
let do_print = |entries: &[CacheHashDataFileEntry]| {
let count_width = (entries.len() as f64).log10().ceil() as usize;
let count_width = width10(entries.len() as u64);
if entries.is_empty() {
println!("(none)");
} else {
Expand All @@ -307,7 +307,7 @@ fn do_diff_files(file1: impl AsRef<Path>, file2: impl AsRef<Path>) -> Result<(),
do_print(&unique_entries2);

println!("Mismatch values:");
let count_width = (mismatch_entries.len() as f64).log10().ceil() as usize;
let count_width = width10(mismatch_entries.len() as u64);
if mismatch_entries.is_empty() {
println!("(none)");
} else {
Expand Down Expand Up @@ -435,7 +435,7 @@ fn do_diff_dirs(
}

let do_print = |entries: &[&CacheFileInfo]| {
let count_width = (entries.len() as f64).log10().ceil() as usize;
let count_width = width10(entries.len() as u64);
if entries.is_empty() {
println!("(none)");
} else {
Expand All @@ -450,7 +450,7 @@ fn do_diff_dirs(
do_print(&uniques2);

println!("Mismatch files:");
let count_width = (mismatches.len() as f64).log10().ceil() as usize;
let count_width = width10(mismatches.len() as u64);
if mismatches.is_empty() {
println!("(none)");
} else {
Expand Down Expand Up @@ -558,13 +558,13 @@ fn do_diff_state(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) -> Result<(), S
drop(timer);

let num_accounts_width = {
let width1 = (num_accounts1 as f64).log10().ceil() as usize;
let width2 = (num_accounts2 as f64).log10().ceil() as usize;
let width1 = width10(num_accounts1 as u64);
let width2 = width10(num_accounts2 as u64);
cmp::max(width1, width2)
};
let lamports_width = {
let width1 = (capitalization1 as f64).log10().ceil() as usize;
let width2 = (capitalization2 as f64).log10().ceil() as usize;
let width1 = width10(capitalization1);
let width2 = width10(capitalization2);
cmp::max(width1, width2)
};

Expand All @@ -575,7 +575,7 @@ fn do_diff_state(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) -> Result<(), S
if unique_entries1.is_empty() {
println!("(none)");
} else {
let count_width = (unique_entries1.len() as f64).log10().ceil() as usize;
let count_width = width10(unique_entries1.len() as u64);
let mut total_lamports = Saturating(0);
for (i, entry) in unique_entries1.iter().enumerate() {
total_lamports += entry.1 .1;
Expand All @@ -593,7 +593,7 @@ fn do_diff_state(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) -> Result<(), S
if unique_entries1.is_empty() {
println!("(none)");
} else {
let count_width = (unique_entries2.len() as f64).log10().ceil() as usize;
let count_width = width10(unique_entries2.len() as u64);
let mut total_lamports = Saturating(0);
for (i, entry) in unique_entries2.iter().enumerate() {
total_lamports += entry.1 .1;
Expand All @@ -608,7 +608,7 @@ fn do_diff_state(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) -> Result<(), S
}

println!("Mismatch values:");
let count_width = (mismatch_entries.len() as f64).log10().ceil() as usize;
let count_width = width10(mismatch_entries.len() as u64);
if mismatch_entries.is_empty() {
println!("(none)");
} else {
Expand Down Expand Up @@ -837,6 +837,11 @@ fn open_file(
Ok((reader, header))
}

/// Returns the number of characters required to print `x` in base-10
fn width10(x: u64) -> usize {
(x as f64).log10().ceil() as usize
}

#[derive(Debug)]
struct CacheFileInfo {
path: PathBuf,
Expand Down

0 comments on commit 0fda304

Please sign in to comment.