From c8c1c4daa8579fe0462bdbd8f5437cca78d3348e Mon Sep 17 00:00:00 2001 From: jakka Date: Mon, 7 Jul 2025 13:02:58 +0300 Subject: removed unneeded deps, cleaned up code a bit --- src/files.rs | 108 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 48 insertions(+), 60 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 46d7727..6b5c20e 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,15 +1,13 @@ use anyhow::{Result, anyhow}; #[cfg(not(test))] use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; -use r2d2::Pool; -use r2d2_sqlite::SqliteConnectionManager; -use rayon::prelude::*; +use rusqlite::Connection; use std::{ error::Error, fmt::Display, path::{Path, PathBuf}, sync::{ - Arc, + Arc, Mutex, atomic::{AtomicBool, Ordering}, }, time::UNIX_EPOCH, @@ -51,7 +49,7 @@ impl Display for FileError { impl Error for FileError {} -fn handle_file(file: impl AsRef, conn: &Database) -> Result<()> { +fn handle_file(file: impl AsRef, conn: &Connection) -> Result<()> { if conn.check_file(&file)? { let modtime = file .as_ref() @@ -77,7 +75,7 @@ fn handle_file(file: impl AsRef, conn: &Database) -> Result<()> { pub fn index_files_recursively( path: impl AsRef, - pool: &Pool, + conn: &Connection, handler: Arc, ) -> Result<()> { if !path.as_ref().is_dir() { @@ -105,7 +103,6 @@ pub fn index_files_recursively( } } - let conn = Database::new(pool.get()?); for entry in WalkDir::new(abspath) { if handler.load(Ordering::SeqCst) { let path = entry.unwrap().into_path(); @@ -113,7 +110,7 @@ pub fn index_files_recursively( continue; } if path.extension().is_some_and(|x| x == "flac") { - if let Err(error) = handle_file(&path, &conn) { + if let Err(error) = handle_file(&path, conn) { eprintln!("{}", FileError::new(path, error)); } else { #[cfg(not(test))] @@ -136,11 +133,7 @@ pub fn index_files_recursively( Ok(()) } -pub fn reencode_files( - pool: &Pool, - handler: Arc, -) -> Result<()> { - let conn = Database::new(pool.get()?); +pub fn reencode_files(conn: Connection, handler: Arc, threads: usize) -> Result<()> { #[cfg(not(test))] let bar = ProgressBar::with_draw_target( Some(conn.get_toencode_number()?), @@ -150,33 +143,42 @@ pub fn reencode_files( .with_message("Reencoding"); let files = conn.get_toencode_files()?; - drop(conn); - files.par_iter().for_each(|file| { - if handler.load(Ordering::SeqCst) { - let conn = match pool.get() { - Ok(conn) => Database::new(conn), - Err(error) => { - eprintln!("{}", FileError::new(file, error.into())); - return; - } - }; + let pool = rayon::ThreadPoolBuilder::new() + .num_threads(threads) + .build()?; - if !file.exists() { - let _ = conn.remove_file(file); - #[cfg(not(test))] - bar.dec_length(1); - return; - } + 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)); + 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) + } + }); } else { - if let Err(error) = conn.update_file(file) { - eprintln!("{}", FileError::new(file, error)); - } - #[cfg(not(test))] - bar.inc(1) + break; } } }); @@ -192,10 +194,8 @@ pub fn reencode_files( Ok(()) } -pub fn clean_files(pool: &Pool, handler: Arc) -> Result<()> { - let conn = Database::new(pool.get()?); +pub fn clean_files(conn: &Connection, handler: Arc) -> Result<()> { let files = conn.init_clean_files()?; - drop(conn); #[cfg(not(test))] let spinner = ProgressBar::with_draw_target(None, ProgressDrawTarget::stdout_with_hz(60)) @@ -203,13 +203,6 @@ pub fn clean_files(pool: &Pool, handler: Arc Database::new(conn), - Err(error) => { - eprintln!("{}", FileError::new(file, error.into())); - return; - } - }; if let Err(error) = conn.remove_file(file) { eprintln!("{}", FileError::new(file, error)) }; @@ -220,8 +213,7 @@ pub fn clean_files(pool: &Pool, handler: Arc, handler: Arc