diff options
| author | jakka <jakkadoujin@gmail.com> | 2025-07-07 13:18:57 +0300 |
|---|---|---|
| committer | jakka <jakkadoujin@gmail.com> | 2025-07-07 13:18:57 +0300 |
| commit | fccc22cd4a4a84211d8b61cf561d0f4463f500f1 (patch) | |
| tree | d36b9ae813a8217cb3cd723223c2c6bd59075cbb /src/files.rs | |
| parent | c8c1c4daa8579fe0462bdbd8f5437cca78d3348e (diff) | |
better threading handling
Diffstat (limited to 'src/files.rs')
| -rw-r--r-- | src/files.rs | 53 |
1 files changed, 19 insertions, 34 deletions
diff --git a/src/files.rs b/src/files.rs index 6b5c20e..9793243 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,6 +1,7 @@ use anyhow::{Result, anyhow}; #[cfg(not(test))] use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; +use rayon::prelude::*; use rusqlite::Connection; use std::{ error::Error, @@ -133,7 +134,7 @@ pub fn index_files_recursively( Ok(()) } -pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>, threads: usize) -> Result<()> { +pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>) -> Result<()> { #[cfg(not(test))] let bar = ProgressBar::with_draw_target( Some(conn.get_toencode_number()?), @@ -144,41 +145,25 @@ pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>, threads: usize let files = conn.get_toencode_files()?; - let pool = rayon::ThreadPoolBuilder::new() - .num_threads(threads) - .build()?; - let lock = Arc::new(Mutex::new(conn)); - pool.scope(|scope| { - for file in files { - if handler.load(Ordering::SeqCst) { - scope.spawn(|_| { - let newconn = if let Ok(conn) = lock.lock() { - conn - } else { - eprintln!("Error setting up lock on file:\t{}", file.to_string_lossy()); - return; - }; - if !file.exists() { - let _ = newconn.remove_file(&file); - #[cfg(not(test))] - bar.dec_length(1); - return; - } - - if let Err(error) = handle_encode(&file) { - eprintln!("{}", FileError::new(&file, error)); - } else { - if let Err(error) = newconn.update_file(&file) { - eprintln!("{}", FileError::new(file, error)); - } - #[cfg(not(test))] - bar.inc(1) - } - }); + files.par_iter().for_each(|file| { + if handler.load(Ordering::SeqCst) { + let conn = match lock.lock() { + Ok(conn) => conn, + Err(_) => { + eprintln!("Lock poisoned on file:\t{}", file.to_string_lossy()); + return; + } + }; + if let Err(error) = handle_encode(file) { + eprintln!("{}", FileError::new(file, error)); } else { - break; + if let Err(error) = conn.update_file(file) { + eprintln!("{}", FileError::new(file, error)); + } + #[cfg(not(test))] + bar.inc(1) } } }); @@ -258,7 +243,7 @@ mod tests { let temp = handler.clone(); index_files_recursively(Path::new("./testfiles"), &conn, temp).unwrap(); println!("\n{}", conn.get_toencode_number().unwrap()); - reencode_files(conn, handler, 2).unwrap(); + reencode_files(conn, handler).unwrap(); let conn = Connection::new(Some(&dbname)).unwrap(); println!("\n{}", conn.get_toencode_number().unwrap()); std::fs::remove_file(dbname).unwrap(); |
