Skip to content

Commit

Permalink
Merge pull request #312 from mziccard/add-support-for-selected-fields
Browse files Browse the repository at this point in the history
Add support for selected fields to blob get and list
  • Loading branch information
aozarov committed Nov 5, 2015
2 parents cb64ccd + af7a08b commit fac7687
Show file tree
Hide file tree
Showing 12 changed files with 681 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.gcloud.spi;

import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER;
import static com.google.gcloud.spi.StorageRpc.Option.FIELDS;
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;
Expand Down Expand Up @@ -150,6 +151,7 @@ public Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) {
.setPrefix(PREFIX.getString(options))
.setMaxResults(MAX_RESULTS.getLong(options))
.setPageToken(PAGE_TOKEN.getString(options))
.setFields(FIELDS.getString(options))
.execute();
return Tuple.<String, Iterable<Bucket>>of(buckets.getNextPageToken(), buckets.getItems());
} catch (IOException ex) {
Expand All @@ -168,6 +170,7 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
.setPrefix(PREFIX.getString(options))
.setMaxResults(MAX_RESULTS.getLong(options))
.setPageToken(PAGE_TOKEN.getString(options))
.setFields(FIELDS.getString(options))
.execute();
return Tuple.<String, Iterable<StorageObject>>of(
objects.getNextPageToken(), objects.getItems());
Expand All @@ -184,6 +187,7 @@ public Bucket get(Bucket bucket, Map<Option, ?> options) {
.setProjection(DEFAULT_PROJECTION)
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
.setFields(FIELDS.getString(options))
.execute();
} catch (IOException ex) {
throw translate(ex);
Expand All @@ -207,7 +211,8 @@ private Storage.Objects.Get getRequest(StorageObject object, Map<Option, ?> opti
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options));
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options))
.setFields(FIELDS.getString(options));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ enum Option {
MAX_RESULTS("maxResults"),
PAGE_TOKEN("pageToken"),
DELIMITER("delimiter"),
VERSIONS("versions");
VERSIONS("versions"),
FIELDS("fields");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gcloud.storage.Storage.BlobGetOption;
import com.google.gcloud.storage.Storage.BlobSourceOption;
import com.google.gcloud.storage.Storage.BlobTargetOption;

Expand All @@ -35,13 +36,13 @@ public final class BatchRequest implements Serializable {

private final Map<BlobId, Iterable<BlobSourceOption>> toDelete;
private final Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate;
private final Map<BlobId, Iterable<BlobSourceOption>> toGet;
private final Map<BlobId, Iterable<BlobGetOption>> toGet;

public static class Builder {

private Map<BlobId, Iterable<BlobSourceOption>> toDelete = new LinkedHashMap<>();
private Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate = new LinkedHashMap<>();
private Map<BlobId, Iterable<BlobSourceOption>> toGet = new LinkedHashMap<>();
private Map<BlobId, Iterable<BlobGetOption>> toGet = new LinkedHashMap<>();

private Builder() {}

Expand Down Expand Up @@ -72,15 +73,15 @@ public Builder update(BlobInfo blobInfo, BlobTargetOption... options) {
/**
* Retrieve metadata for the given blob.
*/
public Builder get(String bucket, String blob, BlobSourceOption... options) {
public Builder get(String bucket, String blob, BlobGetOption... options) {
toGet.put(BlobId.of(bucket, blob), Lists.newArrayList(options));
return this;
}

/**
* Retrieve metadata for the given blob.
*/
public Builder get(BlobId blob, BlobSourceOption... options) {
public Builder get(BlobId blob, BlobGetOption... options) {
toGet.put(blob, Lists.newArrayList(options));
return this;
}
Expand Down Expand Up @@ -120,7 +121,7 @@ public Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate() {
return toUpdate;
}

public Map<BlobId, Iterable<BlobSourceOption>> toGet() {
public Map<BlobId, Iterable<BlobGetOption>> toGet() {
return toGet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gcloud.storage.Blob.BlobSourceOption.convert;
import static com.google.gcloud.storage.Blob.BlobSourceOption.toSourceOptions;
import static com.google.gcloud.storage.Blob.BlobSourceOption.toGetOptions;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
Expand All @@ -29,6 +30,7 @@
import com.google.gcloud.storage.Storage.SignUrlOption;

import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -56,7 +58,7 @@ private BlobSourceOption(StorageRpc.Option rpcOption) {
super(rpcOption, null);
}

private Storage.BlobSourceOption convert(BlobInfo blobInfo) {
private Storage.BlobSourceOption toSourceOptions(BlobInfo blobInfo) {
switch (rpcOption()) {
case IF_GENERATION_MATCH:
return Storage.BlobSourceOption.generationMatch(blobInfo.generation());
Expand All @@ -71,6 +73,21 @@ private Storage.BlobSourceOption convert(BlobInfo blobInfo) {
}
}

private Storage.BlobGetOption toGetOption(BlobInfo blobInfo) {
switch (rpcOption()) {
case IF_GENERATION_MATCH:
return Storage.BlobGetOption.generationMatch(blobInfo.generation());
case IF_GENERATION_NOT_MATCH:
return Storage.BlobGetOption.generationNotMatch(blobInfo.generation());
case IF_METAGENERATION_MATCH:
return Storage.BlobGetOption.metagenerationMatch(blobInfo.metageneration());
case IF_METAGENERATION_NOT_MATCH:
return Storage.BlobGetOption.metagenerationNotMatch(blobInfo.metageneration());
default:
throw new AssertionError("Unexpected enum value");
}
}

public static BlobSourceOption generationMatch() {
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH);
}
Expand All @@ -87,11 +104,21 @@ public static BlobSourceOption metagenerationNotMatch() {
return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
}

static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption... options) {
static Storage.BlobSourceOption[] toSourceOptions(BlobInfo blobInfo,
BlobSourceOption... options) {
Storage.BlobSourceOption[] convertedOptions = new Storage.BlobSourceOption[options.length];
int index = 0;
for (BlobSourceOption option : options) {
convertedOptions[index++] = option.convert(blobInfo);
convertedOptions[index++] = option.toSourceOptions(blobInfo);
}
return convertedOptions;
}

static Storage.BlobGetOption[] toGetOptions(BlobInfo blobInfo, BlobSourceOption... options) {
Storage.BlobGetOption[] convertedOptions = new Storage.BlobGetOption[options.length];
int index = 0;
for (BlobSourceOption option : options) {
convertedOptions[index++] = option.toGetOption(blobInfo);
}
return convertedOptions;
}
Expand All @@ -100,7 +127,7 @@ static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption...
/**
* Constructs a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used
* to issue requests.
*
*
* @param storage the storage service used for issuing requests
* @param info blob's info
*/
Expand All @@ -112,7 +139,7 @@ public Blob(Storage storage, BlobInfo info) {
/**
* Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
* get the latest blob information.
*
*
* @param storage the storage service used for issuing requests
* @param bucket bucket's name
* @param blob blob's name
Expand All @@ -126,7 +153,7 @@ public static Blob load(Storage storage, String bucket, String blob) {
/**
* Creates a {@code Blob} object for the provided {@code blobId}. Performs an RPC call to get the
* latest blob information.
*
*
* @param storage the storage service used for issuing requests
* @param blobId blob's identifier
* @return the {@code Blob} object or {@code null} if not found.
Expand Down Expand Up @@ -159,7 +186,10 @@ public BlobId id() {
* @throws StorageException upon failure
*/
public boolean exists(BlobSourceOption... options) {
return storage.get(info.blobId(), convert(info, options)) != null;
int length = options.length;
Storage.BlobGetOption[] getOptions = Arrays.copyOf(toGetOptions(info, options), length + 1);
getOptions[length] = Storage.BlobGetOption.fields();
return storage.get(info.blobId(), getOptions) != null;
}

/**
Expand All @@ -180,7 +210,7 @@ public byte[] content(Storage.BlobSourceOption... options) {
* @throws StorageException upon failure
*/
public Blob reload(BlobSourceOption... options) {
return new Blob(storage, storage.get(info.blobId(), convert(info, options)));
return new Blob(storage, storage.get(info.blobId(), toGetOptions(info, options)));
}

/**
Expand Down Expand Up @@ -224,7 +254,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
*/
public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
.sourceOptions(convert(info, options)).target(targetBlob).build();
.sourceOptions(toSourceOptions(info, options)).target(targetBlob).build();
return storage.copy(copyRequest);
}

Expand All @@ -236,7 +266,7 @@ public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
* @throws StorageException upon failure
*/
public boolean delete(BlobSourceOption... options) {
return storage.delete(info.blobId(), convert(info, options));
return storage.delete(info.blobId(), toSourceOptions(info, options));
}

/**
Expand Down Expand Up @@ -275,7 +305,7 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
* @throws StorageException upon failure
*/
public BlobReadChannel reader(BlobSourceOption... options) {
return storage.reader(info.blobId(), convert(info, options));
return storage.reader(info.blobId(), toSourceOptions(info, options));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toGetOptions;
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions;

import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Iterators;
import com.google.gcloud.PageImpl;
import com.google.gcloud.Page;
import com.google.gcloud.storage.Storage.BlobSourceOption;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.storage.Storage.BlobGetOption;
import com.google.gcloud.storage.Storage.BlobTargetOption;
import com.google.gcloud.storage.Storage.BlobWriteOption;
import com.google.gcloud.storage.Storage.BucketSourceOption;
import com.google.gcloud.storage.Storage.BucketTargetOption;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -119,6 +122,66 @@ public boolean equals(Object obj) {
}
}

public static class BucketSourceOption extends Option {

private static final long serialVersionUID = 6928872234155522371L;

private BucketSourceOption(StorageRpc.Option rpcOption) {
super(rpcOption, null);
}

private Storage.BucketSourceOption toSourceOptions(BucketInfo bucketInfo) {
switch (rpcOption()) {
case IF_METAGENERATION_MATCH:
return Storage.BucketSourceOption.metagenerationMatch(bucketInfo.metageneration());
case IF_METAGENERATION_NOT_MATCH:
return Storage.BucketSourceOption.metagenerationNotMatch(bucketInfo.metageneration());
default:
throw new AssertionError("Unexpected enum value");
}
}

private Storage.BucketGetOption toGetOption(BucketInfo bucketInfo) {
switch (rpcOption()) {
case IF_METAGENERATION_MATCH:
return Storage.BucketGetOption.metagenerationMatch(bucketInfo.metageneration());
case IF_METAGENERATION_NOT_MATCH:
return Storage.BucketGetOption.metagenerationNotMatch(bucketInfo.metageneration());
default:
throw new AssertionError("Unexpected enum value");
}
}

public static BucketSourceOption metagenerationMatch() {
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH);
}

public static BucketSourceOption metagenerationNotMatch() {
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
}

static Storage.BucketSourceOption[] toSourceOptions(BucketInfo bucketInfo,
BucketSourceOption... options) {
Storage.BucketSourceOption[] convertedOptions =
new Storage.BucketSourceOption[options.length];
int index = 0;
for (BucketSourceOption option : options) {
convertedOptions[index++] = option.toSourceOptions(bucketInfo);
}
return convertedOptions;
}

static Storage.BucketGetOption[] toGetOptions(BucketInfo bucketInfo,
BucketSourceOption... options) {
Storage.BucketGetOption[] convertedOptions = new Storage.BucketGetOption[options.length];
int index = 0;
for (BucketSourceOption option : options) {
convertedOptions[index++] = option.toGetOption(bucketInfo);
}
return convertedOptions;
}
}

/**
* Constructs a {@code Bucket} object for the provided {@code BucketInfo}. The storage service is
* used to issue requests.
Expand Down Expand Up @@ -158,8 +221,11 @@ public BucketInfo info() {
* @return true if this bucket exists, false otherwise
* @throws StorageException upon failure
*/
public boolean exists() {
return storage.get(info.name()) != null;
public boolean exists(BucketSourceOption... options) {
int length = options.length;
Storage.BucketGetOption[] getOptions = Arrays.copyOf(toGetOptions(info, options), length + 1);
getOptions[length] = Storage.BucketGetOption.fields();
return storage.get(info.name(), getOptions) != null;
}

/**
Expand All @@ -170,7 +236,7 @@ public boolean exists() {
* @throws StorageException upon failure
*/
public Bucket reload(BucketSourceOption... options) {
return new Bucket(storage, storage.get(info.name(), options));
return new Bucket(storage, storage.get(info.name(), toGetOptions(info, options)));
}

/**
Expand Down Expand Up @@ -198,7 +264,7 @@ public Bucket update(BucketInfo bucketInfo, BucketTargetOption... options) {
* @throws StorageException upon failure
*/
public boolean delete(BucketSourceOption... options) {
return storage.delete(info.name(), options);
return storage.delete(info.name(), toSourceOptions(info, options));
}

/**
Expand All @@ -221,7 +287,7 @@ public Page<Blob> list(Storage.BlobListOption... options) {
* @param options blob search options
* @throws StorageException upon failure
*/
public Blob get(String blob, BlobSourceOption... options) {
public Blob get(String blob, BlobGetOption... options) {
return new Blob(storage, storage.get(BlobId.of(info.name(), blob), options));
}

Expand Down
Loading

0 comments on commit fac7687

Please sign in to comment.