fix(撞款扫描): 保留期 90→7 天,并单独兜住最新 SUCCESS 行

该表每行含整份聚合 payload(线上实测约 2.3MB/行,24 行占 55MB),
而读取侧只认最新一行(selectLatestFullRow/LightRow 都是 ORDER BY id DESC LIMIT 1),
历史行纯占磁盘。保留期降到 7 天,行数下限仍由 PROTECTED_ROWS=10 兜底。

同时补一条保护线:连续失败多日时,最新 SUCCESS 行会滑出「最新 N 行」保护窗口,
再按时间线被删掉,界面就空白了——现在取「第 N 新行」与「最新 SUCCESS 行」
两者更靠前的那个 id 作为保护线,确保它永不被删。
This commit is contained in:
2026-09-16 09:25:30 +08:00
parent baa4e666fe
commit a4928e8b57
3 changed files with 47 additions and 5 deletions
@@ -2,10 +2,12 @@ package com.nanri.aiimage.modules.shopduplicatecheck.service;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.modules.shopduplicatecheck.mapper.ShopDataDuplicateScanMapper;
import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ScanLightRowDto;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -94,4 +96,29 @@ class ShopDataDuplicateScanRetentionServiceTest {
verify(scanMapper).deleteOlderThanBatch(any(LocalDateTime.class), protectLine.capture(), anyInt());
assertTrue(protectLine.getValue() == Long.MAX_VALUE, "保护线为 Long.MAX_VALUE,等于不额外设限");
}
/**
* 连续失败多日时,最新 SUCCESS 行可能已滑出「最新 N 行」保护窗口;
* 读侧只认它,被删掉界面就空白了,必须单独兜一条更靠前的保护线。
*/
@Test
void latestSuccessRowIsAlwaysProtected() {
lockAvailable();
when(scanMapper.selectNthNewestId(anyInt())).thenReturn(500L);
when(scanMapper.selectLatestLightRow()).thenReturn(successRow(120L));
when(scanMapper.deleteOlderThanBatch(any(LocalDateTime.class), anyLong(), anyInt())).thenReturn(0);
service().purgeExpiredScans();
ArgumentCaptor<Long> protectLine = ArgumentCaptor.forClass(Long.class);
verify(scanMapper).deleteOlderThanBatch(any(LocalDateTime.class), protectLine.capture(), anyInt());
assertEquals(120L, protectLine.getValue(), "取更靠前的保护线,SUCCESS 行不会被删");
}
private static ScanLightRowDto successRow(Long id) {
ScanLightRowDto row = new ScanLightRowDto();
row.setId(id);
row.setCreatedAt(LocalDateTime.now().minusDays(30));
return row;
}
}