Files
romm/backend/handler
Stu Anderson b1e461c843 perf(api): api roms does single pass query (#5804)
* perf(roms): get roms in single pass

* perf(api): cache fixes-separate cache key

* pr-ready: code-review

Fix the PostgreSQL letter test and a strict mypy comparison in the
gallery index tests, place letters along the served id list on a
partial cache hit, reuse _gallery_order_clauses for the ranked index,
require sort_key and visibility on gallery_index_rows, and add tests
for the unscoped cache, other sorts and identical copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Share the filter arguments between filter_roms and gallery_index_rows,
branch once on the one-pass index, and settle cached-or-fresh lists in
place in get_gallery_index without materialising the result rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review follow-up)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(roms): read the gallery index through one handler entry point

get_gallery_index now takes the filtered and unfiltered queries and
returns both sidecars, replacing gallery_index_rows, the rows-based
get_gallery_index and with_char_index.

- It reads the cache before building any statement, and reuses the
  endpoint's filtered query, so filter_roms only runs again for the
  ranked PostgreSQL read on a cache miss.
- Every letter index comes from the ordered id and letter rows, so a
  letter's position is its first tile on every path, including
  ungrouped MariaDB galleries and requests without the id list. The
  separate first-tile cache key goes away.
- Only a request for the id list stores it, so pages still read the
  list an index request built.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(roms): group MariaDB letters by collation weight

DENSE_RANK forced a temporary table and an extra sort, which made the
whole ungrouped library about 2.3x slower than the previous two
queries. WEIGHT_STRING gives each letter its collation key per row, so
accented letters still share a group without the window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(roms): build the ranked index from a shared sibling window

The dedup window moves out of filter_roms into _sibling_window, so the
ranked PostgreSQL read builds its own (id, letter) select instead of
filter_roms switching its return shape behind a flag and a cast. The
read filters hidden rows before the window, which keeps the same
primaries because the window ranks visible versions first.

get_gallery_index reads the cached letters and id list in one Redis
round trip and only builds an id lookup when letters follow a cached
list. search_relevance_leads becomes private, and the gallery index
tests share one query builder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 19:17:01 -05:00
..