From b0a06300dc1dcf238e0806d403e1643e4e50c625 Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Sun, 18 Sep 2022 12:08:25 +0300
Subject: [PATCH] rename fn's + consts + cleanup

---
 .gitignore  |  1 +
 src/main.rs | 71 +++++++++++++++--------------------------------------
 2 files changed, 21 insertions(+), 51 deletions(-)

diff --git a/.gitignore b/.gitignore
index d27aca3..e9479e0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
 **/*.rs.bk
 /db
 test?.zip
+json.zip
 /exp
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index d8c534e..73d206d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -19,6 +19,9 @@ use std::{fs, io};
 
 use ledb::{Options, Storage};
 
+const ZIP_FILENAME: &str = "json.zip";
+const DB_DIR: &str = "db";
+
 macro_rules! make {
     ($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$(
         $field: $src.$field
@@ -277,11 +280,8 @@ pub fn measure_and_print<F: FnOnce()>(func: F) {
     eprintln!("{}", m);
 }
 
-fn writer_v4() {
-    const IN_FILENAME: &str = "test1.zip";
-    const OUT_DIR: &str = "db";
-
-    let out_file: PathBuf = [OUT_DIR, "data.mdb"].into_iter().collect();
+fn write_db() {
+    let out_file: PathBuf = [DB_DIR, "data.mdb"].into_iter().collect();
     match fs::metadata(&out_file) {
         Ok(x) if x.is_file() => {
             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 mut archive = zip::ZipArchive::new(zip_reader).unwrap();
 
@@ -304,7 +304,7 @@ fn writer_v4() {
     }))
     .unwrap();
 
-    let storage = Storage::new("db", options).unwrap();
+    let storage = Storage::new(DB_DIR, options).unwrap();
     let collection = storage.collection("questions").unwrap();
 
     println!("converting...");
@@ -332,7 +332,7 @@ fn writer_v4() {
             let result = collection.insert(&question);
             if result.is_err() {
                 println!("-- {:#?}", question);
-                panic!("-- {:#?}", result);
+                panic!("{:#?}", result);
             } else {
                 *count += 1;
             }
@@ -354,8 +354,8 @@ where
     println!("{:#?}", q)
 }
 
-fn reader_v1(mut file_num: usize, mut num: usize) -> Option<Question> {
-    let zip_file = fs::File::open("test1.zip").unwrap();
+fn read_from_zip(mut file_num: usize, mut num: usize) -> Option<Question> {
+    let zip_file = fs::File::open(ZIP_FILENAME).unwrap();
     let zip_reader = io::BufReader::new(zip_file);
     let mut archive = zip::ZipArchive::new(zip_reader).unwrap();
 
@@ -385,7 +385,7 @@ fn compact_db() {
     }))
     .unwrap();
 
-    let storage = Storage::new("db", options).unwrap();
+    let storage = Storage::new(DB_DIR, options).unwrap();
 
     storage.sync(true).unwrap();
     let stats = storage.stat().unwrap();
@@ -393,25 +393,21 @@ fn compact_db() {
     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!({
-            "read_only": true,
-    //        "map_async": true,
-            "no_lock": true,
-    //        "no_meta_sync": true,
-    //        "no_sync": true,
-    //        "no_tls": true
-        }))
+        "read_only": true,
+        "map_async": true,
+        "no_lock": true,
+    }))
     .unwrap();
 
-    let storage = Storage::new("db", options).unwrap();
+    let storage = Storage::new(DB_DIR, options).unwrap();
     let collection = storage.collection("questions").unwrap();
     let mut rng = rand::thread_rng();
 
     if id == 0 {
         let last_id = collection.last_id().unwrap();
         id = (1..=last_id).choose(&mut rng).unwrap();
-        //let id = ledb::KeyData::Int(id as i64);
     }
 
     collection.get::<Question>(id).unwrap()
@@ -421,14 +417,14 @@ fn main() {
     let args = Cli::parse();
 
     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::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))
         }
         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))
         }
     };
@@ -439,30 +435,3 @@ fn main() {
 
     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());
-    }
-}
-*/