From 1edc2ba4e674c9158a756a7d6231477c5c52e076 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 2 Nov 2022 13:12:29 -0600 Subject: [PATCH] Only changing File.a_text when file has changed. --- wrolpi/files/models.py | 24 +++++++++++++++++++----- wrolpi/files/test/test_lib.py | 15 ++++++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/wrolpi/files/models.py b/wrolpi/files/models.py index 945ed407..b7a20869 100644 --- a/wrolpi/files/models.py +++ b/wrolpi/files/models.py @@ -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): diff --git a/wrolpi/files/test/test_lib.py b/wrolpi/files/test/test_lib.py index 8b842f36..7689096c 100644 --- a/wrolpi/files/test/test_lib.py +++ b/wrolpi/files/test/test_lib.py @@ -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):