rdblue commented on code in PR #4578:
URL: https://github.com/apache/iceberg/pull/4578#discussion_r873048696
##########
core/src/main/java/org/apache/iceberg/RemoveSnapshots.java:
##########
@@ -155,27 +168,93 @@ public List<Snapshot> apply() {
TableMetadata updated = internalApply();
List<Snapshot> removed = Lists.newArrayList(base.snapshots());
removed.removeAll(updated.snapshots());
-
return removed;
}
private TableMetadata internalApply() {
this.base = ops.refresh();
+ if (base.refs().isEmpty()) {
+ return base;
+ }
Set<Long> idsToRetain = Sets.newHashSet();
- List<Long> ancestorIds = SnapshotUtil.ancestorIds(base.currentSnapshot(),
base::snapshot);
- if (minNumSnapshots >= ancestorIds.size()) {
- idsToRetain.addAll(ancestorIds);
- } else {
- idsToRetain.addAll(ancestorIds.subList(0, minNumSnapshots));
+ // Identify refs that should be removed
+ Map<String, SnapshotRef> retainedRefs = computeRetainedRefs(base.refs());
+ Set<Long> retainedRefIds = retainedRefs.entrySet().stream()
+ .map(refEntry -> refEntry.getValue().snapshotId())
+ .collect(Collectors.toSet());
+
+ for (long idToRemove : idsToRemove) {
+ Preconditions.checkArgument(!retainedRefIds.contains(idToRemove),
+ "Cannot expire %s. There is a reference to it", idToRemove);
+ }
+
+ idsToRetain.addAll(retainedRefIds);
+ Set<Long> branchSnapshotsToRetain =
computeAllBranchSnapshotsToRetain(retainedRefs);
+ idsToRetain.addAll(branchSnapshotsToRetain);
+ TableMetadata.Builder updatedMetaBuilder = TableMetadata.buildFrom(base);
+ idsToRemove.addAll(base.snapshots().stream().map(Snapshot::snapshotId)
+ .filter(snapshot ->
!idsToRetain.contains(snapshot)).collect(Collectors.toSet()));
+ base.refs().keySet().stream().filter(ref ->
!retainedRefs.containsKey(ref)).forEach(updatedMetaBuilder::removeRef);
+ updatedMetaBuilder.removeSnapshots(idsToRemove);
+ return updatedMetaBuilder.build();
+ }
+
+ private Set<Long> computeAllBranchSnapshotsToRetain(Map<String, SnapshotRef>
refs) {
+ Set<Long> branchSnapshotsToRetain = Sets.newHashSet();
+ for (Map.Entry<String, SnapshotRef> refEntry : refs.entrySet()) {
+ SnapshotRef ref = refEntry.getValue();
+ if (ref.isBranch()) {
+ long expireSnapshotsOlderThan;
+ int minSnapshotsToKeep;
+ if (ref.maxSnapshotAgeMs() == null) {
+ expireSnapshotsOlderThan = expireOlderThan;
+ } else {
+ expireSnapshotsOlderThan = now - ref.maxSnapshotAgeMs();
+ }
+
+ if (ref.minSnapshotsToKeep() == null) {
+ minSnapshotsToKeep = defaultMinNumSnapshots;
+ } else {
+ minSnapshotsToKeep = ref.minSnapshotsToKeep();
+ }
+
+ branchSnapshotsToRetain.addAll(
+ computeBranchSnapshotsToRetain(ref.snapshotId(),
expireSnapshotsOlderThan, minSnapshotsToKeep));
+ }
+ }
+
+ return branchSnapshotsToRetain;
+ }
+
+ private Map<String, SnapshotRef> computeRetainedRefs(Map<String,
SnapshotRef> refs) {
Review Comment:
Nit: this comes between `computeAllBranchSnapshotsToRetain` and its helper
method, `computeBranchSnapshotsToRetain`. It's also called first. I'd move it
above `computeAllBranchSnapshotsToRetain`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]