update code level
This commit is contained in:
@@ -226,6 +226,13 @@ impl Database {
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// 将所有相册重置为未处理状态(processed_at = NULL),下次管线会重新解析全部相册
|
||||
pub async fn reset_all_processed_at(&self) -> usize {
|
||||
sqlx::query("UPDATE albums SET processed_at = NULL")
|
||||
.execute(&self.pool).await.unwrap_or_default()
|
||||
.rows_affected() as usize
|
||||
}
|
||||
|
||||
pub async fn delete_album(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
let _guard = self.write_lock.lock().await;
|
||||
let aid = album_id.to_string();
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -216,9 +216,15 @@ async fn main() -> std::io::Result<()> {
|
||||
println!(" - GET /api/migration/stats - 迁移状态");
|
||||
println!(" - POST /api/migration/run?path=persons.db - 执行迁移");
|
||||
println!(" - GET /api/queue/stats - 队列状态");
|
||||
println!("管线接口:");
|
||||
println!(" - POST /api/pipeline/run - 执行管线(增量)");
|
||||
println!(" - POST /api/pipeline/reparse - 重置并重新解析全部相册(标签+人物)");
|
||||
println!(" - POST /api/pipeline/reparse-persons - 重置并重新解析全部人物");
|
||||
println!(" - POST /api/pipeline/reparse-tags - 重置并重新解析全部标签");
|
||||
println!("词典管理:");
|
||||
println!(" - GET /api/dict/stats - 词典状态");
|
||||
println!(" - POST /api/dict/reload - 热重载词典");
|
||||
println!(" - POST /api/dict/update - 批量更新 dict 表(自动重载内存词典)");
|
||||
println!("缓存管理:");
|
||||
println!(" - POST /api/cache/refresh - 手动刷新内存缓存");
|
||||
println!("按 Ctrl+C 停止服务");
|
||||
@@ -270,11 +276,15 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/api/migration/run", web::post().to(routes::migration_run))
|
||||
// 管线 API
|
||||
.route("/api/pipeline/run", web::post().to(routes::pipeline_run))
|
||||
.route("/api/pipeline/reparse", web::post().to(routes::pipeline_reparse))
|
||||
.route("/api/pipeline/reparse-persons", web::post().to(routes::pipeline_reparse_persons))
|
||||
.route("/api/pipeline/reparse-tags", web::post().to(routes::pipeline_reparse_tags))
|
||||
// 队列 API
|
||||
.route("/api/queue/stats", web::get().to(routes::get_queue_stats))
|
||||
// 词典 API
|
||||
.route("/api/dict/stats", web::get().to(routes::dict_stats))
|
||||
.route("/api/dict/reload", web::post().to(routes::dict_reload))
|
||||
.route("/api/dict/update", web::post().to(routes::dict_update))
|
||||
// 缓存管理 API
|
||||
.route("/api/cache/refresh", web::post().to(routes::cache_refresh))
|
||||
// 静态文件
|
||||
|
||||
@@ -41,3 +41,42 @@ pub async fn dict_reload(search_db: web::Data<Arc<SearchDb>>) -> impl Responder
|
||||
"message": msg,
|
||||
}))
|
||||
}
|
||||
|
||||
/// dict 条目请求体
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DictEntryInput {
|
||||
pub word: String,
|
||||
pub type_: String,
|
||||
pub category: Option<String>,
|
||||
pub freq: Option<i64>,
|
||||
pub merged_to: Option<String>,
|
||||
}
|
||||
|
||||
/// POST /api/dict/update — 更新 dict 表(新增或覆盖条目)
|
||||
pub async fn dict_update(
|
||||
search_db: web::Data<Arc<SearchDb>>,
|
||||
body: web::Json<Vec<DictEntryInput>>,
|
||||
) -> impl Responder {
|
||||
let entries = body.into_inner();
|
||||
let mut inserted = 0usize;
|
||||
for e in &entries {
|
||||
match search_db.upsert_dict_entry(
|
||||
&e.word, &e.type_, e.category.as_deref(), e.freq, e.merged_to.as_deref(),
|
||||
).await {
|
||||
Ok(_) => inserted += 1,
|
||||
Err(err) => {
|
||||
return HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"success": false,
|
||||
"message": format!("更新词条 '{}' 失败: {}", e.word, err),
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 自动重载内存词典
|
||||
let msg = tag_dict::reload_from_search_db(search_db.get_ref()).await;
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": format!("更新 {} 条字典条目,{}", inserted, msg),
|
||||
"updated": inserted,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -37,4 +37,111 @@ pub async fn pipeline_run(
|
||||
result.persons_merged, result.aliases_moved, result.tags_removed_person, synced
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
/// POST /api/pipeline/reparse — 重置所有相册为未处理状态,重新解析全部标签和人物并同步到 search.db
|
||||
pub async fn pipeline_reparse(
|
||||
db: web::Data<Arc<Database>>,
|
||||
search_db: web::Data<Arc<SearchDb>>,
|
||||
) -> impl Responder {
|
||||
// 1. 清除 search.db 中的旧关联(不影响 tags/persons 字典)
|
||||
sqlx::query("DELETE FROM album_tags").execute(&search_db.pool).await.ok();
|
||||
sqlx::query("DELETE FROM album_persons").execute(&search_db.pool).await.ok();
|
||||
|
||||
// 2. 重置所有相册为未处理
|
||||
let reset_count = db.reset_all_processed_at().await;
|
||||
tracing::info!("[reparse] 重置 {} 个相册的 processed_at", reset_count);
|
||||
|
||||
// 3. 重新运行完整管线+同步
|
||||
let result = engine::pipeline::run_pipeline(db.get_ref(), search_db.get_ref()).await;
|
||||
let synced = engine::pipeline::sync_albums_to_search_db(db.get_ref(), search_db.get_ref()).await;
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"albums_parsed": result.new_albums_found,
|
||||
"persons_updated": result.persons_updated,
|
||||
"tags_updated": result.tags_updated,
|
||||
"synced_to_search_db": synced,
|
||||
"message": format!(
|
||||
"重新解析完成:重置{}个相册,解析{}个,更新人物{}个,标签{}个,同步{}个到search.db",
|
||||
reset_count, result.new_albums_found,
|
||||
result.persons_updated, result.tags_updated, synced,
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
/// POST /api/pipeline/reparse-persons — 重置所有相册,重新解析全部人物
|
||||
///
|
||||
/// 流程:
|
||||
/// 清空 search.db 的 album_persons → 重置所有相册 processed_at →
|
||||
/// 运行完整管线(人物相关逻辑全部执行)→ 全量同步到 search.db
|
||||
pub async fn pipeline_reparse_persons(
|
||||
db: web::Data<Arc<Database>>,
|
||||
search_db: web::Data<Arc<SearchDb>>,
|
||||
) -> impl Responder {
|
||||
// 1. 清除 search.db 中的人物关联(保留 tags 不变)
|
||||
sqlx::query("DELETE FROM album_persons").execute(&search_db.pool).await.ok();
|
||||
|
||||
// 2. 重置所有相册为未处理(触发管线重新解析)
|
||||
let reset_count = db.reset_all_processed_at().await;
|
||||
tracing::info!("[reparse-persons] 重置 {} 个相册的 processed_at", reset_count);
|
||||
|
||||
// 3. 重新运行完整管线(Phase ② 人物清理 + Phase ③ 解析 + Phase ④ 人物合并 + Phase C 重计)
|
||||
let result = engine::pipeline::run_pipeline(db.get_ref(), search_db.get_ref()).await;
|
||||
|
||||
// 4. 全量同步到 search.db(tags 和 persons 都会同步,但 album_persons 已清空确保了干净状态)
|
||||
let synced = engine::pipeline::sync_albums_to_search_db(db.get_ref(), search_db.get_ref()).await;
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"albums_parsed": result.new_albums_found,
|
||||
"persons_updated": result.persons_updated,
|
||||
"persons_merged": result.persons_merged,
|
||||
"aliases_moved": result.aliases_moved,
|
||||
"non_persons_deleted": result.non_persons_deleted,
|
||||
"stop_word_persons_removed": result.stop_word_persons_removed,
|
||||
"synced_to_search_db": synced,
|
||||
"message": format!(
|
||||
"人物重新解析完成:重置{}个相册,解析{}个,更新人物{}个,合并{}个,迁移别名{}个,删除非人物{},清除重叠{},同步{}个到search.db",
|
||||
reset_count, result.new_albums_found,
|
||||
result.persons_updated, result.persons_merged, result.aliases_moved,
|
||||
result.non_persons_deleted, result.stop_word_persons_removed, synced,
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
/// POST /api/pipeline/reparse-tags — 重置所有相册,重新解析全部标签
|
||||
///
|
||||
/// 流程:
|
||||
/// 清空 search.db 的 album_tags → 重置所有相册 processed_at →
|
||||
/// 运行完整管线(标签相关逻辑全部执行)→ 全量同步到 search.db
|
||||
pub async fn pipeline_reparse_tags(
|
||||
db: web::Data<Arc<Database>>,
|
||||
search_db: web::Data<Arc<SearchDb>>,
|
||||
) -> impl Responder {
|
||||
// 1. 清除 search.db 中的标签关联(保留 persons 不变)
|
||||
sqlx::query("DELETE FROM album_tags").execute(&search_db.pool).await.ok();
|
||||
|
||||
// 2. 重置所有相册为未处理(触发管线重新解析)
|
||||
let reset_count = db.reset_all_processed_at().await;
|
||||
tracing::info!("[reparse-tags] 重置 {} 个相册的 processed_at", reset_count);
|
||||
|
||||
// 3. 重新运行完整管线(Phase ③ 解析会重新提取全部标签)
|
||||
let result = engine::pipeline::run_pipeline(db.get_ref(), search_db.get_ref()).await;
|
||||
|
||||
// 4. 全量同步到 search.db
|
||||
let synced = engine::pipeline::sync_albums_to_search_db(db.get_ref(), search_db.get_ref()).await;
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"albums_parsed": result.new_albums_found,
|
||||
"tags_updated": result.tags_updated,
|
||||
"short_tags_removed": result.short_tags_removed,
|
||||
"synced_to_search_db": synced,
|
||||
"message": format!(
|
||||
"标签重新解析完成:重置{}个相册,解析{}个,更新标签{}个,移除短标签{},同步{}个到search.db",
|
||||
reset_count, result.new_albums_found,
|
||||
result.tags_updated, result.short_tags_removed, synced,
|
||||
)
|
||||
}))
|
||||
}
|
||||
@@ -295,6 +295,26 @@ impl SearchDb {
|
||||
self.load_dict().await
|
||||
}
|
||||
|
||||
/// 插入或更新一条 dict 条目(word 为 PK,相同 word 会覆盖)
|
||||
pub async fn upsert_dict_entry(&self, word: &str, type_: &str, category: Option<&str>, freq: Option<i64>, merged_to: Option<&str>) -> Result<(), sqlx::Error> {
|
||||
let rows = sqlx::query(
|
||||
r#"SELECT COUNT(*) as cnt FROM dict WHERE word = ?"#
|
||||
).bind(word).fetch_one(&self.pool).await?;
|
||||
let exists: i64 = rows.get("cnt");
|
||||
if exists > 0 {
|
||||
sqlx::query(
|
||||
"UPDATE dict SET type = ?, category = ?, freq = ?, merged_to = ? WHERE word = ?"
|
||||
).bind(type_).bind(category).bind(freq).bind(merged_to).bind(word)
|
||||
.execute(&self.pool).await?;
|
||||
} else {
|
||||
sqlx::query(
|
||||
"INSERT INTO dict (word, type, category, freq, merged_to) VALUES (?, ?, ?, ?, ?)"
|
||||
).bind(word).bind(type_).bind(category).bind(freq).bind(merged_to)
|
||||
.execute(&self.pool).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn clear_all(&self) -> Result<(), sqlx::Error> {
|
||||
let mut tx = self.pool.begin().await?;
|
||||
sqlx::query("DELETE FROM album_tags").execute(&mut *tx).await?;
|
||||
|
||||
Reference in New Issue
Block a user