perf(scan): skip full library re-stat when the tree is unchanged (#979)
ship-ci / ci (push) Waiting to run

* perf(scan): skip full library re-stat when the tree is unchanged

Startup scans globbed the whole DLC tree twice (*.feedpak, *.wem) and
stat()'d every file to detect changes — ~100k filesystem round trips on
a 50k-song library, and painful on a slow NTFS-3G FUSE mount (the "big
drive churns on every launch" report).

Adds/removes/renames of songs all bump the mtime of the containing
directory (verified on the target mount), so after a full pass we persist
{reldir: mtime_ns} for every library dir (scan_dir_signature.json, keyed
by DLC path). The next scan re-stats only those dirs — a handful vs 100k
ops — and skips the entire listing/stat pass when none changed.

Blind spot: a pack rewritten in place under the same name bumps the file
mtime but not its dir's. Rare for a song library, and the manual Refresh
(/api/rescan + /api/rescan/full) now passes force=True to always do the
full pass. force threads through kick_scan -> _scan_runner and coalesces
like the rescan-pending flag.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(scan): track directory-form songs' own dir in the signature

CodeRabbit: _library_dirs recorded only each song's parent. For a
directory-form song (loose-song folder or directory sloppak bundle),
adding/removing/replacing a file INSIDE the folder bumps that folder's
own mtime, not its parent's — so the fast path would skip a rescan it
should run. Record the song's own dir when f.is_dir(). File-form
sloppaks (a single .feedpak zip) aren't dirs, so the flat file library
is unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Byron Gamatos
2026-07-15 21:23:10 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 2f2a095e4c
commit 0b4b174d33
3 changed files with 217 additions and 7 deletions
+75
View File
@@ -137,6 +137,81 @@ def test_background_scan_discovers_both_suffixes(tmp_path, scan_server):
assert "ignore.zip" not in seen
# ── 2b. directory-signature fast path (skip the full re-stat) ────────────────
def test_dir_signature_fast_path_skips_unchanged_tree(tmp_path, scan_server):
"""After a full scan records the library-dir signature, a second scan with
an unchanged tree takes the fast path and does NOT re-glob/extract — but a
forced scan (manual Refresh) always does the full pass, and a new song
(which bumps the dir mtime) reverts to a full pass on its own."""
import unittest.mock as mock
dlc = tmp_path / "dlc"
dlc.mkdir()
(dlc / "a.feedpak").write_bytes(b"")
(tmp_path / "config.json").write_text('{"dlc_dir": "%s"}' % dlc)
scan = importlib.import_module("scan")
seen: list[str] = []
def mock_extract(f, dlc_dir):
seen.append(f.name)
return {"title": f.name, "artist": "", "album": ""}
with mock.patch("scan_worker._extract_meta_for_file", new=mock_extract):
# 1) first pass: full scan, extracts a.feedpak (+ seeded builtins),
# records the signature
scan.background_scan()
assert "a.feedpak" in seen
assert scan._dir_signature_file().exists()
# 2) unchanged tree: fast path — no glob, no extraction at all
seen.clear()
scan.background_scan()
assert seen == []
assert scan.status()["stage"] == "complete"
# 3) a new song bumps the dlc mtime → signature mismatch → full pass
# picks it up on its own (no manual Refresh needed for adds)
(dlc / "b.feedpak").write_bytes(b"")
seen.clear()
scan.background_scan()
assert "b.feedpak" in seen
# 4) force=True (Refresh) bypasses the fast path even on a settled tree:
# with the signature now current, a plain scan skips, a forced one lists
seen.clear()
scan.background_scan() # fast path
assert seen == []
forced_listed = []
real_delete_missing = scan.appstate.meta_db.delete_missing
def _spy(files):
forced_listed.append(set(files))
return real_delete_missing(files)
with mock.patch.object(scan.appstate.meta_db, "delete_missing", new=_spy):
scan.background_scan(force=True)
assert forced_listed, "force=True must run the full listing pass"
def test_dir_signature_tracks_directory_form_song_own_dir(tmp_path):
"""A directory-form song (loose folder / directory bundle) records its OWN
directory in the signature, so an in-place file change inside it — which
bumps that folder's mtime but not its parent's — invalidates the fast path.
A file-form sloppak (a plain .feedpak zip) is not a dir and adds nothing."""
scan = importlib.import_module("scan")
dlc = tmp_path / "dlc"
(dlc / "packs").mkdir(parents=True)
loose = dlc / "packs" / "my_loose_song" # directory-form song
loose.mkdir()
zipped = dlc / "packs" / "zipped.feedpak" # file-form song
zipped.write_bytes(b"")
rels = scan._library_dirs([loose, zipped], dlc)
assert "packs/my_loose_song" in rels, "directory-form song must track its own dir"
assert "packs" in rels and "." in rels
assert "packs/zipped.feedpak" not in rels, "a file-form sloppak is not a tracked dir"
# ── 3. POST /api/songs/upload gate (endpoint) ────────────────────────────────
@pytest.fixture()