fix(tagging): Images tag search goes through the tag index (#379, PR 3) (#395)

* fix(tagging): Images tag search goes through the tag index (#379, PR 3)

The Images Tags filter and text search no longer stream every image tag
row per request. image_tag_ids reads the names that tag at least one image
(exact under Tags, case-folded substring for q) and image_tag_locators
reads their files by tag_id through idx_media_image_tags_tag. Results are
unchanged: a differential test compares the lookup with the old stream,
and a trace test checks that no statement reads media_image_tags except
by tag id. The search contract in TESTING.md and a browser test (CUJ 8)
follow.

Closes #379

* test(search): pin prompt-only tags and multi-chunk tag lookups (#379)

Reads the tag names and the locators in the same annotation read helper.
This commit is contained in:
Vito
2026-10-27 22:07:59 +00:00
committed by GitHub
parent d2ea705a44
commit 37e4e08327
6 changed files with 282 additions and 47 deletions
+37 -21
View File
@@ -110,12 +110,16 @@ _MARKED_LOCATORS = (
f"SELECT DISTINCT f.locator FROM {AUTOTAG_TABLE} m " # nosec B608
f"JOIN {BYTES_TABLE} b ON b.id=m.byte_id JOIN {FILES_TABLE} f ON f.id=b.file_id"
)
_RECORDS = (
"SELECT f.locator,b.id,b.size,b.mtime_ns,t.name " # nosec B608
f"FROM {FILES_TABLE} f JOIN {BYTES_TABLE} b ON b.file_id=f.id "
f"LEFT JOIN {IMAGE_TAGS_TABLE} mit ON mit.byte_id=b.id "
"LEFT JOIN tags t ON t.id=mit.tag_id "
"ORDER BY f.locator,b.first_seen_at,b.id,mit.position"
# Tag search (#379): the names that tag at least one image (the tag index
# answers the EXISTS), then the locators carrying chosen names (by tag_id).
_IMAGE_TAG_NAMES = (
"SELECT t.id,t.name FROM tags t WHERE EXISTS " # nosec B608
f"(SELECT 1 FROM {IMAGE_TAGS_TABLE} mit WHERE mit.tag_id=t.id)"
)
_TAG_LOCATORS = (
f"SELECT DISTINCT f.locator FROM {IMAGE_TAGS_TABLE} mit " # nosec B608
f"JOIN {BYTES_TABLE} b ON b.id=mit.byte_id "
f"JOIN {FILES_TABLE} f ON f.id=b.file_id WHERE mit.tag_id IN ({{marks}})"
)
@@ -516,20 +520,32 @@ class AnnotationOperations:
with self._annotation_read() as conn:
return {row[0] for row in conn.execute(_MARKED_LOCATORS)}
def iter_image_tag_records(self) -> Iterator[tuple[str, int, int, list[str]]]:
"""Stream every byte record as ``(locator, size, mtime_ns, tags)``.
def image_tag_ids(self, wanted, query: str) -> list[int]:
"""Ids of the tag names on at least one image that a search wants.
One SELECT ordered by locator, then by the bytes' first sighting, with
each set's names in position order.
A name counts when it is in ``wanted`` exactly (the Tags filter) or
contains ``query`` case-folded (the text search; ``query`` is
already folded). One read of the names that tag an image.
"""
conn = self.model.get_connection()
current: tuple | None = None
for locator, byte_id, size, mtime_ns, name in conn.execute(_RECORDS):
if current is None or current[0] != byte_id:
if current is not None:
yield current[1]
current = (byte_id, (locator, size, mtime_ns, []))
if name is not None:
current[1][3].append(name)
if current is not None:
yield current[1]
wanted = set(wanted)
with self._annotation_read() as conn:
return [
tag_id
for tag_id, name in conn.execute(_IMAGE_TAG_NAMES)
if name in wanted or (query and query in name.casefold())
]
def image_tag_locators(self, tag_ids) -> set[str]:
"""Locators with any byte record (current or not) carrying one of
``tag_ids``, through the tag index, in chunks of 500 ids."""
ids = list(dict.fromkeys(tag_ids))
found: set[str] = set()
with self._annotation_read() as conn:
for start in range(0, len(ids), _LOOKUP_CHUNK):
chunk = ids[start : start + _LOOKUP_CHUNK]
marks = ",".join("?" * len(chunk))
found.update(
row[0]
for row in conn.execute(_TAG_LOCATORS.format(marks=marks), chunk)
)
return found