amogh-jahagirdar commented on code in PR #4578:
URL: https://github.com/apache/iceberg/pull/4578#discussion_r873091770
##########
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) {
+ Map<String, SnapshotRef> retainedRefs = Maps.newHashMap();
+ retainedRefs.put(SnapshotRef.MAIN_BRANCH,
refs.get(SnapshotRef.MAIN_BRANCH));
+ for (Map.Entry<String, SnapshotRef> refEntry : refs.entrySet()) {
+ String name = refEntry.getKey();
+ SnapshotRef ref = refEntry.getValue();
+ long maxRefAgeMs = ref.maxRefAgeMs() != null ? ref.maxRefAgeMs() :
defaultMaxRefAgeMs;
+ Snapshot snapshot = base.snapshot(ref.snapshotId());
+ if (snapshot != null && now - snapshot.timestampMillis() <= maxRefAgeMs)
{
+ retainedRefs.put(name, ref);
+ }
+ }
+
+ return retainedRefs;
+ }
+
+ private Set<Long> computeBranchSnapshotsToRetain(long snapshot, long
expireSnapshotsOlderThan,
+ int minSnapshotsToKeep) {
Review Comment:
Updated to use continuing indent for all the args
--
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]