性能优化
This commit is contained in:
@@ -13,8 +13,10 @@ use serde::Serialize;
|
||||
use tauri::{AppHandle, Manager};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use specta::Type;
|
||||
|
||||
/// 单个文件记录(返回给前端)
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone, Serialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileRecord {
|
||||
pub path: String,
|
||||
@@ -25,7 +27,7 @@ pub struct FileRecord {
|
||||
}
|
||||
|
||||
/// 索引状态(返回给前端)
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone, Serialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct IndexStats {
|
||||
pub total: i64,
|
||||
@@ -61,7 +63,7 @@ pub fn init(app: &AppHandle) {
|
||||
let conn = match Connection::open(&path) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
eprintln!("[quickpanel] 文件索引 DB 初始化失败: {}", e);
|
||||
crate::logger::log_error("quickpanel", &format!("文件索引 DB 初始化失败: {}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -84,11 +86,11 @@ pub fn init(app: &AppHandle) {
|
||||
);",
|
||||
);
|
||||
|
||||
let mut guard = index_slot().lock().unwrap();
|
||||
let mut guard = index_slot().lock().unwrap_or_else(|e| e.into_inner());
|
||||
*guard = Some(Inner {
|
||||
conn: Mutex::new(conn),
|
||||
});
|
||||
eprintln!("[quickpanel] 文件索引 DB 已就绪: {}", path.display());
|
||||
crate::logger::log_info("quickpanel", &format!("文件索引 DB 已就绪: {}", path.display()));
|
||||
}
|
||||
|
||||
/// 判断索引是否已初始化
|
||||
@@ -96,7 +98,7 @@ fn with_conn<F, R>(f: F) -> Option<R>
|
||||
where
|
||||
F: FnOnce(&Connection) -> R,
|
||||
{
|
||||
let guard = index_slot().lock().unwrap();
|
||||
let guard = index_slot().lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(inner) = guard.as_ref() {
|
||||
if let Ok(conn) = inner.conn.lock() {
|
||||
return Some(f(&conn));
|
||||
@@ -140,7 +142,7 @@ pub fn build_index(dirs: &[String]) -> Result<i64, String> {
|
||||
// 启动/刷新 notify 监听器
|
||||
start_watcher(dirs);
|
||||
|
||||
eprintln!("[quickpanel] 文件索引完成,共 {} 条", count);
|
||||
crate::logger::log_info("quickpanel", &format!("文件索引完成,共 {} 条", count));
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
@@ -203,10 +205,13 @@ fn upsert_path(p: &Path) -> bool {
|
||||
fn remove_path(p: &Path) {
|
||||
let path_str = p.to_string_lossy().to_string();
|
||||
let _ = with_conn(|conn| {
|
||||
// 删除该路径及其子项(目录被删除时,子文件也失效)
|
||||
// 删除该路径本身及其直接子项(目录被删除时,子文件也失效)。
|
||||
// 用"路径 + 分隔符"的前缀匹配(而非裸前缀),避免误删 dir2/directory 等兄弟目录。
|
||||
let backslash_prefix = format!("{}\\{}", path_str, "%");
|
||||
let slash_prefix = format!("{}/{}", path_str, "%");
|
||||
conn.execute(
|
||||
"DELETE FROM files WHERE path = ?1 OR path LIKE ?2",
|
||||
params![path_str, format!("{}%", path_str)],
|
||||
"DELETE FROM files WHERE path = ?1 OR path LIKE ?2 OR path LIKE ?3",
|
||||
params![path_str, backslash_prefix, slash_prefix],
|
||||
)
|
||||
});
|
||||
}
|
||||
@@ -306,7 +311,7 @@ pub fn start_watcher(dirs: &[String]) {
|
||||
) {
|
||||
Ok(w) => w,
|
||||
Err(e) => {
|
||||
eprintln!("[quickpanel] notify watcher 创建失败: {}", e);
|
||||
crate::logger::log_error("quickpanel", &format!("notify watcher 创建失败: {}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -318,14 +323,14 @@ pub fn start_watcher(dirs: &[String]) {
|
||||
continue;
|
||||
}
|
||||
if let Err(e) = watcher.watch(path, RecursiveMode::Recursive) {
|
||||
eprintln!("[quickpanel] watch {} 失败: {}", dir, e);
|
||||
crate::logger::log_error("quickpanel", &format!("watch {} 失败: {}", dir, e));
|
||||
}
|
||||
}
|
||||
|
||||
// 替换旧 watcher(drop 时自动 unwatch)
|
||||
let mut guard = watcher_slot().lock().unwrap();
|
||||
let mut guard = watcher_slot().lock().unwrap_or_else(|e| e.into_inner());
|
||||
*guard = Some(watcher);
|
||||
eprintln!("[quickpanel] notify 监听已启动,监听 {} 个目录", dirs.len());
|
||||
crate::logger::log_info("quickpanel", &format!("notify 监听已启动,监听 {} 个目录", dirs.len()));
|
||||
}
|
||||
|
||||
/// 处理文件系统事件:创建/修改 → upsert,删除 → remove,重命名 → remove + upsert
|
||||
@@ -348,9 +353,3 @@ fn handle_fs_event(event: ¬ify::Event) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 停止 notify 监听器
|
||||
pub fn stop_watcher() {
|
||||
let mut guard = watcher_slot().lock().unwrap();
|
||||
*guard = None;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user