danielcweeks commented on code in PR #12808:
URL: https://github.com/apache/iceberg/pull/12808#discussion_r2072198227


##########
bigquery/src/main/java/org/apache/iceberg/gcp/bigquery/BigQueryTableOperations.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.gcp.bigquery;
+
+import com.google.api.services.bigquery.model.ExternalCatalogTableOptions;
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableReference;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.iceberg.BaseMetastoreOperations;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.SnapshotSummary;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.CommitStateUnknownException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.ConfigProperties;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Handles BigQuery metastore table operations. */
+public final class BigQueryTableOperations extends 
BaseMetastoreTableOperations {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BigQueryTableOperations.class);
+
+  private static final String TABLE_PROPERTIES_BQ_CONNECTION = "bq_connection";
+
+  private final BigQueryMetastoreClient client;
+  private final FileIO fileIO;
+  private final TableReference tableReference;
+  private final Configuration conf;
+
+  BigQueryTableOperations(
+      BigQueryMetastoreClient client,
+      FileIO fileIO,
+      TableReference tableReference,
+      Configuration conf) {
+    this.client = client;
+    this.fileIO = fileIO;
+    this.tableReference = tableReference;
+    this.conf = conf;
+  }
+
+  // The doRefresh method should provide implementation on how to get the 
metadata location.
+  @Override
+  public void doRefresh() {
+    // Must default to null.
+    String metadataLocation = null;
+    try {
+      metadataLocation =
+          loadMetadataLocationOrThrow(
+              
client.load(this.tableReference).getExternalCatalogTableOptions());
+    } catch (NoSuchTableException e) {
+      if (currentMetadataLocation() != null) {
+        // Re-throws the exception because the table must exist in this case.
+        throw e;
+      }
+    }
+    refreshFromMetadataLocation(metadataLocation);
+  }
+
+  // The doCommit method should provide implementation on how to update with 
metadata location
+  // atomically
+  @Override
+  public void doCommit(TableMetadata base, TableMetadata metadata) {
+    String newMetadataLocation =
+        base == null && metadata.metadataFileLocation() != null
+            ? metadata.metadataFileLocation()
+            : writeNewMetadata(metadata, currentVersion() + 1);
+    BaseMetastoreOperations.CommitStatus commitStatus =
+        BaseMetastoreOperations.CommitStatus.FAILURE;
+    try {
+      if (base == null) {
+        createTable(newMetadataLocation, metadata);
+      } else {
+        updateTable(base.metadataFileLocation(), newMetadataLocation, 
metadata);
+      }
+      commitStatus = BaseMetastoreOperations.CommitStatus.SUCCESS;
+    } catch (CommitFailedException | CommitStateUnknownException e) {
+      throw e;
+    } catch (Throwable e) {
+      LOG.error("Exception thrown on commit: ", e);
+      if (e instanceof AlreadyExistsException) {
+        throw e;
+      }
+      commitStatus =
+          BaseMetastoreOperations.CommitStatus.valueOf(
+              checkCommitStatus(newMetadataLocation, metadata).name());
+      if (commitStatus == BaseMetastoreOperations.CommitStatus.FAILURE) {
+        throw new CommitFailedException(e, "Failed to commit");
+      }
+      if (commitStatus == BaseMetastoreOperations.CommitStatus.UNKNOWN) {
+        throw new CommitStateUnknownException(e);
+      }
+    } finally {
+      try {
+        if (commitStatus == BaseMetastoreOperations.CommitStatus.FAILURE) {
+          LOG.warn("Failed to commit updates to table {}", tableName());
+          io().deleteFile(newMetadataLocation);
+        }
+      } catch (RuntimeException e) {
+        LOG.error(
+            "Failed to cleanup metadata file at {} for table {}",
+            newMetadataLocation,
+            tableName(),
+            e);
+      }
+    }
+  }
+
+  @Override
+  public String tableName() {
+    return String.format("%s.%s", tableReference.getDatasetId(), 
tableReference.getTableId());
+  }
+
+  @Override
+  public FileIO io() {
+    return fileIO;
+  }
+
+  private void createTable(String newMetadataLocation, TableMetadata metadata) 
{
+    LOG.debug("Creating a new Iceberg table: {}", tableName());
+    Table tableBuilder = makeNewTable(metadata, newMetadataLocation);
+    tableBuilder.setTableReference(this.tableReference);

Review Comment:
   remote `this` reference



##########
bigquery/src/main/java/org/apache/iceberg/gcp/bigquery/BigQueryTableOperations.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.gcp.bigquery;
+
+import com.google.api.services.bigquery.model.ExternalCatalogTableOptions;
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableReference;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.iceberg.BaseMetastoreOperations;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.SnapshotSummary;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.CommitStateUnknownException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.ConfigProperties;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Handles BigQuery metastore table operations. */
+public final class BigQueryTableOperations extends 
BaseMetastoreTableOperations {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BigQueryTableOperations.class);
+
+  private static final String TABLE_PROPERTIES_BQ_CONNECTION = "bq_connection";
+
+  private final BigQueryMetastoreClient client;
+  private final FileIO fileIO;
+  private final TableReference tableReference;
+  private final Configuration conf;
+
+  BigQueryTableOperations(
+      BigQueryMetastoreClient client,
+      FileIO fileIO,
+      TableReference tableReference,
+      Configuration conf) {
+    this.client = client;
+    this.fileIO = fileIO;
+    this.tableReference = tableReference;
+    this.conf = conf;
+  }
+
+  // The doRefresh method should provide implementation on how to get the 
metadata location.
+  @Override
+  public void doRefresh() {
+    // Must default to null.
+    String metadataLocation = null;
+    try {
+      metadataLocation =
+          loadMetadataLocationOrThrow(
+              
client.load(this.tableReference).getExternalCatalogTableOptions());
+    } catch (NoSuchTableException e) {
+      if (currentMetadataLocation() != null) {
+        // Re-throws the exception because the table must exist in this case.
+        throw e;
+      }
+    }
+    refreshFromMetadataLocation(metadataLocation);
+  }
+
+  // The doCommit method should provide implementation on how to update with 
metadata location
+  // atomically
+  @Override
+  public void doCommit(TableMetadata base, TableMetadata metadata) {
+    String newMetadataLocation =
+        base == null && metadata.metadataFileLocation() != null
+            ? metadata.metadataFileLocation()
+            : writeNewMetadata(metadata, currentVersion() + 1);
+    BaseMetastoreOperations.CommitStatus commitStatus =
+        BaseMetastoreOperations.CommitStatus.FAILURE;
+    try {
+      if (base == null) {
+        createTable(newMetadataLocation, metadata);
+      } else {
+        updateTable(base.metadataFileLocation(), newMetadataLocation, 
metadata);
+      }
+      commitStatus = BaseMetastoreOperations.CommitStatus.SUCCESS;
+    } catch (CommitFailedException | CommitStateUnknownException e) {
+      throw e;
+    } catch (Throwable e) {
+      LOG.error("Exception thrown on commit: ", e);
+      if (e instanceof AlreadyExistsException) {
+        throw e;
+      }
+      commitStatus =
+          BaseMetastoreOperations.CommitStatus.valueOf(
+              checkCommitStatus(newMetadataLocation, metadata).name());
+      if (commitStatus == BaseMetastoreOperations.CommitStatus.FAILURE) {
+        throw new CommitFailedException(e, "Failed to commit");
+      }
+      if (commitStatus == BaseMetastoreOperations.CommitStatus.UNKNOWN) {
+        throw new CommitStateUnknownException(e);
+      }
+    } finally {
+      try {
+        if (commitStatus == BaseMetastoreOperations.CommitStatus.FAILURE) {
+          LOG.warn("Failed to commit updates to table {}", tableName());
+          io().deleteFile(newMetadataLocation);
+        }
+      } catch (RuntimeException e) {
+        LOG.error(
+            "Failed to cleanup metadata file at {} for table {}",
+            newMetadataLocation,
+            tableName(),
+            e);
+      }
+    }
+  }
+
+  @Override
+  public String tableName() {
+    return String.format("%s.%s", tableReference.getDatasetId(), 
tableReference.getTableId());
+  }
+
+  @Override
+  public FileIO io() {
+    return fileIO;
+  }
+
+  private void createTable(String newMetadataLocation, TableMetadata metadata) 
{
+    LOG.debug("Creating a new Iceberg table: {}", tableName());
+    Table tableBuilder = makeNewTable(metadata, newMetadataLocation);
+    tableBuilder.setTableReference(this.tableReference);
+    addConnectionIfProvided(tableBuilder, metadata.properties());
+
+    client.create(tableBuilder);
+  }
+
+  private void addConnectionIfProvided(Table tableBuilder, Map<String, String> 
metadataProperties) {
+    if (metadataProperties.containsKey(TABLE_PROPERTIES_BQ_CONNECTION)) {
+      tableBuilder
+          .getExternalCatalogTableOptions()
+          
.setConnectionId(metadataProperties.get(TABLE_PROPERTIES_BQ_CONNECTION));
+    }
+  }
+
+  /** Update table properties with concurrent update detection using etag. */
+  private void updateTable(
+      String oldMetadataLocation, String newMetadataLocation, TableMetadata 
metadata) {
+    Table table = client.load(this.tableReference);

Review Comment:
   remote `this` reference



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to