Skip to content

Commit 0bfb9ff

Browse files
committed
chore: encapsulate BlobDescriptorState
1 parent c648d38 commit 0bfb9ff

2 files changed

Lines changed: 82 additions & 29 deletions

File tree

‎google-cloud-storage/src/main/java/com/google/cloud/storage/BlobDescriptorImpl.java‎

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,18 @@
2828
import com.google.cloud.storage.ResponseContentLifecycleHandle.ChildRef;
2929
import com.google.common.annotations.VisibleForTesting;
3030
import com.google.protobuf.ByteString;
31-
import com.google.storage.v2.BidiReadHandle;
3231
import com.google.storage.v2.BidiReadObjectRequest;
3332
import com.google.storage.v2.BidiReadObjectResponse;
34-
import com.google.storage.v2.Object;
3533
import com.google.storage.v2.ObjectRangeData;
3634
import com.google.storage.v2.ReadRange;
3735
import java.io.IOException;
3836
import java.util.ArrayList;
3937
import java.util.Collections;
4038
import java.util.List;
41-
import java.util.Map;
42-
import java.util.concurrent.ConcurrentHashMap;
4339
import java.util.concurrent.ExecutionException;
4440
import java.util.concurrent.Executor;
4541
import java.util.concurrent.TimeUnit;
4642
import java.util.concurrent.TimeoutException;
47-
import java.util.concurrent.atomic.AtomicLong;
48-
import java.util.concurrent.atomic.AtomicReference;
4943

5044
final class BlobDescriptorImpl implements BlobDescriptor {
5145

@@ -56,18 +50,18 @@ final class BlobDescriptorImpl implements BlobDescriptor {
5650
private BlobDescriptorImpl(BlobDescriptorStream stream, BlobDescriptorState state) {
5751
this.stream = stream;
5852
this.state = state;
59-
this.info = Conversions.grpc().blobInfo().decode(state.metadata.get());
53+
this.info = Conversions.grpc().blobInfo().decode(state.getMetadata());
6054
}
6155

6256
@Override
6357
public ApiFuture<byte[]> readRangeAsBytes(ByteRangeSpec range) {
64-
long readId = state.readIdSeq.getAndIncrement();
58+
long readId = state.newReadId();
6559
SettableApiFuture<byte[]> future = SettableApiFuture.create();
6660
OutstandingReadToArray value =
6761
new OutstandingReadToArray(readId, range.beginOffset(), range.length(), future);
6862
BidiReadObjectRequest request =
6963
BidiReadObjectRequest.newBuilder().addReadRanges(value.makeReadRange()).build();
70-
state.outstandingReads.put(readId, value);
64+
state.putOutstandingRead(readId, value);
7165
stream.send(request);
7266
return future;
7367
}
@@ -248,10 +242,10 @@ protected void onResponseImpl(BidiReadObjectResponse response) {
248242
try (ResponseContentLifecycleHandle<BidiReadObjectResponse> handle =
249243
bidiResponseContentLifecycleManager.get(response)) {
250244
if (response.hasMetadata()) {
251-
state.metadata.set(response.getMetadata());
245+
state.setMetadata(response.getMetadata());
252246
}
253247
if (response.hasReadHandle()) {
254-
state.ref.set(response.getReadHandle());
248+
state.setBidiReadHandle(response.getReadHandle());
255249
}
256250
List<ObjectRangeData> rangeData = response.getObjectDataRangesList();
257251
if (rangeData.isEmpty()) {
@@ -260,7 +254,7 @@ protected void onResponseImpl(BidiReadObjectResponse response) {
260254
for (int i = 0; i < rangeData.size(); i++) {
261255
ObjectRangeData d = rangeData.get(i);
262256
long id = d.getReadRange().getReadId();
263-
OutstandingReadToArray read = state.outstandingReads.get(id);
257+
OutstandingReadToArray read = state.getOutstandingRead(id);
264258
if (read == null) {
265259
continue;
266260
}
@@ -277,7 +271,7 @@ protected void onResponseImpl(BidiReadObjectResponse response) {
277271
try {
278272
read.eof();
279273
// don't remove the outstanding read until the future has been resolved
280-
state.outstandingReads.remove(id);
274+
state.removeOutstandingRead(id);
281275
} catch (IOException e) {
282276
// TODO: sync this up with stream restarts when the time comes
283277
throw StorageException.coalesce(e);
@@ -340,20 +334,4 @@ public ReadRange makeReadRange() {
340334
.build();
341335
}
342336
}
343-
344-
private static final class BlobDescriptorState {
345-
private final BidiReadObjectRequest openRequest;
346-
private final AtomicReference<BidiReadHandle> ref;
347-
private final AtomicReference<Object> metadata;
348-
private final AtomicLong readIdSeq;
349-
private final Map<Long, OutstandingReadToArray> outstandingReads;
350-
351-
public BlobDescriptorState(BidiReadObjectRequest openRequest) {
352-
this.openRequest = openRequest;
353-
this.ref = new AtomicReference<>();
354-
this.metadata = new AtomicReference<>();
355-
this.readIdSeq = new AtomicLong(1);
356-
this.outstandingReads = new ConcurrentHashMap<>();
357-
}
358-
}
359337
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 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.cloud.storage.BlobDescriptorImpl.OutstandingReadToArray;
20+
import com.google.storage.v2.BidiReadHandle;
21+
import com.google.storage.v2.BidiReadObjectRequest;
22+
import com.google.storage.v2.Object;
23+
import java.util.Map;
24+
import java.util.concurrent.ConcurrentHashMap;
25+
import java.util.concurrent.atomic.AtomicLong;
26+
import java.util.concurrent.atomic.AtomicReference;
27+
28+
final class BlobDescriptorState {
29+
30+
private final BidiReadObjectRequest openRequest;
31+
private final AtomicReference<BidiReadHandle> bidiReadHandle;
32+
private final AtomicReference<Object> metadata;
33+
private final AtomicLong readIdSeq;
34+
private final Map<Long, OutstandingReadToArray> outstandingReads;
35+
36+
BlobDescriptorState(BidiReadObjectRequest openRequest) {
37+
this.openRequest = openRequest;
38+
this.bidiReadHandle = new AtomicReference<>();
39+
this.metadata = new AtomicReference<>();
40+
this.readIdSeq = new AtomicLong(1);
41+
this.outstandingReads = new ConcurrentHashMap<>();
42+
}
43+
44+
BidiReadHandle getBidiReadHandle() {
45+
return bidiReadHandle.get();
46+
}
47+
48+
void setBidiReadHandle(BidiReadHandle newValue) {
49+
bidiReadHandle.set(newValue);
50+
}
51+
52+
Object getMetadata() {
53+
return metadata.get();
54+
}
55+
56+
void setMetadata(Object metadata) {
57+
this.metadata.set(metadata);
58+
}
59+
60+
long newReadId() {
61+
return readIdSeq.getAndIncrement();
62+
}
63+
64+
OutstandingReadToArray getOutstandingRead(long key) {
65+
return outstandingReads.get(key);
66+
}
67+
68+
void putOutstandingRead(long key, OutstandingReadToArray value) {
69+
outstandingReads.put(key, value);
70+
}
71+
72+
void removeOutstandingRead(long key) {
73+
outstandingReads.remove(key);
74+
}
75+
}

0 commit comments

Comments
 (0)