Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve relink performance #614

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions tagstudio/src/core/utils/missing_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MissingRegistry:
library: Library
files_fixed_count: int = 0
missing_files: list[Entry] = field(default_factory=list)
library_file_cache: set[Path] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#608 already added a library file cache here. Could you leverage that one instead of adding a new one?


@property
def missing_files_count(self) -> int:
Expand All @@ -40,15 +41,18 @@ def match_missing_file(self, match_item: Entry) -> list[Path]:
Works if files were just moved to different subfolders and don't have duplicate names.
"""
matches = []
for item in self.library.library_dir.glob(f"**/{match_item.path.name}"):
if item.name == match_item.path.name: # TODO - implement IGNORE_ITEMS
new_path = Path(item).relative_to(self.library.library_dir)
matches.append(new_path)
# TODO - implement IGNORE_ITEMS
item_name = match_item.path.name
for matched_path in filter(lambda file: file.name == item_name, self.library_file_cache):
logger.info("match_missing_files", matched_path=matched_path)
new_path = Path(matched_path).relative_to(self.library.library_dir)
matches.append(new_path)

return matches

def fix_missing_files(self) -> Iterator[int]:
"""Attempt to fix missing files by finding a match in the library directory."""
self.library_file_cache = set(self.library.library_dir.rglob("*"))
self.files_fixed_count = 0
for i, entry in enumerate(self.missing_files, start=1):
item_matches = self.match_missing_file(entry)
Expand All @@ -59,6 +63,7 @@ def fix_missing_files(self) -> Iterator[int]:
# remove fixed file
self.missing_files.remove(entry)
yield i
self.library_file_cache = None

def execute_deletion(self) -> Iterator[int]:
for i, missing in enumerate(self.missing_files, start=1):
Expand Down
Loading