From 122e47303b86696d9fd1bd9933e1829e52eb78e0 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sat, 17 Sep 2022 19:13:17 +0300 Subject: [PATCH] refactor --- src/main.rs | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7eeab3e..b8ec2df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -246,12 +246,12 @@ impl From<&SourceQuestionsBatch> for BatchInfo { } } -impl Into> for SourceQuestionsBatch { - fn into(self) -> Vec { - let mut result = Vec::::with_capacity(self.questions.len()); - self.questions.iter().for_each(|item| { +impl From for Vec { + fn from(src: SourceQuestionsBatch) -> Self { + let mut result = Vec::::with_capacity(src.questions.len()); + src.questions.iter().for_each(|item| { let mut question: Question = item.clone().into(); - question.batch_info = BatchInfo::from(&self); + question.batch_info = BatchInfo::from(&src); result.push(question); }); @@ -264,7 +264,7 @@ pub fn measure(func: F) -> f64 { let start = Instant::now(); func(); let elapsed = start.elapsed(); - (elapsed.as_secs() as f64) + (elapsed.subsec_nanos() as f64 / 1000_000_000.0) + (elapsed.as_secs() as f64) + (elapsed.subsec_nanos() as f64 / 1_000_000_000.0) } pub fn measure_and_print(func: F) { @@ -308,19 +308,18 @@ fn writer_v4() { (String::from(name_str), data) }) .filter(|(_, data)| data.is_ok()) - .map(|(filename, data)| { + .flat_map(|(filename, data)| { let mut data = data.unwrap(); - data.filename = filename.clone(); + data.filename = filename; let questions: Vec = data.into(); questions }) - .flatten() .for_each(|question| { let result = collection.insert(&question); if result.is_err() { println!( "Error: {:?} \n\ - On: {:?}", + On: {:?}", result, question ); } else { @@ -329,6 +328,11 @@ fn writer_v4() { }); println!("inserted {}", count); + storage.sync(true).unwrap(); + print!("stats: "); + let stats = storage.stat().unwrap(); + println!("{:?}", stats); + drop(storage); } fn print_question(q: Question) { @@ -347,13 +351,7 @@ fn reader_v1() -> Option { let data = data.unwrap(); let questions: Vec = data.into(); - let ret = questions.iter().choose(&mut rng); - - if let Some(question) = ret { - Some(question.clone()) - } else { - None - } + questions.into_iter().choose(&mut rng) } fn compact_db() { @@ -367,13 +365,11 @@ fn compact_db() { .unwrap(); let storage = Storage::new("db", options).unwrap(); - //let collection = storage.collection("questions").unwrap(); - //query!(index for collection - // num int, - // batch_info.filename str, - //) - //.unwrap(); + storage.sync(true).unwrap(); + let stats = storage.stat().unwrap(); + println!("{:?}", stats); + drop(storage); } fn reader_v4() -> Option {