From refactor
This commit is contained in:
parent
9a1f69570e
commit
f5db9eda4d
101
src/main.rs
101
src/main.rs
@ -13,12 +13,21 @@ extern crate zip;
|
|||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use rand::seq::IteratorRandom;
|
use rand::seq::IteratorRandom;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use std::{fs, io};
|
use std::{fs, io};
|
||||||
|
|
||||||
use ledb::{Options, Storage};
|
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)]
|
#[derive(Subcommand, Debug)]
|
||||||
enum Command {
|
enum Command {
|
||||||
Write,
|
Write,
|
||||||
@ -45,7 +54,7 @@ struct Cli {
|
|||||||
measure: bool,
|
measure: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize)]
|
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||||
struct SourceQuestion {
|
struct SourceQuestion {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
num: u32,
|
num: u32,
|
||||||
@ -100,7 +109,7 @@ struct SourceQuestion {
|
|||||||
rating: String,
|
rating: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize)]
|
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||||
struct SourceQuestionsBatch {
|
struct SourceQuestionsBatch {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
filename: String,
|
filename: String,
|
||||||
@ -223,56 +232,32 @@ struct Question {
|
|||||||
|
|
||||||
impl From<SourceQuestion> for Question {
|
impl From<SourceQuestion> for Question {
|
||||||
fn from(src: SourceQuestion) -> Self {
|
fn from(src: SourceQuestion) -> Self {
|
||||||
Self {
|
make! {Self; with defaults and by {
|
||||||
num: src.num,
|
num, id, description, answer, author, comment, comment1, tour, url,
|
||||||
id: src.id,
|
date, processed_by, redacted_by, copyright, theme, kind, source, rating
|
||||||
description: src.description,
|
}; from src}
|
||||||
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(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&SourceQuestionsBatch> for BatchInfo {
|
impl From<SourceQuestionsBatch> for BatchInfo {
|
||||||
fn from(src: &SourceQuestionsBatch) -> Self {
|
fn from(src: SourceQuestionsBatch) -> Self {
|
||||||
Self {
|
make! {Self; by {
|
||||||
filename: src.filename.clone(),
|
filename, description, author, comment, url, date,
|
||||||
description: src.description.clone(),
|
processed_by, redacted_by, copyright, theme, kind, source, rating
|
||||||
author: src.author.clone(),
|
}; from src}
|
||||||
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 Vec<Question> {
|
impl From<SourceQuestionsBatch> for Vec<Question> {
|
||||||
fn from(src: SourceQuestionsBatch) -> Self {
|
fn from(src: SourceQuestionsBatch) -> Self {
|
||||||
let mut result = Vec::<Question>::with_capacity(src.questions.len());
|
let mut result: Vec<Question> = src
|
||||||
src.questions.iter().for_each(|item| {
|
.questions
|
||||||
let mut question: Question = item.clone().into();
|
.iter()
|
||||||
question.batch_info = BatchInfo::from(&src);
|
.map(|item| item.clone().into())
|
||||||
result.push(question);
|
.collect();
|
||||||
|
let batch_info = BatchInfo::from(src);
|
||||||
|
result.iter_mut().for_each(|mut question| {
|
||||||
|
question.batch_info = batch_info.clone();
|
||||||
});
|
});
|
||||||
|
|
||||||
result
|
result
|
||||||
@ -293,7 +278,19 @@ pub fn measure_and_print<F: FnOnce()>(func: F) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn writer_v4() {
|
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 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();
|
||||||
|
|
||||||
@ -309,7 +306,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();
|
|
||||||
|
println!("converting...");
|
||||||
|
|
||||||
let mut count: usize = 0;
|
let mut count: usize = 0;
|
||||||
let count = &mut count;
|
let count = &mut count;
|
||||||
@ -333,17 +331,14 @@ fn writer_v4() {
|
|||||||
.for_each(|question| {
|
.for_each(|question| {
|
||||||
let result = collection.insert(&question);
|
let result = collection.insert(&question);
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
println!(
|
println!("-- {:#?}", question);
|
||||||
"Error: {:?} \n\
|
panic!("-- {:#?}", result);
|
||||||
On: {:?}",
|
|
||||||
result, question
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
*count += 1;
|
*count += 1;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
println!("inserted {}", count);
|
println!("inserted {}\nwriting...", count);
|
||||||
storage.sync(true).unwrap();
|
storage.sync(true).unwrap();
|
||||||
print!("stats: ");
|
print!("stats: ");
|
||||||
let stats = storage.stat().unwrap();
|
let stats = storage.stat().unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user