-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
703bcb2
commit b184c35
Showing
1 changed file
with
39 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |