A refresh compare and the doc modeler read file_group through its indexes (#1083)

A refresh compared its roots with `directory LIKE 'root/%'`, and the doc
modeler found comics with `primary_path LIKE '%.cbz'`.  SQLite cannot use an
index for either, so both read every row of file_group.  With a cold page cache
on a large library, a refresh of one empty directory took ~3.5 minutes, and a
move queued behind it waited that long.

The compare now uses a range on `directory` (as `directory_or_descendant` does),
which also stops `_` and `%` in a path acting as wildcards: refreshing `a_b`
reported the FileGroups of `aXb/...` as deleted.  The doc modeler matches the
indexed, lowercased `suffix` column instead.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-12-02 15:04:04 -07:00 • committed by Roland
parent 1c095d1104
commit 0ccedde3b0
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 82 additions and 10 deletions

View file

@ -1,7 +1,7 @@
import logging
from typing import Callable
from sqlalchemy import or_
from sqlalchemy import and_, or_
from sqlalchemy.orm import Session
from wrolpi.common import register_modeler, register_refresh_cleanup, \
@ -23,13 +23,13 @@ DOC_PROCESSING_LIMIT = 10
DOC_LOCK_RETRIES = 3
def _select_doc_ids(session, skip_ids: set) -> list[int]:
query = session.query(FileGroup.id) \
.outerjoin(Doc, Doc.file_group_id == FileGroup.id) \
.filter(
def _doc_candidate_filter():
return and_(
or_(
*[FileGroup.mimetype == mt for mt in DOC_MIMETYPES],
*[FileGroup.primary_path.like(f'%{suffix}') for suffix in COMIC_BOOK_SUFFIXES],
# A comic whose mimetype was not detected is found by its suffix. `suffix` is indexed and
# lowercased; `primary_path LIKE '%.cbz'` read every row of file_group.
FileGroup.suffix.in_(COMIC_BOOK_SUFFIXES),
),
# Model any doc that has no Doc row yet (even if `apply_indexers` already set
# `indexed=True` before this modeler ran), or any doc explicitly flagged for
@ -40,6 +40,12 @@ def _select_doc_ids(session, skip_ids: set) -> list[int]:
FileGroup.indexed != True,
),
)
def _select_doc_ids(session, skip_ids: set) -> list[int]:
query = session.query(FileGroup.id) \
.outerjoin(Doc, Doc.file_group_id == FileGroup.id) \
.filter(_doc_candidate_filter())
query = skip_clause(query, FileGroup.id, skip_ids)
return [row[0] for row in query.limit(DOC_PROCESSING_LIMIT).all()]