From 3266b6c8b8e723fb8fddf1284cd3344ef5a0a7b3 Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 16 Dec 2022 10:14:55 -0700 Subject: [PATCH] Adding Ebook.find_by_paths to speed up searches. --- wrolpi/files/ebooks.py | 13 +++++++++---- wrolpi/files/models.py | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/wrolpi/files/ebooks.py b/wrolpi/files/ebooks.py index 329e8170..e84068b9 100644 --- a/wrolpi/files/ebooks.py +++ b/wrolpi/files/ebooks.py @@ -163,6 +163,15 @@ class EBook(ModelHelper, Base): if self.cover_file: yield self.cover_file + @staticmethod + def find_by_path(path, session) -> Base: + return session.query(EBook).filter(EBook.ebook_path == path).one_or_none() + + @staticmethod + def find_by_paths(paths: List[pathlib.Path], session) -> List: + ebooks = list(session.query(EBook).filter(EBook.ebook_path.in_(paths))) + return ebooks + def __json__(self): d = dict( cover_path=self.cover_path, @@ -192,10 +201,6 @@ class EBook(ModelHelper, Base): logger.warning(f'Unable to generate cover for {self.ebook_path}') - @staticmethod - def find_by_path(path, session) -> Base: - return session.query(EBook).filter(EBook.ebook_path == path).one_or_none() - def model_ebook(ebook: EBook, files: List[File]) -> EBook: """Creates an EBook model based off a File. Searches for it's cover in the provided `files`.""" diff --git a/wrolpi/files/models.py b/wrolpi/files/models.py index 809a6680..d4b1f2d8 100644 --- a/wrolpi/files/models.py +++ b/wrolpi/files/models.py @@ -25,7 +25,7 @@ class File(ModelHelper, Base): full_stem = Column(String) # f'{directory}/{stem}' idempotency = Column(TZDateTime, default=lambda: now()) indexed = Column(Boolean, default=False) - mimetype = Column(String) # Gotten by python-magic or file command. + mimetype = Column(String) # wrolpi.files.lib.get_mimetype model = Column(String) # Filled out by the modeler. Will be the table name (video, archive, etc.). modification_datetime = Column(TZDateTime) size = Column(Integer)