All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.iceberg.ksyun.ks3.Ks3TableOperations Maven / Gradle / Ivy

The newest version!
/*
 * 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.ksyun.ks3;

import org.apache.iceberg.BaseMetastoreTableOperations;
import org.apache.iceberg.LockManager;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.exceptions.NoSuchTableException;
import org.apache.iceberg.io.FileIO;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;

import java.io.IOException;
import java.util.Map;

public class Ks3TableOperations extends BaseMetastoreTableOperations {

  public static final String ICEBERG_METADATA_LOCATION = "iceberg_metadata_location";

  private final String tableName;
  private final FileIO fileIO;
  private final Ks3Catalog catalog;
  private final Ks3URI tableObject;
//  private final String commitLockEntityId;

  /**
   * Cached E-Tag for CAS commit
   *
   * @see #doRefresh() when reset this field
   * @see #doCommit(TableMetadata, TableMetadata) when use this field
   */
  private String eTag;

  public Ks3TableOperations(String tableName, Ks3URI tableObject, FileIO fileIO, Ks3Catalog catalog) {
    this.tableName = tableName;
    this.tableObject = tableObject;
    this.fileIO = fileIO;
    this.catalog = catalog;
//    this.commitLockEntityId = String.format("%s.%s", tableObject.name(), "lock");
  }

  @Override
  protected String tableName() {
    return tableName;
  }

  @Override
  public FileIO io() {
    return fileIO;
  }

  @Override
  protected void doRefresh() {
    String metadataLocation;
    if (!catalog.objectMetadata(tableObject).isPresent()) {
      if (currentMetadataLocation() != null) {
        throw new NoSuchTableException("Metadata object %s is absent while refresh a loaded table. " +
                "Maybe the table is deleted/moved.", tableObject);
      } else {
        metadataLocation = null;
      }
    } else {
      Ks3Catalog.Properties metadata = catalog.loadProperties(tableObject);
      this.eTag = metadata.eTag();
      metadataLocation = metadata.content().get(ICEBERG_METADATA_LOCATION);
      Preconditions.checkNotNull(metadataLocation,
          "Can't find location from table metadata %s", tableObject);
    }
    refreshFromMetadataLocation(metadataLocation);
  }

  @Override
  protected void doCommit(TableMetadata base, TableMetadata metadata) {
    String newMetadataLocation = writeNewMetadata(metadata, currentVersion() + 1);
    if (base == null) {
//      System.out.println(" -----------  TableMetadata base: NULL ---------");
      // create a new table, the metadataKey should be absent
      if (!catalog.putNewProperties(tableObject, buildProperties(newMetadataLocation))) {
        throw new CommitFailedException("Table is existing when create table %s", tableName());
      }
    } else {
//      System.out.println(" -----------  TableMetadata base: " + base.metadataFileLocation());
      String cachedETag = eTag;
      Preconditions.checkNotNull(cachedETag, "E-Tag must be not null when update table");
//       replace to a new version, the E-Tag should be present and matched
      boolean result = catalog.updatePropertiesObject(
          tableObject, cachedETag, buildProperties(newMetadataLocation));
      if (!result) {
        throw new CommitFailedException("Replace failed, E-Tag %s mismatch for table %s", cachedETag, tableName());
      }
    }
  }

  /**
   * Build properties for table
   */
  private Map buildProperties(String metadataLocation) {
    return ImmutableMap.of(ICEBERG_METADATA_LOCATION, metadataLocation);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy