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

org.apache.iceberg.ksyun.ks3.BaseKs3File 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 com.ksyun.ks3.dto.HeadObjectResult;
import com.ksyun.ks3.dto.ObjectMetadata;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.service.Ks3;
import com.ksyun.ks3.service.request.HeadObjectRequest;
import org.apache.iceberg.ksyun.KsyunProperties;

abstract class BaseKs3File {

  private final Ks3 client;
  private final Ks3URI uri;
  private final KsyunProperties ksyunProperties;
  private ObjectMetadata metadata;

  BaseKs3File(Ks3 client, Ks3URI uri, KsyunProperties ksyunProperties) {
    this.client = client;
    this.uri = uri;
    this.ksyunProperties = ksyunProperties;
  }

  public String location() {
    return uri.location();
  }

  Ks3 client() {
    return client;
  }

  Ks3URI uri() {
    return uri;
  }

  public KsyunProperties ksyunProperties() {
    return ksyunProperties;
  }

  /**
   * Note: this may be stale if file was deleted since metadata is cached for size/existence checks.
   *
   * @return flag
   */
  public boolean exists() {
    try {
      getObjectMetadata();
      return true;
    } catch (Ks3ServiceException e) {
      if (e.getStatueCode() == 404) {
        return false;
      } else {
        throw e;
      }
    }
  }

  public void deleteObject() {
    client.deleteObject(uri.bucket(), uri.name());
  }

  protected ObjectMetadata getObjectMetadata() {
    if (metadata == null) {
      HeadObjectRequest request = new HeadObjectRequest(uri.bucket(), uri.name());
      HeadObjectResult result = client.headObject(request);
      if (result != null) {
        metadata = result.getObjectMetadata();
      }
    }
    return metadata;
  }

  @Override
  public String toString() {
    return uri.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy