refactor: index the database by path once

model.database.record_at and sha1_at read one cached path index with
normpath keys. They replace records_by_path of resolve.packfiles, the
manifest's own sha1 index, the per-call indexes of regionaudit,
emulatorcheck and hidden_extras, and the linear scan of held_record.
resolve_sha1 drops its by_path argument. A path spelled with ./ or a
doubled slash now finds its record everywhere; the database writes none,
so the output is the same.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 20:03:26 +01:00
1 parent 1c978f3083
commit 8bc1fcb5db
9 files changed
+55 -60

No files matched your search

+3 -2
View File
@@ -10,7 +10,8 @@ from core.hashing import compute_hashes, parse_hash_list, parse_md5_list
from core.paths import comparable_destination
from core.ziptools import check_inside_zip
from resolve.localfile import resolve_local_file, variant_base_name
from resolve.packfiles import records_by_path, preferred_entries
from model.database import record_at
from resolve.packfiles import preferred_entries
@dataclass
@@ -124,7 +125,7 @@ def _record_at(path: str, db: dict) -> dict | None:
path verify --all and slots.py both take. The index is the pack
resolver's, so one run holds one copy of it.
"""
return records_by_path(db).get(path)
return record_at(db, path)
def content_sha1(path: str, db: dict) -> str | None:
+2 -4
View File
@@ -21,6 +21,7 @@ from collections.abc import Callable, Iterable, Mapping
from dataclasses import dataclass
from core.hashing import parse_hash_list
from model.database import record_at
from model.declarations import CONFIGS_ROOT, ROMS_ROOT, declared_root, outer_layout
from model.profiles import lists_files
from resolve.content import name_match_size_ok
@@ -1239,10 +1240,7 @@ def held_record(report: dict, db: dict) -> dict | None:
local, status = resolve_local_file(report, db, dest_hint=report.get("path") or "")
if local is None or status in ("not_found", "hash_mismatch"):
return None
for record in db.get("files", {}).values():
if record.get("path") == local:
return record
return None
return record_at(db, local)
def declared_destinations(config: dict) -> set[str]:
+2 -17
View File
@@ -13,6 +13,7 @@ import urllib.error
import urllib.request
from pathlib import Path
from model.database import record_at
from model.largefiles import fetch_large_file
from core.hashing import cut_md5_owner, parse_hash_list
from resolve.content import declared_hash_verdict
@@ -135,21 +136,6 @@ def run_is_offline(offline: bool | None = None) -> bool:
)
_RECORDS_BY_PATH: list[tuple[dict, int, dict[str, dict]]] = []
def records_by_path(db: dict) -> dict[str, dict]:
"""The database records by path, built once per database."""
files = db.get("files", {})
if _RECORDS_BY_PATH:
cached, size, index = _RECORDS_BY_PATH[0]
if cached is files and size == len(files):
return index
index = {record.get("path"): record for record in files.values()}
_RECORDS_BY_PATH[:] = [(files, len(files), index)]
return index
def pending_release_asset(
file_entry: dict, db: dict, registered: list[str] | None = None,
) -> str | None:
@@ -175,10 +161,9 @@ def pending_release_asset(
assets = set(asset_candidates(name, registered))
if not assets:
return None
records = records_by_path(db)
declared_size = file_entry.get("size")
for path, asset in sorted(asset_names(registered).items()):
record = records.get(path)
record = record_at(db, path)
if asset not in assets or record is None:
continue
if isinstance(declared_size, int) and record.get("size") != declared_size:
+4 -6
View File
@@ -9,6 +9,7 @@ import collections
import re
from model import region
from model.database import sha1_at
from model.region import NOINTRO
from model.profiles import lists_files
from resolve.localfile import resolve_local_file
@@ -42,21 +43,19 @@ def catalog_regions(db: dict) -> dict[str, tuple[set[str], str]]:
return out
def resolve_sha1(file_entry: dict, db: dict, by_path: dict | None = None) -> str | None:
def resolve_sha1(file_entry: dict, db: dict) -> str | None:
"""The repo SHA1 a profile entry resolves to, by the pack's own resolver.
A private resolver read sha1, then md5, then a lone name: entries that
identify their file by path (Dolphin's three IPL.bin), by crc32
(geargrafx) or by sha256 (mednafen) were never checked at all.
"""
if by_path is None:
by_path = {entry.get("path", ""): sha1 for sha1, entry in db["files"].items()}
path, status = resolve_local_file(
file_entry, db, dest_hint=file_entry.get("path") or ""
)
if not path or status in ("not_found", "hash_mismatch"):
return None
return by_path.get(path)
return sha1_at(db, path)
def build_report(profiles: dict, db: dict) -> dict:
@@ -69,7 +68,6 @@ def build_report(profiles: dict, db: dict) -> dict:
for slugs, _name in catalog.values():
vocabulary.update(slugs)
by_path = {entry.get("path", ""): sha1 for sha1, entry in db["files"].items()}
for emu_name, profile in sorted(profiles.items()):
if not lists_files(profile):
continue
@@ -77,7 +75,7 @@ def build_report(profiles: dict, db: dict) -> dict:
if not isinstance(f, dict) or not f.get("region"):
continue
declared = region.normalize_declared(f["region"])
sha1 = resolve_sha1(dict(f, source_profile=emu_name), db, by_path)
sha1 = resolve_sha1(dict(f, source_profile=emu_name), db)
if not sha1 or sha1 not in catalog:
unchecked += 1
continue