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

com.google.cloud.storage.GrpcBlobWriteChannel Maven / Gradle / Ivy

There is a newer version: 2.45.0
Show newest version
/*
 * Copyright 2022 Google LLC
 *
 * Licensed 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 com.google.cloud.storage;

import com.google.api.core.ApiFuture;
import com.google.api.gax.retrying.ResultRetryAlgorithm;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.cloud.RestorableState;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Retrying.RetryingDependencies;
import com.google.storage.v2.WriteObjectRequest;
import com.google.storage.v2.WriteObjectResponse;
import java.util.function.Supplier;

final class GrpcBlobWriteChannel extends BaseStorageWriteChannel {

  private final ClientStreamingCallable write;
  private final RetryingDependencies deps;
  private final ResultRetryAlgorithm alg;
  private final Supplier> start;
  private final Hasher hasher;

  GrpcBlobWriteChannel(
      ClientStreamingCallable write,
      RetryingDependencies deps,
      ResultRetryAlgorithm alg,
      Supplier> start,
      Hasher hasher) {
    super(Conversions.grpc().blobInfo().compose(WriteObjectResponse::getResource));
    this.write = write;
    this.deps = deps;
    this.alg = alg;
    this.start = start;
    this.hasher = hasher;
  }

  @Override
  public RestorableState capture() {
    return CrossTransportUtils.throwHttpJsonOnly(WriteChannel.class, "capture");
  }

  @Override
  protected LazyWriteChannel newLazyWriteChannel() {
    return new LazyWriteChannel<>(
        () ->
            ResumableMedia.gapic()
                .write()
                .byteChannel(write)
                .setHasher(hasher)
                .setByteStringStrategy(ByteStringStrategy.copy())
                .resumable()
                .withRetryConfig(deps, alg)
                .buffered(getBufferHandle())
                .setStartAsync(start.get())
                .build());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy