Skip to content

Commit

Permalink
chore: update GrpcBlobReadChannel to configure lazy buffer allocation (
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWhitehead committed Oct 19, 2022
1 parent e78e3d2 commit 5ff8be1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@
import com.google.storage.v2.ReadObjectResponse;
import java.nio.ByteBuffer;
import java.util.function.BiFunction;
import javax.annotation.concurrent.Immutable;

@Immutable
final class GapicDownloadSessionBuilder {
private static final GapicDownloadSessionBuilder INSTANCE = new GapicDownloadSessionBuilder();

private static final int DEFAULT_BUFFER_CAPACITY = ByteSizeConstants._16MiB;

private GapicDownloadSessionBuilder() {}

public static GapicDownloadSessionBuilder create() {
return new GapicDownloadSessionBuilder();
return INSTANCE;
}

/**
Expand All @@ -60,7 +65,7 @@ private ReadableByteChannelSessionBuilder(
}

public BufferedReadableByteChannelSessionBuilder buffered() {
return buffered(Buffers.allocate(16 * 1024 * 1024));
return buffered(BufferHandle.allocate(DEFAULT_BUFFER_CAPACITY));
}

public ReadableByteChannelSessionBuilder setHasher(Hasher hasher) {
Expand All @@ -74,20 +79,20 @@ public ReadableByteChannelSessionBuilder setAutoGzipDecompression(
return this;
}

public BufferedReadableByteChannelSessionBuilder buffered(int capacity) {
return new BufferedReadableByteChannelSessionBuilder(BufferHandle.allocate(capacity), getF());
public BufferedReadableByteChannelSessionBuilder buffered(BufferHandle bufferHandle) {
return new BufferedReadableByteChannelSessionBuilder(bufferHandle, bindFunction());
}

public BufferedReadableByteChannelSessionBuilder buffered(ByteBuffer buffer) {
return new BufferedReadableByteChannelSessionBuilder(BufferHandle.handleOf(buffer), getF());
return buffered(BufferHandle.handleOf(buffer));
}

public UnbufferedReadableByteChannelSessionBuilder unbuffered() {
return new UnbufferedReadableByteChannelSessionBuilder(getF());
return new UnbufferedReadableByteChannelSessionBuilder(bindFunction());
}

private BiFunction<ReadObjectRequest, SettableApiFuture<Object>, UnbufferedReadableByteChannel>
getF() {
bindFunction() {
// for any non-final value, create a reference to the value at this point in time
Hasher hasher = this.hasher;
boolean autoGzipDecompression = this.autoGzipDecompression;
Expand All @@ -103,7 +108,8 @@ public UnbufferedReadableByteChannelSessionBuilder unbuffered() {

public static final class BufferedReadableByteChannelSessionBuilder {

private BiFunction<ReadObjectRequest, SettableApiFuture<Object>, BufferedReadableByteChannel>
private final BiFunction<
ReadObjectRequest, SettableApiFuture<Object>, BufferedReadableByteChannel>
f;
private ReadObjectRequest request;

Expand All @@ -129,7 +135,7 @@ public BufferedReadableByteChannelSession<Object> build() {

public static final class UnbufferedReadableByteChannelSessionBuilder {

private BiFunction<
private final BiFunction<
ReadObjectRequest, SettableApiFuture<Object>, UnbufferedReadableByteChannel>
f;
private ReadObjectRequest request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.storage;

import static com.google.cloud.storage.ByteSizeConstants._16MiB;
import static com.google.cloud.storage.StorageV2ProtoUtils.seekReadObjectRequest;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
Expand All @@ -40,7 +41,7 @@ final class GrpcBlobReadChannel implements ReadChannel {

private Long position;
private Long limit;
private int chunkSize = 16 * 1024 * 1024;
private int chunkSize = _16MiB;

GrpcBlobReadChannel(
ServerStreamingCallable<ReadObjectRequest, ReadObjectResponse> read,
Expand All @@ -57,7 +58,7 @@ final class GrpcBlobReadChannel implements ReadChannel {
.byteChannel(read)
.setHasher(Hasher.noop())
.setAutoGzipDecompression(autoGzipDecompression)
.buffered(Buffers.allocate(chunkSize))
.buffered(BufferHandle.allocate(chunkSize))
.setReadObjectRequest(req)
.build();
}));
Expand Down

0 comments on commit 5ff8be1

Please sign in to comment.