Skip to content

Commit a8337ff

Browse files
committed
Avoid unnecessary legacy hash computation
1 parent 641de7f commit a8337ff

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

bookworm/gui/book_viewer/recents_manager.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import sqlalchemy as sa
3+
24
from bookworm.database import PinnedDocument, RecentDocument
35
from bookworm.document.hash_utils import get_persistent_content_hash
46
from bookworm.logger import logger
@@ -42,6 +44,16 @@ def _get_legacy_documents_by_content_hash(model, content_hash, uri):
4244
return matching_documents
4345

4446

47+
def _has_legacy_documents(model, uri):
48+
documents = model.query.filter(
49+
sa.or_(
50+
model.content_hash_version.is_(None),
51+
model.content_hash_version != CURRENT_CONTENT_HASH_VERSION,
52+
)
53+
)
54+
return any(doc.uri.format == uri.format for doc in documents)
55+
56+
4557
def _merge_matching_documents(uri_document, hash_matching_documents):
4658
matching_documents = []
4759
matching_ids = set()
@@ -90,19 +102,25 @@ def get_document_unique(model, document):
90102
)
91103
if content_hash is _CONTENT_HASH_UNSET:
92104
content_hash = document.get_content_hash()
93-
legacy_content_hash = document.get_legacy_content_hash()
105+
should_check_legacy_documents = content_hash is None or _has_legacy_documents(
106+
model, uri
107+
)
108+
legacy_content_hash = (
109+
document.get_legacy_content_hash() if should_check_legacy_documents else None
110+
)
94111
stored_content_hash, stored_content_hash_version = get_persistent_content_hash(
95112
content_hash,
96113
legacy_content_hash,
97114
)
98115
hash_matching_documents = _get_documents_by_content_hash(model, content_hash, uri)
99-
hash_matching_documents.extend(
100-
_get_legacy_documents_by_content_hash(
101-
model,
102-
legacy_content_hash,
103-
uri,
116+
if should_check_legacy_documents:
117+
hash_matching_documents.extend(
118+
_get_legacy_documents_by_content_hash(
119+
model,
120+
legacy_content_hash,
121+
uri,
122+
)
104123
)
105-
)
106124
matching_documents = _merge_matching_documents(doc, hash_matching_documents)
107125
doc = doc or (matching_documents[0] if matching_documents else None)
108126
if doc is not None:

bookworm/reader.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ def _get_legacy_matching_document_records(self, model, *, uri_for_storage, legac
183183
)
184184
return [record for record in records if record.uri.format == uri_for_storage.format]
185185

186+
def _has_legacy_document_records(self, model, uri_for_storage):
187+
records = model.query.filter(
188+
sa.or_(
189+
model.content_hash_version.is_(None),
190+
model.content_hash_version != CURRENT_CONTENT_HASH_VERSION,
191+
)
192+
)
193+
return any(record.uri.format == uri_for_storage.format for record in records)
194+
186195
def _select_document_record(self, records, uri_for_storage):
187196
for record in records:
188197
if record.uri == uri_for_storage:
@@ -252,10 +261,12 @@ def _get_or_create_document_record(
252261
content_hash = uri_record.content_hash
253262
else:
254263
content_hash = content_hash_provider()
264+
should_check_legacy_records = legacy_content_hash_provider is not None and (
265+
content_hash is None
266+
or self._has_legacy_document_records(model, uri_for_storage)
267+
)
255268
legacy_content_hash = (
256-
legacy_content_hash_provider()
257-
if legacy_content_hash_provider is not None
258-
else None
269+
legacy_content_hash_provider() if should_check_legacy_records else None
259270
)
260271
stored_content_hash, stored_content_hash_version = get_persistent_content_hash(
261272
content_hash,
@@ -266,7 +277,7 @@ def _get_or_create_document_record(
266277
uri_for_storage=uri_for_storage,
267278
content_hash=content_hash,
268279
)
269-
if legacy_content_hash_provider is not None:
280+
if should_check_legacy_records:
270281
matching_records.extend(
271282
self._get_legacy_matching_document_records(
272283
model,

0 commit comments

Comments
 (0)