Skip to content

Commit

Permalink
chore: add ParallelCompositeUploadWritableByteChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWhitehead committed Sep 26, 2023
1 parent 3645146 commit e5c3591
Show file tree
Hide file tree
Showing 9 changed files with 1,399 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.InternalApi;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
Expand Down Expand Up @@ -104,6 +105,11 @@ public boolean equals(Object obj) {
&& Objects.equals(generation, other.generation);
}

@InternalApi
BlobId withGeneration(long generation) {
return new BlobId(bucket, name, generation);
}

/**
* Creates a blob identifier. Generation is set to {@code null}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ static int position(Buffer b) {
return b.position();
}

/** attempt to drain all of {@code content} into {@code dst} */
static long copy(ByteBuffer content, ByteBuffer dst) {
return copy(content, new ByteBuffer[] {dst}, 0, 1);
}

/**
* attempt to drain all of {@code content} into {@code dsts} starting from {@code dsts[0]} through
* {@code dsts[dsts.length - 1]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.cloud.storage;

import java.util.Objects;

abstract class Crc32cValue<Res extends Crc32cValue<Res>> {

private Crc32cValue() {}
Expand Down Expand Up @@ -95,6 +97,23 @@ public String debugString() {
public Crc32cLengthKnown withLength(long length) {
return new Crc32cLengthKnown(value, length);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Crc32cLengthUnknown)) {
return false;
}
Crc32cLengthUnknown that = (Crc32cLengthUnknown) o;
return value == that.value;
}

@Override
public int hashCode() {
return Objects.hash(value);
}
}

static final class Crc32cLengthKnown extends Crc32cValue<Crc32cLengthKnown> {
Expand Down Expand Up @@ -130,5 +149,22 @@ public String toString() {
public String debugString() {
return fmtCrc32cValue(value);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Crc32cLengthKnown)) {
return false;
}
Crc32cLengthKnown that = (Crc32cLengthKnown) o;
return value == that.value && length == that.length;
}

@Override
public int hashCode() {
return Objects.hash(value, length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.storage;

import static com.google.cloud.storage.ByteSizeConstants._16MiB;
import static com.google.cloud.storage.ByteSizeConstants._1MiB;
import static com.google.cloud.storage.ByteSizeConstants._256KiB;
import static com.google.cloud.storage.CrossTransportUtils.fmtMethodName;
import static com.google.cloud.storage.CrossTransportUtils.throwHttpJsonOnly;
Expand Down Expand Up @@ -620,7 +621,7 @@ public CopyWriter copy(CopyRequest copyRequest) {
}

if (copyRequest.getMegabytesCopiedPerChunk() != null) {
b.setMaxBytesRewrittenPerCall(copyRequest.getMegabytesCopiedPerChunk());
b.setMaxBytesRewrittenPerCall(copyRequest.getMegabytesCopiedPerChunk() * _1MiB);
}

RewriteObjectRequest req = mapper.apply(b).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023 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.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.ErrorDetails;
import com.google.api.gax.rpc.StatusCode;
import com.google.common.collect.ImmutableList;
import io.grpc.Status.Code;

final class ParallelCompositeUploadException extends ApiException {

private final ApiFuture<ImmutableList<BlobId>> createdObjects;

private ParallelCompositeUploadException(
Throwable cause,
StatusCode statusCode,
ErrorDetails errorDetails,
ApiFuture<ImmutableList<BlobId>> createdObjects) {
super(cause, statusCode, false, errorDetails);
this.createdObjects = createdObjects;
}

public ApiFuture<ImmutableList<BlobId>> getCreatedObjects() {
return createdObjects;
}

static ParallelCompositeUploadException of(
Throwable t, ApiFuture<ImmutableList<BlobId>> createdObjects) {
StatusCode statusCode;
ErrorDetails errorDetails;

Throwable cause = t;
if (t instanceof StorageException && t.getCause() != null) {
cause = t.getCause();
}

if (cause instanceof ApiException) {
ApiException apiException = (ApiException) cause;
statusCode = apiException.getStatusCode();
errorDetails = apiException.getErrorDetails();
} else {
statusCode = GrpcStatusCode.of(Code.UNKNOWN);
errorDetails = ErrorDetails.builder().build();
}
return new ParallelCompositeUploadException(cause, statusCode, errorDetails, createdObjects);
}
}
Loading

0 comments on commit e5c3591

Please sign in to comment.