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 --- src/files.rs | 66 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 28 deletions(-) (limited to 'src') 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