Repository navigation
refactor(cache): extract repeated Ent query helpers - #1286
Conversation
|
This change is part of the following stack: Change managed by git-spice. |
|
Warning Review limit reached
More reviews will be available in 9 minutes and 47 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (8)
📝 WalkthroughWalkthroughThis PR extracts three duplicated Ent query patterns from ChangesCache Query Helper Refactor
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Code Review
This pull request refactors pkg/cache/cache.go by extracting duplicated inline Ent queries into shared, unexported helper functions in a new file pkg/cache/queries.go. Specifically, it introduces narInfoByHash, chunksByHashes, and totalNarFileSize helpers that accept Ent entity clients, enabling seamless reuse across both transactional and non-transactional callers. The review feedback suggests a valuable optimization in chunksByHashes to return early when the input slice of hashes is empty, preventing unnecessary database queries and potential dialect-specific SQL errors.
Return early with no rows when the hashes slice is empty, rather than issuing a query with an empty SQL IN (). For the current callers this case is unreachable (recordChunkBatch early-returns on an empty batch, and newHashes is built only inside len(creates) > 0), but guarding the shared helper's contract avoids a needless round-trip and the dialect-specific pitfalls of an empty IN for any future caller. Addresses review comment on PR #1286 (thread PRRT_kwDONV6tZs6FvzoU). Co-Authored-By: Claude Opus 4.8 <[email protected]>
cache.go (~7,500 lines) inlined the same Ent queries repeatedly: the single NarInfo-by-hash lookup at several sites against both the client and *ent.Tx, two chunk-by-hashes batch reads, and the total-NAR-size aggregate (SUM(file_size) + sql.NullInt64 unpack) duplicated as a multi-line block at two call sites. Add pkg/cache/queries.go with three unexported helpers — narInfoByHash, chunksByHashes, totalNarFileSize — that take the Ent entity client (*ent.NarInfoClient etc.), which both *ent.Client and *ent.Tx expose, so one helper serves transactional and non-transactional callers. Replace only the byte-identical inline sites; eager-loaded and multi-predicate variants are left untouched. totalNarFileSize returns (int64, error) so each caller keeps its own error policy (metrics warns and continues; cleanup returns the error). Behavior-preserving: identical SQL and error semantics. Dropped the now-unused database/sql import from cache.go and added two justified gosec nolints on uint64(narTotalSize) (file_size is a uint64 column, so the SUM is non-negative). Records a database-orm spec requirement for the shared-helper contract. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Return early with no rows when the hashes slice is empty, rather than issuing a query with an empty SQL IN (). For the current callers this case is unreachable (recordChunkBatch early-returns on an empty batch, and newHashes is built only inside len(creates) > 0), but guarding the shared helper's contract avoids a needless round-trip and the dialect-specific pitfalls of an empty IN for any future caller. Addresses review comment on PR #1286 (thread PRRT_kwDONV6tZs6FvzoU). Co-Authored-By: Claude Opus 4.8 <[email protected]>
8a4b3eb to
d887e91
Compare
Summary
pkg/cache/cache.go(~7,500 lines) inlined the same Ent queries repeatedly: the single NarInfo-by-hash lookup at several sites against both the client and*ent.Tx, two chunk-by-hashes batch reads, and the total-NAR-size aggregate (SUM(file_size)+sql.NullInt64unpack) duplicated as a multi-line block at two call sites.pkg/cache/queries.gowith three unexported helpers —narInfoByHash,chunksByHashes,totalNarFileSize— that take the Ent entity client (*ent.NarInfoClientetc.), which both*ent.Clientand*ent.Txexpose, so one helper serves transactional and non-transactional callers.totalNarFileSizereturns(int64, error)so each caller keeps its own error policy (metrics warns and continues; cleanup returns).database/sqlimport from cache.go; added two justified gosec nolints onuint64(narTotalSize)(file_size is a uint64 column, so the SUM is non-negative).Test plan
task fmt/task lint(0 issues) /task test(full suite green;pkg/cache~30s)