Files
romm/backend/tests/handler/test_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

97 lines
2.8 KiB
Python

import json
from collections.abc import Iterator
from pathlib import Path
import pytest
from handler import fixture_store
from handler.fixture_store import (
FIXTURE_SOURCES,
KNOWN_BIOS_FILE,
MAME_TABLE,
PS1_SERIAL_TABLE,
fixture_get,
)
from models.firmware import Firmware
@pytest.fixture
def isolated_fixture_store(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> Iterator[Path]:
"""Build the fixture store from scratch in a folder of the test's own."""
directory = tmp_path / "fixtures"
monkeypatch.setattr(fixture_store, "FIXTURE_STORE_DIR", directory)
fixture_store.fixture_store.cache_clear()
yield directory
fixture_store.fixture_store.cache_clear()
def test_every_index_is_read_from_the_shipped_json():
for table, path in FIXTURE_SOURCES.items():
with path.open() as f:
key, value = next(iter(json.load(f).items()))
assert fixture_get(table, key) == value, table
def test_a_serial_lookup_resolves_its_title():
with FIXTURE_SOURCES[PS1_SERIAL_TABLE].open() as f:
serial, entry = next(iter(json.load(f).items()))
assert fixture_get(PS1_SERIAL_TABLE, serial)["title"] == entry["title"]
def test_an_unknown_key_reads_as_none():
assert fixture_get(MAME_TABLE, "not-a-real-set") is None
def test_a_known_bios_verifies_by_name_against_the_store():
with KNOWN_BIOS_FILE.open() as f:
dump = json.load(f)["gb:gb_bios.bin"]
assert Firmware.verify_file_hashes(
platform_slug="gb",
file_name="gb_bios.bin",
file_size_bytes=int(dump["size"]),
md5_hash="",
sha1_hash="",
crc_hash=dump["crc"],
)
def test_the_store_is_built_once_and_named_after_its_sources(
isolated_fixture_store: Path,
):
store = fixture_store.fixture_store()
assert store.path.parent == isolated_fixture_store
assert store.revision == fixture_store._sources_digest()
assert fixture_store.fixture_store() is store
def test_a_store_deleted_after_its_build_is_rebuilt_on_read(
isolated_fixture_store: Path,
):
fixture_store.fixture_store().path.unlink()
with FIXTURE_SOURCES[MAME_TABLE].open() as f:
key, value = next(iter(json.load(f).items()))
assert fixture_get(MAME_TABLE, key) == value
def test_changed_sources_get_a_store_of_their_own(
isolated_fixture_store: Path, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
first = fixture_store.fixture_store()
edited = tmp_path / "mame_index.json"
edited.write_text(json.dumps({"pacman": {"description": "Pac-Man (edited)"}}))
monkeypatch.setitem(FIXTURE_SOURCES, MAME_TABLE, edited)
fixture_store.fixture_store.cache_clear()
second = fixture_store.fixture_store()
assert second.path != first.path
assert not first.path.exists()
assert second.get(MAME_TABLE, "pacman") == {"description": "Pac-Man (edited)"}