amogh-jahagirdar commented on a change in pull request #4071:
URL: https://github.com/apache/iceberg/pull/4071#discussion_r835793095



##########
File path: 
core/src/main/java/org/apache/iceberg/UpdateSnapshotReferencesOperation.java
##########
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+
+class UpdateSnapshotReferencesOperation implements PendingUpdate<Map<String, 
SnapshotRef>> {
+
+  private final TableOperations ops;
+  private TableMetadata base;
+  private final Map<String, SnapshotRef> refsToUpdate;
+  private final Set<String> refsToRemove;
+
+  UpdateSnapshotReferencesOperation(TableOperations ops) {
+    this.ops = ops;
+    this.base = ops.current();
+    this.refsToUpdate = Maps.newHashMap();
+    this.refsToRemove = Sets.newHashSet();
+  }
+
+  @Override
+  public Map<String, SnapshotRef> apply() {
+    Map<String, SnapshotRef> refsBeforeUpdate = base.refs();
+    Map<String, SnapshotRef> refsAfterUpdate = internalApply().refs();
+    Map<String, SnapshotRef> changes = Maps.newHashMap();
+
+    // Identify references which have been removed
+    for (Map.Entry<String, SnapshotRef> refsBeforeUpdateEntry : 
refsBeforeUpdate.entrySet()) {
+      if (!refsAfterUpdate.containsKey(refsBeforeUpdateEntry.getKey())) {
+        changes.put(refsBeforeUpdateEntry.getKey(), null);
+      }
+    }
+
+    // Identify references which have been created or updated.
+    for (Map.Entry<String, SnapshotRef> refsAfterUpdateEntry : 
refsAfterUpdate.entrySet()) {
+      SnapshotRef ref = refsBeforeUpdate.get(refsAfterUpdateEntry.getKey());
+      if (ref == null || !ref.equals(refsAfterUpdateEntry.getValue())) {
+        changes.put(refsAfterUpdateEntry.getKey(), 
refsAfterUpdateEntry.getValue());
+      }
+    }
+    return changes;
+  }
+
+  @Override
+  public void commit() {
+    ops.commit(base, internalApply());
+  }
+
+  public UpdateSnapshotReferencesOperation createBranch(String name, long 
snapshotId) {
+    Preconditions.checkNotNull(name, "Branch name cannot be null");
+    ValidationException.check(base.ref(name) == null, "Reference with name %s 
already exists", name);
+    if (refsToRemove.contains(name)) {
+      refsToRemove.remove(name);
+    }
+    SnapshotRef branch = SnapshotRef.branchBuilder(snapshotId).build();
+    SnapshotRef existingRef = refsToUpdate.put(name, branch);
+    ValidationException.check(existingRef == null, "Reference with name %s 
already exists", name);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation createTag(String name, long 
snapshotId) {
+    Preconditions.checkNotNull(name, "Tag name cannot be null");
+    ValidationException.check(base.ref(name) == null, "Reference with name %s 
already exists", name);
+    if (refsToRemove.contains(name)) {
+      refsToRemove.remove(name);
+    }
+    SnapshotRef tag = SnapshotRef.tagBuilder(snapshotId).build();
+    SnapshotRef existingRef = refsToUpdate.put(name, tag);
+    ValidationException.check(existingRef == null, "Reference with name %s 
already exists", name);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation removeBranch(String name) {
+    Preconditions.checkNotNull(name, "Branch name cannot be null");
+    SnapshotRef ref = base.ref(name);
+    ValidationException.check(ref != null, "No such branch with name %s", 
name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    refsToRemove.add(name);
+    if (refsToUpdate.containsKey(name)) {
+      refsToUpdate.remove(name);
+    }
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation removeTag(String name) {
+    Preconditions.checkNotNull(name, "Tag name cannot be null");
+    SnapshotRef ref = base.ref(name);
+    ValidationException.check(ref != null, "No such tag with name %s", name);
+    ValidationException.check(ref.isTag(), "Ref with name %s is a branch not a 
tag", name);
+    refsToRemove.add(name);
+    if (refsToUpdate.containsKey(name)) {
+      refsToUpdate.remove(name);
+    }
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation renameBranch(String name, String 
newName) {
+    Preconditions.checkNotNull(name, "Branch name cannot be null");
+    Preconditions.checkNotNull(newName, "Branch name cannot be null");
+    SnapshotRef ref = validateAndGetLatestRef(name);
+    ValidationException.check(ref != null, "No such ref with name %s", name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    refsToUpdate.put(newName, ref);
+    refsToRemove.add(name);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation replaceBranch(String name, long 
snapshotId) {
+    Preconditions.checkNotNull(name, "Branch name cannot be null");
+    ValidationException.check(!refsToRemove.contains(name), "Cannot replace 
branch %s after removing", name);
+    SnapshotRef ref = validateAndGetLatestRef(name);
+    ValidationException.check(ref != null, "Branch %s does not exist", name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    SnapshotRef.builderFrom(ref, snapshotId).build();
+    refsToUpdate.put(name, ref);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation replaceTag(String name, long 
snapshotId) {
+    Preconditions.checkNotNull(name, "Tag name cannot be null");
+    ValidationException.check(!refsToRemove.contains(name), "Cannot replace 
tag %s after removing", name);
+    SnapshotRef ref = validateAndGetLatestRef(name);
+    ValidationException.check(ref != null, "No such ref with name %s", name);
+    ValidationException.check(ref.isTag(), "Ref with name %s is a tag not a 
branch", name);
+    SnapshotRef.builderFrom(ref, snapshotId).build();
+    refsToUpdate.put(name, ref);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation setMinSnapshotsToKeep(String name, 
int minSnapshotsToKeep) {
+    Preconditions.checkNotNull(name, "Branch name cannot be null");
+    SnapshotRef ref = validateAndGetLatestRef(name);
+    ValidationException.check(ref != null, "No such ref with name %s", name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    SnapshotRef updateBranch = SnapshotRef.builderFrom(ref)
+        .minSnapshotsToKeep(minSnapshotsToKeep)
+        .build();
+    refsToUpdate.put(name, updateBranch);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation setMaxSnapshotAgeMs(String name, 
long maxSnapshotAgeMs) {
+    Preconditions.checkNotNull(name, "Branch name cannot be null");
+    SnapshotRef ref = validateAndGetLatestRef(name);
+    ValidationException.check(ref != null, "No such ref with name %s", name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    SnapshotRef updateBranch = SnapshotRef.builderFrom(ref)
+        .maxSnapshotAgeMs(maxSnapshotAgeMs)
+        .build();
+    refsToUpdate.put(name, updateBranch);
+    return this;
+  }
+
+  public UpdateSnapshotReferencesOperation setMaxRefAgeMs(String name, long 
maxRefAgeMs) {
+    Preconditions.checkNotNull(name, "Reference name cannot be null");
+    SnapshotRef ref = validateAndGetLatestRef(name);
+    ValidationException.check(ref != null, "No such ref with name %s", name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    SnapshotRef updatedTag = SnapshotRef.builderFrom(ref)
+        .maxRefAgeMs(maxRefAgeMs)
+        .build();
+    refsToUpdate.put(name, updatedTag);
+    return this;
+  }
+
+  /**
+   * Helper which validates if a reference exists either in existing
+   * metadata or in the ongoing operation. If the reference is updated in the 
ongoing operation
+   * or if the reference does not exist in the existing metadata return that 
view of the reference.
+   */
+  private SnapshotRef validateAndGetLatestRef(String name) {
+    SnapshotRef ref = base.refs().get(name);
+    if (ref == null || refsToUpdate.containsKey(name)) {
+      ref = refsToUpdate.get(name);
+    }
+    ValidationException.check(ref != null, "No such ref with name %s", name);
+    ValidationException.check(ref.isBranch(), "Ref with name %s is a tag not a 
branch", name);
+    return ref;
+  }
+
+  private TableMetadata internalApply() {
+    this.base = ops.refresh();
+    TableMetadata.Builder updatedBuilder = TableMetadata.buildFrom(base);
+    for (String refName : refsToRemove) {
+      SnapshotRef ref = base.ref(refName);
+      ValidationException.check(ref != null, "Cannot remove nonexistent 
snapshot ref %s", refName);
+      if (ref.isBranch()) {
+        updatedBuilder.removeBranch(refName);
+      } else {
+        updatedBuilder.removeTag(refName);
+      }
+    }
+    Map<String, SnapshotRef> refs = Maps.newHashMap();
+    for (Map.Entry<String, SnapshotRef> refEntry : refsToUpdate.entrySet()) {
+      String name = refEntry.getKey();
+      SnapshotRef snapshotRef = refEntry.getValue();
+      refs.put(name, snapshotRef);
+      updatedBuilder.setBranch(name, snapshotRef);

Review comment:
       Should be updated to call setBranch depending on if it's a branch or 
setTag if it's a tag




-- 
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]

Reply via email to