perf: optimize toggle-favourite to avoid fetching all images (fixes #805)
Closes #805
Problem
The previous implementation fetched ALL images from the database just to get the updated favourite status of a single image:
# BEFORE (Slow - O(n))
image = next(
(img for img in db_get_all_images() if img["id"] == image_id), None
)
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
## Release Notes
* **Performance Improvements**
* Optimized favorite toggle functionality to return results instantly without additional requests.
* **Bug Fixes**
* Improved error handling for non-existent images with consistent response patterns.
* **New Features**
* Added image details retrieval with comprehensive metadata and associated tag information.
<sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Walkthrough
The changes optimize the /images/toggle-favourite endpoint by introducing a targeted single-image database lookup function and refactoring the route to use direct status returns, eliminating an O(n) operation that previously fetched all images to toggle one image's favourite status.
Changes
| Cohort / File(s) | Summary |
|---|---|
Database layer enhancements backend/app/database/images.py |
Added db_get_image_by_id(image_id: str) function to retrieve a single image with metadata and tags via multi-row join query. Modified db_toggle_image_favourite_status to return Union[bool, None] with the new favourite status on success or None if not found. Replaced direct sqlite3.connect with central _connect() utility. |
Route optimization backend/app/routes/images.py |
Updated toggle_favourite route to directly obtain new favourite status from db_toggle_image_favourite_status instead of fetching all images and iterating. Returns 404 when status is None. Response now includes the updated isFavourite value without requiring a second full-image fetch. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
- Review new multi-row query logic in
db_get_image_by_id, including join correctness and metadata/tag aggregation - Verify return type changes (
Union[bool, None],Union[dict, None]) don't break existing callers beyond the updated route - Validate error handling and connection finalization paths in both modified functions
- Confirm the optimization resolves the O(n) performance issue described in the linked issue
Poem
🐰 A hop and a skip through the database,
No more fetching every image in haste—
One query, one answer, swift and lean,
The fastest toggle we've ever seen! ✨
Pre-merge checks and finishing touches
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title accurately and concisely describes the main optimization: avoiding fetching all images for toggle-favourite operation, and directly references the issue being fixed (#805). |
| Linked Issues check | ✅ Passed | The PR successfully implements the core requirement from issue #805: replacing O(n) full-fetch-and-scan with O(1) single-image lookup via new db_get_image_by_id function. |
| Out of Scope Changes check | ✅ Passed | All changes directly support the performance optimization objective. The db_toggle_image_favourite_status enhancement and new db_get_image_by_id function enable the toggle-favourite route optimization without unrelated modifications. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
✨ Finishing touches
- [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
⚠️ This PR has merge conflicts.
Please resolve the merge conflicts before review.
Your PR will only be reviewed by a maintainer after all conflicts have been resolved.
📺 Watch this video to understand why conflicts occur and how to resolve them: https://www.youtube.com/watch?v=Sqsz1-o7nXk
⚠️ This PR still has unresolved merge conflicts and has been inactive for the past 30 days.
The PR has merge conflicts label is still applied, and there has been no recent activity on this PR.
Please resolve the merge conflicts or leave a comment if you are still actively working on it.
If there is no further activity within the next 5 days, this PR may be automatically closed to help keep the PR backlog manageable.