2828import com .google .cloud .storage .ResponseContentLifecycleHandle .ChildRef ;
2929import com .google .common .annotations .VisibleForTesting ;
3030import com .google .protobuf .ByteString ;
31- import com .google .storage .v2 .BidiReadHandle ;
3231import com .google .storage .v2 .BidiReadObjectRequest ;
3332import com .google .storage .v2 .BidiReadObjectResponse ;
34- import com .google .storage .v2 .Object ;
3533import com .google .storage .v2 .ObjectRangeData ;
3634import com .google .storage .v2 .ReadRange ;
3735import java .io .IOException ;
3836import java .util .ArrayList ;
3937import java .util .Collections ;
4038import java .util .List ;
41- import java .util .Map ;
42- import java .util .concurrent .ConcurrentHashMap ;
4339import java .util .concurrent .ExecutionException ;
4440import java .util .concurrent .Executor ;
4541import java .util .concurrent .TimeUnit ;
4642import java .util .concurrent .TimeoutException ;
47- import java .util .concurrent .atomic .AtomicLong ;
48- import java .util .concurrent .atomic .AtomicReference ;
4943
5044final 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}
0 commit comments