Skip to content

Commit bea0157

Browse files
committed
Add support for selected fields to bucket get and list
- Remove AclField and make BlobField an enum type - Add BucketField enum to Storage - Add BucketGetOption class and use if for storage.get(bucket) - Add BucketSourceOption class to Bucket - Updated and add tests
1 parent deecc14 commit bea0157

6 files changed

Lines changed: 282 additions & 191 deletions

File tree

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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;
2222
import static com.google.gcloud.storage.Blob.BlobSourceOption.toGetOptions;
2323

2424
import com.google.common.base.Function;
@@ -58,7 +58,7 @@ private BlobSourceOption(StorageRpc.Option rpcOption) {
5858
super(rpcOption, null);
5959
}
6060

61-
private Storage.BlobSourceOption convert(BlobInfo blobInfo) {
61+
private Storage.BlobSourceOption toSourceOptions(BlobInfo blobInfo) {
6262
switch (rpcOption()) {
6363
case IF_GENERATION_MATCH:
6464
return Storage.BlobSourceOption.generationMatch(blobInfo.generation());
@@ -104,11 +104,12 @@ public static BlobSourceOption metagenerationNotMatch() {
104104
return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
105105
}
106106

107-
static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption... options) {
107+
static Storage.BlobSourceOption[] toSourceOptions(BlobInfo blobInfo,
108+
BlobSourceOption... options) {
108109
Storage.BlobSourceOption[] convertedOptions = new Storage.BlobSourceOption[options.length];
109110
int index = 0;
110111
for (BlobSourceOption option : options) {
111-
convertedOptions[index++] = option.convert(blobInfo);
112+
convertedOptions[index++] = option.toSourceOptions(blobInfo);
112113
}
113114
return convertedOptions;
114115
}
@@ -126,7 +127,7 @@ static Storage.BlobGetOption[] toGetOptions(BlobInfo blobInfo, BlobSourceOption.
126127
/**
127128
* Constructs a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used
128129
* to issue requests.
129-
*
130+
*
130131
* @param storage the storage service used for issuing requests
131132
* @param info blob's info
132133
*/
@@ -138,7 +139,7 @@ public Blob(Storage storage, BlobInfo info) {
138139
/**
139140
* Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
140141
* get the latest blob information.
141-
*
142+
*
142143
* @param storage the storage service used for issuing requests
143144
* @param bucket bucket's name
144145
* @param blob blob's name
@@ -152,7 +153,7 @@ public static Blob load(Storage storage, String bucket, String blob) {
152153
/**
153154
* Creates a {@code Blob} object for the provided {@code blobId}. Performs an RPC call to get the
154155
* latest blob information.
155-
*
156+
*
156157
* @param storage the storage service used for issuing requests
157158
* @param blobId blob's identifier
158159
* @return the {@code Blob} object or {@code null} if not found.
@@ -253,7 +254,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
253254
*/
254255
public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
255256
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
256-
.sourceOptions(convert(info, options)).target(targetBlob).build();
257+
.sourceOptions(toSourceOptions(info, options)).target(targetBlob).build();
257258
return storage.copy(copyRequest);
258259
}
259260

@@ -265,7 +266,7 @@ public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
265266
* @throws StorageException upon failure
266267
*/
267268
public boolean delete(BlobSourceOption... options) {
268-
return storage.delete(info.blobId(), convert(info, options));
269+
return storage.delete(info.blobId(), toSourceOptions(info, options));
269270
}
270271

271272
/**
@@ -304,7 +305,7 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
304305
* @throws StorageException upon failure
305306
*/
306307
public BlobReadChannel reader(BlobSourceOption... options) {
307-
return storage.reader(info.blobId(), convert(info, options));
308+
return storage.reader(info.blobId(), toSourceOptions(info, options));
308309
}
309310

310311
/**

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import com.google.common.collect.Iterators;
2525
import com.google.gcloud.PageImpl;
2626
import com.google.gcloud.Page;
27+
import com.google.gcloud.spi.StorageRpc;
2728
import com.google.gcloud.storage.Storage.BlobGetOption;
2829
import com.google.gcloud.storage.Storage.BlobTargetOption;
2930
import com.google.gcloud.storage.Storage.BlobWriteOption;
30-
import com.google.gcloud.storage.Storage.BucketSourceOption;
3131
import com.google.gcloud.storage.Storage.BucketTargetOption;
3232

3333
import java.io.IOException;
@@ -119,6 +119,66 @@ public boolean equals(Object obj) {
119119
}
120120
}
121121

122+
public static class BucketSourceOption extends Option {
123+
124+
private static final long serialVersionUID = 6928872234155522371L;
125+
126+
private BucketSourceOption(StorageRpc.Option rpcOption) {
127+
super(rpcOption, null);
128+
}
129+
130+
private Storage.BucketSourceOption toSourceOptions(BucketInfo bucketInfo) {
131+
switch (rpcOption()) {
132+
case IF_METAGENERATION_MATCH:
133+
return Storage.BucketSourceOption.metagenerationMatch(bucketInfo.metageneration());
134+
case IF_METAGENERATION_NOT_MATCH:
135+
return Storage.BucketSourceOption.metagenerationNotMatch(bucketInfo.metageneration());
136+
default:
137+
throw new AssertionError("Unexpected enum value");
138+
}
139+
}
140+
141+
private Storage.BucketGetOption toGetOption(BucketInfo bucketInfo) {
142+
switch (rpcOption()) {
143+
case IF_METAGENERATION_MATCH:
144+
return Storage.BucketGetOption.metagenerationMatch(bucketInfo.metageneration());
145+
case IF_METAGENERATION_NOT_MATCH:
146+
return Storage.BucketGetOption.metagenerationNotMatch(bucketInfo.metageneration());
147+
default:
148+
throw new AssertionError("Unexpected enum value");
149+
}
150+
}
151+
152+
public static BucketSourceOption metagenerationMatch() {
153+
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH);
154+
}
155+
156+
public static BucketSourceOption metagenerationNotMatch() {
157+
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
158+
}
159+
160+
static Storage.BucketSourceOption[] toSourceOptions(BucketInfo bucketInfo,
161+
BucketSourceOption... options) {
162+
Storage.BucketSourceOption[] convertedOptions =
163+
new Storage.BucketSourceOption[options.length];
164+
int index = 0;
165+
for (BucketSourceOption option : options) {
166+
convertedOptions[index++] = option.toSourceOptions(bucketInfo);
167+
}
168+
return convertedOptions;
169+
}
170+
171+
static Storage.BucketGetOption[] toGetOptions(BucketInfo bucketInfo,
172+
BucketSourceOption... options) {
173+
Storage.BucketGetOption[] convertedOptions = new Storage.BucketGetOption[options.length];
174+
int index = 0;
175+
for (BucketSourceOption option : options) {
176+
convertedOptions[index++] = option.toGetOption(bucketInfo);
177+
}
178+
return convertedOptions;
179+
}
180+
}
181+
122182
/**
123183
* Constructs a {@code Bucket} object for the provided {@code BucketInfo}. The storage service is
124184
* used to issue requests.
@@ -170,7 +230,8 @@ public boolean exists() {
170230
* @throws StorageException upon failure
171231
*/
172232
public Bucket reload(BucketSourceOption... options) {
173-
return new Bucket(storage, storage.get(info.name(), options));
233+
return new Bucket(storage, storage.get(info.name(),
234+
BucketSourceOption.toGetOptions(info, options)));
174235
}
175236

176237
/**
@@ -198,7 +259,7 @@ public Bucket update(BucketInfo bucketInfo, BucketTargetOption... options) {
198259
* @throws StorageException upon failure
199260
*/
200261
public boolean delete(BucketSourceOption... options) {
201-
return storage.delete(info.name(), options);
262+
return storage.delete(info.name(), BucketSourceOption.toSourceOptions(info, options));
202263
}
203264

204265
/**

0 commit comments

Comments
 (0)