fix: workspace pre_chunks dedup — stop 19GB re-dispatch bloat (A1) + charade analysis

This commit is contained in:
Accusys
2026-08-16 08:44:29 +08:00
parent 5e6a7b7d51
commit 0533248f2c
2 changed files with 194 additions and 1 deletions
+39 -1
View File
@@ -75,6 +75,30 @@ impl WorkspaceDb {
.execute(&self.pool)
.await?;
// Dedup guard: prevent the worker's repeated dispatch (skip path) from
// re-appending identical pre_chunks on every poll cycle, which caused a
// single large file to accumulate 9.28M duplicate rows (19 GB).
//
// Uses COALESCE so NULL start_frame/end_frame/start_time (e.g. asrx rows)
// still participate in uniqueness. On legacy DBs that already contain
// duplicates the index creation fails — log a warning and continue so we
// never crash; `INSERT OR IGNORE` in store_pre_chunk still blocks new dupes.
if let Err(e) = sqlx::query(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_pre_chunks_dedup ON pre_chunks(
file_uuid, processor_type, chunk_type,
COALESCE(start_frame,-1), COALESCE(end_frame,-1), COALESCE(start_time,-1)
)",
)
.execute(&self.pool)
.await
{
tracing::warn!(
"pre_chunks dedup index not created for {} (legacy duplicates may exist): {}",
self.file_uuid,
e
);
}
sqlx::query(
"CREATE TABLE IF NOT EXISTS face_detections (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -223,7 +247,7 @@ impl WorkspaceDb {
text_content: Option<&str>,
) -> Result<()> {
sqlx::query(
"INSERT INTO pre_chunks (file_uuid, processor_type, chunk_type, \
"INSERT OR IGNORE INTO pre_chunks (file_uuid, processor_type, chunk_type, \
start_frame, end_frame, start_time, end_time, data, text_content) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
)
@@ -241,6 +265,20 @@ impl WorkspaceDb {
Ok(())
}
/// Returns true if any pre_chunks already exist for this processor.
/// Used by the worker's skip path to avoid re-appending rows that were
/// already stored on a previous dispatch.
pub async fn has_pre_chunks(&self, processor_type: &str) -> Result<bool> {
let exists: Option<i64> = sqlx::query_scalar(
"SELECT 1 FROM pre_chunks WHERE file_uuid = ?1 AND processor_type = ?2 LIMIT 1",
)
.bind(&self.file_uuid)
.bind(processor_type)
.fetch_optional(&self.pool)
.await?;
Ok(exists.is_some())
}
pub async fn get_all_pre_chunks(&self) -> Result<Vec<PreChunkRow>> {
let rows = sqlx::query_as::<_, PreChunkRow>(
"SELECT id, file_uuid, processor_type, chunk_type, \