feat: refactor sibling data handling in RomSchema and update related methods

This commit is contained in:
zurdi
2026-05-26 08:21:45 +00:00
parent 8f48c92cbf
commit 2a35ca04e7
5 changed files with 134 additions and 41 deletions

View File

@@ -38,7 +38,15 @@ from decorators.database import begin_session
from handler.metadata.base_handler import UniversalPlatformSlug as UPS
from models.assets import Save, Screenshot, State
from models.platform import Platform
from models.rom import Rom, RomFile, RomFileCategory, RomMetadata, RomNote, RomUser, SiblingRom
from models.rom import (
Rom,
RomFile,
RomFileCategory,
RomMetadata,
RomNote,
RomUser,
SiblingRom,
)
from utils.database import (
json_array_contains_all,
json_array_contains_any,
@@ -209,30 +217,64 @@ class DBRomsHandler(DBBaseHandler):
return []
return session.scalars(query.filter(Rom.id.in_(ids))).all()
def get_sibling_ids_for_roms(
def get_sibling_data_for_roms(
self,
rom_ids: list[int],
user_id: int,
*,
session: Session,
) -> dict[int, list[int]]:
"""Return {rom_id: [sibling_rom_id, ...]} for the given rom IDs.
) -> dict[int, list[dict[str, Any]]]:
"""Return {rom_id: [{id, name, fs_name_no_tags, fs_name_no_ext, is_main_sibling}, ...]}.
Single query against the sibling_roms view, projecting only the two `id` columns.
Single query joining sibling_roms → roms (scalar columns only) → rom_user
(filtered to the request user). No Rom JSON metadata is hydrated, so this
stays cheap even on large pages — the cost is one extra JOIN with a few
scalar columns vs. the bare-IDs variant.
"""
if not rom_ids:
return {}
rows = session.execute(
select(SiblingRom.rom_id, SiblingRom.sibling_rom_id).where(
SiblingRom.rom_id.in_(rom_ids)
select(
SiblingRom.rom_id,
SiblingRom.sibling_rom_id,
Rom.name,
Rom.fs_name_no_tags,
Rom.fs_name_no_ext,
func.coalesce(RomUser.is_main_sibling, false()).label(
"is_main_sibling"
),
)
.join(Rom, Rom.id == SiblingRom.sibling_rom_id)
.outerjoin(
RomUser,
and_(
RomUser.rom_id == SiblingRom.sibling_rom_id,
RomUser.user_id == user_id,
),
)
.where(SiblingRom.rom_id.in_(rom_ids))
).all()
buckets: dict[int, set[int]] = {rom_id: set() for rom_id in rom_ids}
for rom_id, sibling_rom_id in rows:
buckets[rom_id].add(sibling_rom_id)
# Dedupe by (parent rom, sibling id) so a duplicate row in the join
# doesn't surface twice on the wire.
seen: dict[int, set[int]] = {rom_id: set() for rom_id in rom_ids}
buckets: dict[int, list[dict[str, Any]]] = {rom_id: [] for rom_id in rom_ids}
for rom_id, sib_id, name, fs_name_no_tags, fs_name_no_ext, is_main in rows:
if sib_id in seen[rom_id]:
continue
seen[rom_id].add(sib_id)
buckets[rom_id].append(
{
"id": sib_id,
"name": name,
"fs_name_no_tags": fs_name_no_tags,
"fs_name_no_ext": fs_name_no_ext,
"is_main_sibling": bool(is_main),
}
)
return {rom_id: sorted(ids) for rom_id, ids in buckets.items()}
return buckets
def filter_by_platform_id(self, query: Query, platform_id: int):
return query.filter(Rom.platform_id == platform_id)
@@ -602,14 +644,9 @@ class DBRomsHandler(DBBaseHandler):
selectinload(Rom.metadatum).options(noload(RomMetadata.rom)),
# Required for multi-file ROM actions and 3DS QR code
selectinload(Rom.files),
# Show sibling rom badges on cards
selectinload(Rom.sibling_roms).options(
noload(Rom.platform),
noload(Rom.metadatum),
# Required so SiblingRomSchema.is_main_sibling can be
# computed per sibling for the request user.
selectinload(Rom.rom_users).options(noload(RomUser.rom)),
),
# Sibling badge data is populated separately via
# get_sibling_data_for_roms — avoids hydrating full Rom rows
# (including JSON metadata) per sibling on every page.
# Notes indicator on cards
selectinload(Rom.notes),
)