Files
romm/backend/tests/endpoints/roms/test_rom_sidecar_cache.py
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

641 lines
22 KiB
Python

"""Cache gating for the gallery sidecars (filter values, char index, id index).
`GET /roms` returns two whole-library aggregates alongside the page, and
`GET /roms/filter-values` returns the third. Only the fully unscoped scan may be
memoised under the shared "all" key, because that key encodes the user, sort
order and grouping but none of the filters.
The filter-value list is the exception: it is built with only the platform,
collection and search applied, so the row-level filters (missing, favorite,
duplicate, ...) never reach its query and its result is byte-identical to the
unfiltered one.
These tests pin the split gate:
1. a row-level filter reads and writes the shared filter-values entry,
2. a scope filter (platform / collection / search) does neither,
3. the char index and the id index stay live under any row-level filter,
since both narrow with it,
4. RomUser-column sorts key on a per-user sort version, so a rom_user
write refreshes that user's sorted index without touching other
users' entries or the name-sorted one, and `hidden` still bumps
globally,
5. grouped sets key on a per-user sibling version moved only by
main-sibling picks, and filter values use one order-free per-user
entry,
6. grouped pages read their window of the id list the index request
cached, which a grouped single-platform set also caches, under a key
naming its platform.
"""
from datetime import datetime, timedelta, timezone
from typing import Any, cast
import pytest
from fastapi import status
from fastapi.testclient import TestClient
from tests.factories import make_rom
from endpoints.roms import (
build_grouped_page_cache_key,
build_unscoped_filter_values_cache_key,
build_unscoped_sidecar_cache_key,
)
from handler.database import db_rom_handler
from handler.database.roms_handler import (
_char_index_redis_key,
_filter_values_cache_keys_key,
_filter_values_cache_version,
_filter_values_redis_key,
_rom_id_index_redis_key,
_store_versioned_cache,
_store_versioned_id_list,
)
from handler.redis_handler import as_text, sync_cache
from models.platform import Platform
from models.rom import Rom
from models.user import User
# A recognisable filter-value payload: if the endpoint answers with this, it
# read the shared cache entry rather than recomputing over the library.
SENTINEL_FILTER_VALUES: dict[str, Any] = {
"genres": ["Sentinel Genre"],
"franchises": [],
"collections": [],
"companies": [],
"publishers": [],
"developers": [],
"game_modes": [],
"age_ratings": [],
"player_counts": [],
"regions": [],
"languages": [],
"tags": [],
"platforms": [],
}
@pytest.fixture
def missing_rom(rom: Rom) -> Rom:
"""Flag the shared rom as missing from the filesystem."""
db_rom = db_rom_handler.update_rom(rom.id, {"missing_from_fs": True})
return db_rom
def _unscoped_key(
user_id: int, order_by: str = "", order_dir: str = "asc", group: bool = False
) -> str:
"""The shared sidecar key for an unscoped request."""
key = build_unscoped_sidecar_cache_key(user_id, order_by, order_dir, group, True)
assert key is not None
return key
def _platform_key(user_id: int, platform_id: int) -> str:
"""The sidecar key for a grouped, default-ordered platform gallery."""
return build_grouped_page_cache_key(user_id, "", "asc", platform_id)
def _filter_values_key(user_id: int) -> str:
key = build_unscoped_filter_values_cache_key(user_id, True)
assert key is not None
return key
def _seed_filter_values(user_id: int) -> str:
version = _filter_values_cache_version()
redis_key = _filter_values_redis_key(_filter_values_key(user_id), version)
_store_versioned_cache(redis_key, version, SENTINEL_FILTER_VALUES)
return redis_key
def _seed_rom_id_index(cache_key: str, ids: list[int]) -> str:
version = _filter_values_cache_version()
redis_key = _rom_id_index_redis_key(cache_key, version)
_store_versioned_id_list(redis_key, version, ids)
return redis_key
def _get_roms(client: TestClient, access_token: str, **params: Any) -> dict[str, Any]:
response = client.get(
"/api/roms",
headers={"Authorization": f"Bearer {access_token}"},
params=params,
)
assert response.status_code == status.HTTP_200_OK
return cast(dict[str, Any], response.json())
def _get_filter_values(
client: TestClient, access_token: str, **params: Any
) -> dict[str, Any]:
response = client.get(
"/api/roms/filter-values",
headers={"Authorization": f"Bearer {access_token}"},
params=params,
)
assert response.status_code == status.HTTP_200_OK
return cast(dict[str, Any], response.json())
def test_row_filter_reads_unscoped_filter_values_cache(
client: TestClient, access_token: str, admin_user: User, missing_rom: Rom
):
"""`missing=true` reuses the whole-library entry the unfiltered scan wrote."""
_seed_filter_values(admin_user.id)
body = _get_filter_values(client, access_token, missing=True)
assert body["genres"] == ["Sentinel Genre"]
def test_row_filter_writes_unscoped_filter_values_cache(
client: TestClient, access_token: str, admin_user: User, missing_rom: Rom
):
"""A row-filtered request also warms the shared entry for everyone else."""
version = _filter_values_cache_version()
redis_key = _filter_values_redis_key(_filter_values_key(admin_user.id), version)
assert sync_cache.get(redis_key) is None
_get_filter_values(client, access_token, missing=True)
assert sync_cache.get(redis_key) is not None
def test_row_filtered_and_unfiltered_filter_values_match(
client: TestClient, access_token: str, missing_rom: Rom
):
"""The two are the same list, which is what makes sharing the key correct."""
filtered = _get_filter_values(client, access_token, missing=True)
unfiltered = _get_filter_values(client, access_token)
assert filtered == unfiltered
def test_platform_scope_does_not_read_unscoped_filter_values_cache(
client: TestClient, access_token: str, admin_user: User, platform: Platform
):
"""A platform narrows the filter-value list, so it must be computed live."""
_seed_filter_values(admin_user.id)
body = _get_filter_values(client, access_token, platform_ids=platform.id)
assert body["genres"] == []
def test_platform_scope_does_not_write_unscoped_filter_values_cache(
client: TestClient, access_token: str, admin_user: User, platform: Platform
):
"""A scoped list must never leak into the shared whole-library entry."""
redis_key = _seed_filter_values(admin_user.id)
_get_filter_values(client, access_token, platform_ids=platform.id)
cached = sync_cache.get(redis_key)
assert cached is not None
assert b"Sentinel Genre" in (
cached if isinstance(cached, bytes) else cached.encode()
)
def test_search_scope_does_not_read_unscoped_filter_values_cache(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""A search term narrows the filter-value list the same way a platform does."""
_seed_filter_values(admin_user.id)
body = _get_filter_values(client, access_token, search_term="nothing-matches-this")
assert body["genres"] == []
def test_row_filter_does_not_read_unscoped_char_index_cache(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""The char index covers filtered rows, so the shared entry does not apply."""
version = _filter_values_cache_version()
_store_versioned_cache(
_char_index_redis_key(_unscoped_key(admin_user.id), version),
version,
[["Z", 41]],
)
body = _get_roms(client, access_token, missing=True)
assert "Z" not in body["char_index"]
def test_row_filter_does_not_read_unscoped_rom_id_index_cache(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""Same for the id index: it is the filtered result set, not the library."""
_seed_rom_id_index(_unscoped_key(admin_user.id), [424242])
body = _get_roms(client, access_token, missing=True)
assert body["rom_id_index"] == []
def test_length_filter_does_not_read_unscoped_rom_id_index_cache(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""A HowLongToBeat range narrows the result set like any other row filter."""
_seed_rom_id_index(_unscoped_key(admin_user.id), [424242])
body = _get_roms(client, access_token, hltb_main_story_max=3600)
assert body["rom_id_index"] == []
def test_unfiltered_request_still_reads_unscoped_char_index_cache(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""The unscoped scan keeps its memoisation (the case the key was built for)."""
version = _filter_values_cache_version()
_store_versioned_cache(
_char_index_redis_key(_unscoped_key(admin_user.id), version),
version,
[["Z", 41]],
)
body = _get_roms(client, access_token, with_rom_id_index=False)
assert body["char_index"] == {"Z": 41}
def test_unfiltered_index_request_reads_unscoped_letters_beside_ids(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""An index request serves the first-tile letters cached beside its ids."""
cache_key = _unscoped_key(admin_user.id, group=True)
_seed_rom_id_index(cache_key, [rom.id])
version = _filter_values_cache_version()
_store_versioned_cache(
_char_index_redis_key(cache_key, version), version, [["Z", 41]]
)
body = _get_roms(client, access_token, group_by_meta_id=True)
assert body["rom_id_index"] == [rom.id]
assert body["char_index"] == {"Z": 41}
@pytest.mark.parametrize(
("params", "expected"),
[
# Relevance leads, so the rows run in no letter order.
({"search_term": "final fantasy"}, {}),
({"search_term": "final fantasy", "order_by": "name"}, {"f": 0}),
({"search_term": "fantasy"}, {}),
# Too short to rank, so the name order stands.
({"search_term": "fa"}, {"f": 0}),
({}, {"f": 0}),
],
)
def test_char_index_follows_whether_relevance_leads(
client: TestClient,
access_token: str,
platform: Platform,
params: dict[str, str],
expected: dict[str, int],
):
make_rom(platform, "Final Fantasy")
make_rom(platform, "Final Fantasy VII")
body = _get_roms(client, access_token, **params)
assert body["char_index"] == expected
def _put_props(
client: TestClient,
access_token: str,
rom_id: int,
body: dict[str, Any] | None = None,
**params: Any,
) -> None:
response = client.put(
f"/api/roms/{rom_id}/props",
headers={"Authorization": f"Bearer {access_token}"},
params=params,
json=body or {},
)
assert response.status_code == status.HTTP_200_OK
@pytest.fixture
def played_rom(rom: Rom, admin_user: User) -> Rom:
"""The shared rom with an old `last_played`, so a later play must reorder past it."""
rom_user = db_rom_handler.get_rom_user(rom.id, admin_user.id)
assert rom_user is not None
db_rom_handler.update_rom_user(
rom_user.id, {"last_played": datetime(2020, 1, 1, tzinfo=timezone.utc)}
)
return rom
def _ingest_play_session(client: TestClient, access_token: str, rom_id: int) -> None:
end = datetime.now(timezone.utc).replace(microsecond=0)
response = client.post(
"/api/play-sessions",
headers={"Authorization": f"Bearer {access_token}"},
json={
"sessions": [
{
"rom_id": rom_id,
"start_time": (end - timedelta(minutes=30)).isoformat(),
"end_time": end.isoformat(),
"duration_ms": 30 * 60 * 1000,
}
]
},
)
assert response.status_code == status.HTTP_201_CREATED
@pytest.mark.parametrize("trigger", ["props", "play_session"])
def test_last_played_write_refreshes_the_sorted_index(
client: TestClient,
access_token: str,
played_rom: Rom,
second_rom: Rom,
trigger: str,
):
"""Both `last_played` writers reorder the next sorted index."""
first = _get_roms(client, access_token, order_by="last_played", order_dir="desc")
assert first["rom_id_index"] == [played_rom.id, second_rom.id]
if trigger == "props":
_put_props(client, access_token, second_rom.id, update_last_played=True)
else:
_ingest_play_session(client, access_token, second_rom.id)
second = _get_roms(client, access_token, order_by="last_played", order_dir="desc")
assert second["rom_id_index"] == [second_rom.id, played_rom.id]
def test_main_sibling_write_refreshes_grouped_entry(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""Grouped sets pick the user's main sibling, so that write refreshes
the grouped index even under the default name sort."""
_seed_rom_id_index(_unscoped_key(admin_user.id, group=True), [424242])
first = _get_roms(client, access_token, group_by_meta_id=True)
assert first["rom_id_index"] == [424242]
_put_props(client, access_token, rom.id, body={"is_main_sibling": True})
second = _get_roms(client, access_token, group_by_meta_id=True)
assert second["rom_id_index"] == [rom.id]
def test_non_sibling_write_keeps_the_grouped_entry(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""A play must not rotate the default grouped gallery's whole-library
index; only main-sibling picks move a name-sorted grouped set."""
_seed_rom_id_index(_unscoped_key(admin_user.id, group=True), [424242])
_put_props(client, access_token, rom.id, update_last_played=True)
body = _get_roms(client, access_token, group_by_meta_id=True)
assert body["rom_id_index"] == [424242]
def test_rom_user_write_keeps_filter_values_entry(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""Filter values read no sortable RomUser column, so a play must not rotate them."""
_seed_filter_values(admin_user.id)
_put_props(client, access_token, rom.id, update_last_played=True)
body = _get_filter_values(client, access_token)
assert body["genres"] == ["Sentinel Genre"]
def test_rom_user_write_keeps_name_sorted_entry(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""A rating write must not rotate or delete the user's name-sorted entry."""
redis_key = _seed_rom_id_index(_unscoped_key(admin_user.id), [424242])
_put_props(client, access_token, rom.id, body={"rating": 8})
body = _get_roms(client, access_token)
assert body["rom_id_index"] == [424242]
assert sync_cache.exists(redis_key)
def test_rom_user_write_leaves_other_users_sorted_entries_alone(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""One user's play rotates only their own key; another user's entry stays."""
other_user_id = admin_user.id + 1
writer_key = _unscoped_key(admin_user.id, "last_played", "desc")
other_key = _unscoped_key(other_user_id, "last_played", "desc")
other_redis_key = _seed_rom_id_index(other_key, [424242])
_put_props(client, access_token, rom.id, update_last_played=True)
assert _unscoped_key(other_user_id, "last_played", "desc") == other_key
assert sync_cache.exists(other_redis_key)
assert _unscoped_key(admin_user.id, "last_played", "desc") != writer_key
def test_hidden_write_still_bumps_the_global_version(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""`hidden` keeps its global invalidation on top of the per-user bump."""
old_version = _filter_values_cache_version()
redis_key = _seed_rom_id_index(_unscoped_key(admin_user.id), [424242])
_put_props(client, access_token, rom.id, body={"hidden": True})
assert int(_filter_values_cache_version()) == int(old_version) + 1
assert not sync_cache.exists(redis_key)
def _page_ids(body: dict[str, Any]) -> list[int]:
return [item["id"] for item in body["items"]]
GROUPED_PAGE = {"group_by_meta_id": True, "with_rom_id_index": False}
def test_grouped_page_slices_the_cached_unscoped_index(
client: TestClient, access_token: str, admin_user: User, rom: Rom, second_rom: Rom
):
"""A grouped page without the index follows the cached id list."""
_seed_rom_id_index(
_unscoped_key(admin_user.id, group=True), [second_rom.id, rom.id]
)
body = _get_roms(client, access_token, **GROUPED_PAGE, with_total=True)
assert _page_ids(body) == [second_rom.id, rom.id]
assert body["total"] == 2
assert body["rom_id_index"] == []
body = _get_roms(client, access_token, **GROUPED_PAGE, limit=1, offset=1)
assert _page_ids(body) == [rom.id]
def test_grouped_page_miss_stays_live_and_writes_nothing(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""Only the index request builds the cached list; a page miss runs live."""
version = _filter_values_cache_version()
redis_key = _rom_id_index_redis_key(
_unscoped_key(admin_user.id, group=True), version
)
body = _get_roms(client, access_token, **GROUPED_PAGE, with_total=True)
assert _page_ids(body) == [rom.id]
assert body["total"] == 1
assert not sync_cache.exists(redis_key)
_get_roms(client, access_token, group_by_meta_id=True)
assert [int(i) for i in sync_cache.lrange(redis_key, 0, -1)] == [rom.id]
def test_grouped_platform_index_shares_the_page_key(
client: TestClient,
access_token: str,
admin_user: User,
platform: Platform,
rom: Rom,
second_rom: Rom,
):
"""The index request, its char index and the pages read one cached key."""
page_key = _platform_key(admin_user.id, platform.id)
_seed_rom_id_index(page_key, [second_rom.id, rom.id])
version = _filter_values_cache_version()
_store_versioned_cache(
_char_index_redis_key(page_key, version), version, [["Z", 41]]
)
body = _get_roms(
client, access_token, group_by_meta_id=True, platform_ids=platform.id
)
assert body["rom_id_index"] == [second_rom.id, rom.id]
assert body["total"] == 2
assert body["char_index"] == {"Z": 41}
body = _get_roms(client, access_token, **GROUPED_PAGE, platform_ids=platform.id)
assert _page_ids(body) == [second_rom.id, rom.id]
def test_grouped_platform_page_with_another_scope_stays_live(
client: TestClient,
access_token: str,
admin_user: User,
platform: Platform,
rom: Rom,
):
"""A search on top of the platforms narrows the set past the cached list."""
page_key = _platform_key(admin_user.id, platform.id)
_seed_rom_id_index(page_key, [424242])
body = _get_roms(
client,
access_token,
**GROUPED_PAGE,
platform_ids=platform.id,
search_term="test_rom",
)
assert _page_ids(body) == [rom.id]
def test_grouped_excluded_platform_page_skips_its_platform_key(
client: TestClient,
access_token: str,
admin_user: User,
platform: Platform,
other_platform: Platform,
rom: Rom,
):
"""Excluding one platform must not read that platform's cached list."""
_seed_rom_id_index(_platform_key(admin_user.id, other_platform.id), [424242])
body = _get_roms(
client,
access_token,
**GROUPED_PAGE,
platform_ids=other_platform.id,
platforms_logic="none",
)
assert _page_ids(body) == [rom.id]
@pytest.mark.parametrize("platform_count", [2, 0])
def test_grouped_index_skips_unbounded_platform_keys(
client: TestClient,
access_token: str,
platform: Platform,
other_platform: Platform,
rom: Rom,
platform_count: int,
):
"""Multi-platform sets and empty lists (e.g. an unknown id) are not cached."""
platform_ids = [platform.id, other_platform.id] if platform_count else [999999]
version = _filter_values_cache_version()
_get_roms(client, access_token, group_by_meta_id=True, platform_ids=platform_ids)
cached_keys = sync_cache.smembers(_filter_values_cache_keys_key(version))
assert not [key for key in cached_keys if "rom_id_list" in as_text(key)]
def test_platform_delete_bumps_the_global_version(
client: TestClient, access_token: str, admin_user: User, platform: Platform
):
"""Grouped pages slice the cached list, so deleting a platform's roms drops it."""
redis_key = _seed_rom_id_index(_unscoped_key(admin_user.id, group=True), [424242])
old_version = _filter_values_cache_version()
response = client.delete(
f"/api/platforms/{platform.id}",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
assert int(_filter_values_cache_version()) == int(old_version) + 1
assert not sync_cache.exists(redis_key)
def test_grouped_page_with_a_row_filter_stays_live(
client: TestClient, access_token: str, admin_user: User, missing_rom: Rom
):
"""A row filter narrows the set, so neither cached list may answer it."""
_seed_rom_id_index(_unscoped_key(admin_user.id, group=True), [424242])
platform_key = _platform_key(admin_user.id, missing_rom.platform_id)
_seed_rom_id_index(platform_key, [424242])
body = _get_roms(client, access_token, **GROUPED_PAGE, missing=True)
assert _page_ids(body) == [missing_rom.id]
body = _get_roms(
client,
access_token,
**GROUPED_PAGE,
missing=True,
platform_ids=missing_rom.platform_id,
)
assert _page_ids(body) == [missing_rom.id]
def test_ungrouped_page_does_not_read_the_cached_index(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
"""Without grouping a page is a plain indexed query and stays live."""
_seed_rom_id_index(_unscoped_key(admin_user.id), [424242])
body = _get_roms(client, access_token, with_rom_id_index=False)
assert _page_ids(body) == [rom.id]