-
Notifications
You must be signed in to change notification settings - Fork 70
/
BlobReadChannelV2.java
206 lines (179 loc) · 7.13 KB
/
BlobReadChannelV2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.StorageObject;
import com.google.cloud.ReadChannel;
import com.google.cloud.RestorableState;
import com.google.cloud.storage.ApiaryUnbufferedReadableByteChannel.ApiaryReadRequest;
import com.google.cloud.storage.HttpDownloadSessionBuilder.ReadableByteChannelSessionBuilder;
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
final class BlobReadChannelV2 extends BaseStorageReadChannel<StorageObject> {
private final StorageObject storageObject;
private final Map<StorageRpc.Option, ?> opts;
private final BlobReadChannelContext blobReadChannelContext;
private final boolean autoGzipDecompression;
BlobReadChannelV2(
StorageObject storageObject,
Map<StorageRpc.Option, ?> opts,
BlobReadChannelContext blobReadChannelContext) {
super(Conversions.apiary().blobInfo());
this.storageObject = storageObject;
this.opts = opts;
this.blobReadChannelContext = blobReadChannelContext;
this.autoGzipDecompression =
// RETURN_RAW_INPUT_STREAM means do not add GZIPInputStream to the pipeline. Meaning, if
// RETURN_RAW_INPUT_STREAM is false, automatically attempt to decompress if Content-Encoding
// gzip.
Boolean.FALSE.equals(opts.get(StorageRpc.Option.RETURN_RAW_INPUT_STREAM));
}
@Override
public synchronized RestorableState<ReadChannel> capture() {
ApiaryReadRequest apiaryReadRequest = getApiaryReadRequest();
return new BlobReadChannelV2State(
apiaryReadRequest, blobReadChannelContext.getStorageOptions(), getChunkSize());
}
protected LazyReadChannel<?, StorageObject> newLazyReadChannel() {
return new LazyReadChannel<>(
() -> {
ReadableByteChannelSessionBuilder b =
ResumableMedia.http()
.read()
.byteChannel(blobReadChannelContext)
.setAutoGzipDecompression(autoGzipDecompression);
BufferHandle bufferHandle = getBufferHandle();
// because we're erasing the specific type of channel, we need to declare it here.
// If we don't, the compiler complains we're not returning a compliant type.
ReadableByteChannelSession<?, StorageObject> session;
if (bufferHandle.capacity() > 0) {
session = b.buffered(bufferHandle).setApiaryReadRequest(getApiaryReadRequest()).build();
} else {
session = b.unbuffered().setApiaryReadRequest(getApiaryReadRequest()).build();
}
return session;
});
}
private ApiaryReadRequest getApiaryReadRequest() {
StorageObject object = getResolvedObject() != null ? getResolvedObject() : storageObject;
return new ApiaryReadRequest(object, opts, getByteRangeSpec());
}
static final class BlobReadChannelV2State implements RestorableState<ReadChannel>, Serializable {
private static final long serialVersionUID = -7595661593080505431L;
private final ApiaryReadRequest request;
private final HttpStorageOptions options;
private final Integer chunkSize;
private BlobReadChannelV2State(
ApiaryReadRequest request, HttpStorageOptions options, Integer chunkSize) {
this.request = request;
this.options = options;
this.chunkSize = chunkSize;
}
@Override
public ReadChannel restore() {
BlobReadChannelV2 channel =
new BlobReadChannelV2(
request.getObject(), request.getOptions(), BlobReadChannelContext.from(options));
channel.setByteRangeSpec(request.getByteRangeSpec());
if (chunkSize != null) {
channel.setChunkSize(chunkSize);
}
return channel;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof BlobReadChannelV2State)) {
return false;
}
BlobReadChannelV2State that = (BlobReadChannelV2State) o;
return Objects.equals(request, that.request)
&& Objects.equals(options, that.options)
&& Objects.equals(chunkSize, that.chunkSize);
}
@Override
public int hashCode() {
return Objects.hash(request, options, chunkSize);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("request", request)
.add("options", options)
.add("chunkSize", chunkSize)
.toString();
}
}
static final class BlobReadChannelContext {
private final HttpStorageOptions storageOptions;
private final HttpRetryAlgorithmManager retryAlgorithmManager;
private final Storage apiaryClient;
private BlobReadChannelContext(
HttpStorageOptions storageOptions,
Storage apiaryClient,
HttpRetryAlgorithmManager retryAlgorithmManager) {
this.storageOptions = storageOptions;
this.apiaryClient = apiaryClient;
this.retryAlgorithmManager = retryAlgorithmManager;
}
public HttpStorageOptions getStorageOptions() {
return storageOptions;
}
public HttpRetryAlgorithmManager getRetryAlgorithmManager() {
return retryAlgorithmManager;
}
public Storage getApiaryClient() {
return apiaryClient;
}
static BlobReadChannelContext from(HttpStorageOptions options) {
return new BlobReadChannelContext(
options, options.getStorageRpcV1().getStorage(), options.getRetryAlgorithmManager());
}
static BlobReadChannelContext from(com.google.cloud.storage.Storage s) {
StorageOptions options = s.getOptions();
if (options instanceof HttpStorageOptions) {
HttpStorageOptions httpStorageOptions = (HttpStorageOptions) options;
return from(httpStorageOptions);
}
throw new IllegalArgumentException("Only HttpStorageOptions based instance supported");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof BlobReadChannelContext)) {
return false;
}
BlobReadChannelContext that = (BlobReadChannelContext) o;
return Objects.equals(storageOptions, that.storageOptions)
&& Objects.equals(retryAlgorithmManager, that.retryAlgorithmManager);
}
@Override
public int hashCode() {
return Objects.hash(storageOptions, retryAlgorithmManager);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("storageOptions", storageOptions).toString();
}
}
}