From e033630ae342e433b88289556499a6bf94f4b90f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sat, 17 Sep 2022 21:08:13 +0300 Subject: [PATCH] cli parser --- src/main.rs | 102 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index b8ec2df..38495fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,9 @@ -/* -#[cfg(test)] -#![feature(test)] -extern crate test; -*/ - extern crate serde; #[macro_use] extern crate serde_derive; // This allows inserting JSON documents #[macro_use] extern crate serde_json; -#[macro_use] extern crate ledb; // This allows define typed documents easy #[macro_use] @@ -18,6 +11,7 @@ extern crate ledb_derive; extern crate ledb_types; extern crate zip; +use clap::{Parser, Subcommand}; use rand::seq::IteratorRandom; use std::time::Instant; @@ -25,6 +19,32 @@ use std::{fs, io}; use ledb::{Options, Storage}; +#[derive(Subcommand, Debug)] +enum Command { + Write, + Compact, + Print { + #[clap(value_parser, default_value = "0")] + id: u32, + }, + ZipPrint { + #[clap(value_parser, default_value = "0")] + file_num: usize, + #[clap(value_parser, default_value = "0")] + num: usize, + }, +} + +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +#[clap(propagate_version = true)] +struct Cli { + #[clap(subcommand)] + command: Command, + #[clap(short, long, action)] + measure: bool, +} + #[derive(Debug, Default, Clone, Deserialize)] struct SourceQuestion { #[serde(default)] @@ -289,12 +309,8 @@ fn writer_v4() { let storage = Storage::new("db", options).unwrap(); let collection = storage.collection("questions").unwrap(); + collection.purge().unwrap(); - query!(index for collection - num int, - batch_info.filename str, - ) - .unwrap(); let mut count: usize = 0; let count = &mut count; (0..archive.len()) @@ -335,23 +351,32 @@ fn writer_v4() { drop(storage); } -fn print_question(q: Question) { - println!("{:?}", q) +fn print_question_from(get_q: F) +where + F: FnOnce() -> Option, +{ + let q = get_q().unwrap(); + println!("{:#?}", q) } -fn reader_v1() -> Option { +fn reader_v1(mut file_num: usize, mut num: usize) -> Option { let zip_file = fs::File::open("test1.zip").unwrap(); let zip_reader = io::BufReader::new(zip_file); let mut archive = zip::ZipArchive::new(zip_reader).unwrap(); let mut rng = rand::thread_rng(); - let file_n = (0..archive.len()).choose(&mut rng).unwrap(); - let file = archive.by_index(file_n).unwrap(); + if file_num == 0 { + file_num = (1..=archive.len()).choose(&mut rng).unwrap(); + } + let file = archive.by_index(file_num - 1).unwrap(); let data: Result = serde_json::from_reader(file); let data = data.unwrap(); let questions: Vec = data.into(); - questions.into_iter().choose(&mut rng) + if num == 0 { + num = (1..=questions.len()).choose(&mut rng).unwrap(); + } + Some(questions[num - 1].clone()) } fn compact_db() { @@ -361,6 +386,7 @@ fn compact_db() { "no_lock": true, "no_meta_sync": true, "no_sync": true, + "compact": true, })) .unwrap(); @@ -372,7 +398,7 @@ fn compact_db() { drop(storage); } -fn reader_v4() -> Option { +fn reader_v4(mut id: u32) -> Option { let options: Options = serde_json::from_value(json!({ "read_only": true, // "map_async": true, @@ -386,23 +412,37 @@ fn reader_v4() -> Option { let storage = Storage::new("db", options).unwrap(); let collection = storage.collection("questions").unwrap(); let mut rng = rand::thread_rng(); - let last_id = collection.last_id().unwrap(); - let id = (1..(last_id + 1)).choose(&mut rng).unwrap(); - //let id = ledb::KeyData::Int(id as i64); + + if id == 0 { + let last_id = collection.last_id().unwrap(); + id = (1..=last_id).choose(&mut rng).unwrap(); + //let id = ledb::KeyData::Int(id as i64); + } collection.get::(id).unwrap() } fn main() { - //println!("-- v1: "); - //measure_and_print(|| print_question(reader_v1().unwrap())); - //println!(" --- "); - //println!("-- v4: "); - //measure_and_print(|| print_question(reader_v4().unwrap())); - //println!(" --- "); - measure_and_print(writer_v4); - //measure_and_print(compact_db); - //measure_and_print(|| print_question(reader_v4().unwrap())); + let args = Cli::parse(); + + let mut action: Box = match &args.command { + Command::Write => Box::new(writer_v4), + Command::Compact => Box::new(compact_db), + Command::Print { id } => { + let get_question = Box::new(|| reader_v4(*id)); + Box::new(|| print_question_from(get_question)) + } + Command::ZipPrint { file_num, num } => { + let get_question = Box::new(|| reader_v1(*file_num, *num)); + Box::new(|| print_question_from(get_question)) + } + }; + + if args.measure { + action = Box::new(|| measure_and_print(action)); + } + + action(); } /*