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


##########
core/src/main/java/org/apache/iceberg/view/ViewMetadata.java:
##########
@@ -121,9 +128,178 @@ default ViewMetadata checkAndNormalize() {
           versions().subList(versions().size() - versionHistorySizeToKeep, 
versions().size());
       List<ViewHistoryEntry> history =
           history().subList(history().size() - versionHistorySizeToKeep, 
history().size());
-      return 
ImmutableViewMetadata.builder().from(this).versions(versions).history(history).build();
+      return ViewMetadata.buildFrom(this)
+          .discardChanges()
+          .versions(versions)
+          .history(history)
+          .build();
     }
 
     return this;
   }
+
+  static Builder builder() {
+    return new Builder();
+  }
+
+  static Builder buildFrom(ViewMetadata base) {
+    return new Builder(base);
+  }
+
+  class Builder {
+    private int formatVersion = 1;
+    private String location;
+    private Integer currentSchemaId;
+    private List<Schema> schemas = Lists.newArrayList();
+    private int currentVersionId = 1;
+    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 formatVersion(int newFormatVersion) {
+      this.formatVersion = newFormatVersion;
+      this.changes.add(new 
MetadataUpdate.UpgradeFormatVersion(newFormatVersion));
+      return this;
+    }
+
+    public Builder location(String newLocation) {
+      this.location = newLocation;
+      this.changes.add(new MetadataUpdate.SetLocation(newLocation));
+      return this;
+    }
+
+    public Builder currentVersionId(int versionId) {
+      this.currentVersionId = versionId;
+      this.changes.add(new MetadataUpdate.SetCurrentViewVersion(versionId));
+      return this;
+    }
+
+    public Builder currentSchemaId(Integer schemaId) {
+      this.currentSchemaId = schemaId;
+      this.changes.add(new MetadataUpdate.SetCurrentSchema(schemaId));
+      return this;
+    }
+
+    public Builder addSchema(Schema schema) {
+      this.schemas.add(schema);
+      this.changes.add(new MetadataUpdate.AddSchema(schema, 
schema.highestFieldId()));
+      return this;
+    }
+
+    public Builder addAllSchemas(Iterable<Schema> schemaEntries) {
+      for (Schema schema : schemaEntries) {
+        this.schemas.add(schema);
+        this.changes.add(new MetadataUpdate.AddSchema(schema, 
schema.highestFieldId()));
+      }
+      return this;
+    }
+
+    public Builder schemas(Iterable<Schema> newSchemas) {
+      this.schemas = Lists.newArrayList(newSchemas);
+      for (Schema schema : newSchemas) {
+        this.changes.add(new MetadataUpdate.AddSchema(schema, 
schema.highestFieldId()));
+      }
+      return this;
+    }
+
+    public Builder addVersion(ViewVersion version) {
+      this.versions.add(version);
+      this.changes.add(new MetadataUpdate.AddViewVersion(version));
+      return this;
+    }
+
+    public Builder addAllVersions(Iterable<ViewVersion> versionEntries) {
+      for (ViewVersion version : versionEntries) {
+        this.versions.add(version);
+        this.changes.add(new MetadataUpdate.AddViewVersion(version));
+      }
+      return this;
+    }
+
+    public Builder versions(Iterable<ViewVersion> newVersions) {
+      this.versions = Lists.newArrayList(newVersions);
+      for (ViewVersion version : newVersions) {
+        this.changes.add(new MetadataUpdate.AddViewVersion(version));
+      }
+      return this;
+    }
+
+    public Builder addHistory(ViewHistoryEntry historyEntry) {
+      this.history.add(historyEntry);
+      return this;
+    }
+
+    public Builder addAllHistory(Iterable<ViewHistoryEntry> historyEntries) {
+      for (ViewHistoryEntry entry : historyEntries) {
+        this.history.add(entry);
+      }
+      return this;
+    }
+
+    public Builder history(Iterable<ViewHistoryEntry> newHistory) {
+      this.history = Lists.newArrayList(newHistory);
+      return this;
+    }
+
+    public Builder putProperties(String key, String value) {
+      this.properties.put(key, value);
+      this.changes.add(new MetadataUpdate.SetProperties(ImmutableMap.of(key, 
value)));
+      return this;
+    }
+
+    public Builder putAllProperties(Map<String, String> newProperties) {
+      this.properties.putAll(newProperties);
+      this.changes.add(new MetadataUpdate.SetProperties(newProperties));
+      return this;
+    }
+
+    public Builder properties(Map<String, String> newProperties) {
+      this.properties = newProperties;
+      this.changes.add(new MetadataUpdate.SetProperties(newProperties));
+      return this;
+    }
+
+    public Builder removeProperties(Set<String> propertiesToRemove) {
+      if (propertiesToRemove.isEmpty()) {
+        return this;
+      }
+
+      propertiesToRemove.forEach(this.properties::remove);
+      this.changes.add(new 
MetadataUpdate.RemoveProperties(propertiesToRemove));
+      return this;
+    }
+
+    public Builder discardChanges() {
+      this.changes.clear();

Review Comment:
   I think this needs to be called inside the `build` method. Otherwise, the 
behavior changes based on when this is called.



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