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


##########
core/src/main/java/org/apache/iceberg/view/ViewMetadata.java:
##########
@@ -117,13 +128,187 @@ default ViewMetadata checkAndNormalize() {
           ViewProperties.VERSION_HISTORY_SIZE,
           versionHistorySizeToKeep);
     } else if (versions().size() > versionHistorySizeToKeep) {
-      List<ViewVersion> versions =
+      List<ViewVersion> versionsToKeep =
           versions().subList(versions().size() - versionHistorySizeToKeep, 
versions().size());
-      List<ViewHistoryEntry> history =
+      List<ViewHistoryEntry> historyToKeep =
           history().subList(history().size() - versionHistorySizeToKeep, 
history().size());
-      return 
ImmutableViewMetadata.builder().from(this).versions(versions).history(history).build();
+      List<MetadataUpdate> changesToKeep = Lists.newArrayList(changes());
+      Set<MetadataUpdate.AddViewVersion> toRemove =
+          changesToKeep.stream()
+              .filter(update -> update instanceof 
MetadataUpdate.AddViewVersion)
+              .map(update -> (MetadataUpdate.AddViewVersion) update)
+              .filter(
+                  update ->
+                      update.viewVersion().versionId() != currentVersionId()
+                          && !versionsToKeep.contains(update.viewVersion()))
+              .collect(Collectors.toSet());
+      changesToKeep.removeAll(toRemove);
+
+      return ImmutableViewMetadata.of(
+          formatVersion(),
+          location(),
+          currentSchemaId(),
+          schemas(),
+          currentVersionId(),
+          versionsToKeep,
+          historyToKeep,
+          properties(),
+          changesToKeep);
     }
 
     return this;
   }
+
+  static Builder builder() {
+    return new Builder();
+  }
+
+  static Builder buildFrom(ViewMetadata base) {
+    return new Builder(base);
+  }
+
+  class Builder {
+    private int formatVersion = DEFAULT_VIEW_FORMAT_VERSION;
+    private String location;
+    private Integer currentSchemaId;
+    private List<Schema> schemas = Lists.newArrayList();
+    private int currentVersionId;
+    private List<ViewVersion> versions = Lists.newArrayList();
+    private List<ViewHistoryEntry> history = Lists.newArrayList();
+    private Map<String, String> properties = Maps.newHashMap();
+    private List<MetadataUpdate> changes = Lists.newArrayList();
+
+    private Builder() {}
+
+    private Builder(ViewMetadata base) {
+      this.formatVersion = base.formatVersion();
+      this.location = base.location();
+      this.currentSchemaId = base.currentSchemaId();
+      this.schemas = Lists.newArrayList(base.schemas());
+      this.currentVersionId = base.currentVersionId();
+      this.versions = Lists.newArrayList(base.versions());
+      this.history = Lists.newArrayList(base.history());
+      this.properties = Maps.newHashMap(base.properties());
+      this.changes = Lists.newArrayList(base.changes());
+    }
+
+    public Builder upgradeFormatVersion(int newFormatVersion) {

Review Comment:
   Shouldn't this also check that the version is <= the supported version?
   
   Otherwise, it's possible for this builder to create an invalid change: 
`UpgradeFormatVersion(v)` where `v > SUPPORTED_VIEW_FORMAT_VERSION`. That may 
not leak right now, but it could in the future.



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