优化,贴图
This commit is contained in:
@@ -114,6 +114,8 @@ pub async fn quickpanel_build_file_index(app: AppHandle) -> Result<i64, String>
|
||||
} else {
|
||||
settings.index_dirs
|
||||
};
|
||||
// 懒加载:首次构建时自动初始化 DB 连接
|
||||
file_index::ensure_initialized(&app);
|
||||
// 阻塞操作放到 spawn_blocking
|
||||
tauri::async_runtime::spawn_blocking(move || file_index::build_index(&dirs))
|
||||
.await
|
||||
@@ -126,7 +128,10 @@ pub async fn quickpanel_build_file_index(app: AppHandle) -> Result<i64, String>
|
||||
pub async fn quickpanel_search_files(
|
||||
query: String,
|
||||
limit: Option<i64>,
|
||||
app: AppHandle,
|
||||
) -> Result<Vec<file_index::FileRecord>, String> {
|
||||
// 懒加载:首次搜索时自动初始化 DB 连接
|
||||
file_index::ensure_initialized(&app);
|
||||
tauri::async_runtime::spawn_blocking(move || file_index::search(&query, limit.unwrap_or(50)))
|
||||
.await
|
||||
.map_err(|e| format!("搜索任务失败: {}", e))
|
||||
@@ -135,7 +140,9 @@ pub async fn quickpanel_search_files(
|
||||
/// 获取索引状态
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn quickpanel_file_index_stats() -> file_index::IndexStats {
|
||||
pub async fn quickpanel_file_index_stats(app: AppHandle) -> file_index::IndexStats {
|
||||
// 懒加载:查询状态前确保 DB 已初始化(未初始化时 stats 返回全 0)
|
||||
file_index::ensure_initialized(&app);
|
||||
file_index::stats()
|
||||
}
|
||||
|
||||
|
||||
@@ -93,6 +93,17 @@ pub fn init(app: &AppHandle) {
|
||||
crate::logger::log_info("quickpanel", &format!("文件索引 DB 已就绪: {}", path.display()));
|
||||
}
|
||||
|
||||
/// 懒加载:首次访问文件索引时自动初始化(若尚未初始化)。
|
||||
/// 避免应用启动时即打开 SQLite 连接,降低启动 IO 开销。
|
||||
pub fn ensure_initialized(app: &AppHandle) {
|
||||
let guard = index_slot().lock().unwrap_or_else(|e| e.into_inner());
|
||||
if guard.is_some() {
|
||||
return;
|
||||
}
|
||||
drop(guard);
|
||||
init(app);
|
||||
}
|
||||
|
||||
/// 判断索引是否已初始化
|
||||
fn with_conn<F, R>(f: F) -> Option<R>
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user