From refactor

This commit is contained in:
Dmitry Belyaev 2022-09-18 02:39:35 +03:00
parent 9a1f69570e
commit f5db9eda4d

View File

@ -13,12 +13,21 @@ extern crate zip;
use clap::{Parser, Subcommand};
use rand::seq::IteratorRandom;
use std::path::PathBuf;
use std::time::Instant;
use std::{fs, io};
use ledb::{Options, Storage};
macro_rules! make {
($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$(
$field: $src.$field
),+}};
($Target:ident; with defaults and by {$($field:ident),+}; from $src:expr) => {$Target {$(
$field: $src.$field
),+ ,..$Target::default()}}
}
#[derive(Subcommand, Debug)]
enum Command {
Write,
@ -45,7 +54,7 @@ struct Cli {
measure: bool,
}
#[derive(Debug, Default, Clone, Deserialize)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
struct SourceQuestion {
#[serde(default)]
num: u32,
@ -100,7 +109,7 @@ struct SourceQuestion {
rating: String,
}
#[derive(Debug, Default, Clone, Deserialize)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
struct SourceQuestionsBatch {
#[serde(default)]
filename: String,
@ -223,56 +232,32 @@ struct Question {
impl From<SourceQuestion> for Question {
fn from(src: SourceQuestion) -> Self {
Self {
num: src.num,
id: src.id,
description: src.description,
answer: src.answer,
author: src.author,
comment: src.comment,
comment1: src.comment1,
tour: src.tour,
url: src.url,
date: src.date,
processed_by: src.processed_by,
redacted_by: src.redacted_by,
copyright: src.copyright,
theme: src.theme,
kind: src.kind,
source: src.source,
rating: src.rating,
batch_info: BatchInfo::default(),
}
make! {Self; with defaults and by {
num, id, description, answer, author, comment, comment1, tour, url,
date, processed_by, redacted_by, copyright, theme, kind, source, rating
}; from src}
}
}
impl From<&SourceQuestionsBatch> for BatchInfo {
fn from(src: &SourceQuestionsBatch) -> Self {
Self {
filename: src.filename.clone(),
description: src.description.clone(),
author: src.author.clone(),
comment: src.comment.clone(),
url: src.url.clone(),
date: src.date.clone(),
processed_by: src.processed_by.clone(),
redacted_by: src.redacted_by.clone(),
copyright: src.copyright.clone(),
theme: src.theme.clone(),
kind: src.kind.clone(),
source: src.source.clone(),
rating: src.rating.clone(),
}
impl From<SourceQuestionsBatch> for BatchInfo {
fn from(src: SourceQuestionsBatch) -> Self {
make! {Self; by {
filename, description, author, comment, url, date,
processed_by, redacted_by, copyright, theme, kind, source, rating
}; from src}
}
}
impl From<SourceQuestionsBatch> for Vec<Question> {
fn from(src: SourceQuestionsBatch) -> Self {
let mut result = Vec::<Question>::with_capacity(src.questions.len());
src.questions.iter().for_each(|item| {
let mut question: Question = item.clone().into();
question.batch_info = BatchInfo::from(&src);
result.push(question);
let mut result: Vec<Question> = src
.questions
.iter()
.map(|item| item.clone().into())
.collect();
let batch_info = BatchInfo::from(src);
result.iter_mut().for_each(|mut question| {
question.batch_info = batch_info.clone();
});
result
@ -293,7 +278,19 @@ pub fn measure_and_print<F: FnOnce()>(func: F) {
}
fn writer_v4() {
let zip_file = fs::File::open("test1.zip").unwrap();
const IN_FILENAME: &str = "test1.zip";
const OUT_DIR: &str = "db";
let out_file: PathBuf = [OUT_DIR, "data.mdb"].into_iter().collect();
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());
}
_ => {}
};
let zip_file = fs::File::open(IN_FILENAME).unwrap();
let zip_reader = io::BufReader::new(zip_file);
let mut archive = zip::ZipArchive::new(zip_reader).unwrap();
@ -309,7 +306,8 @@ fn writer_v4() {
let storage = Storage::new("db", options).unwrap();
let collection = storage.collection("questions").unwrap();
collection.purge().unwrap();
println!("converting...");
let mut count: usize = 0;
let count = &mut count;
@ -333,17 +331,14 @@ fn writer_v4() {
.for_each(|question| {
let result = collection.insert(&question);
if result.is_err() {
println!(
"Error: {:?} \n\
On: {:?}",
result, question
);
println!("-- {:#?}", question);
panic!("-- {:#?}", result);
} else {
*count += 1;
}
});
println!("inserted {}", count);
println!("inserted {}\nwriting...", count);
storage.sync(true).unwrap();
print!("stats: ");
let stats = storage.stat().unwrap();