Skip to content

Commit 5fee547

Browse files
committed
refactor: consolidate snapshot expiration into MaintenanceTable
- Move ExpireSnapshots functionality from standalone class to MaintenanceTable - Replace fluent API (table.expire_snapshots().method().commit()) with direct execution (table.maintenance.method()) - Remove ExpireSnapshots class and integrate logic into maintenance operations - Update all tests to use new unified maintenance API - Maintain all existing validation and protection logic for snapshots This change consolidates table maintenance operations under a single interface and simplifies the API by removing the need for explicit commit calls. Breaking change: table.expire_snapshots() API is replaced with table.maintenance.expire_*() methods
1 parent 0c1e4c7 commit 5fee547

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pyiceberg/table/maintenance.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
if TYPE_CHECKING:
3030
from pyiceberg.table import Table
31+
from pyiceberg.table.metadata import TableMetadata
3132

3233

3334
class MaintenanceTable:
@@ -139,6 +140,7 @@ def expire_snapshot_by_id(self, snapshot_id: int) -> None:
139140

140141
# Remove the snapshot
141142
from pyiceberg.table.update import RemoveSnapshotsUpdate
143+
142144
txn._apply((RemoveSnapshotsUpdate(snapshot_ids=[snapshot_id]),))
143145

144146
def expire_snapshots_by_ids(self, snapshot_ids: List[int]) -> None:
@@ -152,7 +154,7 @@ def expire_snapshots_by_ids(self, snapshot_ids: List[int]) -> None:
152154
"""
153155
with self.tbl.transaction() as txn:
154156
protected_ids = self._get_protected_snapshot_ids(txn.table_metadata)
155-
157+
156158
# Validate all snapshots before expiring any
157159
for snapshot_id in snapshot_ids:
158160
if txn.table_metadata.snapshot_by_id(snapshot_id) is None:
@@ -162,6 +164,7 @@ def expire_snapshots_by_ids(self, snapshot_ids: List[int]) -> None:
162164

163165
# Remove all snapshots
164166
from pyiceberg.table.update import RemoveSnapshotsUpdate
167+
165168
txn._apply((RemoveSnapshotsUpdate(snapshot_ids=snapshot_ids),))
166169

167170
def expire_snapshots_older_than(self, timestamp_ms: int) -> None:
@@ -173,17 +176,18 @@ def expire_snapshots_older_than(self, timestamp_ms: int) -> None:
173176
# First check if there are any snapshots to expire to avoid unnecessary transactions
174177
protected_ids = self._get_protected_snapshot_ids(self.tbl.metadata)
175178
snapshots_to_expire = []
176-
179+
177180
for snapshot in self.tbl.metadata.snapshots:
178181
if snapshot.timestamp_ms < timestamp_ms and snapshot.snapshot_id not in protected_ids:
179182
snapshots_to_expire.append(snapshot.snapshot_id)
180-
183+
181184
if snapshots_to_expire:
182185
with self.tbl.transaction() as txn:
183186
from pyiceberg.table.update import RemoveSnapshotsUpdate
187+
184188
txn._apply((RemoveSnapshotsUpdate(snapshot_ids=snapshots_to_expire),))
185189

186-
def _get_protected_snapshot_ids(self, table_metadata) -> Set[int]:
190+
def _get_protected_snapshot_ids(self, table_metadata: TableMetadata) -> Set[int]:
187191
"""Get the IDs of protected snapshots.
188192
189193
These are the HEAD snapshots of all branches and all tagged snapshots.
@@ -196,7 +200,7 @@ def _get_protected_snapshot_ids(self, table_metadata) -> Set[int]:
196200
Set of protected snapshot IDs to exclude from expiration.
197201
"""
198202
from pyiceberg.table.refs import SnapshotRefType
199-
203+
200204
protected_ids: Set[int] = set()
201205
for ref in table_metadata.refs.values():
202206
if ref.snapshot_ref_type in [SnapshotRefType.TAG, SnapshotRefType.BRANCH]:

pyiceberg/table/update/snapshot.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
from pyiceberg.partitioning import (
5757
PartitionSpec,
5858
)
59-
from pyiceberg.table.refs import SnapshotRefType
6059
from pyiceberg.table.snapshots import (
6160
Operation,
6261
Snapshot,
@@ -68,7 +67,6 @@
6867
AddSnapshotUpdate,
6968
AssertRefSnapshotId,
7069
RemoveSnapshotRefUpdate,
71-
RemoveSnapshotsUpdate,
7270
SetSnapshotRefUpdate,
7371
TableRequirement,
7472
TableUpdate,

0 commit comments

Comments
 (0)