Skip to content

Commit

Permalink
make batch gets return null as well on 404
Browse files Browse the repository at this point in the history
  • Loading branch information
aozarov committed May 12, 2015
1 parent 535b0a1 commit 9761b3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/google/gcloud/storage/BatchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public class BatchResponse implements Serializable {
public static class Result<T extends Serializable> implements Serializable {

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

private final T value;
private final StorageServiceException exception;


Result(T value) {
this.value = value;
this.exception = null;
Expand All @@ -50,13 +52,15 @@ public static class Result<T extends Serializable> implements Serializable {
this.value = null;
}


/**
* Returns the result.
*
* @throws StorageServiceException if failed
*/
public T result() throws StorageServiceException {
if (failed()) {
throw failure();
}
return value;
}

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

@Override
public String toString() {
return MoreObjects.firstNonNull(value, exception).toString();
return MoreObjects.toStringHelper(this)
.add("value", value)
.add("exception", exception)
.toString();
}

@SuppressWarnings("unchecked")
static <T extends Serializable> Result<T> empty() {
return EMPTY;
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/google/gcloud/storage/StorageServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
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;
Expand All @@ -52,6 +54,7 @@
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 {
Expand Down Expand Up @@ -325,20 +328,28 @@ public BatchResponse apply(BatchRequest batchRequest) {
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);
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) {
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 {
response.add(new BatchResponse.Result<O>(result.y()));
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;
Expand Down

0 comments on commit 9761b3c

Please sign in to comment.