Skip to content

Commit 616256a

Browse files
committed
chore: make AppendableBlobUpload an interface to allow decoration
Pre-work for otel tracing
1 parent ba3af58 commit 616256a

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage;
18+
19+
import com.google.api.core.BetaApi;
20+
import com.google.api.core.InternalExtensionOnly;
21+
import com.google.cloud.storage.Storage.BlobWriteOption;
22+
import java.io.IOException;
23+
import java.nio.ByteBuffer;
24+
import java.nio.channels.WritableByteChannel;
25+
import java.util.concurrent.ExecutionException;
26+
27+
/**
28+
* Interface representing those methods which can be used to write to and interact with an
29+
* appendable upload.
30+
*
31+
* @see Storage#appendableBlobUpload(BlobInfo, AppendableBlobUploadConfig, BlobWriteOption...)
32+
*/
33+
@BetaApi
34+
@InternalExtensionOnly
35+
public interface AppendableBlobUpload extends AutoCloseable, WritableByteChannel {
36+
37+
/**
38+
* Write some bytes to the appendable session. Whether a flush happens will depend on how many
39+
* bytes have been written prior, how many bytes are being written now and what {@link
40+
* AppendableBlobUploadConfig} was provided when creating the {@link AppendableBlobUpload}.
41+
*
42+
* <p>This method can block the invoking thread in order to ensure written bytes are acknowledged
43+
* by Google Cloud Storage.
44+
*
45+
* @see Storage#appendableBlobUpload(BlobInfo, AppendableBlobUploadConfig, BlobWriteOption...)
46+
*/
47+
@Override
48+
int write(ByteBuffer src) throws IOException;
49+
50+
/**
51+
* Close this instance to further {@link #write(ByteBuffer)}ing. This will close any underlying
52+
* stream and release any releasable resources once out of scope.
53+
*
54+
* <p>{@link #finalizeUpload()} can be called after this method, but it will not carry any bytes
55+
* with it.
56+
*/
57+
@Override
58+
void close() throws IOException;
59+
60+
/**
61+
* Finalize the appendable upload, close any underlying stream and release any releasable
62+
* resources once out of scope.
63+
*
64+
* <p>Once this method is called, and returns no more write to the object will be allowed by GCS.
65+
*/
66+
@BetaApi
67+
BlobInfo finalizeUpload() throws IOException, ExecutionException, InterruptedException;
68+
}

google-cloud-storage/src/main/java/com/google/cloud/storage/AppendableBlobUploadImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Google LLC
2+
* Copyright 2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,16 +21,15 @@
2121
import com.google.cloud.storage.BufferedWritableByteChannelSession.BufferedWritableByteChannel;
2222
import java.io.IOException;
2323
import java.nio.ByteBuffer;
24-
import java.nio.channels.WritableByteChannel;
2524
import java.util.concurrent.ExecutionException;
2625
import java.util.concurrent.locks.ReentrantLock;
2726

2827
@BetaApi
29-
public final class AppendableBlobUpload implements AutoCloseable, WritableByteChannel {
28+
final class AppendableBlobUploadImpl implements AppendableBlobUpload {
3029
private final AppendableObjectBufferedWritableByteChannel channel;
3130
private final ApiFuture<BlobInfo> result;
3231

33-
private AppendableBlobUpload(BlobInfo blob, BlobWriteSession session, boolean takeover)
32+
private AppendableBlobUploadImpl(BlobInfo blob, BlobWriteSession session, boolean takeover)
3433
throws IOException {
3534
channel = (AppendableObjectBufferedWritableByteChannel) (session.open());
3635
result = session.getResult();
@@ -41,12 +40,12 @@ private AppendableBlobUpload(BlobInfo blob, BlobWriteSession session, boolean ta
4140

4241
static AppendableBlobUpload createNewAppendableBlob(BlobInfo blob, BlobWriteSession session)
4342
throws IOException {
44-
return new AppendableBlobUpload(blob, session, false);
43+
return new AppendableBlobUploadImpl(blob, session, false);
4544
}
4645

4746
static AppendableBlobUpload resumeAppendableUpload(BlobInfo blob, BlobWriteSession session)
4847
throws IOException {
49-
return new AppendableBlobUpload(blob, session, true);
48+
return new AppendableBlobUploadImpl(blob, session, true);
5049
}
5150

5251
void startTakeoverStream() {

google-cloud-storage/src/main/java/com/google/cloud/storage/GapicBidiWritableByteChannelSessionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ BufferedWritableByteChannelSession<BidiWriteObjectResponse> build() {
248248
Retrying::newCallContext))
249249
.andThen(
250250
c ->
251-
new AppendableBlobUpload.AppendableObjectBufferedWritableByteChannel(
251+
new AppendableBlobUploadImpl.AppendableObjectBufferedWritableByteChannel(
252252
new DefaultBufferedWritableByteChannel(bufferHandle, c), c)));
253253
}
254254
}

google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,8 +1480,8 @@ public boolean shouldRetry(
14801480
build, BidiBlobWriteSessionConfig.Factory.WRITE_OBJECT_RESPONSE_BLOB_INFO_DECODER);
14811481
BlobWriteSession session = BlobWriteSessions.of(dec);
14821482
return takeOver
1483-
? AppendableBlobUpload.resumeAppendableUpload(blob, session)
1484-
: AppendableBlobUpload.createNewAppendableBlob(blob, session);
1483+
? AppendableBlobUploadImpl.resumeAppendableUpload(blob, session)
1484+
: AppendableBlobUploadImpl.createNewAppendableBlob(blob, session);
14851485
}
14861486

14871487
@Override

0 commit comments

Comments
 (0)