Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private void flushBatch(
Map<String, Map<Motif, List<GameFeatures.MotifOccurrence>>> occurrencesBatch) {
if (featureBatch.isEmpty()) return;
gameFeatureStore.insertBatch(featureBatch);
gameFeatureStore.deleteOccurrencesByGameUrls(new ArrayList<>(occurrencesBatch.keySet()));
gameFeatureStore.insertOccurrencesBatch(occurrencesBatch);
featureBatch.clear();
occurrencesBatch.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,29 @@ public void query_paginatesStablyWhenPlayedAtIsEqual() {
assertThat(page2.get(0).gameUrl()).isEqualTo("https://chess.com/game/zzz-last");
}

@Test
public void deleteOccurrencesByGameUrls_thenReinsert_doesNotDuplicate() {
// Regression test: re-indexing a partial month must not accumulate duplicate occurrences.
// The fix is in IndexWorker.flushBatch: delete occurrences for each game_url before inserting.
String gameUrl = "https://chess.com/game/reindex-dedup";
dao.insertBatch(List.of(createGame(gameUrl)));

GameFeatures.MotifOccurrence pin =
new GameFeatures.MotifOccurrence(
5, 3, "white", "Pin on c6", null, "Bb5", "nc6", false, false, "ABSOLUTE");
Map<String, Map<Motif, List<GameFeatures.MotifOccurrence>>> occurrences =
Map.of(gameUrl, Map.of(Motif.PIN, List.of(pin)));

// First index run
dao.insertOccurrencesBatch(occurrences);
// Simulate re-index: delete then re-insert (what flushBatch now does)
dao.deleteOccurrencesByGameUrls(List.of(gameUrl));
dao.insertOccurrencesBatch(occurrences);

Map<String, Map<String, List<OccurrenceRow>>> result = dao.queryOccurrences(List.of(gameUrl));
assertThat(result.get(gameUrl).get("pin")).hasSize(1);
}

private GameFeature createGame(String url) {
return new GameFeature(
null,
Expand Down
Loading