rdblue commented on code in PR #4601: URL: https://github.com/apache/iceberg/pull/4601#discussion_r855643712
########## core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java: ########## @@ -0,0 +1,160 @@ +/* + * 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 com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import java.util.Locale; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.util.JsonUtil; + +public class MetadataUpdateParser { + + private MetadataUpdateParser() { + } + + private static final String ACTION = "action"; + + // action types + private static final String ASSIGN_UUID = "assign-uuid"; + private static final String UPGRADE_FORMAT_VERSION = "upgrade-format-version"; + private static final String ADD_SCHEMA = "add-schema"; + private static final String SET_CURRENT_SCHEMA = "set-current-schema"; + private static final String ADD_PARTITION_SPEC = "add-spec"; + private static final String SET_DEFAULT_PARTITION_SPEC = "set-default-spec"; + private static final String ADD_SORT_ORDER = "add-sort-order"; + private static final String SET_DEFAULT_SORT_ORDER = "set-default-sort-order"; + private static final String ADD_SNAPSHOT = "add-snapshot"; + private static final String REMOVE_SNAPSHOTS = "remove-snapshots"; + private static final String SET_SNAPSHOT_REF = "set-snapshot-ref"; + private static final String SET_PROPERTIES = "set-properties"; + private static final String REMOVE_PROPERTIES = "remove-properties"; + private static final String SET_LOCATION = "set-location"; + + // UpgradeFormatVersion + private static final String FORMAT_VERSION = "format-version"; + + private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap + .<Class<? extends MetadataUpdate>, String>builder() + .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID) + .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION) + .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA) + .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA) + .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC) + .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC) + .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER) + .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER) + .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT) + .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOTS) + .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF) + .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES) + .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES) + .put(MetadataUpdate.SetLocation.class, SET_LOCATION) + .build(); + + public static String toJson(MetadataUpdate metadataUpdate) { + return toJson(metadataUpdate, false); + } + + public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) { + try { + StringWriter writer = new StringWriter(); + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); + if (pretty) { + generator.useDefaultPrettyPrinter(); + } + toJson(metadataUpdate, generator); + generator.flush(); + return writer.toString(); + } catch (IOException e) { + throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e); + } + } + + public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException { + String updateAction = ACTIONS.get(metadataUpdate.getClass()); Review Comment: I think this should check whether an action was found and throw an `UnsupportedOperationException` if it is not. -- 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]
