2022-09-17 15:45:41 +00:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate ledb;
|
|
|
|
extern crate ledb_types;
|
|
|
|
extern crate zip;
|
|
|
|
|
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-07 08:53:17 +00:00
|
|
|
use std::io::{Read, Seek, Write};
|
2022-09-17 23:39:35 +00:00
|
|
|
use std::path::PathBuf;
|
2022-09-17 15:45:41 +00:00
|
|
|
use std::time::Instant;
|
2022-10-05 13:21:52 +00:00
|
|
|
use std::{fs, io, sync::mpsc, thread};
|
2022-09-17 15:45:41 +00:00
|
|
|
|
|
|
|
use ledb::{Options, Storage};
|
|
|
|
|
2022-10-05 11:55:05 +00:00
|
|
|
mod questions;
|
|
|
|
mod source;
|
|
|
|
|
|
|
|
use crate::questions::{Question, QuestionsConverter};
|
|
|
|
use crate::source::ReadSourceQuestionsBatches;
|
|
|
|
|
2022-09-18 09:08:25 +00:00
|
|
|
const ZIP_FILENAME: &str = "json.zip";
|
|
|
|
const DB_DIR: &str = "db";
|
|
|
|
|
2022-09-17 18:08:13 +00:00
|
|
|
#[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,
|
|
|
|
},
|
2022-10-06 18:22:19 +00:00
|
|
|
Write2,
|
|
|
|
Print2 {
|
|
|
|
#[clap(value_parser, default_value = "0")]
|
|
|
|
id: u32,
|
|
|
|
},
|
2022-09-17 18:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-05 13:21:52 +00:00
|
|
|
fn reader_task(tx: mpsc::Sender<Question>) {
|
|
|
|
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();
|
|
|
|
archive
|
|
|
|
.source_questions()
|
|
|
|
.convert()
|
|
|
|
.for_each(|question| tx.send(question).expect("send question"));
|
|
|
|
println!("read done");
|
|
|
|
}
|
|
|
|
fn db_writer_task(rx: mpsc::Receiver<Question>) {
|
2022-09-18 09:08:25 +00:00
|
|
|
let out_file: PathBuf = [DB_DIR, "data.mdb"].into_iter().collect();
|
2022-09-17 23:39:35 +00:00
|
|
|
match fs::metadata(&out_file) {
|
|
|
|
Ok(x) if x.is_file() => {
|
|
|
|
fs::remove_file(&out_file).unwrap();
|
|
|
|
println!(r#""{}" removed"#, out_file.to_str().unwrap());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
2022-09-17 15:45:41 +00:00
|
|
|
let options: Options = serde_json::from_value(json!({
|
|
|
|
"map_size": 900 * 1024 * 1024, // 900mb
|
|
|
|
"write_map": true,
|
|
|
|
"map_async": true,
|
|
|
|
"no_lock": true,
|
|
|
|
"no_meta_sync": true,
|
|
|
|
"no_sync": true,
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
2022-09-18 09:08:25 +00:00
|
|
|
let storage = Storage::new(DB_DIR, options).unwrap();
|
2022-09-17 15:45:41 +00:00
|
|
|
let collection = storage.collection("questions").unwrap();
|
2022-09-17 23:39:35 +00:00
|
|
|
|
2022-09-17 15:45:41 +00:00
|
|
|
let mut count: usize = 0;
|
|
|
|
let count = &mut count;
|
2022-10-05 13:21:52 +00:00
|
|
|
rx.into_iter().for_each(|question| {
|
2022-10-05 11:55:05 +00:00
|
|
|
let result = collection.insert(&question);
|
|
|
|
if result.is_err() {
|
|
|
|
println!("-- {:#?}", question);
|
|
|
|
panic!("{:#?}", result);
|
|
|
|
} else {
|
|
|
|
*count += 1;
|
|
|
|
}
|
|
|
|
});
|
2022-09-17 15:45:41 +00:00
|
|
|
|
2022-09-17 23:39:35 +00:00
|
|
|
println!("inserted {}\nwriting...", count);
|
2022-09-17 16:13:17 +00:00
|
|
|
storage.sync(true).unwrap();
|
|
|
|
print!("stats: ");
|
|
|
|
let stats = storage.stat().unwrap();
|
|
|
|
println!("{:?}", stats);
|
|
|
|
drop(storage);
|
2022-10-05 13:21:52 +00:00
|
|
|
println!("write done");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_db() {
|
|
|
|
let (tx, rx) = mpsc::channel::<Question>();
|
|
|
|
[
|
|
|
|
thread::spawn(move || reader_task(tx)),
|
|
|
|
thread::spawn(move || db_writer_task(rx)),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|handle| handle.join().expect("thread panic"));
|
|
|
|
println!("all 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>,
|
|
|
|
{
|
|
|
|
let q = get_q().unwrap();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn compact_db() {
|
|
|
|
let options: Options = serde_json::from_value(json!({
|
|
|
|
"write_map": true,
|
|
|
|
"map_async": true,
|
|
|
|
"no_lock": true,
|
|
|
|
"no_meta_sync": true,
|
|
|
|
"no_sync": true,
|
2022-09-17 18:08:13 +00:00
|
|
|
"compact": true,
|
2022-09-17 15:45:41 +00:00
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
2022-09-18 09:08:25 +00:00
|
|
|
let storage = Storage::new(DB_DIR, options).unwrap();
|
2022-09-17 15:45:41 +00:00
|
|
|
|
2022-09-17 16:13:17 +00:00
|
|
|
storage.sync(true).unwrap();
|
|
|
|
let stats = storage.stat().unwrap();
|
|
|
|
println!("{:?}", stats);
|
|
|
|
drop(storage);
|
2022-09-17 15:45:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 09:08:25 +00:00
|
|
|
fn read_from_db(mut id: u32) -> Option<Question> {
|
2022-09-17 15:45:41 +00:00
|
|
|
let options: Options = serde_json::from_value(json!({
|
2022-09-18 09:08:25 +00:00
|
|
|
"read_only": true,
|
|
|
|
"map_async": true,
|
|
|
|
"no_lock": true,
|
|
|
|
}))
|
2022-09-17 15:45:41 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2022-09-18 09:08:25 +00:00
|
|
|
let storage = Storage::new(DB_DIR, options).unwrap();
|
2022-09-17 15:45:41 +00:00
|
|
|
let collection = storage.collection("questions").unwrap();
|
|
|
|
let mut rng = rand::thread_rng();
|
2022-09-17 18:08:13 +00:00
|
|
|
|
|
|
|
if id == 0 {
|
|
|
|
let last_id = collection.last_id().unwrap();
|
|
|
|
id = (1..=last_id).choose(&mut rng).unwrap();
|
|
|
|
}
|
2022-09-17 15:45:41 +00:00
|
|
|
|
|
|
|
collection.get::<Question>(id).unwrap()
|
|
|
|
}
|
|
|
|
|
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::Compact => Box::new(compact_db),
|
|
|
|
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))
|
|
|
|
}
|
2022-10-06 18:22:19 +00:00
|
|
|
Command::Write2 => Box::new(write_db2),
|
|
|
|
Command::Print2 { id } => {
|
|
|
|
let get_question = Box::new(|| read_from_db2(*id));
|
|
|
|
Box::new(|| print_question_from(get_question))
|
|
|
|
}
|
2022-09-17 18:08:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
fn read_from_db2(id: u32) -> Option<Question> {
|
|
|
|
use std::io::Cursor;
|
|
|
|
const LEN_SIZE: usize = std::mem::size_of::<u64>();
|
|
|
|
let cfg = bincode::config::standard().with_fixed_int_encoding();
|
|
|
|
|
|
|
|
let input = fs::File::open("test.bin").expect("open input");
|
|
|
|
let mut input = std::io::BufReader::with_capacity(100 * 1024 * 1024, input);
|
|
|
|
|
|
|
|
let mut len_data: [u8; LEN_SIZE] = [0; LEN_SIZE];
|
|
|
|
input.read_exact(&mut len_data).expect("read len");
|
|
|
|
let len = u64::from_le_bytes(len_data) as usize;
|
|
|
|
|
|
|
|
//println!("read len done");
|
|
|
|
|
|
|
|
let mut zdata = vec![0u8; len];
|
|
|
|
input.read_exact(&mut zdata[..len]).expect("read ztab");
|
|
|
|
let tab_data = zstd::decode_all(Cursor::new(zdata)).expect("zstd decode table");
|
2022-10-07 08:53:17 +00:00
|
|
|
let tab: (Vec<u32>, usize) =
|
2022-10-06 18:22:19 +00:00
|
|
|
bincode::decode_from_slice(&tab_data, cfg).expect("bincode decode tab");
|
|
|
|
let tab = tab.0;
|
|
|
|
|
2022-10-07 08:53:17 +00:00
|
|
|
//println!("read tab done, len {}", tab.len());
|
|
|
|
let index = match id {
|
|
|
|
0 => {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
(1..tab.len()).into_iter().choose(&mut rng).unwrap()
|
|
|
|
}
|
|
|
|
_ => (id - 1) as usize,
|
|
|
|
};
|
|
|
|
|
|
|
|
//println!("index {}", index);
|
|
|
|
|
|
|
|
let pos = *tab.get(index).expect("get pos");
|
|
|
|
let pos_next = *tab.get((index + 1) as usize).expect("get pos next");
|
|
|
|
let len = pos_next - pos;
|
|
|
|
|
|
|
|
//println!("pos {} | next {} | len {}", pos, pos_next, len);
|
2022-10-06 18:22:19 +00:00
|
|
|
|
2022-10-07 08:53:17 +00:00
|
|
|
input.seek_relative(pos as i64).expect("q seek");
|
|
|
|
let reader = input.take(len as u64);
|
2022-10-06 18:22:19 +00:00
|
|
|
let data = zstd::decode_all(reader).expect("zstd decode data");
|
|
|
|
|
2022-10-07 08:53:17 +00:00
|
|
|
//println!("zstd decoded len {}", data.len());
|
|
|
|
|
2022-10-06 18:22:19 +00:00
|
|
|
let question: (Question, usize) =
|
|
|
|
bincode::decode_from_slice(&data, cfg).expect("bincode decode q");
|
|
|
|
let question = question.0;
|
|
|
|
|
|
|
|
//println!("read done");
|
|
|
|
|
|
|
|
Some(question)
|
|
|
|
}
|
|
|
|
fn write_db2() {
|
|
|
|
let (tx, rx) = mpsc::channel::<Question>();
|
|
|
|
[
|
|
|
|
thread::spawn(move || reader_task(tx)),
|
|
|
|
thread::spawn(move || db_writer2_task(rx)),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|handle| handle.join().expect("thread panic"));
|
|
|
|
println!("all done");
|
|
|
|
}
|
|
|
|
fn db_writer2_task(rx: mpsc::Receiver<Question>) {
|
|
|
|
const LEN_SIZE: usize = std::mem::size_of::<u64>();
|
2022-10-07 08:53:17 +00:00
|
|
|
const COMP_DATA_LEVEL: i32 = 2;
|
|
|
|
const COMP_HDR_LEVEL: i32 = 2;
|
2022-10-06 18:22:19 +00:00
|
|
|
|
|
|
|
let cfg = bincode::config::standard().with_fixed_int_encoding();
|
2022-10-07 08:53:17 +00:00
|
|
|
let mut table: Vec<u32> = vec![];
|
2022-10-06 18:22:19 +00:00
|
|
|
|
|
|
|
let buf_data: Vec<u8> = Vec::with_capacity(500 * 1024 * 1024);
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buf = Cursor::new(buf_data);
|
|
|
|
let mut pos: u32 = 0;
|
2022-10-07 08:53:17 +00:00
|
|
|
let mut num = 1;
|
|
|
|
rx.into_iter().for_each(|mut q| {
|
|
|
|
q.num = num;
|
2022-10-06 18:22:19 +00:00
|
|
|
let data = bincode::encode_to_vec(q, cfg).expect("bincode q encode");
|
2022-10-07 08:53:17 +00:00
|
|
|
let data = zstd::encode_all(Cursor::new(data), COMP_DATA_LEVEL).expect("zstd q encode");
|
2022-10-06 18:22:19 +00:00
|
|
|
let len = buf.write(&data).expect("write question");
|
2022-10-07 08:53:17 +00:00
|
|
|
table.push(pos);
|
|
|
|
|
2022-10-06 18:22:19 +00:00
|
|
|
pos += len as u32;
|
2022-10-07 08:53:17 +00:00
|
|
|
num += 1;
|
2022-10-06 18:22:19 +00:00
|
|
|
});
|
2022-10-07 08:53:17 +00:00
|
|
|
table.push(pos);
|
2022-10-06 18:22:19 +00:00
|
|
|
|
2022-10-07 08:53:17 +00:00
|
|
|
println!(
|
|
|
|
"zbuf done, tab len {}, buf size {}",
|
|
|
|
table.len(),
|
|
|
|
buf.position()
|
|
|
|
);
|
2022-10-06 18:22:19 +00:00
|
|
|
|
2022-10-07 08:53:17 +00:00
|
|
|
buf.set_position(0);
|
2022-10-06 18:22:19 +00:00
|
|
|
|
|
|
|
let tab_data = bincode::encode_to_vec(&table, cfg).expect("encode table");
|
2022-10-07 08:53:17 +00:00
|
|
|
let zdata = zstd::encode_all(Cursor::new(tab_data), COMP_HDR_LEVEL).expect("zstd enc table");
|
2022-10-06 18:22:19 +00:00
|
|
|
let zlen = zdata.len() as u64;
|
|
|
|
|
2022-10-07 08:53:17 +00:00
|
|
|
println!("z tab done, tab data_len = {}", zlen);
|
2022-10-06 18:22:19 +00:00
|
|
|
|
|
|
|
let out = fs::File::create("test.bin").expect("out create");
|
|
|
|
let mut out = std::io::BufWriter::with_capacity(500 * 1024 * 1024, out);
|
|
|
|
let len_writed = out.write(&zlen.to_le_bytes()).expect("write zlen");
|
|
|
|
assert_eq!(len_writed, LEN_SIZE);
|
|
|
|
let ztab_writed = out.write(&zdata).expect("write tab zdata");
|
|
|
|
assert_eq!(ztab_writed, zdata.len());
|
|
|
|
drop(zdata);
|
2022-10-07 08:53:17 +00:00
|
|
|
|
|
|
|
println!("header write done, pos: {}", out.stream_position().unwrap());
|
|
|
|
|
2022-10-06 18:22:19 +00:00
|
|
|
std::io::copy(&mut buf, &mut out).expect("copy z buf");
|
|
|
|
drop(buf);
|
|
|
|
out.flush().expect("out flush");
|
|
|
|
|
|
|
|
println!("write done");
|
|
|
|
}
|