rust: simplificar
This commit is contained in:
parent
92a9ab8340
commit
c4a1418cc2
1 changed files with 25 additions and 25 deletions
|
@ -16,33 +16,36 @@ fn db_path() -> &'static str {
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
_ = remove_file(db_path());
|
_ = 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 input = Path::new("./input");
|
||||||
|
|
||||||
let paths = iterator(input).collect::<Vec<PathBuf>>();
|
let paths = iterator(input).collect::<Vec<PathBuf>>();
|
||||||
|
|
||||||
let chunks = paths.chunks(paths.len() / 4);
|
let chunks = paths.chunks(paths.len() / 4 + paths.len() % 4);
|
||||||
println!("{}", chunks.len());
|
println!("threads: {}", chunks.len());
|
||||||
|
|
||||||
let (tx, rx): (Sender<Entry>, Receiver<Entry>) = mpsc::channel();
|
let (rx, threads) = {
|
||||||
|
let (tx, rx): (Sender<Entry>, Receiver<Entry>) = mpsc::channel();
|
||||||
let threads = chunks
|
let producers = chunks
|
||||||
.map(|chunk| {
|
.map(|chunk| {
|
||||||
let chunkk = chunk.to_vec();
|
let chunkk = chunk.to_vec();
|
||||||
let ttx = tx.clone();
|
let ttx = tx.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let input = Path::new("./input");
|
let input = Path::new("./input");
|
||||||
let params = file_compress_params();
|
let params = file_compress_params();
|
||||||
for path in chunkk {
|
for path in chunkk {
|
||||||
compress_file(&path, input, ¶ms, &ttx);
|
compress_file(&path, input, ¶ms, &ttx);
|
||||||
}
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
.collect::<Vec<JoinHandle<()>>>();
|
||||||
.collect::<Vec<JoinHandle<()>>>();
|
|
||||||
let collector = thread::spawn(|| {
|
(rx, producers)
|
||||||
|
};
|
||||||
|
{
|
||||||
let mut conn = make_conn().unwrap();
|
let mut conn = make_conn().unwrap();
|
||||||
println!("thread");
|
|
||||||
let trans = conn.transaction().unwrap();
|
let trans = conn.transaction().unwrap();
|
||||||
{
|
{
|
||||||
let mut insert_stmt = trans
|
let mut insert_stmt = trans
|
||||||
|
@ -53,10 +56,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
trans.commit().unwrap();
|
trans.commit().unwrap();
|
||||||
});
|
}
|
||||||
drop(tx);
|
|
||||||
|
|
||||||
collector.join().unwrap();
|
|
||||||
for thread in threads {
|
for thread in threads {
|
||||||
thread.join().unwrap();
|
thread.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue