mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
perf(siblings): regroup only on identity changes and scope the post-scan repair
add_rom skips the regroup when the merge changed no identity column. The post-scan repair regroups the scanned ROMs for a ROM-scoped scan and rebuilds only the scanned platforms otherwise, since no group spans two platforms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1420,10 +1420,23 @@ async def scan_platforms(
|
||||
|
||||
log.info(f"{emoji.EMOJI_CHECK_MARK} Scan completed")
|
||||
|
||||
# The scan may have added platforms.
|
||||
db_platforms = db_platform_handler.get_platforms()
|
||||
db_platforms_by_slug = {p.fs_slug: p for p in db_platforms}
|
||||
|
||||
# Repair any group two scan workers rewrote at once. This runs first
|
||||
# because the cache drop and the smart collection refresh read the groups.
|
||||
try:
|
||||
db_rom_handler.rebuild_sibling_groups()
|
||||
if roms_ids:
|
||||
db_rom_handler.regroup_siblings(scanned_rom_ids)
|
||||
else:
|
||||
db_rom_handler.rebuild_sibling_groups(
|
||||
[
|
||||
db_platforms_by_slug[slug].id
|
||||
for slug in platforms_to_scan
|
||||
if slug in db_platforms_by_slug
|
||||
]
|
||||
)
|
||||
except Exception as e:
|
||||
log.error(f"Couldn't rebuild the sibling groups: {e}")
|
||||
|
||||
@@ -1459,10 +1472,6 @@ async def scan_platforms(
|
||||
# Export metadata files if enabled in config
|
||||
config = cm.get_config()
|
||||
|
||||
# Update the list of platforms after the scan to ensure we have the latest data
|
||||
db_platforms = db_platform_handler.get_platforms()
|
||||
db_platforms_by_slug = {p.fs_slug: p for p in db_platforms}
|
||||
|
||||
# Export only what this scan touched: a new-platforms scan skips every
|
||||
# platform already in the database, so rewriting those is wasted work.
|
||||
if config.GAMELIST_AUTO_EXPORT_ON_SCAN:
|
||||
|
||||
@@ -899,8 +899,14 @@ class DBRomsHandler(DBBaseHandler):
|
||||
session: Session = INJECTED_SESSION,
|
||||
) -> Rom:
|
||||
rom = session.merge(rom)
|
||||
state = sa_inspect(rom)
|
||||
identity_changed = state.pending or any(
|
||||
state.attrs[column].history.has_changes()
|
||||
for column in IDENTITY_KEY_SOURCE_COLUMNS
|
||||
)
|
||||
session.flush()
|
||||
sibling_groups.regroup(session, [rom.id])
|
||||
if identity_changed:
|
||||
sibling_groups.regroup(session, [rom.id])
|
||||
|
||||
return session.scalars(query.filter_by(id=rom.id).limit(1)).one()
|
||||
|
||||
@@ -4107,11 +4113,24 @@ class DBRomsHandler(DBBaseHandler):
|
||||
@begin_session
|
||||
def rebuild_sibling_groups(
|
||||
self,
|
||||
platform_ids: Iterable[int] | None = None,
|
||||
*,
|
||||
session: Session = INJECTED_SESSION,
|
||||
) -> None:
|
||||
"""Recompute every sibling group, repairing any a concurrent write raced."""
|
||||
sibling_groups.rebuild(session)
|
||||
"""Recompute the sibling groups on `platform_ids`, or on every platform when None."""
|
||||
sibling_groups.rebuild(
|
||||
session, None if platform_ids is None else set(platform_ids)
|
||||
)
|
||||
|
||||
@begin_session
|
||||
def regroup_siblings(
|
||||
self,
|
||||
rom_ids: Iterable[int],
|
||||
*,
|
||||
session: Session = INJECTED_SESSION,
|
||||
) -> None:
|
||||
"""Recompute the sibling groups `rom_ids` belong to."""
|
||||
sibling_groups.regroup(session, set(rom_ids))
|
||||
|
||||
@staticmethod
|
||||
def invalidate_filter_values_cache() -> None:
|
||||
|
||||
@@ -89,10 +89,18 @@ def regroup(session: Session, rom_ids: Collection[int]) -> None:
|
||||
_write(session, sibling_groups(key_rows), scope=visited)
|
||||
|
||||
|
||||
def rebuild(session: Session) -> None:
|
||||
"""Recompute every group from scratch."""
|
||||
rows = session.execute(select(RomIdentityKey.rom_id, *_KEY_COLUMNS)).all()
|
||||
_write(session, sibling_groups(_as_key_rows(rows)), scope=None)
|
||||
def rebuild(session: Session, platform_ids: Collection[int] | None = None) -> None:
|
||||
"""Recompute the groups on `platform_ids` (every platform when None) from scratch."""
|
||||
query = select(RomIdentityKey.rom_id, *_KEY_COLUMNS)
|
||||
scope: Collection[int] | None = None
|
||||
if platform_ids is not None:
|
||||
# Keys carry their platform, so no group spans two platforms.
|
||||
query = query.where(RomIdentityKey.platform_id.in_(platform_ids))
|
||||
scope = session.scalars(
|
||||
select(Rom.id).where(Rom.platform_id.in_(platform_ids))
|
||||
).all()
|
||||
rows = session.execute(query).all()
|
||||
_write(session, sibling_groups(_as_key_rows(rows)), scope=scope)
|
||||
|
||||
|
||||
def _key_rows(
|
||||
|
||||
@@ -127,6 +127,32 @@ class TestSiblingGroups:
|
||||
|
||||
assert _groups() == {first.id: first.id, bridge.id: first.id, last.id: first.id}
|
||||
|
||||
def test_a_platform_rebuild_leaves_other_platforms_alone(
|
||||
self, platform: Platform, other_platform: Platform
|
||||
):
|
||||
first, bridge, last = _chain(platform)
|
||||
usa = make_rom(other_platform, "Other (USA)", igdb_id=10)
|
||||
japan = make_rom(other_platform, "Other (Japan)", igdb_id=10)
|
||||
with sync_session.begin() as session:
|
||||
session.execute(delete(RomSiblingGroup))
|
||||
|
||||
db_rom_handler.rebuild_sibling_groups([platform.id])
|
||||
|
||||
assert _groups() == {first.id: first.id, bridge.id: first.id, last.id: first.id}
|
||||
assert usa.id not in _groups() and japan.id not in _groups()
|
||||
|
||||
def test_re_adding_a_rom_regroups_only_when_its_ids_change(
|
||||
self, platform: Platform, mocker
|
||||
):
|
||||
rom = make_rom(platform, "Readd (USA)", igdb_id=70)
|
||||
regroup = mocker.spy(sibling_groups, "regroup")
|
||||
|
||||
make_rom(platform, "Readd (USA) Rev 1", id=rom.id, igdb_id=70)
|
||||
assert regroup.call_count == 0
|
||||
|
||||
make_rom(platform, "Readd (USA)", id=rom.id, igdb_id=71)
|
||||
assert regroup.call_count == 1
|
||||
|
||||
def test_two_writers_grouping_one_rom_both_commit(self, platform: Platform):
|
||||
"""The second writer reads before the first commits, then meets its row."""
|
||||
existing = make_rom(platform, "Raced (USA)", igdb_id=90)
|
||||
|
||||
Reference in New Issue
Block a user