Skip to content

Commit

Permalink
Extract classes to own files, more StorageClient initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseLovelace committed Apr 10, 2024
1 parent 7b41aa1 commit 4ad2a62
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,83 +232,3 @@ private Iterator<ReadObjectResponse> ensureResponseIteratorOpen() {
}
}

/**
* Shrink wraps a beginning, offset and limit for tracking state of an individual invocation of
* {@link #read}
*/
final class ReadCursor {
private final long beginning;
private long offset;
private final long limit;

ReadCursor(long beginning, long limit) {
this.limit = limit;
this.beginning = beginning;
this.offset = beginning;
}

public boolean hasRemaining() {
return limit - offset > 0;
}

public void advance(long incr) {
checkArgument(incr >= 0);
offset += incr;
}

public long read() {
return offset - beginning;
}

@Override
public String toString() {
return String.format("ReadCursor{begin=%d, offset=%d, limit=%d}", beginning, offset, limit);
}
}

interface ResponseContentLifecycleManager {
ResponseContentLifecycleHandle get(ReadObjectResponse response);

static ResponseContentLifecycleManager noop() {
return response ->
new ResponseContentLifecycleHandle(
response,
() -> {
// no-op
});
}
}

final class ResponseContentLifecycleHandle implements Closeable {
@Nullable private final Closeable dispose;

private final List<ByteBuffer> buffers;

ResponseContentLifecycleHandle(ReadObjectResponse resp, @Nullable Closeable dispose) {
this.dispose = dispose;

this.buffers = resp.getChecksummedData().getContent().asReadOnlyByteBufferList();
}

void copy(ReadCursor c, ByteBuffer[] dsts, int offset, int length) {
for (ByteBuffer b : buffers) {
long copiedBytes = Buffers.copy(b, dsts, offset, length);
c.advance(copiedBytes);
if (b.hasRemaining()) break;
}
}

boolean hasRemaining() {
for (ByteBuffer b : buffers) {
if (b.hasRemaining()) return true;
}
return false;
}

@Override
public void close() throws IOException {
if (dispose != null) {
dispose.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ public Storage create(StorageOptions options) {
Tuple<StorageSettings, Opts<UserProject>> t = grpcStorageOptions.resolveSettingsAndOpts();
StorageSettings storageSettings = t.x();
Opts<UserProject> defaultOpts = t.y();
StorageClient client;
if (ZeroCopyReadinessChecker.isReady()) {
StorageStubSettings stubSettings =
(StorageStubSettings) storageSettings.getStubSettings();
Expand All @@ -698,15 +697,15 @@ public Storage create(StorageOptions options) {
InternalZeroCopyGrpcStorageStub stub =
new InternalZeroCopyGrpcStorageStub(
stubSettings, clientContext, grpcStorageCallableFactory);
client = new InternalStorageClient(stub);
StorageClient client = new InternalStorageClient(stub);
return new GrpcStorageImpl(
grpcStorageOptions,
client,
stub.getObjectMediaResponseMarshaller,
grpcStorageOptions.blobWriteSessionConfig.createFactory(Clock.systemUTC()),
defaultOpts);
} else {
client = StorageClient.create(storageSettings);
StorageClient client = StorageClient.create(storageSettings);
return new GrpcStorageImpl(
grpcStorageOptions,
client,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.google.cloud.storage;

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

/**
* Shrink wraps a beginning, offset and limit for tracking state of an individual invocation of
* {@link #read}
*/
final class ReadCursor {
private final long beginning;
private long offset;
private final long limit;

ReadCursor(long beginning, long limit) {
this.limit = limit;
this.beginning = beginning;
this.offset = beginning;
}

public boolean hasRemaining() {
return limit - offset > 0;
}

public void advance(long incr) {
checkArgument(incr >= 0);
offset += incr;
}

public long read() {
return offset - beginning;
}

@Override
public String toString() {
return String.format("ReadCursor{begin=%d, offset=%d, limit=%d}", beginning, offset, limit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.google.cloud.storage;

import com.google.storage.v2.ReadObjectResponse;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

final class ResponseContentLifecycleHandle implements Closeable {
@Nullable
private final Closeable dispose;

private final List<ByteBuffer> buffers;

ResponseContentLifecycleHandle(ReadObjectResponse resp, @Nullable Closeable dispose) {
this.dispose = dispose;

this.buffers = resp.getChecksummedData().getContent().asReadOnlyByteBufferList();
}

void copy(ReadCursor c, ByteBuffer[] dsts, int offset, int length) {
for (ByteBuffer b : buffers) {
long copiedBytes = Buffers.copy(b, dsts, offset, length);
c.advance(copiedBytes);
if (b.hasRemaining()) break;
}
}

boolean hasRemaining() {
for (ByteBuffer b : buffers) {
if (b.hasRemaining()) return true;
}
return false;
}

@Override
public void close() throws IOException {
if (dispose != null) {
dispose.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.google.cloud.storage;

import com.google.storage.v2.ReadObjectResponse;

interface ResponseContentLifecycleManager {
ResponseContentLifecycleHandle get(ReadObjectResponse response);

static ResponseContentLifecycleManager noop() {
return response ->
new ResponseContentLifecycleHandle(
response,
() -> {
// no-op
});
}
}

0 comments on commit 4ad2a62

Please sign in to comment.