2022-09-17 15:45:41 +00:00
|
|
|
extern crate serde_json;
|
2022-09-17 18:08:13 +00:00
|
|
|
use clap::{Parser, Subcommand};
|
2022-09-17 15:45:41 +00:00
|
|
|
use rand::seq::IteratorRandom;
|
2022-10-05 13:21:52 +00:00
|
|
|
|
2022-10-09 13:43:17 +00:00
|
|
|
use std::io;
|
2022-09-17 15:45:41 +00:00
|
|
|
use std::time::Instant;
|
2022-10-07 23:02:27 +00:00
|
|
|
use std::{fs, sync::mpsc, thread};
|
|
|
|
|
2022-11-10 18:43:15 +00:00
|
|
|
use chgk_ledb_lib::db;
|
|
|
|
use chgk_ledb_lib::questions;
|
|
|
|
use chgk_ledb_lib::source;
|
2022-10-05 11:55:05 +00:00
|
|
|
|
|
|
|
use crate::questions::{Question, QuestionsConverter};
|
|
|
|
use crate::source::ReadSourceQuestionsBatches;
|
|
|
|
|
2022-09-18 09:08:25 +00:00
|
|
|
const ZIP_FILENAME: &str = "json.zip";
|
2023-01-03 20:51:09 +00:00
|
|
|
const NEW_DB_FILENAME: &str = "db.dat";
|
2022-09-18 09:08:25 +00:00
|
|
|
|
2022-09-17 18:08:13 +00:00
|
|
|
#[derive(Subcommand, Debug)]
|
|
|
|
enum Command {
|
|
|
|
Write,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:13:27 +00:00
|
|
|
fn zip_reader_task(tx: mpsc::Sender<Question>) {
|
2022-10-05 13:21:52 +00:00
|
|
|
let zip_file = fs::File::open(ZIP_FILENAME).unwrap();
|
|
|
|
let zip_reader = io::BufReader::new(zip_file);
|
|
|
|
let archive = zip::ZipArchive::new(zip_reader).unwrap();
|
2022-10-07 12:43:22 +00:00
|
|
|
let mut source_questions = archive.source_questions();
|
2022-10-10 12:44:32 +00:00
|
|
|
|
|
|
|
let questions = source_questions
|
|
|
|
.convert()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(num, mut question)| {
|
|
|
|
question.num = 1 + num as u32;
|
|
|
|
question
|
|
|
|
});
|
|
|
|
for question in questions {
|
2022-10-07 12:43:22 +00:00
|
|
|
let res = tx.send(question);
|
|
|
|
if res.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 13:21:52 +00:00
|
|
|
println!("read done");
|
|
|
|
}
|
2022-09-17 15:45:41 +00:00
|
|
|
|
2022-09-17 18:08:13 +00:00
|
|
|
fn print_question_from<F>(get_q: F)
|
|
|
|
where
|
|
|
|
F: FnOnce() -> Option<Question>,
|
|
|
|
{
|
2023-03-29 10:00:17 +00:00
|
|
|
let q = get_q().expect("question not found");
|
2022-09-17 18:08:13 +00:00
|
|
|
println!("{:#?}", q)
|
2022-09-17 15:45:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:58:57 +00:00
|
|
|
fn read_from_zip(file_num: usize, mut num: usize) -> Option<Question> {
|
|
|
|
let mut rng = rand::thread_rng();
|
2022-09-18 09:08:25 +00:00
|
|
|
let zip_file = fs::File::open(ZIP_FILENAME).unwrap();
|
2022-09-17 15:45:41 +00:00
|
|
|
let zip_reader = io::BufReader::new(zip_file);
|
2022-10-04 19:58:57 +00:00
|
|
|
let archive = zip::ZipArchive::new(zip_reader).unwrap();
|
2022-09-17 15:45:41 +00:00
|
|
|
|
2022-10-04 19:58:57 +00:00
|
|
|
let mut source_questions = archive.source_questions();
|
2022-10-05 11:55:05 +00:00
|
|
|
let (filename, batch) = if file_num == 0 {
|
2022-10-04 19:58:57 +00:00
|
|
|
source_questions.choose(&mut rng).unwrap()
|
|
|
|
} else {
|
|
|
|
source_questions.nth(file_num - 1).unwrap()
|
|
|
|
};
|
2022-10-05 11:55:05 +00:00
|
|
|
let mut batch = batch.unwrap();
|
|
|
|
batch.filename = filename;
|
2022-10-04 19:58:57 +00:00
|
|
|
let questions: Vec<Question> = batch.into();
|
2022-09-17 18:08:13 +00:00
|
|
|
if num == 0 {
|
|
|
|
num = (1..=questions.len()).choose(&mut rng).unwrap();
|
|
|
|
}
|
|
|
|
Some(questions[num - 1].clone())
|
2022-09-17 15:45:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:58:57 +00:00
|
|
|
// measure and return time elapsed in `func` in seconds
|
|
|
|
pub fn measure<F: FnOnce()>(func: F) -> f64 {
|
|
|
|
let start = Instant::now();
|
|
|
|
func();
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
(elapsed.as_secs() as f64) + (elapsed.subsec_nanos() as f64 / 1_000_000_000.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn measure_and_print<F: FnOnce()>(func: F) {
|
|
|
|
let m = measure(func);
|
|
|
|
eprintln!("{}", m);
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:45:41 +00:00
|
|
|
fn main() {
|
2022-09-17 18:08:13 +00:00
|
|
|
let args = Cli::parse();
|
|
|
|
|
|
|
|
let mut action: Box<dyn FnOnce()> = match &args.command {
|
2022-09-18 09:08:25 +00:00
|
|
|
Command::Write => Box::new(write_db),
|
2022-09-17 18:08:13 +00:00
|
|
|
Command::Print { id } => {
|
2022-09-18 09:08:25 +00:00
|
|
|
let get_question = Box::new(|| read_from_db(*id));
|
2022-09-17 18:08:13 +00:00
|
|
|
Box::new(|| print_question_from(get_question))
|
|
|
|
}
|
|
|
|
Command::ZipPrint { file_num, num } => {
|
2022-09-18 09:08:25 +00:00
|
|
|
let get_question = Box::new(|| read_from_zip(*file_num, *num));
|
2022-09-17 18:08:13 +00:00
|
|
|
Box::new(|| print_question_from(get_question))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if args.measure {
|
|
|
|
action = Box::new(|| measure_and_print(action));
|
|
|
|
}
|
|
|
|
|
|
|
|
action();
|
2022-09-17 15:45:41 +00:00
|
|
|
}
|
2022-10-06 18:22:19 +00:00
|
|
|
|
2023-03-29 10:00:17 +00:00
|
|
|
fn read_from_db(id: u32) -> Option<Question> {
|
2023-03-28 11:59:44 +00:00
|
|
|
let reader: db::Reader<Question> =
|
2022-10-09 13:43:17 +00:00
|
|
|
db::Reader::new(NEW_DB_FILENAME, 2048).expect("new db reader");
|
2022-10-10 12:44:32 +00:00
|
|
|
|
2023-03-29 10:00:17 +00:00
|
|
|
let mut questions = reader.into_iter();
|
2022-10-06 18:22:19 +00:00
|
|
|
|
2022-10-09 21:08:57 +00:00
|
|
|
match id {
|
2022-10-07 08:53:17 +00:00
|
|
|
0 => {
|
|
|
|
let mut rng = rand::thread_rng();
|
2022-10-09 21:08:57 +00:00
|
|
|
questions.choose(&mut rng)
|
2022-10-07 08:53:17 +00:00
|
|
|
}
|
2022-10-09 21:08:57 +00:00
|
|
|
_ => questions.nth((id - 1) as usize),
|
2022-10-09 10:38:25 +00:00
|
|
|
}
|
2022-10-06 18:22:19 +00:00
|
|
|
}
|
2023-03-29 10:00:17 +00:00
|
|
|
fn write_db() {
|
2022-10-06 18:22:19 +00:00
|
|
|
let (tx, rx) = mpsc::channel::<Question>();
|
|
|
|
[
|
2022-10-10 13:13:27 +00:00
|
|
|
thread::spawn(move || zip_reader_task(tx)),
|
2023-03-29 10:00:17 +00:00
|
|
|
thread::spawn(move || db_writer_task(rx)),
|
2022-10-06 18:22:19 +00:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|handle| handle.join().expect("thread panic"));
|
|
|
|
println!("all done");
|
|
|
|
}
|
2023-03-29 10:00:17 +00:00
|
|
|
fn db_writer_task(rx: mpsc::Receiver<Question>) {
|
2022-10-09 13:43:17 +00:00
|
|
|
let writer_opts = db::WriterOpts::default();
|
|
|
|
let mut writer: db::Writer<Question> =
|
|
|
|
db::Writer::new(NEW_DB_FILENAME, writer_opts).expect("new db writer");
|
2022-10-09 10:38:25 +00:00
|
|
|
|
2022-10-09 15:26:00 +00:00
|
|
|
writer
|
2022-10-10 12:44:32 +00:00
|
|
|
.load(&mut rx.iter())
|
2022-10-09 21:08:57 +00:00
|
|
|
.unwrap_or_else(|e| panic!("db writer load, {e:#?}"));
|
2022-10-09 15:26:00 +00:00
|
|
|
|
2022-10-09 13:43:17 +00:00
|
|
|
writer.finish().expect("db writer finish");
|
2022-10-06 18:22:19 +00:00
|
|
|
|
|
|
|
println!("write done");
|
|
|
|
}
|