rdblue commented on code in PR #7880: URL: https://github.com/apache/iceberg/pull/7880#discussion_r1315201974
########## core/src/main/java/org/apache/iceberg/view/BaseViewOperations.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.view; + +import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Function; +import java.util.function.Predicate; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.NoSuchViewException; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.OutputFile; +import org.apache.iceberg.relocated.com.google.common.base.Objects; +import org.apache.iceberg.util.Tasks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class BaseViewOperations implements ViewOperations { + private static final Logger LOG = LoggerFactory.getLogger(BaseViewOperations.class); + + private static final String METADATA_FOLDER_NAME = "metadata"; + + private ViewMetadata currentMetadata = null; + private String currentMetadataLocation = null; + private boolean shouldRefresh = true; + private int version = -1; + + protected BaseViewOperations() {} + + protected void requestRefresh() { + this.shouldRefresh = true; + } + + protected void disableRefresh() { + this.shouldRefresh = false; + } + + protected void doRefresh() { + throw new UnsupportedOperationException("Not implemented: doRefresh"); + } + + protected void doCommit(ViewMetadata base, ViewMetadata metadata) { + throw new UnsupportedOperationException("Not implemented: doCommit"); + } + + protected abstract String viewName(); + + protected abstract FileIO io(); + + public int currentVersion() { + return version; + } + + public String currentMetadataLocation() { + return currentMetadataLocation; + } + + @Override + public ViewMetadata current() { + if (shouldRefresh) { + return refresh(); + } + + return currentMetadata; + } + + @Override + public ViewMetadata refresh() { + boolean currentMetadataWasAvailable = currentMetadata != null; + try { + doRefresh(); + } catch (NoSuchViewException e) { + if (currentMetadataWasAvailable) { + LOG.warn("Could not find the view during refresh, setting current metadata to null", e); + shouldRefresh = true; + } + + currentMetadata = null; + currentMetadataLocation = null; + version = -1; + throw e; + } + + return current(); + } + + @Override + public void commit(ViewMetadata base, ViewMetadata metadata) { + // if the metadata is already out of date, reject it + if (base != current()) { + if (base != null) { + throw new CommitFailedException("Cannot commit: stale view metadata"); + } else { + // when current is non-null, the view exists. but when base is null, the commit is trying + // to create the view + throw new AlreadyExistsException("View already exists: %s", viewName()); + } + } + + // if the metadata is not changed, return early + if (base == metadata) { + LOG.info("Nothing to commit."); + return; + } + + long start = System.currentTimeMillis(); + doCommit(base, metadata); + requestRefresh(); + + LOG.info( + "Successfully committed to view {} in {} ms", + viewName(), + System.currentTimeMillis() - start); + } + + protected String writeNewMetadata(ViewMetadata metadata, int newVersion) { Review Comment: I think this can be private. Only `InMemoryTableOperations` calls it on the table side and that's probably incorrect. -- 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]
