Skip to content

Commit 9761b3c

Browse files
committed
make batch gets return null as well on 404
1 parent 535b0a1 commit 9761b3c

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/main/java/com/google/gcloud/storage/BatchResponse.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public class BatchResponse implements Serializable {
3636
public static class Result<T extends Serializable> implements Serializable {
3737

3838
private static final long serialVersionUID = -1946539570170529094L;
39+
private static final Result EMPTY = new BatchResponse.Result(null);
3940

4041
private final T value;
4142
private final StorageServiceException exception;
4243

44+
4345
Result(T value) {
4446
this.value = value;
4547
this.exception = null;
@@ -50,13 +52,15 @@ public static class Result<T extends Serializable> implements Serializable {
5052
this.value = null;
5153
}
5254

53-
5455
/**
5556
* Returns the result.
5657
*
5758
* @throws StorageServiceException if failed
5859
*/
5960
public T result() throws StorageServiceException {
61+
if (failed()) {
62+
throw failure();
63+
}
6064
return value;
6165
}
6266

@@ -76,7 +80,15 @@ public boolean failed() {
7680

7781
@Override
7882
public String toString() {
79-
return MoreObjects.firstNonNull(value, exception).toString();
83+
return MoreObjects.toStringHelper(this)
84+
.add("value", value)
85+
.add("exception", exception)
86+
.toString();
87+
}
88+
89+
@SuppressWarnings("unchecked")
90+
static <T extends Serializable> Result<T> empty() {
91+
return EMPTY;
8092
}
8193
}
8294

src/main/java/com/google/gcloud/storage/StorageServiceImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import com.google.common.collect.Iterables;
3838
import com.google.common.collect.Lists;
3939
import com.google.common.collect.Maps;
40+
import com.google.common.collect.Sets;
41+
import com.google.common.primitives.Ints;
4042
import com.google.gcloud.BaseService;
4143
import com.google.gcloud.ExceptionHandler;
4244
import com.google.gcloud.ExceptionHandler.Interceptor;
@@ -52,6 +54,7 @@
5254
import java.util.Arrays;
5355
import java.util.List;
5456
import java.util.Map;
57+
import java.util.Set;
5558
import java.util.concurrent.Callable;
5659

5760
final class StorageServiceImpl extends BaseService<StorageServiceOptions> implements StorageService {
@@ -325,20 +328,28 @@ public BatchResponse apply(BatchRequest batchRequest) {
325328
List<BatchResponse.Result<Blob>> updates = transformBatchResult(
326329
toUpdate, response.updates, Blob.FROM_PB_FUNCTION);
327330
List<BatchResponse.Result<Blob>> gets = transformBatchResult(
328-
toGet, response.gets, Blob.FROM_PB_FUNCTION);
331+
toGet, response.gets, Blob.FROM_PB_FUNCTION, 404);
329332
return new BatchResponse(deletes, updates, gets);
330333
}
331334

332335
private <I, O extends Serializable> List<BatchResponse.Result<O>> transformBatchResult(
333336
Iterable<Tuple<StorageObject, Map<StorageRpc.Option, ?>>> request,
334-
Map<StorageObject, Tuple<I, StorageServiceException>> results, Function<I, O> transform) {
337+
Map<StorageObject, Tuple<I, StorageServiceException>> results, Function<I, O> transform,
338+
int... nullOnErrorCodes) {
339+
Set nullOnErrorCodesSet = Sets.newHashSet(Ints.asList(nullOnErrorCodes));
335340
List<BatchResponse.Result<O>> response = Lists.newArrayListWithCapacity(results.size());
336341
for (Tuple<StorageObject, ?> tuple : request) {
337342
Tuple<I, StorageServiceException> result = results.get(tuple.x());
338343
if (result.x() != null) {
339344
response.add(new BatchResponse.Result<>(transform.apply(result.x())));
340345
} else {
341-
response.add(new BatchResponse.Result<O>(result.y()));
346+
StorageServiceException exception = result.y();
347+
if (nullOnErrorCodesSet.contains(exception.code())) {
348+
//noinspection unchecked
349+
response.add(BatchResponse.Result.<O>empty());
350+
} else {
351+
response.add(new BatchResponse.Result<O>(result.y()));
352+
}
342353
}
343354
}
344355
return response;

0 commit comments

Comments
 (0)