cli parser
This commit is contained in:
		
							
								
								
									
										102
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										102
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,16 +1,9 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
#[cfg(test)]
 | 
					 | 
				
			||||||
#![feature(test)]
 | 
					 | 
				
			||||||
extern crate test;
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
extern crate serde;
 | 
					extern crate serde;
 | 
				
			||||||
#[macro_use]
 | 
					#[macro_use]
 | 
				
			||||||
extern crate serde_derive;
 | 
					extern crate serde_derive;
 | 
				
			||||||
// This allows inserting JSON documents
 | 
					// This allows inserting JSON documents
 | 
				
			||||||
#[macro_use]
 | 
					#[macro_use]
 | 
				
			||||||
extern crate serde_json;
 | 
					extern crate serde_json;
 | 
				
			||||||
#[macro_use]
 | 
					 | 
				
			||||||
extern crate ledb;
 | 
					extern crate ledb;
 | 
				
			||||||
// This allows define typed documents easy
 | 
					// This allows define typed documents easy
 | 
				
			||||||
#[macro_use]
 | 
					#[macro_use]
 | 
				
			||||||
@@ -18,6 +11,7 @@ extern crate ledb_derive;
 | 
				
			|||||||
extern crate ledb_types;
 | 
					extern crate ledb_types;
 | 
				
			||||||
extern crate zip;
 | 
					extern crate zip;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use clap::{Parser, Subcommand};
 | 
				
			||||||
use rand::seq::IteratorRandom;
 | 
					use rand::seq::IteratorRandom;
 | 
				
			||||||
use std::time::Instant;
 | 
					use std::time::Instant;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -25,6 +19,32 @@ use std::{fs, io};
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use ledb::{Options, Storage};
 | 
					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)]
 | 
					#[derive(Debug, Default, Clone, Deserialize)]
 | 
				
			||||||
struct SourceQuestion {
 | 
					struct SourceQuestion {
 | 
				
			||||||
    #[serde(default)]
 | 
					    #[serde(default)]
 | 
				
			||||||
@@ -289,12 +309,8 @@ fn writer_v4() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let storage = Storage::new("db", options).unwrap();
 | 
					    let storage = Storage::new("db", options).unwrap();
 | 
				
			||||||
    let collection = storage.collection("questions").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 mut count: usize = 0;
 | 
				
			||||||
    let count = &mut count;
 | 
					    let count = &mut count;
 | 
				
			||||||
    (0..archive.len())
 | 
					    (0..archive.len())
 | 
				
			||||||
@@ -335,23 +351,32 @@ fn writer_v4() {
 | 
				
			|||||||
    drop(storage);
 | 
					    drop(storage);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn print_question(q: Question) {
 | 
					fn print_question_from<F>(get_q: F)
 | 
				
			||||||
    println!("{:?}", q)
 | 
					where
 | 
				
			||||||
 | 
					    F: FnOnce() -> Option<Question>,
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    let q = get_q().unwrap();
 | 
				
			||||||
 | 
					    println!("{:#?}", q)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn reader_v1() -> Option<Question> {
 | 
					fn reader_v1(mut file_num: usize, mut num: usize) -> Option<Question> {
 | 
				
			||||||
    let zip_file = fs::File::open("test1.zip").unwrap();
 | 
					    let zip_file = fs::File::open("test1.zip").unwrap();
 | 
				
			||||||
    let zip_reader = io::BufReader::new(zip_file);
 | 
					    let zip_reader = io::BufReader::new(zip_file);
 | 
				
			||||||
    let mut archive = zip::ZipArchive::new(zip_reader).unwrap();
 | 
					    let mut archive = zip::ZipArchive::new(zip_reader).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut rng = rand::thread_rng();
 | 
					    let mut rng = rand::thread_rng();
 | 
				
			||||||
    let file_n = (0..archive.len()).choose(&mut rng).unwrap();
 | 
					    if file_num == 0 {
 | 
				
			||||||
    let file = archive.by_index(file_n).unwrap();
 | 
					        file_num = (1..=archive.len()).choose(&mut rng).unwrap();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    let file = archive.by_index(file_num - 1).unwrap();
 | 
				
			||||||
    let data: Result<SourceQuestionsBatch, _> = serde_json::from_reader(file);
 | 
					    let data: Result<SourceQuestionsBatch, _> = serde_json::from_reader(file);
 | 
				
			||||||
    let data = data.unwrap();
 | 
					    let data = data.unwrap();
 | 
				
			||||||
    let questions: Vec<Question> = data.into();
 | 
					    let questions: Vec<Question> = 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() {
 | 
					fn compact_db() {
 | 
				
			||||||
@@ -361,6 +386,7 @@ fn compact_db() {
 | 
				
			|||||||
        "no_lock": true,
 | 
					        "no_lock": true,
 | 
				
			||||||
        "no_meta_sync": true,
 | 
					        "no_meta_sync": true,
 | 
				
			||||||
        "no_sync": true,
 | 
					        "no_sync": true,
 | 
				
			||||||
 | 
					        "compact": true,
 | 
				
			||||||
    }))
 | 
					    }))
 | 
				
			||||||
    .unwrap();
 | 
					    .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -372,7 +398,7 @@ fn compact_db() {
 | 
				
			|||||||
    drop(storage);
 | 
					    drop(storage);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn reader_v4() -> Option<Question> {
 | 
					fn reader_v4(mut id: u32) -> Option<Question> {
 | 
				
			||||||
    let options: Options = serde_json::from_value(json!({
 | 
					    let options: Options = serde_json::from_value(json!({
 | 
				
			||||||
            "read_only": true,
 | 
					            "read_only": true,
 | 
				
			||||||
    //        "map_async": true,
 | 
					    //        "map_async": true,
 | 
				
			||||||
@@ -386,23 +412,37 @@ fn reader_v4() -> Option<Question> {
 | 
				
			|||||||
    let storage = Storage::new("db", options).unwrap();
 | 
					    let storage = Storage::new("db", options).unwrap();
 | 
				
			||||||
    let collection = storage.collection("questions").unwrap();
 | 
					    let collection = storage.collection("questions").unwrap();
 | 
				
			||||||
    let mut rng = rand::thread_rng();
 | 
					    let mut rng = rand::thread_rng();
 | 
				
			||||||
    let last_id = collection.last_id().unwrap();
 | 
					
 | 
				
			||||||
    let id = (1..(last_id + 1)).choose(&mut rng).unwrap();
 | 
					    if id == 0 {
 | 
				
			||||||
    //let id = ledb::KeyData::Int(id as i64);
 | 
					        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::<Question>(id).unwrap()
 | 
					    collection.get::<Question>(id).unwrap()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
    //println!("-- v1: ");
 | 
					    let args = Cli::parse();
 | 
				
			||||||
    //measure_and_print(|| print_question(reader_v1().unwrap()));
 | 
					
 | 
				
			||||||
    //println!(" --- ");
 | 
					    let mut action: Box<dyn FnOnce()> = match &args.command {
 | 
				
			||||||
    //println!("-- v4: ");
 | 
					        Command::Write => Box::new(writer_v4),
 | 
				
			||||||
    //measure_and_print(|| print_question(reader_v4().unwrap()));
 | 
					        Command::Compact => Box::new(compact_db),
 | 
				
			||||||
    //println!(" --- ");
 | 
					        Command::Print { id } => {
 | 
				
			||||||
    measure_and_print(writer_v4);
 | 
					            let get_question = Box::new(|| reader_v4(*id));
 | 
				
			||||||
    //measure_and_print(compact_db);
 | 
					            Box::new(|| print_question_from(get_question))
 | 
				
			||||||
    //measure_and_print(|| print_question(reader_v4().unwrap()));
 | 
					        }
 | 
				
			||||||
 | 
					        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();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user