|
| 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.api.core.SettableApiFuture; |
| 20 | +import com.google.cloud.storage.BlobDescriptor.ZeroCopySupport.DisposableByteString; |
| 21 | +import com.google.cloud.storage.ResponseContentLifecycleHandle.ChildRef; |
| 22 | +import com.google.protobuf.ByteString; |
| 23 | +import com.google.storage.v2.ReadRange; |
| 24 | +import java.io.Closeable; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +abstract class BlobDescriptorStreamRead implements AutoCloseable, Closeable { |
| 31 | + |
| 32 | + protected final long readId; |
| 33 | + protected final ReadCursor readCursor; |
| 34 | + protected final List<ChildRef> childRefs; |
| 35 | + protected boolean closed; |
| 36 | + |
| 37 | + private BlobDescriptorStreamRead(long readId, long readOffset, long readLimit) { |
| 38 | + this.readId = readId; |
| 39 | + this.readCursor = new ReadCursor(readOffset, readOffset + readLimit); |
| 40 | + this.childRefs = Collections.synchronizedList(new ArrayList<>()); |
| 41 | + this.closed = false; |
| 42 | + } |
| 43 | + |
| 44 | + abstract void accept(ChildRef childRef) throws IOException; |
| 45 | + |
| 46 | + abstract void eof() throws IOException; |
| 47 | + |
| 48 | + final ReadRange makeReadRange() { |
| 49 | + return ReadRange.newBuilder() |
| 50 | + .setReadId(readId) |
| 51 | + .setReadOffset(readCursor.position()) |
| 52 | + .setReadLength(readCursor.remaining()) |
| 53 | + .build(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void close() throws IOException { |
| 58 | + if (!closed) { |
| 59 | + closed = true; |
| 60 | + GrpcUtils.closeAll(childRefs); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static AccumulatingRead<byte[]> createByteArrayAccumulatingRead( |
| 65 | + long readId, ByteRangeSpec range, SettableApiFuture<byte[]> complete) { |
| 66 | + return new ByteArrayAccumulatingRead(readId, range.beginOffset(), range.length(), complete); |
| 67 | + } |
| 68 | + |
| 69 | + static AccumulatingRead<DisposableByteString> createZeroCopyByteStringAccumulatingRead( |
| 70 | + long readId, ByteRangeSpec range, SettableApiFuture<DisposableByteString> complete) { |
| 71 | + return new ZeroCopyByteStringAccumulatingRead( |
| 72 | + readId, range.beginOffset(), range.length(), complete); |
| 73 | + } |
| 74 | + |
| 75 | + /** Base class of a read that will accumulate before completing by resolving a future */ |
| 76 | + abstract static class AccumulatingRead<Result> extends BlobDescriptorStreamRead { |
| 77 | + protected final SettableApiFuture<Result> complete; |
| 78 | + |
| 79 | + private AccumulatingRead( |
| 80 | + long readId, long readOffset, long readLimit, SettableApiFuture<Result> complete) { |
| 81 | + super(readId, readOffset, readLimit); |
| 82 | + this.complete = complete; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Base class of a read that will be processed in a streaming manner (e.g. {@link |
| 88 | + * java.nio.channels.ReadableByteChannel}) |
| 89 | + */ |
| 90 | + abstract static class StreamingRead extends BlobDescriptorStreamRead { |
| 91 | + private StreamingRead(long readId, long readOffset, long readLimit) { |
| 92 | + super(readId, readOffset, readLimit); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static final class ByteArrayAccumulatingRead extends AccumulatingRead<byte[]> { |
| 97 | + |
| 98 | + private ByteArrayAccumulatingRead( |
| 99 | + long readId, long readOffset, long readLimit, SettableApiFuture<byte[]> complete) { |
| 100 | + super(readId, readOffset, readLimit, complete); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + void accept(ChildRef childRef) throws IOException { |
| 105 | + int size = childRef.byteString().size(); |
| 106 | + childRefs.add(childRef); |
| 107 | + readCursor.advance(size); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + void eof() throws IOException { |
| 112 | + try { |
| 113 | + ByteString base = ByteString.empty(); |
| 114 | + for (ChildRef ref : childRefs) { |
| 115 | + base = base.concat(ref.byteString()); |
| 116 | + } |
| 117 | + complete.set(base.toByteArray()); |
| 118 | + } finally { |
| 119 | + close(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + static final class ZeroCopyByteStringAccumulatingRead |
| 125 | + extends AccumulatingRead<DisposableByteString> implements DisposableByteString { |
| 126 | + |
| 127 | + private volatile ByteString byteString; |
| 128 | + |
| 129 | + private ZeroCopyByteStringAccumulatingRead( |
| 130 | + long readId, |
| 131 | + long readOffset, |
| 132 | + long readLimit, |
| 133 | + SettableApiFuture<DisposableByteString> complete) { |
| 134 | + super(readId, readOffset, readLimit, complete); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public ByteString byteString() { |
| 139 | + return byteString; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + void accept(ChildRef childRef) throws IOException { |
| 144 | + int size = childRef.byteString().size(); |
| 145 | + childRefs.add(childRef); |
| 146 | + readCursor.advance(size); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + void eof() throws IOException { |
| 151 | + ByteString base = ByteString.empty(); |
| 152 | + for (ChildRef ref : childRefs) { |
| 153 | + base = base.concat(ref.byteString()); |
| 154 | + } |
| 155 | + byteString = base; |
| 156 | + complete.set(this); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments