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/db.rs | 130 ++++++++++++++++++++++++++++------------------------------- src/files.rs | 108 ++++++++++++++++++++++--------------------------- src/main.rs | 17 +++----- 3 files changed, 116 insertions(+), 139 deletions(-) (limited to 'src') diff --git a/src/db.rs b/src/db.rs index 86f2083..92bfecd 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,8 +1,6 @@ use anyhow::{Result, anyhow}; use directories::BaseDirs; -use r2d2::{Pool, PooledConnection}; -use r2d2_sqlite::SqliteConnectionManager; -use rusqlite::params; +use rusqlite::{Connection, params}; use std::{ path::{Path, PathBuf}, time::UNIX_EPOCH, @@ -22,36 +20,36 @@ const DEDUPE_DB: &str = "DELETE FROM flacs WHERE rowid NOT IN (SELECT MAX(rowid) FROM flacs GROUP BY path)"; const GET_MODTIME: &str = "SELECT modtime FROM flacs WHERE path = ?1"; -pub fn open_db( - path: Option>, - threads: usize, -) -> Result> { - if let Some(file) = path { - let manager = SqliteConnectionManager::file(file); - let pool = Pool::builder().max_size(threads as u32).build(manager)?; - let conn = pool.get()?; - conn.execute(TABLE_CREATE, ())?; - Ok(pool) - } else if let Some(base_dir) = BaseDirs::new() { - let file = Path::new(base_dir.data_dir()).join("reencoder.db"); - let manager = SqliteConnectionManager::file(file); - let pool = Pool::builder().max_size(threads as u32).build(manager)?; - let conn = pool.get()?; - conn.execute(TABLE_CREATE, ())?; - Ok(pool) - } else { - Err(anyhow!("Failed to locate data directory")) - } +pub trait Database { + type Conn; + fn new(path: Option>) -> Result; + fn insert_file(&self, filename: impl AsRef) -> Result<()>; + fn update_file(&self, filename: impl AsRef) -> Result<()>; + fn check_file(&self, filename: impl AsRef) -> Result; + fn init_clean_files(&self) -> Result, rusqlite::Error>; + fn remove_file(&self, filename: impl AsRef) -> Result<()>; + fn get_toencode_files(&self) -> Result, rusqlite::Error>; + fn get_toencode_number(&self) -> Result; + fn get_modtime(&self, file: impl AsRef) -> Result; + fn vacuum(&self) -> Result<()>; } -pub struct Database(pub PooledConnection); - -impl Database { - pub fn new(conn: PooledConnection) -> Self { - Database(conn) +impl Database for Connection { + type Conn = Connection; + fn new(path: Option>) -> Result { + let conn = if let Some(file) = path { + Connection::open(file)? + } else if let Some(base_dir) = BaseDirs::new() { + let file = Path::new(base_dir.data_dir()).join("reencoder.db"); + Connection::open(file)? + } else { + return Err(anyhow!("Failed to locate data directory")); + }; + conn.execute(TABLE_CREATE, ())?; + Ok(conn) } - pub fn insert_file(&self, filename: impl AsRef) -> Result<()> { + fn insert_file(&self, filename: impl AsRef) -> Result<()> { let toencode = !matches!(get_vendor(&filename)?.as_str(), CURRENT_VENDOR); let modtime = filename @@ -61,7 +59,7 @@ impl Database { .duration_since(UNIX_EPOCH)? .as_secs(); - self.0.execute( + self.execute( ADD_NEW_ITEM, params![filename.as_ref().to_str().unwrap(), toencode, modtime], )?; @@ -69,7 +67,7 @@ impl Database { Ok(()) } - pub fn update_file(&self, filename: impl AsRef) -> Result<()> { + fn update_file(&self, filename: impl AsRef) -> Result<()> { let modtime = filename .as_ref() .metadata()? @@ -77,7 +75,7 @@ impl Database { .duration_since(UNIX_EPOCH)? .as_secs(); - self.0.execute( + self.execute( REPLACE_ITEM, params![filename.as_ref().to_str().unwrap(), false, modtime], )?; @@ -85,8 +83,8 @@ impl Database { Ok(()) } - pub fn check_file(&self, filename: impl AsRef) -> Result { - if self.0.query_one( + fn check_file(&self, filename: impl AsRef) -> Result { + if self.query_one( CHECK_FILE, params!(filename.as_ref().to_str().unwrap()), |row| { @@ -100,9 +98,9 @@ impl Database { } } - pub fn init_clean_files(&self) -> Result, rusqlite::Error> { - self.0.execute(DEDUPE_DB, ())?; - let mut stmt = self.0.prepare(FETCH_FILES)?; + fn init_clean_files(&self) -> Result, rusqlite::Error> { + self.execute(DEDUPE_DB, ())?; + let mut stmt = self.prepare(FETCH_FILES)?; let mut rows = stmt.query(())?; let mut files = Vec::new(); while let Ok(Some(row)) = rows.next() { @@ -112,14 +110,13 @@ impl Database { Ok(files) } - pub fn remove_file(&self, filename: impl AsRef) -> Result<()> { - self.0 - .execute(REMOVE_FILE, params!(filename.as_ref().to_str().unwrap()))?; + fn remove_file(&self, filename: impl AsRef) -> Result<()> { + self.execute(REMOVE_FILE, params!(filename.as_ref().to_str().unwrap()))?; Ok(()) } - pub fn get_toencode_files(&self) -> Result, rusqlite::Error> { - let mut stmt = self.0.prepare(TOENCODE_QUERY)?; + fn get_toencode_files(&self) -> Result, rusqlite::Error> { + let mut stmt = self.prepare(TOENCODE_QUERY)?; let mut rows = stmt.query(())?; let mut files: Vec = Vec::new(); while let Ok(Some(row)) = rows.next() { @@ -129,15 +126,15 @@ impl Database { Ok(files) } - pub fn get_toencode_number(&self) -> Result { - self.0.query_one(TOENCODE_NUMBER, (), |row| { + fn get_toencode_number(&self) -> Result { + self.query_one(TOENCODE_NUMBER, (), |row| { let num: u64 = row.get(0)?; Ok(num) }) } - pub fn get_modtime(&self, file: impl AsRef) -> Result { - Ok(self.0.query_one( + fn get_modtime(&self, file: impl AsRef) -> Result { + Ok(self.query_one( GET_MODTIME, params![file.as_ref().to_str().unwrap()], |row| { @@ -147,8 +144,8 @@ impl Database { )?) } - pub fn vaccum(&self) -> Result<()> { - self.0.execute("VACUUM", ())?; + fn vacuum(&self) -> Result<()> { + self.execute("VACUUM", ())?; Ok(()) } } @@ -163,12 +160,11 @@ mod tests { let dbname = String::from("temp1.db"); let filenames = ["16bit.flac", "24bit.flac", "32bit.flac"]; let mut counter = 0; - let pool = open_db(Some(&dbname), 10).unwrap(); - let conn = Database::new(pool.get().unwrap()); + let conn = Connection::new(Some(&dbname)).unwrap(); for file in filenames { conn.insert_file(&file.to_string()).unwrap(); } - let mut stmt = conn.0.prepare(TOENCODE_QUERY).unwrap(); + let mut stmt = conn.prepare(TOENCODE_QUERY).unwrap(); let mut returned = stmt.query(()).unwrap(); while let Ok(Some(_)) = returned.next() { @@ -182,27 +178,25 @@ mod tests { fn check_update() { let dbname = String::from("temp2.db"); let filenames = ["16bit.flac", "24bit.flac", "32bit.flac"]; - let pool = open_db(Some(&dbname), 10).unwrap(); - let conn = Database::new(pool.get().unwrap()); + let conn = Connection::new(Some(&dbname)).unwrap(); for file in filenames { conn.insert_file(Path::new(file).canonicalize().unwrap()) .unwrap(); } - conn.0 - .execute( - REPLACE_ITEM, - params![ - Path::new("16bit.flac") - .canonicalize() - .unwrap() - .to_str() - .unwrap(), - true, - "" - ], - ) - .unwrap(); + conn.execute( + REPLACE_ITEM, + params![ + Path::new("16bit.flac") + .canonicalize() + .unwrap() + .to_str() + .unwrap(), + true, + "" + ], + ) + .unwrap(); conn.update_file( Path::new("16bit.flac") @@ -213,7 +207,7 @@ mod tests { ) .unwrap(); - let mut stmt = conn.0.prepare(TOENCODE_QUERY).unwrap(); + let mut stmt = conn.prepare(TOENCODE_QUERY).unwrap(); let mut returned = stmt.query(()).unwrap(); let mut counter = 0; while let Ok(Some(_)) = returned.next() { 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 Result<()> { r.store(false, Ordering::SeqCst); })?; - let threads = *args.get_one::("threads").unwrap(); - - let dbpool = db::open_db(args.get_one::("db"), threads)?; + let conn = Connection::new(args.get_one::("db"))?; let path = args.get_one::("path"); if path.is_none() && !args.get_flag("clean") && !args.get_flag("doit") { - let conn = Database::new(dbpool.get()?); let count = conn.get_toencode_number()?; println!("Files to reencode:\t{}", style(count).green()); return Ok(()); } - let pool = rayon::ThreadPoolBuilder::new() - .num_threads(threads) - .build()?; - if let Some(realpath) = path { let hanlder = running.clone(); - files::index_files_recursively(realpath, &dbpool, hanlder)?; + files::index_files_recursively(realpath, &conn, hanlder)?; } if args.get_flag("clean") { let handler = running.clone(); - pool.install(|| files::clean_files(&dbpool, handler))?; + files::clean_files(&conn, handler)?; } if args.get_flag("doit") { let hanlder = running.clone(); - pool.install(|| files::reencode_files(&dbpool, hanlder))?; + let threads = *args.get_one::("threads").unwrap(); + files::reencode_files(conn, hanlder, threads)?; } Ok::<(), anyhow::Error>(()) } -- cgit v1.3.1