rdblue commented on a change in pull request #4348: URL: https://github.com/apache/iceberg/pull/4348#discussion_r828332295
########## File path: core/src/main/java/org/apache/iceberg/rest/RESTTableOperations.java ########## @@ -0,0 +1,212 @@ +/* + * 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.rest; + +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; +import org.apache.commons.compress.utils.Lists; +import org.apache.iceberg.LocationProviders; +import org.apache.iceberg.MetadataUpdate; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.encryption.EncryptionManager; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.LocationProvider; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.rest.requests.UpdateTableRequest; +import org.apache.iceberg.rest.responses.ErrorResponse; +import org.apache.iceberg.rest.responses.LoadTableResponse; + +class RESTTableOperations implements TableOperations { + private static final String METADATA_FOLDER_NAME = "metadata"; + + enum UpdateType { + CREATE, + REPLACE, + SIMPLE + } + + private final RESTClient client; + private final String path; + private final FileIO io; + private final List<MetadataUpdate> createChanges; + private final TableMetadata replaceBase; + private UpdateType updateType; + private TableMetadata current; + + RESTTableOperations(RESTClient client, String path, FileIO io, TableMetadata current) { + this(client, path, io, UpdateType.SIMPLE, Lists.newArrayList(), current); + } + + RESTTableOperations(RESTClient client, String path, FileIO io, UpdateType updateType, + List<MetadataUpdate> createChanges, TableMetadata current) { + this.client = client; + this.path = path; + this.io = io; + this.updateType = updateType; + this.createChanges = createChanges; + this.replaceBase = current; + if (updateType == UpdateType.CREATE) { + this.current = null; + } else { + this.current = current; + } + } + + @Override + public TableMetadata current() { + return current; + } + + @Override + public TableMetadata refresh() { + return updateCurrentMetadata(client.get(path, LoadTableResponse.class, ErrorHandlers.tableErrorHandler())); + } + + @Override + public void commit(TableMetadata base, TableMetadata metadata) { + UpdateTableRequest.Builder requestBuilder; + List<MetadataUpdate> baseChanges; + Consumer<ErrorResponse> errorHandler; + switch (updateType) { + case CREATE: + Preconditions.checkState(base == null, "Invalid base metadata for create transaction, expected null: %s", base); + requestBuilder = UpdateTableRequest.builderForCreate(); + baseChanges = createChanges; + errorHandler = ErrorHandlers.tableErrorHandler(); // throws NoSuchTableException + break; + + case REPLACE: + Preconditions.checkState(base != null, "Invalid base metadata: null"); + // use the original replace base metadata because the transaction will refresh + requestBuilder = UpdateTableRequest.builderForReplace(replaceBase); + baseChanges = createChanges; + errorHandler = ErrorHandlers.tableCommitHandler(); + break; + + case SIMPLE: + Preconditions.checkState(base != null, "Invalid base metadata: null"); + requestBuilder = UpdateTableRequest.builderFor(base); + baseChanges = ImmutableList.of(); + errorHandler = ErrorHandlers.tableCommitHandler(); + break; + + default: + throw new UnsupportedOperationException(String.format("Update type %s is not supported", updateType)); + } + + baseChanges.forEach(requestBuilder::update); + metadata.changes().forEach(requestBuilder::update); + UpdateTableRequest request = requestBuilder.build(); + + // the error handler will throw necessary exceptions like CommitFailedException and UnknownCommitStateException + // TODO: ensure that the HTTP client lib passes HTTP client errors to the error handler Review comment: @kbendick, this is relevant to the HTTP implementation of `RESTClient`. How does it handle errors that are coming from the HTTP connection and not from the service? In some cases, we need to throw `CommitStateUnknownException`. -- 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]
