Merge pull request #3781 from rommapp/posthog-code/roms-optional-id-index

perf(roms): make full rom id index opt-out on GET /api/roms
This commit is contained in:
Georges-Antoine Assi
2026-07-16 21:20:19 -04:00
committed by GitHub
5 changed files with 86 additions and 15 deletions

View File

@@ -307,6 +307,14 @@ def get_roms(
with_filter_values: Annotated[
bool, Query(description="Whether to return filter values.")
] = True,
with_rom_id_index: Annotated[
bool,
Query(
description=(
"Whether to return the full ordered rom id index that backs virtual scroll."
)
),
] = True,
search_term: Annotated[
str | None,
Query(description="Search term to filter roms."),
@@ -729,14 +737,18 @@ def get_roms(
filter_values = RomFiltersDict(**query_filters)
# The full ordered id list backs virtual scroll, so it's computed over the
# whole result set on every request. Memoise the unscoped library scan (same
# key scheme as the other sidecars); scoped/searched sets stay live.
rom_id_index_cache_key = build_unscoped_sidecar_cache_key(
request.user.id, order_by, order_dir, group_by_meta_id, is_unscoped
)
rom_id_index = db_rom_handler.get_rom_id_index(
query=query, cache_key=rom_id_index_cache_key
)
# whole result set. Callers that only need a page (e.g. the home rails) opt
# out with with_rom_id_index=false and avoid the full-library scan.
rom_id_index: list[int] = []
if with_rom_id_index:
# Memoise the unscoped library scan (same key scheme as the other
# sidecars); scoped/searched sets stay live.
rom_id_index_cache_key = build_unscoped_sidecar_cache_key(
request.user.id, order_by, order_dir, group_by_meta_id, is_unscoped
)
rom_id_index = db_rom_handler.get_rom_id_index(
query=query, cache_key=rom_id_index_cache_key
)
# Hydrate the requested page and its additional data
with sync_session.begin() as session:
@@ -779,14 +791,22 @@ def get_roms(
]
params = resolve_params()
total = len(rom_id_index)
page_ids = list(rom_id_index[params.offset : params.offset + params.limit])
if page_ids:
page_rows = session.scalars(query.where(Rom.id.in_(page_ids))).all()
rows_by_id = {rom.id: rom for rom in page_rows}
page_items = [rows_by_id[i] for i in page_ids if i in rows_by_id]
if with_rom_id_index:
total = len(rom_id_index)
page_ids = list(rom_id_index[params.offset : params.offset + params.limit])
if page_ids:
page_rows = session.scalars(query.where(Rom.id.in_(page_ids))).all()
rows_by_id = {rom.id: rom for rom in page_rows}
page_items = [rows_by_id[i] for i in page_ids if i in rows_by_id]
else:
page_items = []
else:
page_items = []
# Let the database serve the page from the sort index instead of
# walking the whole primary key to build a full id list.
page_items = list(
session.scalars(query.offset(params.offset).limit(params.limit)).all()
)
total = db_rom_handler.get_rom_count(query=query, session=session)
return CustomLimitOffsetPage.create(
_transform(page_items),

View File

@@ -1403,6 +1403,26 @@ class DBRomsHandler(DBBaseHandler):
_store_versioned_cache(redis_key, version, ids)
return ids
@begin_session
def get_rom_count(
self,
query: Query,
*,
session: Session = None, # type: ignore
) -> int:
"""Count matching roms without materialising their ids.
For callers that need a page and its total but not the full
ordered id list, so the database can serve the page from the
sort index instead of scanning the whole library.
"""
return (
session.scalar(
select(func.count()).select_from(query.order_by(None).subquery())
)
or 0
)
@begin_session
def get_roms_by_fs_name(
self,

View File

@@ -174,6 +174,31 @@ def test_get_all_roms(
assert items[0]["sibling_roms"] == []
def test_get_roms_without_rom_id_index(
client: TestClient, access_token: str, rom: Rom, platform: Platform
):
response = client.get(
"/api/roms",
headers={"Authorization": f"Bearer {access_token}"},
params={
"platform_id": platform.id,
"limit": 15,
"with_rom_id_index": False,
},
)
assert response.status_code == status.HTTP_200_OK
body = response.json()
# The page and total stay correct, but the full id index is not built.
assert body["total"] == 1
assert body["rom_id_index"] == []
items = body["items"]
assert len(items) == 1
assert items[0]["id"] == rom.id
def test_get_roms_filter_by_metadata_providers(
client: TestClient, access_token: str, rom: Rom, platform: Platform
):

View File

@@ -362,6 +362,7 @@ async function getRecentRoms() {
limit: RECENT_ROMS_LIMIT,
with_char_index: false,
with_filter_values: false,
with_rom_id_index: false,
},
});
}
@@ -374,6 +375,7 @@ async function getRecentPlayedRoms() {
limit: RECENT_PLAYED_ROMS_LIMIT,
with_char_index: false,
with_filter_values: false,
with_rom_id_index: false,
last_played: true,
},
});

View File

@@ -170,6 +170,7 @@ class CachedApiService {
limit: 15,
with_char_index: false,
with_filter_values: false,
with_rom_id_index: false,
});
return cacheService.request<GetRomsResponse>(config, onBackgroundUpdate);
@@ -184,6 +185,7 @@ class CachedApiService {
limit: 15,
with_char_index: false,
with_filter_values: false,
with_rom_id_index: false,
last_played: true,
});
@@ -202,6 +204,7 @@ class CachedApiService {
limit: 15,
with_char_index: false,
with_filter_values: false,
with_rom_id_index: false,
});
}
@@ -212,6 +215,7 @@ class CachedApiService {
limit: 15,
with_char_index: false,
with_filter_values: false,
with_rom_id_index: false,
last_played: true,
});
}