Files
romm/backend/handler/fixture_store.py
Georges-Antoine Assi ec1684d2af perf(metadata): serve the metadata dumps and fixtures from SQLite files (#5844)
* perf(metadata): serve the metadata dumps and fixtures from SQLite files

The LaunchBox and Switch TitleDB stores sat in valkey, which keeps every
byte resident: about 300MB on an instance with both imported, against
roughly 1MB for an empty server. Every read is an exact-key lookup and
nothing writes between imports, so they now live in read-only SQLite
files under RESOURCES_BASE_PATH/metadata, where the OS page cache holds
only the pages lookups touch.

A build writes a temporary file and renames it over the previous one,
so readers see the old store or the new one and never a partial import.
That replaces the initial-import flag and the schema stamp: the store
revision is part of the file name. A failed import keeps serving the
previous file.

The startup fixture indexes (MAME, ScummVM, Sony serials, known BIOS)
no longer get copied into valkey on every boot. They build on first use
into a file under the cache path, named after a hash of the JSON.

Startup deletes the old valkey keys and any file of an older revision,
and queues a rebuild when a store had data, so an upgrade refills
LaunchBox and TitleDB on its own. nginx returns 404 for the metadata
folder, since /assets/romm/resources is otherwise served without auth.

Measured against the real dumps: LaunchBox builds in 22s into 263MB on
disk, TitleDB in 1.2s into 54MB, the fixtures in 0.2s into 8MB. A hit
costs 10-23us including decompression, a miss about 7us.

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

* pr-ready: code-review

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

* pr-ready: simplify

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

* pr-ready: review-polish

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 fixes)

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

* Keep the previous metadata store when a dump comes out empty

A LaunchBox or TitleDB build that writes no rows to its main table now
fails with EmptyDumpError instead of swapping an empty store over a
populated one.

Each store keeps one connection per process behind a lock instead of one
per thread. A build closes it once it swaps the file in, and any other
process closes its own on its next lookup, so a replaced file's disk
space is released instead of held by idle threads.

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

* Address Greptile review: trim DumpStore comments

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

* Restore valkey's own snapshot policy by default

The hourly default (48259b3a9, #3980) kept valkey from rewriting the
hundreds of MB of LaunchBox and TitleDB data it held. With those moved to
SQLite files, a snapshot holds only sessions, queues and query caches, so
an unset REDIS_SAVE_POLICY now leaves valkey's stock policy in place and
loses less on a crash. An empty value still disables snapshots.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 08:49:15 -05:00

72 lines
2.4 KiB
Python

"""The lookup indexes RomM ships as JSON, built on first use into a SQLite file
named after a hash of its sources."""
import hashlib
import json
from functools import cache
from pathlib import Path
from typing import Any, Final
from config import CACHE_BASE_PATH
from handler.dump_store import DumpStore
_METADATA_FIXTURES_DIR: Final = Path(__file__).parent / "metadata" / "fixtures"
_MODEL_FIXTURES_DIR: Final = Path(__file__).parents[1] / "models" / "fixtures"
MAME_TABLE: Final = "mame"
SCUMMVM_TABLE: Final = "scummvm"
PS2_OPL_TABLE: Final = "ps2_opl"
PS1_SERIAL_TABLE: Final = "ps1_serial"
PS2_SERIAL_TABLE: Final = "ps2_serial"
PSP_SERIAL_TABLE: Final = "psp_serial"
KNOWN_BIOS_TABLE: Final = "known_bios"
KNOWN_BIOS_FILE: Final = _MODEL_FIXTURES_DIR / "known_bios_files.json"
FIXTURE_SOURCES: Final[dict[str, Path]] = {
MAME_TABLE: _METADATA_FIXTURES_DIR / "mame_index.json",
SCUMMVM_TABLE: _METADATA_FIXTURES_DIR / "scummvm_index.json",
PS2_OPL_TABLE: _METADATA_FIXTURES_DIR / "ps2_opl_index.json",
PS1_SERIAL_TABLE: _METADATA_FIXTURES_DIR / "ps1_serial_index.json",
PS2_SERIAL_TABLE: _METADATA_FIXTURES_DIR / "ps2_serial_index.json",
PSP_SERIAL_TABLE: _METADATA_FIXTURES_DIR / "psp_serial_index.json",
KNOWN_BIOS_TABLE: KNOWN_BIOS_FILE,
}
FIXTURE_STORE_DIR: Final = Path(CACHE_BASE_PATH) / "fixtures"
def _sources_digest() -> str:
digest = hashlib.sha256()
for table, path in sorted(FIXTURE_SOURCES.items()):
digest.update(table.encode())
digest.update(path.read_bytes())
return digest.hexdigest()[:16]
@cache
def fixture_store() -> DumpStore:
"""Return the fixture store for this image's sources, building it if no process has yet."""
store = DumpStore(
FIXTURE_STORE_DIR, "fixtures", _sources_digest(), tuple(FIXTURE_SOURCES)
)
if not store.exists():
with store.build() as writer:
for table, path in FIXTURE_SOURCES.items():
with path.open() as f:
for key, value in json.load(f).items():
writer.put(table, key, value)
return store
def fixture_get(table: str, key: str) -> Any:
"""Read one fixture record, or None when the key is not in the index."""
store = fixture_store()
if not store.exists():
# A tmp cleaner can delete the file long after this process built it.
fixture_store.cache_clear()
store = fixture_store()
return store.get(table, key)