rdblue commented on code in PR #4071:
URL: https://github.com/apache/iceberg/pull/4071#discussion_r841292752


##########
core/src/main/java/org/apache/iceberg/MetadataUpdate.java:
##########
@@ -223,31 +225,106 @@ public String name() {
 
     @Override
     public void applyTo(TableMetadata.Builder metadataBuilder) {
-      // TODO: this should be generalized when tagging is supported
-      metadataBuilder.removeBranch(name);
+      SnapshotRef ref = metadataBuilder.ref(name);
+      ValidationException.check(ref != null, "Ref %s not found", name);
+      if (ref.isBranch()) {
+        metadataBuilder.removeBranch(name);
+      } else {
+        metadataBuilder.removeTag(name);
+      }
     }
   }
 
   class SetSnapshotRef implements MetadataUpdate {
-    private final String name;
-    private final long snapshotId;
+    private final String ref;
+    private final Long snapshotId;
+    private final SnapshotRefType type;
+    private Integer minSnapshotsToKeep;
+    private Long maxSnapshotAgeMs;
+    private Long maxRefAgeMs;
+
+    public SetSnapshotRef(String ref, Long snapshotId, SnapshotRefType type, 
Integer minSnapshotsToKeep,
+                          Long maxSnapshotAgeMs, Long maxRefAgeMs) {
+      this.ref = ref;
+      this.snapshotId = snapshotId;
+      this.type = type;
+      this.minSnapshotsToKeep = minSnapshotsToKeep;
+      this.maxSnapshotAgeMs = maxSnapshotAgeMs;
+      this.maxRefAgeMs = maxRefAgeMs;
+    }
 
-    public SetSnapshotRef(String name, long snapshotId) {
-      this.name = name;
+    public SetSnapshotRef(String ref, Long snapshotId, SnapshotRefType type) {
+      this.ref = ref;
       this.snapshotId = snapshotId;
+      this.type = type;
     }
 
-    public String name() {
-      return name;
+    public String ref() {
+      return ref;
+    }
+
+    public SnapshotRefType type() {
+      return type;
     }
 
     public long snapshotId() {
       return snapshotId;
     }
 
+    public Integer minSnapshotsToKeep() {
+      return minSnapshotsToKeep;
+    }
+
+    public Long maxSnapshotAgeMs() {
+      return maxSnapshotAgeMs;
+    }
+
+    public Long maxRefAgeMs() {
+      return maxRefAgeMs;
+    }
+
     @Override
     public void applyTo(TableMetadata.Builder metadataBuilder) {
-      metadataBuilder.setBranchSnapshot(snapshotId, name);
+      SnapshotRef currentRef = metadataBuilder.ref(ref);
+      Preconditions.checkArgument(currentRef == null || currentRef.type() == 
type,
+          "Cannot apply SetSnapshotRef where current ref is of type %s and 
update is of type %s",
+          currentRef.type(), type);
+      SnapshotRef.Builder builder;
+      boolean newRef = currentRef == null;
+
+      if (!newRef) {
+        builder = SnapshotRef.builderFrom(currentRef);
+      } else {
+        builder = SnapshotRef.builderFor(snapshotId, type);
+      }
+
+      if (newRef || (currentRef.isBranch() && minSnapshotsToKeep != null)) {
+        builder.minSnapshotsToKeep(minSnapshotsToKeep);
+      }
+      if (newRef || (currentRef.isBranch() && maxSnapshotAgeMs != null)) {
+        builder.maxSnapshotAgeMs(maxSnapshotAgeMs);
+      }
+      if (newRef || maxRefAgeMs != null) {
+        builder.maxRefAgeMs(maxRefAgeMs);
+      }
+      SnapshotRef updatedRef = builder.build();
+      if (updatedRef.isBranch()) {
+        metadataBuilder.setBranch(ref, updatedRef);
+      } else {
+        metadataBuilder.setTag(ref, updatedRef);
+      }
+    }
+
+    public void setMinSnapshotsToKeep(Integer minSnapshotsToKeep) {
+      this.minSnapshotsToKeep = minSnapshotsToKeep;
+    }

Review Comment:
   These objects are immutable, so there should be no setters.



-- 
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: dev-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to