Skip to content

Commit fac7687

Browse files
committed
Merge pull request #312 from mziccard/add-support-for-selected-fields
Add support for selected fields to blob get and list
2 parents cb64ccd + af7a08b commit fac7687

File tree

12 files changed

+681
-59
lines changed

12 files changed

+681
-59
lines changed

gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.gcloud.spi;
1616

1717
import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER;
18+
import static com.google.gcloud.spi.StorageRpc.Option.FIELDS;
1819
import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH;
1920
import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH;
2021
import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_MATCH;
@@ -150,6 +151,7 @@ public Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) {
150151
.setPrefix(PREFIX.getString(options))
151152
.setMaxResults(MAX_RESULTS.getLong(options))
152153
.setPageToken(PAGE_TOKEN.getString(options))
154+
.setFields(FIELDS.getString(options))
153155
.execute();
154156
return Tuple.<String, Iterable<Bucket>>of(buckets.getNextPageToken(), buckets.getItems());
155157
} catch (IOException ex) {
@@ -168,6 +170,7 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
168170
.setPrefix(PREFIX.getString(options))
169171
.setMaxResults(MAX_RESULTS.getLong(options))
170172
.setPageToken(PAGE_TOKEN.getString(options))
173+
.setFields(FIELDS.getString(options))
171174
.execute();
172175
return Tuple.<String, Iterable<StorageObject>>of(
173176
objects.getNextPageToken(), objects.getItems());
@@ -184,6 +187,7 @@ public Bucket get(Bucket bucket, Map<Option, ?> options) {
184187
.setProjection(DEFAULT_PROJECTION)
185188
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
186189
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
190+
.setFields(FIELDS.getString(options))
187191
.execute();
188192
} catch (IOException ex) {
189193
throw translate(ex);
@@ -207,7 +211,8 @@ private Storage.Objects.Get getRequest(StorageObject object, Map<Option, ?> opti
207211
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
208212
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
209213
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
210-
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options));
214+
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options))
215+
.setFields(FIELDS.getString(options));
211216
}
212217

213218
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ enum Option {
4747
MAX_RESULTS("maxResults"),
4848
PAGE_TOKEN("pageToken"),
4949
DELIMITER("delimiter"),
50-
VERSIONS("versions");
50+
VERSIONS("versions"),
51+
FIELDS("fields");
5152

5253
private final String value;
5354

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BatchRequest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.common.collect.Lists;
21+
import com.google.gcloud.storage.Storage.BlobGetOption;
2122
import com.google.gcloud.storage.Storage.BlobSourceOption;
2223
import com.google.gcloud.storage.Storage.BlobTargetOption;
2324

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

3637
private final Map<BlobId, Iterable<BlobSourceOption>> toDelete;
3738
private final Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate;
38-
private final Map<BlobId, Iterable<BlobSourceOption>> toGet;
39+
private final Map<BlobId, Iterable<BlobGetOption>> toGet;
3940

4041
public static class Builder {
4142

4243
private Map<BlobId, Iterable<BlobSourceOption>> toDelete = new LinkedHashMap<>();
4344
private Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate = new LinkedHashMap<>();
44-
private Map<BlobId, Iterable<BlobSourceOption>> toGet = new LinkedHashMap<>();
45+
private Map<BlobId, Iterable<BlobGetOption>> toGet = new LinkedHashMap<>();
4546

4647
private Builder() {}
4748

@@ -72,15 +73,15 @@ public Builder update(BlobInfo blobInfo, BlobTargetOption... options) {
7273
/**
7374
* Retrieve metadata for the given blob.
7475
*/
75-
public Builder get(String bucket, String blob, BlobSourceOption... options) {
76+
public Builder get(String bucket, String blob, BlobGetOption... options) {
7677
toGet.put(BlobId.of(bucket, blob), Lists.newArrayList(options));
7778
return this;
7879
}
7980

8081
/**
8182
* Retrieve metadata for the given blob.
8283
*/
83-
public Builder get(BlobId blob, BlobSourceOption... options) {
84+
public Builder get(BlobId blob, BlobGetOption... options) {
8485
toGet.put(blob, Lists.newArrayList(options));
8586
return this;
8687
}
@@ -120,7 +121,7 @@ public Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate() {
120121
return toUpdate;
121122
}
122123

123-
public Map<BlobId, Iterable<BlobSourceOption>> toGet() {
124+
public Map<BlobId, Iterable<BlobGetOption>> toGet() {
124125
return toGet;
125126
}
126127

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
21-
import static com.google.gcloud.storage.Blob.BlobSourceOption.convert;
21+
import static com.google.gcloud.storage.Blob.BlobSourceOption.toSourceOptions;
22+
import static com.google.gcloud.storage.Blob.BlobSourceOption.toGetOptions;
2223

2324
import com.google.common.base.Function;
2425
import com.google.common.collect.Lists;
@@ -29,6 +30,7 @@
2930
import com.google.gcloud.storage.Storage.SignUrlOption;
3031

3132
import java.net.URL;
33+
import java.util.Arrays;
3234
import java.util.Collections;
3335
import java.util.List;
3436
import java.util.Objects;
@@ -56,7 +58,7 @@ private BlobSourceOption(StorageRpc.Option rpcOption) {
5658
super(rpcOption, null);
5759
}
5860

59-
private Storage.BlobSourceOption convert(BlobInfo blobInfo) {
61+
private Storage.BlobSourceOption toSourceOptions(BlobInfo blobInfo) {
6062
switch (rpcOption()) {
6163
case IF_GENERATION_MATCH:
6264
return Storage.BlobSourceOption.generationMatch(blobInfo.generation());
@@ -71,6 +73,21 @@ private Storage.BlobSourceOption convert(BlobInfo blobInfo) {
7173
}
7274
}
7375

76+
private Storage.BlobGetOption toGetOption(BlobInfo blobInfo) {
77+
switch (rpcOption()) {
78+
case IF_GENERATION_MATCH:
79+
return Storage.BlobGetOption.generationMatch(blobInfo.generation());
80+
case IF_GENERATION_NOT_MATCH:
81+
return Storage.BlobGetOption.generationNotMatch(blobInfo.generation());
82+
case IF_METAGENERATION_MATCH:
83+
return Storage.BlobGetOption.metagenerationMatch(blobInfo.metageneration());
84+
case IF_METAGENERATION_NOT_MATCH:
85+
return Storage.BlobGetOption.metagenerationNotMatch(blobInfo.metageneration());
86+
default:
87+
throw new AssertionError("Unexpected enum value");
88+
}
89+
}
90+
7491
public static BlobSourceOption generationMatch() {
7592
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH);
7693
}
@@ -87,11 +104,21 @@ public static BlobSourceOption metagenerationNotMatch() {
87104
return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
88105
}
89106

90-
static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption... options) {
107+
static Storage.BlobSourceOption[] toSourceOptions(BlobInfo blobInfo,
108+
BlobSourceOption... options) {
91109
Storage.BlobSourceOption[] convertedOptions = new Storage.BlobSourceOption[options.length];
92110
int index = 0;
93111
for (BlobSourceOption option : options) {
94-
convertedOptions[index++] = option.convert(blobInfo);
112+
convertedOptions[index++] = option.toSourceOptions(blobInfo);
113+
}
114+
return convertedOptions;
115+
}
116+
117+
static Storage.BlobGetOption[] toGetOptions(BlobInfo blobInfo, BlobSourceOption... options) {
118+
Storage.BlobGetOption[] convertedOptions = new Storage.BlobGetOption[options.length];
119+
int index = 0;
120+
for (BlobSourceOption option : options) {
121+
convertedOptions[index++] = option.toGetOption(blobInfo);
95122
}
96123
return convertedOptions;
97124
}
@@ -100,7 +127,7 @@ static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption...
100127
/**
101128
* Constructs a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used
102129
* to issue requests.
103-
*
130+
*
104131
* @param storage the storage service used for issuing requests
105132
* @param info blob's info
106133
*/
@@ -112,7 +139,7 @@ public Blob(Storage storage, BlobInfo info) {
112139
/**
113140
* Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
114141
* get the latest blob information.
115-
*
142+
*
116143
* @param storage the storage service used for issuing requests
117144
* @param bucket bucket's name
118145
* @param blob blob's name
@@ -126,7 +153,7 @@ public static Blob load(Storage storage, String bucket, String blob) {
126153
/**
127154
* Creates a {@code Blob} object for the provided {@code blobId}. Performs an RPC call to get the
128155
* latest blob information.
129-
*
156+
*
130157
* @param storage the storage service used for issuing requests
131158
* @param blobId blob's identifier
132159
* @return the {@code Blob} object or {@code null} if not found.
@@ -159,7 +186,10 @@ public BlobId id() {
159186
* @throws StorageException upon failure
160187
*/
161188
public boolean exists(BlobSourceOption... options) {
162-
return storage.get(info.blobId(), convert(info, options)) != null;
189+
int length = options.length;
190+
Storage.BlobGetOption[] getOptions = Arrays.copyOf(toGetOptions(info, options), length + 1);
191+
getOptions[length] = Storage.BlobGetOption.fields();
192+
return storage.get(info.blobId(), getOptions) != null;
163193
}
164194

165195
/**
@@ -180,7 +210,7 @@ public byte[] content(Storage.BlobSourceOption... options) {
180210
* @throws StorageException upon failure
181211
*/
182212
public Blob reload(BlobSourceOption... options) {
183-
return new Blob(storage, storage.get(info.blobId(), convert(info, options)));
213+
return new Blob(storage, storage.get(info.blobId(), toGetOptions(info, options)));
184214
}
185215

186216
/**
@@ -224,7 +254,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
224254
*/
225255
public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
226256
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
227-
.sourceOptions(convert(info, options)).target(targetBlob).build();
257+
.sourceOptions(toSourceOptions(info, options)).target(targetBlob).build();
228258
return storage.copy(copyRequest);
229259
}
230260

@@ -236,7 +266,7 @@ public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
236266
* @throws StorageException upon failure
237267
*/
238268
public boolean delete(BlobSourceOption... options) {
239-
return storage.delete(info.blobId(), convert(info, options));
269+
return storage.delete(info.blobId(), toSourceOptions(info, options));
240270
}
241271

242272
/**
@@ -275,7 +305,7 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
275305
* @throws StorageException upon failure
276306
*/
277307
public BlobReadChannel reader(BlobSourceOption... options) {
278-
return storage.reader(info.blobId(), convert(info, options));
308+
return storage.reader(info.blobId(), toSourceOptions(info, options));
279309
}
280310

281311
/**

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toGetOptions;
22+
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions;
2123

2224
import com.google.common.base.Function;
2325
import com.google.common.base.MoreObjects;
2426
import com.google.common.collect.Iterators;
2527
import com.google.gcloud.PageImpl;
2628
import com.google.gcloud.Page;
27-
import com.google.gcloud.storage.Storage.BlobSourceOption;
29+
import com.google.gcloud.spi.StorageRpc;
30+
import com.google.gcloud.storage.Storage.BlobGetOption;
2831
import com.google.gcloud.storage.Storage.BlobTargetOption;
2932
import com.google.gcloud.storage.Storage.BlobWriteOption;
30-
import com.google.gcloud.storage.Storage.BucketSourceOption;
3133
import com.google.gcloud.storage.Storage.BucketTargetOption;
3234

3335
import java.io.IOException;
3436
import java.io.InputStream;
3537
import java.io.ObjectInputStream;
3638
import java.io.Serializable;
3739
import java.util.ArrayList;
40+
import java.util.Arrays;
3841
import java.util.Collections;
3942
import java.util.Iterator;
4043
import java.util.List;
@@ -119,6 +122,66 @@ public boolean equals(Object obj) {
119122
}
120123
}
121124

125+
public static class BucketSourceOption extends Option {
126+
127+
private static final long serialVersionUID = 6928872234155522371L;
128+
129+
private BucketSourceOption(StorageRpc.Option rpcOption) {
130+
super(rpcOption, null);
131+
}
132+
133+
private Storage.BucketSourceOption toSourceOptions(BucketInfo bucketInfo) {
134+
switch (rpcOption()) {
135+
case IF_METAGENERATION_MATCH:
136+
return Storage.BucketSourceOption.metagenerationMatch(bucketInfo.metageneration());
137+
case IF_METAGENERATION_NOT_MATCH:
138+
return Storage.BucketSourceOption.metagenerationNotMatch(bucketInfo.metageneration());
139+
default:
140+
throw new AssertionError("Unexpected enum value");
141+
}
142+
}
143+
144+
private Storage.BucketGetOption toGetOption(BucketInfo bucketInfo) {
145+
switch (rpcOption()) {
146+
case IF_METAGENERATION_MATCH:
147+
return Storage.BucketGetOption.metagenerationMatch(bucketInfo.metageneration());
148+
case IF_METAGENERATION_NOT_MATCH:
149+
return Storage.BucketGetOption.metagenerationNotMatch(bucketInfo.metageneration());
150+
default:
151+
throw new AssertionError("Unexpected enum value");
152+
}
153+
}
154+
155+
public static BucketSourceOption metagenerationMatch() {
156+
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH);
157+
}
158+
159+
public static BucketSourceOption metagenerationNotMatch() {
160+
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
161+
}
162+
163+
static Storage.BucketSourceOption[] toSourceOptions(BucketInfo bucketInfo,
164+
BucketSourceOption... options) {
165+
Storage.BucketSourceOption[] convertedOptions =
166+
new Storage.BucketSourceOption[options.length];
167+
int index = 0;
168+
for (BucketSourceOption option : options) {
169+
convertedOptions[index++] = option.toSourceOptions(bucketInfo);
170+
}
171+
return convertedOptions;
172+
}
173+
174+
static Storage.BucketGetOption[] toGetOptions(BucketInfo bucketInfo,
175+
BucketSourceOption... options) {
176+
Storage.BucketGetOption[] convertedOptions = new Storage.BucketGetOption[options.length];
177+
int index = 0;
178+
for (BucketSourceOption option : options) {
179+
convertedOptions[index++] = option.toGetOption(bucketInfo);
180+
}
181+
return convertedOptions;
182+
}
183+
}
184+
122185
/**
123186
* Constructs a {@code Bucket} object for the provided {@code BucketInfo}. The storage service is
124187
* used to issue requests.
@@ -158,8 +221,11 @@ public BucketInfo info() {
158221
* @return true if this bucket exists, false otherwise
159222
* @throws StorageException upon failure
160223
*/
161-
public boolean exists() {
162-
return storage.get(info.name()) != null;
224+
public boolean exists(BucketSourceOption... options) {
225+
int length = options.length;
226+
Storage.BucketGetOption[] getOptions = Arrays.copyOf(toGetOptions(info, options), length + 1);
227+
getOptions[length] = Storage.BucketGetOption.fields();
228+
return storage.get(info.name(), getOptions) != null;
163229
}
164230

165231
/**
@@ -170,7 +236,7 @@ public boolean exists() {
170236
* @throws StorageException upon failure
171237
*/
172238
public Bucket reload(BucketSourceOption... options) {
173-
return new Bucket(storage, storage.get(info.name(), options));
239+
return new Bucket(storage, storage.get(info.name(), toGetOptions(info, options)));
174240
}
175241

176242
/**
@@ -198,7 +264,7 @@ public Bucket update(BucketInfo bucketInfo, BucketTargetOption... options) {
198264
* @throws StorageException upon failure
199265
*/
200266
public boolean delete(BucketSourceOption... options) {
201-
return storage.delete(info.name(), options);
267+
return storage.delete(info.name(), toSourceOptions(info, options));
202268
}
203269

204270
/**
@@ -221,7 +287,7 @@ public Page<Blob> list(Storage.BlobListOption... options) {
221287
* @param options blob search options
222288
* @throws StorageException upon failure
223289
*/
224-
public Blob get(String blob, BlobSourceOption... options) {
290+
public Blob get(String blob, BlobGetOption... options) {
225291
return new Blob(storage, storage.get(BlobId.of(info.name(), blob), options));
226292
}
227293

0 commit comments

Comments
 (0)