rename fn's + consts + cleanup

This commit is contained in:
Dmitry Belyaev 2022-09-18 12:08:25 +03:00
parent f5db9eda4d
commit b0a06300dc
2 changed files with 21 additions and 51 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@
**/*.rs.bk **/*.rs.bk
/db /db
test?.zip test?.zip
json.zip
/exp /exp

View File

@ -19,6 +19,9 @@ use std::{fs, io};
use ledb::{Options, Storage}; use ledb::{Options, Storage};
const ZIP_FILENAME: &str = "json.zip";
const DB_DIR: &str = "db";
macro_rules! make { macro_rules! make {
($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$( ($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$(
$field: $src.$field $field: $src.$field
@ -277,11 +280,8 @@ pub fn measure_and_print<F: FnOnce()>(func: F) {
eprintln!("{}", m); eprintln!("{}", m);
} }
fn writer_v4() { fn write_db() {
const IN_FILENAME: &str = "test1.zip"; let out_file: PathBuf = [DB_DIR, "data.mdb"].into_iter().collect();
const OUT_DIR: &str = "db";
let out_file: PathBuf = [OUT_DIR, "data.mdb"].into_iter().collect();
match fs::metadata(&out_file) { match fs::metadata(&out_file) {
Ok(x) if x.is_file() => { Ok(x) if x.is_file() => {
fs::remove_file(&out_file).unwrap(); fs::remove_file(&out_file).unwrap();
@ -290,7 +290,7 @@ fn writer_v4() {
_ => {} _ => {}
}; };
let zip_file = fs::File::open(IN_FILENAME).unwrap(); let zip_file = fs::File::open(ZIP_FILENAME).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();
@ -304,7 +304,7 @@ fn writer_v4() {
})) }))
.unwrap(); .unwrap();
let storage = Storage::new("db", options).unwrap(); let storage = Storage::new(DB_DIR, options).unwrap();
let collection = storage.collection("questions").unwrap(); let collection = storage.collection("questions").unwrap();
println!("converting..."); println!("converting...");
@ -332,7 +332,7 @@ fn writer_v4() {
let result = collection.insert(&question); let result = collection.insert(&question);
if result.is_err() { if result.is_err() {
println!("-- {:#?}", question); println!("-- {:#?}", question);
panic!("-- {:#?}", result); panic!("{:#?}", result);
} else { } else {
*count += 1; *count += 1;
} }
@ -354,8 +354,8 @@ where
println!("{:#?}", q) println!("{:#?}", q)
} }
fn reader_v1(mut file_num: usize, mut num: usize) -> Option<Question> { fn read_from_zip(mut file_num: usize, mut num: usize) -> Option<Question> {
let zip_file = fs::File::open("test1.zip").unwrap(); let zip_file = fs::File::open(ZIP_FILENAME).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();
@ -385,7 +385,7 @@ fn compact_db() {
})) }))
.unwrap(); .unwrap();
let storage = Storage::new("db", options).unwrap(); let storage = Storage::new(DB_DIR, options).unwrap();
storage.sync(true).unwrap(); storage.sync(true).unwrap();
let stats = storage.stat().unwrap(); let stats = storage.stat().unwrap();
@ -393,25 +393,21 @@ fn compact_db() {
drop(storage); drop(storage);
} }
fn reader_v4(mut id: u32) -> Option<Question> { fn read_from_db(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,
"no_lock": true, "no_lock": true,
// "no_meta_sync": true,
// "no_sync": true,
// "no_tls": true
})) }))
.unwrap(); .unwrap();
let storage = Storage::new("db", options).unwrap(); let storage = Storage::new(DB_DIR, 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();
if id == 0 { if id == 0 {
let last_id = collection.last_id().unwrap(); let last_id = collection.last_id().unwrap();
id = (1..=last_id).choose(&mut rng).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()
@ -421,14 +417,14 @@ fn main() {
let args = Cli::parse(); let args = Cli::parse();
let mut action: Box<dyn FnOnce()> = match &args.command { let mut action: Box<dyn FnOnce()> = match &args.command {
Command::Write => Box::new(writer_v4), Command::Write => Box::new(write_db),
Command::Compact => Box::new(compact_db), Command::Compact => Box::new(compact_db),
Command::Print { id } => { Command::Print { id } => {
let get_question = Box::new(|| reader_v4(*id)); let get_question = Box::new(|| read_from_db(*id));
Box::new(|| print_question_from(get_question)) Box::new(|| print_question_from(get_question))
} }
Command::ZipPrint { file_num, num } => { Command::ZipPrint { file_num, num } => {
let get_question = Box::new(|| reader_v1(*file_num, *num)); let get_question = Box::new(|| read_from_zip(*file_num, *num));
Box::new(|| print_question_from(get_question)) Box::new(|| print_question_from(get_question))
} }
}; };
@ -439,30 +435,3 @@ fn main() {
action(); action();
} }
/*
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[test]
fn reader_v1_works() {
reader_v1().unwrap();
}
fn reader_v4_works() {
reader_v4().unwrap();
}
#[bench]
fn bench_reader_v1(b: &mut Bencher) {
b.iter(|| reader_v1());
}
#[bench]
fn bench_reader_v4(b: &mut Bencher) {
b.iter(|| reader_v4());
}
}
*/