|
14 | 14 |
|
15 | 15 | from prime_backup.db import schema, db_constants |
16 | 16 | from prime_backup.db.values import FileRole, BackupTagDict, OffsetChunk, OffsetChunkGroup, BlobStorageMethod, ChunkGroupChunkBindingIdentifier, BlobChunkGroupBindingIdentifier, FileIdentifier |
17 | | -from prime_backup.exceptions import BackupNotFound, BackupFileNotFound, BlobHashNotFound, PrimeBackupError, FilesetNotFound, FilesetFileNotFound, BlobIdNotFound, ChunkHashNotFound, ChunkIdNotFound, ChunkGroupChunkBindingNotFound, BlobChunkGroupBindingNotFound |
| 17 | +from prime_backup.exceptions import BackupNotFound, BackupFileNotFound, BlobHashNotFound, PrimeBackupError, FilesetNotFound, FilesetFileNotFound, BlobIdNotFound, ChunkHashNotFound, ChunkIdNotFound, ChunkGroupChunkBindingNotFound, BlobChunkGroupBindingNotFound, ChunkGroupIdNotFound, ChunkGroupHashNotFound |
18 | 18 | from prime_backup.types.backup_filter import BackupFilter, BackupTagFilter, BackupSortOrder |
19 | 19 | from prime_backup.utils import collection_utils, db_utils, validation_utils |
20 | 20 |
|
@@ -380,6 +380,10 @@ def list_chunks(self, limit: Optional[int] = None, offset: Optional[int] = None) |
380 | 380 | s = s.offset(offset) |
381 | 381 | return _list_it(self.session.execute(s).scalars().all()) |
382 | 382 |
|
| 383 | + def list_chunk_with_hash_prefix(self, hash_prefix: str, limit: int) -> List[schema.Chunk]: |
| 384 | + s = select(schema.Chunk).where(schema.Chunk.hash.startswith(hash_prefix, autoescape=True)).limit(limit) |
| 385 | + return _list_it(self.session.execute(s).scalars().all()) |
| 386 | + |
383 | 387 | def iterate_chunk_batch(self, *, batch_size: int) -> Iterator[List[schema.Chunk]]: |
384 | 388 | limit, offset = batch_size, 0 |
385 | 389 | while True: |
@@ -430,11 +434,26 @@ def create_and_add_chunk_group(self, **kwargs: Unpack[CreateChunkGroupKwargs]) - |
430 | 434 | def get_chunk_group_count(self) -> int: |
431 | 435 | return _int_or_0(self.session.execute(select(func.count()).select_from(schema.ChunkGroup)).scalar_one()) |
432 | 436 |
|
433 | | - def get_chunk_group_opt(self, h: str) -> Optional[schema.ChunkGroup]: |
| 437 | + def get_chunk_group_by_id_opt(self, chunk_group_id: int) -> Optional[schema.ChunkGroup]: |
| 438 | + return self.session.get(schema.ChunkGroup, chunk_group_id) |
| 439 | + |
| 440 | + def get_chunk_group_by_id(self, chunk_group_id: int) -> schema.ChunkGroup: |
| 441 | + chunk_group = self.get_chunk_group_by_id_opt(chunk_group_id) |
| 442 | + if chunk_group is None: |
| 443 | + raise ChunkGroupIdNotFound(chunk_group_id) |
| 444 | + return chunk_group |
| 445 | + |
| 446 | + def get_chunk_group_by_hash_opt(self, h: str) -> Optional[schema.ChunkGroup]: |
434 | 447 | return self.session.execute( |
435 | 448 | select(schema.ChunkGroup).where(schema.ChunkGroup.hash == h) |
436 | 449 | ).scalars().one_or_none() |
437 | 450 |
|
| 451 | + def get_chunk_group_by_hash(self, h: str) -> schema.ChunkGroup: |
| 452 | + chunk_group = self.get_chunk_group_by_hash_opt(h) |
| 453 | + if chunk_group is None: |
| 454 | + raise ChunkGroupHashNotFound(h) |
| 455 | + return chunk_group |
| 456 | + |
438 | 457 | def get_chunk_groups_by_ids(self, chunk_group_ids: List[int]) -> Dict[int, Optional[schema.ChunkGroup]]: |
439 | 458 | """ |
440 | 459 | :return: a dict, id -> optional chunk group. All given ids are in the dict |
@@ -463,6 +482,10 @@ def list_chunk_groups(self, limit: Optional[int] = None, offset: Optional[int] = |
463 | 482 | s = s.offset(offset) |
464 | 483 | return _list_it(self.session.execute(s).scalars().all()) |
465 | 484 |
|
| 485 | + def list_chunk_group_with_hash_prefix(self, hash_prefix: str, limit: int) -> List[schema.ChunkGroup]: |
| 486 | + s = select(schema.ChunkGroup).where(schema.ChunkGroup.hash.startswith(hash_prefix, autoescape=True)).limit(limit) |
| 487 | + return _list_it(self.session.execute(s).scalars().all()) |
| 488 | + |
466 | 489 | def iterate_chunk_group_batch(self, *, batch_size: int) -> Iterator[List[schema.ChunkGroup]]: |
467 | 490 | limit, offset = batch_size, 0 |
468 | 491 | while True: |
|
0 commit comments