Skip to content

Commit d1fc92b

Browse files
committed
Removed a duplicate computation of body's filehash (#1074)
1 parent e45edfb commit d1fc92b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/paperqa/agents/search.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,12 @@ async def index_files(self) -> dict[str, str]:
261261
def filehash(body: str) -> str:
262262
return hexdigest(body)
263263

264-
async def filecheck(self, filename: str, body: str | None = None) -> bool:
264+
async def filecheck(self, filename: str, body_filehash: str | None = None) -> bool:
265265
"""Check if this index contains the filename and if the body's filehash matches."""
266-
filehash: str | None = self.filehash(body) if body else None
267266
index_files = await self.index_files
268267
return bool(
269268
index_files.get(filename)
270-
and (filehash is None or index_files[filename] == filehash)
269+
and (body_filehash is None or index_files[filename] == body_filehash)
271270
)
272271

273272
async def mark_failed_document(self, path: str | os.PathLike) -> None:
@@ -297,19 +296,20 @@ async def add_document(
297296
retry=retry_if_exception_type(AsyncRetryError),
298297
)
299298
async def _add_document() -> None:
300-
if not await self.filecheck(index_doc["file_location"], index_doc["body"]):
299+
body_filehash = self.filehash(index_doc["body"])
300+
if not await self.filecheck(index_doc["file_location"], body_filehash):
301301
try:
302302
async with self.writer() as writer:
303303
# Let caller handle commit to allow for batching
304304
writer.add_document(Document.from_dict(index_doc))
305305

306-
filehash = self.filehash(index_doc["body"])
307-
(await self.index_files)[index_doc["file_location"]] = filehash
306+
(await self.index_files)[index_doc["file_location"]] = body_filehash
308307

309308
if document:
310309
docs_index_dir = await self.docs_index_directory
311310
async with await anyio.open_file(
312-
docs_index_dir / f"{filehash}.{self.storage.extension()}",
311+
docs_index_dir
312+
/ f"{body_filehash}.{self.storage.extension()}",
313313
"wb",
314314
) as f:
315315
await f.write(self.storage.write_to_string(document))

0 commit comments

Comments
 (0)