-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathStorageServiceImpl.java
More file actions
652 lines (591 loc) · 24.7 KB
/
Copy pathStorageServiceImpl.java
File metadata and controls
652 lines (591 loc) · 24.7 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.gcloud.storage;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gcloud.RetryHelper.runWithRetries;
import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER;
import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_NOT_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH;
import static java.util.concurrent.Executors.callable;
import com.google.api.services.storage.model.StorageObject;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
import com.google.gcloud.BaseService;
import com.google.gcloud.ExceptionHandler;
import com.google.gcloud.ExceptionHandler.Interceptor;
import com.google.gcloud.RetryParams;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.Tuple;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
final class StorageServiceImpl extends BaseService<StorageServiceOptions> implements StorageService {
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
private static final long serialVersionUID = -7758580330857881124L;
@Override
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
return null;
}
@Override
public RetryResult beforeEval(Exception exception) {
if (exception instanceof StorageServiceException) {
boolean retriable = ((StorageServiceException) exception).retryable();
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.ABORT;
}
return null;
}
};
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
private static final byte[] EMPTY_BYTE_ARRAY = {};
private final StorageRpc storageRpc;
private final RetryParams retryParams;
StorageServiceImpl(StorageServiceOptions options) {
super(options);
storageRpc = options.storageRpc();
retryParams = firstNonNull(options.retryParams(), RetryParams.noRetries());
// todo: replace nulls with Value.asNull (per toPb)
// todo: configure timeouts - https://developers.google.com/api-client-library/java/google-api-java-client/errors
// todo: provide rewrite - https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite
// todo: provide signed urls - https://cloud.google.com/storage/docs/access-control#Signed-URLs
// todo: check if we need to expose https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/insert vs using bucket update/patch
}
@Override
public Bucket create(Bucket bucket, BucketTargetOption... options) {
final com.google.api.services.storage.model.Bucket bucketPb = bucket.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(bucket, options);
return Bucket.fromPb(runWithRetries(
new Callable<com.google.api.services.storage.model.Bucket>() {
@Override
public com.google.api.services.storage.model.Bucket call() {
return storageRpc.create(bucketPb, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER));
}
@Override
public Blob create(Blob blob, final byte[] content, BlobTargetOption... options) {
final StorageObject blobPb = blob.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
return Blob.fromPb(runWithRetries(new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.create(blobPb, firstNonNull(content, EMPTY_BYTE_ARRAY), optionsMap);
}
}, retryParams, EXCEPTION_HANDLER));
}
@Override
public Bucket get(String bucket, BucketSourceOption... options) {
final com.google.api.services.storage.model.Bucket bucketPb = Bucket.of(bucket).toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
com.google.api.services.storage.model.Bucket answer = runWithRetries(
new Callable<com.google.api.services.storage.model.Bucket>() {
@Override
public com.google.api.services.storage.model.Bucket call() {
try {
return storageRpc.get(bucketPb, optionsMap);
} catch (StorageServiceException ex) {
if (ex.code() == 404) {
return null;
}
throw ex;
}
}
}, retryParams, EXCEPTION_HANDLER);
return answer == null ? null : Bucket.fromPb(answer);
}
@Override
public Blob get(String bucket, String blob, BlobSourceOption... options) {
final StorageObject storedObject = Blob.of(bucket, blob).toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
StorageObject storageObject = runWithRetries(new Callable<StorageObject>() {
@Override
public StorageObject call() {
try {
return storageRpc.get(storedObject, optionsMap);
} catch (StorageServiceException ex) {
if (ex.code() == 404) {
return null;
}
throw ex;
}
}
}, retryParams, EXCEPTION_HANDLER);
return storageObject == null ? null : Blob.fromPb(storageObject);
}
@Override
public ListResult<Bucket> list(BucketListOption... options) {
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>> result = runWithRetries(
new Callable<Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>>>() {
@Override
public Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>> call() {
return storageRpc.list(optionsMap);
}
}, retryParams, EXCEPTION_HANDLER);
return new ListResult<>(result.x(), Iterables.transform(result.y(),
new Function<com.google.api.services.storage.model.Bucket, Bucket>() {
@Override
public Bucket apply(com.google.api.services.storage.model.Bucket bucketPb) {
return Bucket.fromPb(bucketPb);
}
}));
}
@Override
public ListResult<Blob> list(final String bucket, BlobListOption... options) {
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
Tuple<String, Iterable<StorageObject>> result = runWithRetries(
new Callable<Tuple<String, Iterable<StorageObject>>>() {
@Override
public Tuple<String, Iterable<StorageObject>> call() {
return storageRpc.list(bucket, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER);
return new ListResult<>(result.x(), Iterables.transform(result.y(),
new Function<StorageObject, Blob>() {
@Override
public Blob apply(StorageObject storageObject) {
return Blob.fromPb(storageObject);
}
}));
}
@Override
public Bucket update(Bucket bucket, BucketTargetOption... options) {
final com.google.api.services.storage.model.Bucket bucketPb = bucket.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(bucket, options);
return Bucket.fromPb(runWithRetries(
new Callable<com.google.api.services.storage.model.Bucket>() {
@Override
public com.google.api.services.storage.model.Bucket call() {
return storageRpc.patch(bucketPb, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER));
}
@Override
public Blob update(Blob blob, BlobTargetOption... options) {
final StorageObject storageObject = blob.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
return Blob.fromPb(runWithRetries(new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.patch(storageObject, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER));
}
@Override
public boolean delete(String bucket, BucketSourceOption... options) {
final com.google.api.services.storage.model.Bucket bucketPb = Bucket.of(bucket).toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
return runWithRetries(new Callable<Boolean>() {
@Override
public Boolean call() {
return storageRpc.delete(bucketPb, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER);
}
@Override
public boolean delete(String bucket, String blob, BlobSourceOption... options) {
final StorageObject storageObject = Blob.of(bucket, blob).toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
return runWithRetries(new Callable<Boolean>() {
@Override
public Boolean call() {
return storageRpc.delete(storageObject, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER);
}
@Override
public Blob compose(final ComposeRequest composeRequest) {
final List<StorageObject> sources =
Lists.newArrayListWithCapacity(composeRequest.sourceBlobs().size());
for (ComposeRequest.SourceBlob sourceBlob : composeRequest.sourceBlobs()) {
sources.add(Blob.builder(composeRequest.target().bucket(), sourceBlob.name())
.generation(sourceBlob.generation()).build().toPb());
}
final StorageObject target = composeRequest.target().toPb();
final Map<StorageRpc.Option, ?> targetOptions = optionMap(composeRequest.target().generation(),
composeRequest.target().metageneration(), composeRequest.targetOptions());
return Blob.fromPb(runWithRetries(new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.compose(sources, target, targetOptions);
}
}, retryParams, EXCEPTION_HANDLER));
}
@Override
public Blob copy(CopyRequest copyRequest) {
final StorageObject source =
Blob.of(copyRequest.sourceBucket(), copyRequest.sourceBlob()).toPb();
copyRequest.sourceOptions();
final Map<StorageRpc.Option, ?> sourceOptions =
optionMap(null, null, copyRequest.sourceOptions(), true);
final StorageObject target = copyRequest.target().toPb();
final Map<StorageRpc.Option, ?> targetOptions = optionMap(copyRequest.target().generation(),
copyRequest.target().metageneration(), copyRequest.targetOptions());
return Blob.fromPb(runWithRetries(new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.copy(source, sourceOptions, target, targetOptions);
}
}, retryParams, EXCEPTION_HANDLER));
}
@Override
public byte[] load(String bucket, String blob, BlobSourceOption... options) {
final StorageObject storageObject = Blob.of(bucket, blob).toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
return runWithRetries(new Callable<byte[]>() {
@Override
public byte[] call() {
return storageRpc.load(storageObject, optionsMap);
}
}, retryParams, EXCEPTION_HANDLER);
}
@Override
public BatchResponse apply(BatchRequest batchRequest) {
List<Tuple<StorageObject, Map<StorageRpc.Option, ?>>> toDelete =
Lists.newArrayListWithCapacity(batchRequest.toDelete().size());
for (Map.Entry<Blob, BlobSourceOption[]> entry : batchRequest.toDelete().entrySet()) {
Blob blob = entry.getKey();
Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, entry.getValue());
StorageObject storageObject = blob.toPb();
toDelete.add(Tuple.<StorageObject, Map<StorageRpc.Option, ?>>of(storageObject, optionsMap));
}
List<Tuple<StorageObject, Map<StorageRpc.Option, ?>>> toUpdate =
Lists.newArrayListWithCapacity(batchRequest.toUpdate().size());
for (Map.Entry<Blob, BlobTargetOption[]> entry : batchRequest.toUpdate().entrySet()) {
Blob blob = entry.getKey();
Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, entry.getValue());
toUpdate.add(Tuple.<StorageObject, Map<StorageRpc.Option, ?>>of(blob.toPb(), optionsMap));
}
List<Tuple<StorageObject, Map<StorageRpc.Option, ?>>> toGet =
Lists.newArrayListWithCapacity(batchRequest.toGet().size());
for (Map.Entry<Blob, BlobSourceOption[]> entry : batchRequest.toGet().entrySet()) {
Blob blob = entry.getKey();
Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, entry.getValue());
toGet.add(Tuple.<StorageObject, Map<StorageRpc.Option, ?>>of(blob.toPb(), optionsMap));
}
StorageRpc.BatchResponse response =
storageRpc.batch(new StorageRpc.BatchRequest(toDelete, toUpdate, toGet));
List<BatchResponse.Result<Boolean>> deletes = transformBatchResult(
toDelete, response.deletes, Functions.<Boolean>identity());
List<BatchResponse.Result<Blob>> updates = transformBatchResult(
toUpdate, response.updates, Blob.FROM_PB_FUNCTION);
List<BatchResponse.Result<Blob>> gets = transformBatchResult(
toGet, response.gets, Blob.FROM_PB_FUNCTION, 404);
return new BatchResponse(deletes, updates, gets);
}
private <I, O extends Serializable> List<BatchResponse.Result<O>> transformBatchResult(
Iterable<Tuple<StorageObject, Map<StorageRpc.Option, ?>>> request,
Map<StorageObject, Tuple<I, StorageServiceException>> results, Function<I, O> transform,
int... nullOnErrorCodes) {
Set nullOnErrorCodesSet = Sets.newHashSet(Ints.asList(nullOnErrorCodes));
List<BatchResponse.Result<O>> response = Lists.newArrayListWithCapacity(results.size());
for (Tuple<StorageObject, ?> tuple : request) {
Tuple<I, StorageServiceException> result = results.get(tuple.x());
if (result.x() != null) {
response.add(new BatchResponse.Result<>(transform.apply(result.x())));
} else {
StorageServiceException exception = result.y();
if (nullOnErrorCodesSet.contains(exception.code())) {
//noinspection unchecked
response.add(BatchResponse.Result.<O>empty());
} else {
response.add(new BatchResponse.Result<O>(result.y()));
}
}
}
return response;
}
private static class BlobReadChannelImpl implements BlobReadChannel {
private final StorageServiceOptions serviceOptions;
private final Blob blob;
private final Map<StorageRpc.Option, ?> requestOptions;
private int position;
private boolean isOpen;
private boolean endOfStream;
private transient StorageRpc storageRpc;
private transient RetryParams retryParams;
private transient StorageObject storageObject;
private transient int bufferPos;
private transient byte[] buffer;
BlobReadChannelImpl(StorageServiceOptions serviceOptions, Blob blob,
Map<StorageRpc.Option, ?> requestOptions) {
this.serviceOptions = serviceOptions;
this.blob = blob;
this.requestOptions = requestOptions;
isOpen = true;
initTransients();
}
private void writeObject(ObjectOutputStream out) throws IOException {
if (buffer != null) {
position += bufferPos;
buffer = null;
bufferPos = 0;
endOfStream = false;
}
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
initTransients();
}
private void initTransients() {
storageRpc = serviceOptions.storageRpc();
retryParams = firstNonNull(serviceOptions.retryParams(), RetryParams.noRetries());
storageObject = blob.toPb();
}
@Override
public boolean isOpen() {
return isOpen;
}
@Override
public void close() {
if (isOpen) {
buffer = null;
isOpen = false;
}
}
private void validateOpen() throws IOException {
if (!isOpen) {
throw new IOException("stream is closed");
}
}
@Override
public void seek(int position) throws IOException {
validateOpen();
this.position = position;
buffer = null;
bufferPos = 0;
endOfStream = false;
}
@Override
public int read(ByteBuffer byteBuffer) throws IOException {
validateOpen();
if (buffer == null) {
if (endOfStream) {
return -1;
}
final int toRead = Math.max(byteBuffer.remaining(), 256 * 1024);
buffer = runWithRetries(new Callable<byte[]>() {
@Override
public byte[] call() {
return storageRpc.read(storageObject, requestOptions, position, toRead);
}
}, retryParams, EXCEPTION_HANDLER);
if (toRead > buffer.length) {
endOfStream = true;
}
}
int toWrite = Math.min(buffer.length - bufferPos, byteBuffer.remaining());
byteBuffer.put(buffer, bufferPos, toWrite);
bufferPos += toWrite;
if (bufferPos >= buffer.length) {
position += buffer.length;
buffer = null;
bufferPos = 0;
}
return toWrite;
}
}
@Override
public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... options) {
Map<StorageRpc.Option, ?> optionsMap = optionMap(options);
return new BlobReadChannelImpl(options(), Blob.of(bucket, blob), optionsMap);
}
private static class BlobWriterChannelImpl implements BlobWriteChannel {
private static final int CHUNK_SIZE = 256 * 1024;
private static final int COMPACT_THRESHOLD = (int) Math.round(CHUNK_SIZE * 0.8);
private final StorageServiceOptions options;
private final Blob blob;
private final String uploadId;
private int position;
private byte[] buffer = new byte[CHUNK_SIZE];
private int limit;
private boolean isOpen = true;
private transient StorageRpc storageRpc;
private transient RetryParams retryParams;
private transient StorageObject storageObject;
public BlobWriterChannelImpl(StorageServiceOptions options, Blob blob,
Map<StorageRpc.Option, ?> optionsMap) {
this.options = options;
this.blob = blob;
initTransients();
uploadId = options.storageRpc().open(storageObject, optionsMap);
}
private void writeObject(ObjectOutputStream out) throws IOException {
if (!isOpen) {
out.defaultWriteObject();
return;
}
flush();
byte[] temp = buffer;
if (limit < COMPACT_THRESHOLD) {
buffer = Arrays.copyOf(buffer, limit);
}
out.defaultWriteObject();
buffer = temp;
}
private void flush() {
if (limit >= CHUNK_SIZE) {
final int length = limit - limit % CHUNK_SIZE;
runWithRetries(callable(new Runnable() {
@Override
public void run() {
storageRpc.write(uploadId, buffer, 0, storageObject, position, length, false);
}
}));
position += length;
limit -= length;
byte[] temp = new byte[CHUNK_SIZE];
System.arraycopy(buffer, length, temp, 0, limit);
buffer = temp;
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (isOpen) {
if (buffer.length < CHUNK_SIZE) {
buffer = Arrays.copyOf(buffer, CHUNK_SIZE);
}
initTransients();
}
}
private void initTransients() {
storageRpc = options.storageRpc();
retryParams = firstNonNull(options.retryParams(), RetryParams.noRetries());
storageObject = blob.toPb();
}
private void validateOpen() throws IOException {
if (!isOpen) {
throw new IOException("stream is closed");
}
}
@Override
public int write(ByteBuffer byteBuffer) throws IOException {
validateOpen();
int toWrite = byteBuffer.remaining();
int spaceInBuffer = buffer.length - limit;
if (spaceInBuffer >= toWrite) {
byteBuffer.get(buffer, limit, toWrite);
} else {
buffer = Arrays.copyOf(buffer, buffer.length + toWrite - spaceInBuffer);
byteBuffer.get(buffer, limit, toWrite);
}
limit += toWrite;
flush();
return toWrite;
}
@Override
public boolean isOpen() {
return isOpen;
}
@Override
public void close() throws IOException {
if (isOpen) {
runWithRetries(callable(new Runnable() {
@Override
public void run() {
storageRpc.write(uploadId, buffer, 0, storageObject, position, limit, true);
}
}));
position += buffer.length;
isOpen = false;
buffer = null;
}
}
}
@Override
public BlobWriteChannel writer(Blob blob, BlobTargetOption... options) {
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
return new BlobWriterChannelImpl(options(), blob, optionsMap);
}
private Map<StorageRpc.Option, ?> optionMap(Long generation, Long metaGeneration,
Iterable<? extends Option> options) {
return optionMap(generation, metaGeneration, options, false);
}
private Map<StorageRpc.Option, ?> optionMap(Long generation, Long metaGeneration,
Iterable<? extends Option> options, boolean useAsSource) {
Map<StorageRpc.Option, Object> temp = Maps.newEnumMap(StorageRpc.Option.class);
for (Option option : options) {
Object prev = temp.put(option.rpcOption(), option.value());
checkArgument(prev == null, "Duplicate option %s", option);
}
Boolean value = (Boolean) temp.remove(DELIMITER);
if (Boolean.TRUE.equals(value)) {
temp.put(DELIMITER, options().pathDelimiter());
}
if (useAsSource) {
addToOptionMap(IF_GENERATION_MATCH, IF_SOURCE_GENERATION_MATCH, generation, temp);
addToOptionMap(IF_GENERATION_NOT_MATCH, IF_SOURCE_GENERATION_NOT_MATCH, generation, temp);
addToOptionMap(IF_METAGENERATION_MATCH, IF_SOURCE_METAGENERATION_MATCH, metaGeneration, temp);
addToOptionMap(IF_METAGENERATION_NOT_MATCH,
IF_SOURCE_METAGENERATION_NOT_MATCH, metaGeneration, temp);
} else {
addToOptionMap(IF_GENERATION_MATCH, generation, temp);
addToOptionMap(IF_GENERATION_NOT_MATCH, generation, temp);
addToOptionMap(IF_METAGENERATION_MATCH, metaGeneration, temp);
addToOptionMap(IF_METAGENERATION_NOT_MATCH, metaGeneration, temp);
}
return ImmutableMap.copyOf(temp);
}
private static <T> void addToOptionMap(StorageRpc.Option option, T defaultValue,
Map<StorageRpc.Option, Object> map) {
addToOptionMap(option, option, defaultValue, map);
}
private static <T> void addToOptionMap(StorageRpc.Option getOption, StorageRpc.Option putOption,
T defaultValue, Map<StorageRpc.Option, Object> map) {
if (map.containsKey(getOption)) {
@SuppressWarnings("unchecked")
T value = (T) map.remove(getOption);
checkArgument(value != null || defaultValue != null,
"Option " + getOption.value() + " is missing a value");
value = firstNonNull(value, defaultValue);
map.put(putOption, value);
}
}
private Map<StorageRpc.Option, ?> optionMap(Option... options) {
return optionMap(null, null, Arrays.asList(options));
}
private Map<StorageRpc.Option, ?> optionMap(Long generation, Long metaGeneration,
Option... options) {
return optionMap(generation, metaGeneration, Arrays.asList(options));
}
private Map<StorageRpc.Option, ?> optionMap(Bucket bucket, Option... options) {
return optionMap(null, bucket.metageneration(), options);
}
private Map<StorageRpc.Option, ?> optionMap(Blob blob, Option... options) {
return optionMap(blob.generation(), blob.metageneration(), options);
}
}