diff --git a/rust/src/main.rs b/rust/src/main.rs index 5932d64..1ceb148 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -16,33 +16,36 @@ fn db_path() -> &'static str { fn main() -> Result<(), Box> { _ = remove_file(db_path()); - let conn = make_conn()?; - make_db_schema(&conn)?; + { + let conn = make_conn()?; + make_db_schema(&conn)?; + } let input = Path::new("./input"); - let paths = iterator(input).collect::>(); - let chunks = paths.chunks(paths.len() / 4); - println!("{}", chunks.len()); + let chunks = paths.chunks(paths.len() / 4 + paths.len() % 4); + println!("threads: {}", chunks.len()); - let (tx, rx): (Sender, Receiver) = mpsc::channel(); - - let threads = chunks - .map(|chunk| { - let chunkk = chunk.to_vec(); - let ttx = tx.clone(); - thread::spawn(move || { - let input = Path::new("./input"); - let params = file_compress_params(); - for path in chunkk { - compress_file(&path, input, ¶ms, &ttx); - } + let (rx, threads) = { + let (tx, rx): (Sender, Receiver) = mpsc::channel(); + let producers = chunks + .map(|chunk| { + let chunkk = chunk.to_vec(); + let ttx = tx.clone(); + thread::spawn(move || { + let input = Path::new("./input"); + let params = file_compress_params(); + for path in chunkk { + compress_file(&path, input, ¶ms, &ttx); + } + }) }) - }) - .collect::>>(); - let collector = thread::spawn(|| { + .collect::>>(); + + (rx, producers) + }; + { let mut conn = make_conn().unwrap(); - println!("thread"); let trans = conn.transaction().unwrap(); { let mut insert_stmt = trans @@ -53,10 +56,7 @@ fn main() -> Result<(), Box> { } } trans.commit().unwrap(); - }); - drop(tx); - - collector.join().unwrap(); + } for thread in threads { thread.join().unwrap(); }