| 1 |
// Copyright 2025 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::io::Write as _; |
| 16 |
use std::path::PathBuf; |
| 17 |
|
| 18 |
use itertools::Itertools as _; |
| 19 |
use jj_lib::stacked_table::TableSegment as _; |
| 20 |
use jj_lib::stacked_table::TableStore; |
| 21 |
|
| 22 |
use crate::cli_util::CommandHelper; |
| 23 |
use crate::command_error::CommandError; |
| 24 |
use crate::command_error::user_error_with_message; |
| 25 |
use crate::ui::Ui; |
| 26 |
|
| 27 |
/// Show stats of stacked table |
| 28 |
#[derive(clap::Args, Clone, Debug)] |
| 29 |
pub struct DebugStackedTableArgs { |
| 30 |
/// Path to table store directory |
| 31 |
#[arg(value_hint = clap::ValueHint::DirPath)] |
| 32 |
dir: String, |
| 33 |
|
| 34 |
/// Key size in bytes |
| 35 |
#[arg(long, short = 'n')] |
| 36 |
key_size: usize, |
| 37 |
} |
| 38 |
|
| 39 |
pub async fn cmd_debug_stacked_table( |
| 40 |
ui: &mut Ui, |
| 41 |
_command: &CommandHelper, |
| 42 |
args: &DebugStackedTableArgs, |
| 43 |
) -> Result<(), CommandError> { |
| 44 |
let store = TableStore::load(PathBuf::from(&args.dir), args.key_size); |
| 45 |
let table = store |
| 46 |
.get_head() |
| 47 |
.map_err(|err| user_error_with_message("Failed to load stacked table", err))?; |
| 48 |
let mut table_segments = table.ancestor_segments().collect_vec(); |
| 49 |
table_segments.reverse(); |
| 50 |
|
| 51 |
let total_num_entries: usize = table_segments |
| 52 |
.iter() |
| 53 |
.map(|table| table.segment_num_entries()) |
| 54 |
.sum(); |
| 55 |
writeln!(ui.stdout(), "Number of entries: {total_num_entries}")?; |
| 56 |
|
| 57 |
writeln!(ui.stdout(), "Stats per level:")?; |
| 58 |
for (i, table) in table_segments.iter().enumerate() { |
| 59 |
writeln!(ui.stdout(), " Level {i}:")?; |
| 60 |
writeln!( |
| 61 |
ui.stdout(), |
| 62 |
" Number of entries: {}", |
| 63 |
table.segment_num_entries() |
| 64 |
)?; |
| 65 |
writeln!(ui.stdout(), " Name: {}", table.name())?; |
| 66 |
} |
| 67 |
Ok(()) |
| 68 |
} |