From 2ae34c91a739c4963b52c22027a16a2aa34a9561 Mon Sep 17 00:00:00 2001 From: jakka Date: Thu, 19 Jun 2025 23:28:13 +0300 Subject: added file checking when opening db from file. added colors to cleaning information and total indexed files. higher fps for progress bars --- src/db.rs | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 79099b0..6622010 100644 --- a/src/db.rs +++ b/src/db.rs @@ -3,7 +3,7 @@ use directories::BaseDirs; use futures_util::Stream; use libsql::{Builder, Connection, params}; use std::{ - path::{Path, PathBuf}, + path::Path, time::{Duration, UNIX_EPOCH}, }; @@ -26,10 +26,14 @@ pub struct Database(pub Connection); impl Database { pub async fn new(path: impl AsRef) -> Result { - let conn = Builder::new_local(path).build().await?.connect()?; - conn.execute(TABLE_CREATE, ()).await?; + if path.as_ref().is_file() { + let conn = Builder::new_local(path).build().await?.connect()?; + conn.execute(TABLE_CREATE, ()).await?; - Ok(Database(conn)) + Ok(Database(conn)) + } else { + Err(anyhow!("Not a file")) + } } pub async fn insert_file(&self, filename: impl AsRef) -> Result<()> { @@ -102,26 +106,17 @@ impl Database { } } - pub async fn clean_files(&self) -> Result<()> { - let mut tasks = tokio::task::JoinSet::new(); + pub async fn init_clean_files( + &self, + ) -> Result>> { self.0.execute(DEDUPE_DB, ()).await?; - let mut query_res = self.0.query(FETCH_FILES, ()).await?; - while let Ok(Some(row)) = query_res.next().await { - let path = PathBuf::from(row.get_str(0)?); - let conn = self.0.clone(); - tasks.spawn(async move { - if !path.exists() { - let _ = conn - .execute(REMOVE_FILE, params!(path.to_str().unwrap())) - .await; - } - }); - } - - tasks.join_all().await; - - self.0.execute("VACUUM", ()).await?; + Ok(self.0.query(FETCH_FILES, ()).await?.into_stream()) + } + pub async fn remove_file(&self, filename: impl AsRef) -> Result<()> { + self.0 + .execute(REMOVE_FILE, params!(filename.as_ref().to_str().unwrap())) + .await?; Ok(()) } -- cgit v1.3.1