Skip to content

Commit

Permalink
bin: clean up toydump command
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jul 21, 2024
1 parent 703bcb2 commit b184c35
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/bin/toydump.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
//! toydump is a debug tool that prints a toyDB BitCask database in
//! human-readable form. It only prints live BitCask data, not garbage entries.
//! human-readable form. It can print both the SQL database, and the Raft log
//! (via --raft). It only outputs live BitCask data, not garbage entries.
#![warn(clippy::all)]

use toydb::encoding::format::{self, Formatter as _};
use toydb::error::Result;
use toydb::storage::{BitCask, Engine as _};

fn main() -> Result<()> {
let args = clap::command!()
.about("Prints toyDB file contents in human-readable form.")
.args([
clap::Arg::new("raft")
.long("raft")
.num_args(0)
.help("file is a Raft log, not SQL database"),
clap::Arg::new("raw").long("raw").num_args(0).help("also show raw key/value"),
clap::Arg::new("file").required(true),
])
.get_matches();
let raft: bool = *args.get_one("raft").unwrap();
let raw: bool = *args.get_one("raw").unwrap();
let file: &String = args.get_one("file").unwrap();
use clap::Parser as _;

fn main() {
if let Err(error) = Command::parse().run() {
eprintln!("Error: {error}")
}
}

/// The toydump command.
#[derive(clap::Parser)]
#[command(about = "Prints toyDB file contents.", version, propagate_version = true)]
struct Command {
/// The BitCask file to dump (SQL database unless --raft).
file: String,
/// The file is a Raft log, not SQL database.
#[arg(long)]
raft: bool,
/// Also show raw key and value.
#[arg(long)]
raw: bool,
}

let mut engine = BitCask::new(file.into())?;
let mut scan = engine.scan(..);
while let Some((key, value)) = scan.next().transpose()? {
let mut string = match raft {
true => format::Raft::<format::SQLCommand>::key_value(&key, &value),
false => format::MVCC::<format::SQL>::key_value(&key, &value),
};
if raw {
string = format!("{string} [{}]", format::Raw::key_value(&key, &value))
impl Command {
/// Runs the command.
fn run(self) -> Result<()> {
let mut engine = BitCask::new(self.file.into())?;
let mut scan = engine.scan(..);
while let Some((key, value)) = scan.next().transpose()? {
let mut string = match self.raft {
true => format::Raft::<format::SQLCommand>::key_value(&key, &value),
false => format::MVCC::<format::SQL>::key_value(&key, &value),
};
if self.raw {
string = format!("{string} [{}]", format::Raw::key_value(&key, &value))
}
println!("{string}");
}
println!("{string}");
Ok(())
}
Ok(())
}

0 comments on commit b184c35

Please sign in to comment.