Skip to content

Commit 146d7c3

Browse files
committed
---
yaml --- r: 199 b: refs/heads/master c: 2fc739f h: refs/heads/master i: 197: 952c7f8 195: 5cd4b90 191: 86846de v: v3
1 parent d4a6635 commit 146d7c3

8 files changed

Lines changed: 123 additions & 19 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 83871026a7d17d89cf63b75803d60677ec1c8675
2+
refs/heads/master: 2fc739f589a0107b5488c643af5466b4e219efee

trunk/src/main/java/com/google/gcloud/examples/StorageExample.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.gcloud.examples;
1818

1919
import com.google.gcloud.spi.StorageRpc.Tuple;
20+
import com.google.gcloud.storage.BatchRequest;
2021
import com.google.gcloud.storage.Blob;
2122
import com.google.gcloud.storage.Bucket;
2223
import com.google.gcloud.storage.StorageService;
@@ -45,7 +46,7 @@
4546
* <li>run using maven -
4647
* {@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample"
4748
* -Dexec.args="project_id list [<bucket>]| info [<bucket> [<file>]]| get <bucket> <path>|
48-
* upload <local_file> <bucket> [<path>]| delete <bucket> <path>|
49+
* upload <local_file> <bucket> [<path>]| delete <bucket> <path>+|
4950
* cp <from_bucket> <from_path> <to_bucket> <to_path>| compose <bucket> <from_path>+ <to_path>"}
5051
* </li>
5152
* </ol>
@@ -81,6 +82,26 @@ public String params() {
8182
}
8283
}
8384

85+
private static abstract class BlobsAction extends StorageAction<Blob[]> {
86+
87+
@Override
88+
Blob[] parse(String... args) {
89+
if (args.length < 2) {
90+
throw new IllegalArgumentException();
91+
}
92+
Blob[] blobs = new Blob[args.length - 1];
93+
for (int i = 1; i < args.length; i++) {
94+
blobs[i - 1] = Blob.of(args[0], args[i]);
95+
}
96+
return blobs;
97+
}
98+
99+
@Override
100+
public String params() {
101+
return "<bucket> <path>+";
102+
}
103+
}
104+
84105
private static class InfoAction extends BlobAction {
85106
@Override
86107
public void run(StorageService storage, Blob blob) {
@@ -105,10 +126,20 @@ public String params() {
105126
}
106127
}
107128

108-
private static class DeleteAction extends BlobAction {
129+
private static class DeleteAction extends BlobsAction {
109130
@Override
110-
public void run(StorageService storage, Blob blob) {
111-
storage.delete(blob);
131+
public void run(StorageService storage, Blob... blobs) {
132+
if (blobs.length == 1) {
133+
System.out.println("Calling delete");
134+
System.out.println(storage.delete(blobs[0]));
135+
} else {
136+
BatchRequest batch = new BatchRequest();
137+
for (Blob blob : blobs) {
138+
batch.delete(blob);
139+
}
140+
System.out.println("Calling batch.delete");
141+
storage.apply(batch);
142+
}
112143
}
113144
}
114145

trunk/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

trunk/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.api.services.storage.model.Bucket;import com.google.api.services.storage.model.StorageObject;import com.google.gcloud.storage.BlobReadChannel;import com.google.gcloud.storage.BlobWriteChannel;import com.google.gcloud.storage.StorageServiceException;import java.util.Map;public interface StorageRpc { enum Option { PREDEFINED_ACL("predefinedAcl"), PREDEFINED_DEFAULT_OBJECT_ACL("predefinedDefaultObjectAcl"), IF_METAGENERATION_MATCH("ifMetagenerationMatch"), IF_METAGENERATION_NOT_MATCH("ifMetagenerationNotMatch"), IF_GENERATION_NOT_MATCH("ifGenerationMatch"), IF_GENERATION_MATCH("ifGenerationNotMatch"), IF_SOURCE_METAGENERATION_MATCH("ifSourceMetagenerationMatch"), IF_SOURCE_METAGENERATION_NOT_MATCH("ifSourceMetagenerationNotMatch"), IF_SOURCE_GENERATION_MATCH("ifSourceGenerationMatch"), IF_SOURCE_GENERATION_NOT_MATCH("ifSourceGenerationNotMatch"), PREFIX("prefix"), MAX_RESULTS("maxResults"), PAGE_TOKEN("pageToken"), DELIMITER("delimiter"), VERSIONS("versions"); private final String value; Option(String value) { this.value = value; } public String value() { return value; } @SuppressWarnings("unchecked") <T> T get(Map<Option, ?> options) { return (T) options.get(this); } String getString(Map<Option, ?> options) { return get(options); } Long getLong(Map<Option, ?> options) { return get(options); } Boolean getBoolean(Map<Option, ?> options) { return get(options); } } class Tuple<X, Y> { private final X x; private final Y y; private Tuple(X x, Y y) { this.x = x; this.y = y; } public static <X, Y> Tuple<X, Y> of(X x, Y y) { return new Tuple<>(x, y); } public X x() { return x; } public Y y() { return y; } } Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options) throws StorageServiceException; Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageServiceException; Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?> options) throws StorageServiceException; Bucket get(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; StorageObject get(StorageObject object, Map<Option, ?> options) throws StorageServiceException; Bucket patch(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; StorageObject patch(StorageObject storageObject, Map<Option, ?> options) throws StorageServiceException; void delete(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; void delete(StorageObject object, Map<Option, ?> options) throws StorageServiceException; StorageObject compose(Iterable<StorageObject> sources, StorageObject target, Map<Option, ?> targetOptions) throws StorageServiceException; StorageObject copy(StorageObject source, Map<Option, ?> sourceOptions, StorageObject target, Map<Option, ?> targetOptions) throws StorageServiceException; byte[] load(StorageObject storageObject, Map<Option, ?> options) throws StorageServiceException; BlobReadChannel reader(StorageObject from, Map<Option, ?> options) throws StorageServiceException; BlobWriteChannel writer(StorageObject to, Map<Option, ?> options) throws StorageServiceException;}
1+
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.api.services.storage.model.Bucket;import com.google.api.services.storage.model.StorageObject;import com.google.gcloud.storage.BlobReadChannel;import com.google.gcloud.storage.BlobWriteChannel;import com.google.gcloud.storage.StorageServiceException;import java.util.Map;public interface StorageRpc { enum Option { PREDEFINED_ACL("predefinedAcl"), PREDEFINED_DEFAULT_OBJECT_ACL("predefinedDefaultObjectAcl"), IF_METAGENERATION_MATCH("ifMetagenerationMatch"), IF_METAGENERATION_NOT_MATCH("ifMetagenerationNotMatch"), IF_GENERATION_NOT_MATCH("ifGenerationMatch"), IF_GENERATION_MATCH("ifGenerationNotMatch"), IF_SOURCE_METAGENERATION_MATCH("ifSourceMetagenerationMatch"), IF_SOURCE_METAGENERATION_NOT_MATCH("ifSourceMetagenerationNotMatch"), IF_SOURCE_GENERATION_MATCH("ifSourceGenerationMatch"), IF_SOURCE_GENERATION_NOT_MATCH("ifSourceGenerationNotMatch"), PREFIX("prefix"), MAX_RESULTS("maxResults"), PAGE_TOKEN("pageToken"), DELIMITER("delimiter"), VERSIONS("versions"); private final String value; Option(String value) { this.value = value; } public String value() { return value; } @SuppressWarnings("unchecked") <T> T get(Map<Option, ?> options) { return (T) options.get(this); } String getString(Map<Option, ?> options) { return get(options); } Long getLong(Map<Option, ?> options) { return get(options); } Boolean getBoolean(Map<Option, ?> options) { return get(options); } } class Tuple<X, Y> { private final X x; private final Y y; private Tuple(X x, Y y) { this.x = x; this.y = y; } public static <X, Y> Tuple<X, Y> of(X x, Y y) { return new Tuple<>(x, y); } public X x() { return x; } public Y y() { return y; } } Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options) throws StorageServiceException; Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageServiceException; Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?> options) throws StorageServiceException; Bucket get(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; StorageObject get(StorageObject object, Map<Option, ?> options) throws StorageServiceException; Bucket patch(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; StorageObject patch(StorageObject storageObject, Map<Option, ?> options) throws StorageServiceException; boolean delete(Bucket bucket, Map<Option, ?> options) throws StorageServiceException; boolean delete(StorageObject object, Map<Option, ?> options) throws StorageServiceException; void batch(Iterable<Tuple<StorageObject, Map<Option, ?>>> toDelete) throws StorageServiceException; StorageObject compose(Iterable<StorageObject> sources, StorageObject target, Map<Option, ?> targetOptions) throws StorageServiceException; StorageObject copy(StorageObject source, Map<Option, ?> sourceOptions, StorageObject target, Map<Option, ?> targetOptions) throws StorageServiceException; byte[] load(StorageObject storageObject, Map<Option, ?> options) throws StorageServiceException; BlobReadChannel reader(StorageObject from, Map<Option, ?> options) throws StorageServiceException; BlobWriteChannel writer(StorageObject to, Map<Option, ?> options) throws StorageServiceException;}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.storage;
18+
19+
import com.google.gcloud.storage.StorageService.BlobSourceOption;
20+
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
23+
24+
/**
25+
* Created by ozarov on 4/30/15.
26+
*/
27+
public class BatchRequest {
28+
29+
Map<Blob, BlobSourceOption[]> toDelete = new LinkedHashMap<>();
30+
31+
public void delete(Blob blob, BlobSourceOption... options) {
32+
toDelete.put(blob, options);
33+
}
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.storage;
18+
19+
/**
20+
* Created by ozarov on 4/30/15.
21+
*/
22+
public class BatchResponse {
23+
}

trunk/src/main/java/com/google/gcloud/storage/StorageService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,13 @@ public static Builder builder() {
391391
/**
392392
* @throws StorageServiceException upon failure
393393
*/
394-
void delete(Bucket bucket, BucketSourceOption... options);
394+
boolean delete(Bucket bucket, BucketSourceOption... options);
395395

396396
/**
397397
* @throws StorageServiceException upon failure
398398
*/
399-
void delete(Blob blob, BlobSourceOption... options);
399+
boolean delete(Blob blob, BlobSourceOption... options);
400+
400401

401402
/**
402403
* @throws StorageServiceException upon failure
@@ -414,6 +415,9 @@ public static Builder builder() {
414415
*/
415416
byte[] load(Blob blob, BlobSourceOption... options);
416417

418+
419+
BatchResponse apply(BatchRequest batchRequest);
420+
417421
/**
418422
* @throws StorageServiceException upon failure
419423
*/

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,25 @@ public StorageObject call() {
185185
}
186186

187187
@Override
188-
public void delete(Bucket bucket, BucketSourceOption... options) {
188+
public boolean delete(Bucket bucket, BucketSourceOption... options) {
189189
final com.google.api.services.storage.model.Bucket bucketPb = bucket.toPb();
190190
final Map<StorageRpc.Option, ?> optionsMap = optionMap(bucket, options);
191-
runWithRetries(new Callable<Void>() {
191+
return runWithRetries(new Callable<Boolean>() {
192192
@Override
193-
public Void call() {
194-
storageRpc.delete(bucketPb, optionsMap);
195-
return null;
193+
public Boolean call() {
194+
return storageRpc.delete(bucketPb, optionsMap);
196195
}
197196
}, retryParams, EXCEPTION_HANDLER);
198197
}
199198

200199
@Override
201-
public void delete(Blob blob, BlobSourceOption... options) {
200+
public boolean delete(Blob blob, BlobSourceOption... options) {
202201
final StorageObject storageObject = blob.toPb();
203202
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
204-
runWithRetries(new Callable<Void>() {
203+
return runWithRetries(new Callable<Boolean>() {
205204
@Override
206-
public Void call() {
207-
storageRpc.delete(storageObject, optionsMap);
208-
return null;
205+
public Boolean call() {
206+
return storageRpc.delete(storageObject, optionsMap);
209207
}
210208
}, retryParams, EXCEPTION_HANDLER);
211209
}
@@ -258,6 +256,20 @@ public byte[] call() {
258256
}, retryParams, EXCEPTION_HANDLER);
259257
}
260258

259+
@Override
260+
public BatchResponse apply(BatchRequest batchRequest) {
261+
BatchResponse response = new BatchResponse();
262+
List<Tuple<StorageObject, Map<StorageRpc.Option, ?>>> request =
263+
Lists.newArrayListWithCapacity(batchRequest.toDelete.size());
264+
for (Map.Entry<Blob, BlobSourceOption[]> entry : batchRequest.toDelete.entrySet()) {
265+
Blob blob = entry.getKey();
266+
Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, entry.getValue());
267+
request.add(Tuple.<StorageObject, Map<StorageRpc.Option, ?>>of(blob.toPb(), optionsMap));
268+
}
269+
storageRpc.batch(request);
270+
return response;
271+
}
272+
261273
@Override
262274
public BlobReadChannel reader(Blob blob, BlobSourceOption... options) {
263275
// todo: Use retry helper on retriable failures

0 commit comments

Comments
 (0)