|
21 | 21 |
|
22 | 22 | if TYPE_CHECKING: |
23 | 23 | from sqlalchemy.sql.type_api import TypeEngine |
| 24 | + from prime_backup.types.chunker import PrettyChunk |
24 | 25 |
|
25 | 26 |
|
26 | 27 | _T = TypeVar('_T') |
@@ -1097,6 +1098,43 @@ def get_blob_chunks(self, blob_id: int, *, limit: Optional[int] = None) -> List[ |
1097 | 1098 | result: Sequence[Row[Tuple[int, schema.Chunk]]] = self.session.execute(stmt).all() |
1098 | 1099 | return [OffsetChunk(offset, chunk) for offset, chunk in result] |
1099 | 1100 |
|
| 1101 | + def batch_get_blob_pretty_chunks(self, blob_ids: List[int]) -> Dict[int, List['PrettyChunk']]: |
| 1102 | + """ |
| 1103 | + The result contains all given blob ids. Chunk lists are sorted. |
| 1104 | + """ |
| 1105 | + from prime_backup.types.chunker import PrettyChunk |
| 1106 | + |
| 1107 | + blob_ids = collection_utils.deduplicated_list(blob_ids) |
| 1108 | + result: Dict[int, List['PrettyChunk']] = {blob_id: [] for blob_id in blob_ids} |
| 1109 | + if len(blob_ids) == 0: |
| 1110 | + return result |
| 1111 | + |
| 1112 | + absolute_offset = (schema.BlobChunkGroupBinding.chunk_group_offset + schema.ChunkGroupChunkBinding.chunk_offset).label('absolute_offset') |
| 1113 | + for view in collection_utils.slicing_iterate(blob_ids, self.__safe_var_limit): |
| 1114 | + stmt = ( |
| 1115 | + select( |
| 1116 | + schema.BlobChunkGroupBinding.blob_id, |
| 1117 | + absolute_offset, |
| 1118 | + schema.Chunk.raw_size, |
| 1119 | + schema.Chunk.hash, |
| 1120 | + ). |
| 1121 | + select_from(schema.BlobChunkGroupBinding). |
| 1122 | + join( |
| 1123 | + schema.ChunkGroupChunkBinding, |
| 1124 | + schema.BlobChunkGroupBinding.chunk_group_id == schema.ChunkGroupChunkBinding.chunk_group_id |
| 1125 | + ). |
| 1126 | + join( |
| 1127 | + schema.Chunk, |
| 1128 | + schema.ChunkGroupChunkBinding.chunk_id == schema.Chunk.id |
| 1129 | + ). |
| 1130 | + where(schema.BlobChunkGroupBinding.blob_id.in_(view)). |
| 1131 | + order_by(schema.BlobChunkGroupBinding.blob_id, absolute_offset) |
| 1132 | + ) |
| 1133 | + rows: Sequence[Row[Tuple[int, int, int, str]]] = self.session.execute(stmt).all() |
| 1134 | + for blob_id, offset, raw_size, chunk_hash in rows: |
| 1135 | + result[blob_id].append(PrettyChunk(offset, raw_size, chunk_hash)) |
| 1136 | + return result |
| 1137 | + |
1100 | 1138 | @dataclasses.dataclass(frozen=True) |
1101 | 1139 | class ListBlobChunkGroupBindingsItem: |
1102 | 1140 | binding: schema.BlobChunkGroupBinding |
|
0 commit comments