Only changing File.a_text when file has changed.

This commit is contained in:
Roland 2022-11-02 13:12:29 -06:00
parent 602aa32ff0
commit 1edc2ba4e6
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
2 changed files with 31 additions and 8 deletions

View file

@ -108,9 +108,17 @@ class File(ModelHelper, Base):
except Exception as e:
logger.error(f'Failed to index {self.path}', exc_info=e)
def do_stats(self):
"""Assign the mimetype, title, size, modification_time of this file."""
def do_stats(self) -> bool:
"""Assign the mimetype, title, size, modification_time of this file.
Returns True if the file has changed since last index. Change is detected by comparing old size, mimetype
modification datetime."""
from wrolpi.files.lib import split_path_stem_and_suffix
old_mimetype = self.mimetype
old_size = self.size
old_modification_datetime = self.modification_datetime
if not self.mimetype:
from wrolpi.files.lib import get_mimetype
self.mimetype = get_mimetype(self.path)
@ -120,9 +128,15 @@ class File(ModelHelper, Base):
self.modification_datetime = from_timestamp(stat.st_mtime)
name, self.suffix = split_path_stem_and_suffix(self.path)
# Use title as the a_text temporarily, this may be overwritten by indexer.
# TODO this is very slow. What can be done instead?
self.a_text = self.a_text or name
changed = old_mimetype != self.mimetype \
or old_size != self.size \
or old_modification_datetime != self.modification_datetime
if changed:
self.indexed = False
# Use title as the a_text temporarily, this may be overwritten by indexer.
self.a_text = self.a_text or name
return changed
@validates('path')
def validate_path(self, key, value):

View file

@ -13,7 +13,7 @@ from PIL import Image
from sqlalchemy.orm import Session
from modules import videos
from wrolpi.common import get_media_directory
from wrolpi.common import get_media_directory, timer
from wrolpi.errors import InvalidFile
from wrolpi.files import lib, indexers
from wrolpi.files.models import File
@ -187,12 +187,15 @@ async def test_refresh_files(test_session, make_files_structure, test_directory)
@pytest.mark.asyncio
async def test_refresh_many_files(test_session, make_files_structure):
"""Used to profile file refreshing"""
count = 1_000
make_files_structure([f'{uuid4()}.txt' for _ in range(count)])
await lib.refresh_files()
with timer('first refresh'):
await lib.refresh_files()
assert test_session.query(File).count() == count
await lib.refresh_files()
with timer('second refresh'):
await lib.refresh_files()
assert test_session.query(File).count() == count
@ -344,6 +347,12 @@ async def test_files_indexer(test_session, make_files_structure):
mock_create_index.side_effect = Exception('This should not be called twice')
await lib.refresh_files()
# Change the contents, the file should be re-indexed.
text_path.write_text('new text contents')
await lib.refresh_files()
files, total = lib.search_files('new', 10, 0)
assert total == 1
@pytest.mark.asyncio
async def test_large_text_indexer(test_session, make_files_structure):