From d531f8909d3ee47ea11a7a0dc60ea70819c16032 Mon Sep 17 00:00:00 2001 From: jakka Date: Tue, 30 Sep 2025 17:00:59 +0300 Subject: removed useless trait, reorganized code, isolated modules --- src/files.rs | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index afb42d7..3b00de1 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,3 +1,5 @@ +use crate::db; +use crate::flac::handle_encode; use anyhow::{Result, anyhow}; #[cfg(not(test))] use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; @@ -15,8 +17,6 @@ use std::{ }; use walkdir::WalkDir; -use crate::{db::Database, flac::handle_encode}; - #[cfg(not(test))] const BAR_TEMPLATE: &str = "{msg:<} [{wide_bar:.green/cyan}] Elapsed: {elapsed} {pos:>7}/{len:7}"; #[cfg(not(test))] @@ -51,25 +51,25 @@ impl Display for FileError { impl Error for FileError {} fn handle_file(file: &Path, conn: &Connection) -> Result<()> { - if conn.check_file(file)? { + if db::check_file(conn, file)? { let modtime = file .metadata()? .modified()? .duration_since(UNIX_EPOCH)? .as_secs(); - let db_modtime = conn.get_modtime(file)?; + let db_modtime = db::get_modtime(conn, file)?; if modtime != db_modtime { - conn.update_file(file)?; + db::update_file(conn, file)?; } return Ok(()); } - conn.insert_file(file)?; + db::insert_file(conn, file)?; Ok(()) } -pub fn index_files_recursively( +pub(crate) fn index_files_recursively( path: &Path, conn: &Connection, handler: Arc, @@ -129,16 +129,20 @@ pub fn index_files_recursively( Ok(()) } -pub fn reencode_files(conn: Connection, handler: Arc, threads: usize) -> Result<()> { +pub(crate) fn reencode_files( + conn: Connection, + handler: Arc, + threads: usize, +) -> Result<()> { #[cfg(not(test))] let bar = ProgressBar::with_draw_target( - Some(conn.get_toencode_number()?), + Some(db::get_toencode_number(&conn)?), ProgressDrawTarget::stdout_with_hz(60), ) .with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-")) .with_message("Reencoding"); - let mut files = conn.get_toencode_files()?.into_iter(); + let mut files = db::get_toencode_files(&conn)?.into_iter(); let lock = Arc::new(Mutex::new(conn)); @@ -170,7 +174,7 @@ pub fn reencode_files(conn: Connection, handler: Arc, threads: usize match handle_encode(&file, handler) { Err(error) => eprintln!("{}", FileError::new(&file, error)), Ok(false) => { - if let Err(error) = lock.lock().unwrap().update_file(&file) { + if let Err(error) = db::update_file(&lock.lock().unwrap(), &file) { eprintln!("{}", FileError::new(&file, error)); } #[cfg(not(test))] @@ -194,8 +198,8 @@ pub fn reencode_files(conn: Connection, handler: Arc, threads: usize Ok(()) } -pub fn clean_files(conn: &Connection, handler: Arc) -> Result<()> { - let files = conn.init_clean_files()?; +pub(crate) fn clean_files(conn: &Connection, handler: Arc) -> Result<()> { + let files = db::init_clean_files(conn)?; #[cfg(not(test))] let spinner = ProgressBar::with_draw_target(None, ProgressDrawTarget::stdout_with_hz(60)) @@ -206,7 +210,7 @@ pub fn clean_files(conn: &Connection, handler: Arc) -> Result<()> { files.iter().for_each(|file| { #[allow(clippy::collapsible_if)] if handler.load(Ordering::SeqCst) && !file.exists() { - if let Err(error) = conn.remove_file(file) { + if let Err(error) = db::remove_file(conn, file) { eprintln!("{}", FileError::new(file, error)) }; #[cfg(not(test))] @@ -216,7 +220,7 @@ pub fn clean_files(conn: &Connection, handler: Arc) -> Result<()> { #[cfg(not(test))] spinner.finish(); - conn.vacuum()?; + db::vacuum(conn)?; Ok(()) } @@ -229,7 +233,7 @@ mod tests { fn test_index_lots_of_files() { let dbname = PathBuf::from("temp3.db"); let handler = Arc::new(AtomicBool::new(true)); - let conn = Connection::new(Some(&dbname)).unwrap(); + let conn = db::init_connection(Some(&dbname)).unwrap(); index_files_recursively(Path::new("./testfiles"), &conn, handler).unwrap(); std::fs::remove_file(dbname).unwrap(); } @@ -238,7 +242,7 @@ mod tests { fn test_clean_files() { let dbname = PathBuf::from("temp4.db"); let handler = Arc::new(AtomicBool::new(true)); - let conn = Connection::new(Some(&dbname)).unwrap(); + let conn = db::init_connection(Some(&dbname)).unwrap(); let filenames = [ "./samples/16bit.flac", "./samples/24bit.flac", @@ -248,13 +252,13 @@ mod tests { std::fs::copy("./samples/32bit.flac", "./samples/nonexisting.flac").unwrap(); for file in filenames { let filename = PathBuf::from(file); - conn.insert_file(&filename).unwrap(); + db::insert_file(&conn, &filename).unwrap(); } std::fs::remove_file("./samples/nonexisting.flac").unwrap(); clean_files(&conn, handler).unwrap(); - let counter = conn.init_clean_files().unwrap().len(); + let counter = db::init_clean_files(&conn).unwrap().len(); std::fs::remove_file(dbname).unwrap(); assert!(counter == 3) } @@ -263,13 +267,13 @@ mod tests { fn test_reencode_lots_of_files() { let dbname = PathBuf::from("temp5.db"); let handler = Arc::new(AtomicBool::new(true)); - let conn = Connection::new(Some(&dbname)).unwrap(); + let conn = db::init_connection(Some(&dbname)).unwrap(); let temp = handler.clone(); index_files_recursively(Path::new("./testfiles"), &conn, temp).unwrap(); - println!("\n{}", conn.get_toencode_number().unwrap()); + println!("\n{}", db::get_toencode_number(&conn).unwrap()); reencode_files(conn, handler, 4).unwrap(); - let conn = Connection::new(Some(&dbname)).unwrap(); - println!("\n{}", conn.get_toencode_number().unwrap()); + let conn = db::init_connection(Some(&dbname)).unwrap(); + println!("\n{}", db::get_toencode_number(&conn).unwrap()); std::fs::remove_file(dbname).unwrap(); } } -- cgit v1.3.1 From d9871c81dd945ea188d9d7a31993b7bb1735d257 Mon Sep 17 00:00:00 2001 From: jakka Date: Wed, 8 Oct 2025 14:27:11 +0300 Subject: better file scanning logic - stopped walking down the filetree two times, now files are sent thru channels from a walker thread --- Cargo.lock | 56 +++++++++++++++++++++++++-------------------------- src/files.rs | 66 ++++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 66 insertions(+), 56 deletions(-) (limited to 'src/files.rs') diff --git a/Cargo.lock b/Cargo.lock index fbae675..a934c2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,7 +168,7 @@ dependencies = [ "libc", "once_cell", "unicode-width", - "windows-sys 0.61.1", + "windows-sys 0.61.2", ] [[package]] @@ -213,7 +213,7 @@ checksum = "881c5d0a13b2f1498e2306e82cbada78390e152d4b1378fb28a84f4dcd0dc4f3" dependencies = [ "dispatch", "nix", - "windows-sys 0.61.1", + "windows-sys 0.61.2", ] [[package]] @@ -234,7 +234,7 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys 0.61.1", + "windows-sys 0.61.2", ] [[package]] @@ -614,9 +614,9 @@ checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" [[package]] name = "unicode-width" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" [[package]] name = "unit-prefix" @@ -727,14 +727,14 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.1", + "windows-sys 0.61.2", ] [[package]] name = "windows-link" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" [[package]] name = "windows-sys" @@ -747,18 +747,18 @@ dependencies = [ [[package]] name = "windows-sys" -version = "0.61.1" +version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f109e41dd4a3c848907eb83d5a42ea98b3769495597450cf6d153507b166f0f" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ "windows-link", ] [[package]] name = "windows-targets" -version = "0.53.4" +version = "0.53.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d42b7b7f66d2a06854650af09cfdf8713e427a439c97ad65a6375318033ac4b" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" dependencies = [ "windows-link", "windows_aarch64_gnullvm", @@ -773,48 +773,48 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" [[package]] name = "windows_aarch64_msvc" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" [[package]] name = "windows_i686_gnu" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" [[package]] name = "windows_i686_gnullvm" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" [[package]] name = "windows_i686_msvc" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" [[package]] name = "windows_x86_64_gnu" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" [[package]] name = "windows_x86_64_gnullvm" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" [[package]] name = "windows_x86_64_msvc" -version = "0.53.0" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" diff --git a/src/files.rs b/src/files.rs index 3b00de1..995e938 100644 --- a/src/files.rs +++ b/src/files.rs @@ -11,6 +11,7 @@ use std::{ sync::{ Arc, Mutex, atomic::{AtomicBool, AtomicUsize, Ordering}, + mpsc, }, thread::{self, sleep}, time::{Duration, UNIX_EPOCH}, @@ -83,40 +84,49 @@ pub(crate) fn index_files_recursively( let bar = ProgressBar::with_draw_target(Some(0), ProgressDrawTarget::stdout_with_hz(60)) .with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-")) .with_message("Indexing"); + thread::scope(|s| { + let (filesend, filerecv) = mpsc::channel(); - for entry in WalkDir::new(&abspath) { - if handler.load(Ordering::SeqCst) { - let path = entry?.into_path(); - if !path.is_file() { - continue; - } - if path.extension().is_some_and(|x| x == "flac") { - #[cfg(not(test))] - bar.inc_length(1); - } - } else { - break; - } - } + #[cfg(not(test))] + let newbar = bar.clone(); - for entry in WalkDir::new(abspath) { - if handler.load(Ordering::SeqCst) { - let path = entry.unwrap().into_path(); - if !path.is_file() { - continue; - } - if path.extension().is_some_and(|x| x == "flac") { - if let Err(error) = handle_file(&path, conn) { - eprintln!("{}", FileError::new(&path, error)); + let newhandler = handler.clone(); + + s.spawn(move || { + for entry in WalkDir::new(&abspath) { + if newhandler.load(Ordering::SeqCst) { + if let Err(error) = entry { + #[cfg(not(test))] + newbar.println(format!("{}", error)); + } else { + let path = entry.unwrap().into_path(); + if !path.is_file() { + continue; + } + if path.extension().is_some_and(|x| x == "flac") { + let _ = filesend.send(path.to_owned()); + #[cfg(not(test))] + newbar.inc_length(1); + } + } } else { - #[cfg(not(test))] - bar.inc(1); + break; } } - } else { - break; + }); + + while let Ok(path) = filerecv.recv() + && handler.load(Ordering::SeqCst) + { + if let Err(error) = handle_file(&path, conn) { + #[cfg(not(test))] + bar.println(format!("{}", FileError::new(&path, error))); + } else { + #[cfg(not(test))] + bar.inc(1); + } } - } + }); #[cfg(not(test))] { -- cgit v1.3.1 From 6924f47d1d216698501a65c0ff9bec5019d8e05e Mon Sep 17 00:00:00 2001 From: jakka Date: Wed, 8 Oct 2025 14:49:46 +0300 Subject: handled lints --- src/files.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 995e938..63c87fa 100644 --- a/src/files.rs +++ b/src/files.rs @@ -92,6 +92,7 @@ pub(crate) fn index_files_recursively( let newhandler = handler.clone(); + #[allow(unused_variables)] s.spawn(move || { for entry in WalkDir::new(&abspath) { if newhandler.load(Ordering::SeqCst) { @@ -118,6 +119,7 @@ pub(crate) fn index_files_recursively( while let Ok(path) = filerecv.recv() && handler.load(Ordering::SeqCst) { + #[allow(unused_variables)] if let Err(error) = handle_file(&path, conn) { #[cfg(not(test))] bar.println(format!("{}", FileError::new(&path, error))); -- cgit v1.3.1