diff --git a/src/engine/pipeline.rs b/src/engine/pipeline.rs index b7fa1a1..b755f6a 100644 --- a/src/engine/pipeline.rs +++ b/src/engine/pipeline.rs @@ -508,6 +508,8 @@ pub async fn phase_clean_tags_after_parse(db: &Database) -> usize { } let mut tag_removed = 0usize; + let mut updates: Vec<(String, String, String)> = Vec::new(); + for album in &albums { let tags_json = match album.tags.as_deref() { Some(t) if !t.is_empty() && t != "[]" => t, _ => continue, @@ -528,7 +530,18 @@ pub async fn phase_clean_tags_after_parse(db: &Database) -> usize { let tags_json_new = serde_json::to_string(&good_tags).unwrap_or_else(|_| "[]".to_string()); let words_json_new = serde_json::to_string(&all_words_new).unwrap_or_else(|_| "[]".to_string()); - db.update_album_tags(&album.id, &tags_json_new, &words_json_new).await; + updates.push((album.id.clone(), tags_json_new, words_json_new)); + } + + // ── 批量写入:单个事务 ── + if !updates.is_empty() { + let _guard = db.write_lock.lock().await; + if let Ok(mut tx) = db.pool.begin().await { + for (album_id, tags_json, all_words_json) in &updates { + db.update_album_tags_with_tx(&mut tx, album_id, tags_json, all_words_json).await; + } + tx.commit().await.ok(); + } } if tag_removed > 0 { @@ -558,6 +571,8 @@ struct PreprocessResult { /// 2. 移除短标签 /// 3. 检测并移除标签中的人名 /// 4. 收集新相册/半成品 +/// +/// 所有写库操作在单个事务中完成,避免逐条事务的 WAL fsync 开销。 async fn phase_preprocess_combined(db: &Database) -> PreprocessResult { let albums = db.get_all_albums_full().await; @@ -565,6 +580,8 @@ async fn phase_preprocess_combined(db: &Database) -> PreprocessResult { .into_iter().map(|(n, _)| n.to_lowercase()).collect(); let mut result = PreprocessResult::default(); + // 收集需要更新的相册变更:(album_id, tags_json, all_words_json) + let mut updates: Vec<(String, String, String)> = Vec::new(); for album in &albums { let tags_json = album.tags.as_deref(); @@ -681,7 +698,7 @@ async fn phase_preprocess_combined(db: &Database) -> PreprocessResult { result.tags_cleaned += 1; result.dirty.add(&album.id); - // 写回 + // 收集变更,等下批量写 let tags_json_new = serde_json::to_string(&new_tags).unwrap_or_else(|_| "[]".to_string()); let all_words_json = album.all_words.as_deref().unwrap_or("[]"); let all_words_new: Vec<[String; 2]> = serde_json::from_str::>(all_words_json) @@ -695,7 +712,27 @@ async fn phase_preprocess_combined(db: &Database) -> PreprocessResult { }) .collect(); let all_words_json_new = serde_json::to_string(&all_words_new).unwrap_or_else(|_| "[]".to_string()); - db.update_album_tags(&album.id, &tags_json_new, &all_words_json_new).await; + updates.push((album.id.clone(), tags_json_new, all_words_json_new)); + } + + // ── 批量写入:单个事务 ── + if !updates.is_empty() { + let _guard = db.write_lock.lock().await; + let mut tx = match db.pool.begin().await { + Ok(t) => t, + Err(e) => { + eprintln!("[管线·Phase①] 开启事务失败: {}", e); + return result; + } + }; + + for (album_id, tags_json, all_words_json) in &updates { + db.update_album_tags_with_tx(&mut tx, album_id, tags_json, all_words_json).await; + } + + if let Err(e) = tx.commit().await { + eprintln!("[管线·Phase①] 提交事务失败: {}", e); + } } println!("[管线·Phase①] 合并预处理完成:清洗{}相册,移除短标签{},移除人名标签{},新相册{},脏集合计{}",