rust: simplificar

This commit is contained in:
Cat /dev/Nulo 2023-04-23 21:40:13 -03:00
parent 92a9ab8340
commit c4a1418cc2

View file

@ -16,18 +16,19 @@ fn db_path() -> &'static str {
fn main() -> Result<(), Box<dyn Error>> {
_ = remove_file(db_path());
{
let conn = make_conn()?;
make_db_schema(&conn)?;
}
let input = Path::new("./input");
let paths = iterator(input).collect::<Vec<PathBuf>>();
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 (rx, threads) = {
let (tx, rx): (Sender<Entry>, Receiver<Entry>) = mpsc::channel();
let threads = chunks
let producers = chunks
.map(|chunk| {
let chunkk = chunk.to_vec();
let ttx = tx.clone();
@ -40,9 +41,11 @@ fn main() -> Result<(), Box<dyn Error>> {
})
})
.collect::<Vec<JoinHandle<()>>>();
let collector = thread::spawn(|| {
(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<dyn Error>> {
}
}
trans.commit().unwrap();
});
drop(tx);
collector.join().unwrap();
}
for thread in threads {
thread.join().unwrap();
}