Skip to content

Replace binary search with cumulative size scan for threshold cleanup#3898

Merged
guiyanakuang merged 2 commits intomainfrom
fix/binary-search-excessive-db-queries
Feb 16, 2026
Merged

Replace binary search with cumulative size scan for threshold cleanup#3898
guiyanakuang merged 2 commits intomainfrom
fix/binary-search-excessive-db-queries

Conversation

@guiyanakuang
Copy link
Copy Markdown
Member

Summary

  • Replace O(log N) binary search (30+ DB queries per cleanup) with single-pass cumulative size scan (1-2 queries)
  • Add getOldestNonFavoriteCreateTimeAndSize SQL query for batched row streaming
  • Add findCleanTimeByCumulativeSize() in PasteDao for cursor-based accumulation
  • Clean up unused DateUtils import and @OptIn(ExperimentalTime::class) annotation

Closes #3897

Test plan

  • All existing CleanPasteTaskExecutorTest tests pass
  • ./gradlew ktlintFormat passes
  • ./gradlew app:desktopTest — all 209 tests pass

The old deleteStorageOfApproximateSize used binary search over timestamp
space, issuing 30+ DB queries (O(log N) iterations × 1 query each) per
cleanup cycle. Replace with a single-pass cumulative size scan that
streams oldest non-favorite rows in batches of 500, stopping as soon as
the target cleanup size is reached — typically 1-2 queries for moderate
datasets.
Use (createTime, id) composite cursor instead of createTime alone to
avoid skipping rows with duplicate createTime values at batch boundaries.
@guiyanakuang guiyanakuang merged commit 2d3c24b into main Feb 16, 2026
2 checks passed
@guiyanakuang guiyanakuang deleted the fix/binary-search-excessive-db-queries branch February 16, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Threshold cleanup binary search issues excessive DB queries

1 participant