Skip to content

Commit 50d97e6

Browse files
committed
optimize BackupFinalizer.finalize_files_and_backup blob querying
1 parent 94c774e commit 50d97e6

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

prime_backup/action/helpers/backup_finalizer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ def __init__(self, session: DbSession):
1818
def finalize_files_and_backup(self, backup: schema.Backup, files: List[schema.File]):
1919
self.session.flush() # ensure all blobs has their blob.id allocated
2020

21-
file_blobs = self.session.get_blobs_by_hashes_opt([
21+
hash_to_blob_id = self.session.get_blob_ids_by_hashes_opt([
2222
file.blob_hash for file in files
2323
if file.blob_hash is not None
2424
])
2525
for file in files:
2626
if file.blob_hash is not None:
27-
file_blob = file_blobs[file.blob_hash]
28-
if file_blob is None:
27+
blob_id = hash_to_blob_id[file.blob_hash]
28+
if blob_id is None:
2929
raise AssertionError('blob of file does not exists: {}'.format(file))
30-
file.blob_id = file_blob.id
30+
file.blob_id = blob_id
3131

3232
allocate_args = FilesetAllocateArgs.from_config(self.config)
3333
allocate_result = FilesetAllocator(self.session, files).allocate(allocate_args)

prime_backup/db/session.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,19 @@ def get_blobs_by_hashes_opt(self, hashes: List[str]) -> Dict[str, Optional[schem
347347
result[blob.hash] = blob
348348
return result
349349

350+
def get_blob_ids_by_hashes_opt(self, hashes: List[str]) -> Dict[str, Optional[int]]:
351+
"""
352+
:return: a dict, hash -> optional blob id. All given hashes are in the dict
353+
"""
354+
result: Dict[str, Optional[int]] = {h: None for h in hashes}
355+
for view in collection_utils.slicing_iterate(hashes, self.__safe_var_limit):
356+
for blob_hash, blob_id in self.session.execute(
357+
select(schema.Blob.hash, schema.Blob.id).
358+
where(schema.Blob.hash.in_(view))
359+
).all():
360+
result[blob_hash] = blob_id
361+
return result
362+
350363
def get_blobs_by_hashes(self, hashes: List[str]) -> Dict[str, schema.Blob]:
351364
"""
352365
:return: a dict, hash -> blob. All given hashes are in the dict

0 commit comments

Comments
 (0)