rdblue commented on a change in pull request #2957:
URL: https://github.com/apache/iceberg/pull/2957#discussion_r698075710



##########
File path: core/src/main/java/org/apache/iceberg/TableMetadataBuilder.java
##########
@@ -0,0 +1,252 @@
+/*
+ * 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.List;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+
+public class TableMetadataBuilder {
+
+  private InputFile file;
+  private Integer formatVersion;
+  private String uuid;
+  private String location;
+  private Long lastSequenceNumber;
+  private Long lastUpdatedMillis;
+  private Integer lastColumnId;
+  private Integer currentSchemaId;
+  private List<Schema> schemas;
+  private Integer defaultSpecId;
+  private List<PartitionSpec> specs;
+  private Integer lastAssignedPartitionId;
+  private Integer defaultSortOrderId;
+  private List<SortOrder> sortOrders;
+  private Map<String, String> properties;
+  private Long currentSnapshotId;
+  private List<Snapshot> snapshots;
+  private List<HistoryEntry> snapshotLog;
+  private List<TableMetadata.MetadataLogEntry> previousFiles;
+
+  private int baseFormatVersion;
+  private InputFile baseFile;
+  private long baseLastUpdateMillis;
+  private List<TableMetadata.MetadataLogEntry> basePreviousFiles;
+
+  TableMetadataBuilder() {
+  }
+
+  public TableMetadataBuilder withFile(InputFile inputFile) {
+    this.file = inputFile;
+    return this;
+  }
+
+  public TableMetadataBuilder withLocation(String inputLocation) {
+    this.location = inputLocation;
+    return this;
+  }
+
+  public TableMetadataBuilder withFormatVersion(int inputFormatVersion) {
+    this.formatVersion = inputFormatVersion;
+    return this;
+  }
+
+  public TableMetadataBuilder withUUID(String inputUuid) {
+    this.uuid = inputUuid;
+    return this;
+  }
+
+  public TableMetadataBuilder generateUUID() {
+    return withUUID(UUID.randomUUID().toString());
+  }
+
+  public TableMetadataBuilder withLastSequenceNumber(long 
inputLastSequenceNumber) {
+    this.lastSequenceNumber = inputLastSequenceNumber;
+    return this;
+  }
+
+  public TableMetadataBuilder refreshLastUpdateMillis() {
+    return withLastUpdatedMillis(System.currentTimeMillis());
+  }
+
+  public TableMetadataBuilder withLastUpdatedMillis(Long 
inputLastUpdatedMillis) {
+    this.lastUpdatedMillis = inputLastUpdatedMillis;
+    return this;
+  }
+
+  public TableMetadataBuilder withLastColumnId(int inputLastColumnId) {
+    this.lastColumnId = inputLastColumnId;
+    return this;
+  }
+
+  public TableMetadataBuilder withCurrentSchemaId(int inputCurrentSchemaId) {
+    this.currentSchemaId = inputCurrentSchemaId;
+    return this;
+  }
+
+  public TableMetadataBuilder withSchemas(List<Schema> inputSchemas) {
+    this.schemas = inputSchemas;
+    return this;
+  }
+
+  public TableMetadataBuilder withDefaultSpecId(int inputDefaultSpecId) {
+    this.defaultSpecId = inputDefaultSpecId;
+    return this;
+  }
+
+  public TableMetadataBuilder withSpecs(List<PartitionSpec> inputSpecs) {
+    this.specs = inputSpecs;
+    return this;
+  }
+
+  public TableMetadataBuilder withLastAssignedPartitionId(int 
inputLastAssignedPartitionId) {
+    this.lastAssignedPartitionId = inputLastAssignedPartitionId;
+    return this;
+  }
+
+  public TableMetadataBuilder withDefaultSortOrderId(int 
inputDefaultSortOrderId) {
+    this.defaultSortOrderId = inputDefaultSortOrderId;
+    return this;
+  }
+
+  public TableMetadataBuilder withSortOrders(List<SortOrder> inputSortOrders) {
+    this.sortOrders = inputSortOrders;
+    return this;
+  }
+
+  public TableMetadataBuilder withProperties(Map<String, String> 
inputProperties) {
+    this.properties = inputProperties;
+    return this;
+  }
+
+  public TableMetadataBuilder withCurrentSnapshotId(long 
inputCurrentSnapshotId) {
+    this.currentSnapshotId = inputCurrentSnapshotId;
+    return this;
+  }
+
+  public TableMetadataBuilder withSnapshots(List<Snapshot> inputSnapshots) {
+    this.snapshots = inputSnapshots;
+    return this;
+  }
+
+  public TableMetadataBuilder withSnapshotLog(List<HistoryEntry> 
inputSnapshotLog) {
+    this.snapshotLog = inputSnapshotLog;
+    return this;
+  }
+
+  public TableMetadataBuilder 
withPreviousFiles(List<TableMetadata.MetadataLogEntry> inputPreviousFiles) {
+    this.previousFiles = inputPreviousFiles;
+    return this;
+  }
+
+  public TableMetadataBuilder from(TableMetadata base) {
+    this.formatVersion = base.formatVersion();
+    this.uuid = base.uuid();
+    this.location = base.location();
+    this.lastSequenceNumber = base.lastSequenceNumber();
+    this.lastUpdatedMillis = base.lastUpdatedMillis();
+    this.lastColumnId = base.lastColumnId();
+    this.currentSchemaId = base.currentSchemaId();
+    this.schemas = base.schemas();
+    this.specs = base.specs();
+    this.defaultSpecId = base.defaultSpecId();
+    this.lastAssignedPartitionId = base.lastAssignedPartitionId();
+    this.defaultSortOrderId = base.defaultSortOrderId();
+    this.sortOrders = base.sortOrders();
+    this.properties = base.properties();
+    this.currentSnapshotId = base.currentSnapshotId();
+    this.snapshots = base.snapshots();
+    this.snapshotLog = base.snapshotLog();
+
+    this.baseFormatVersion = base.formatVersion();
+    this.baseFile = base.file();
+    this.baseLastUpdateMillis = base.lastUpdatedMillis();
+    this.basePreviousFiles = base.previousFiles();
+    return this;
+  }
+
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")
+  public TableMetadata build() {
+    if (formatVersion == null) {
+      formatVersion = TableMetadata.DEFAULT_TABLE_FORMAT_VERSION;
+    } else if (formatVersion != baseFormatVersion) {
+      Preconditions.checkArgument(formatVersion <= 
TableMetadata.SUPPORTED_TABLE_FORMAT_VERSION,
+          "Cannot upgrade table to unsupported format version: v%s (supported: 
v%s)",
+          formatVersion, TableMetadata.SUPPORTED_TABLE_FORMAT_VERSION);
+      Preconditions.checkArgument(formatVersion >= baseFormatVersion,
+          "Cannot downgrade v%s table to v%s", baseFormatVersion, 
formatVersion);
+    }

Review comment:
       I generally prefer to structure builders so that validation is done as 
soon as possible. That keeps `build` methods smaller. This could be moved into 
the `withFormatVersion` method.




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