-
Notifications
You must be signed in to change notification settings - Fork 71
/
StorageBatch.java
207 lines (186 loc) · 7.71 KB
/
StorageBatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright 2016 Google LLC
*
* 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.cloud.storage;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.services.storage.model.StorageObject;
import com.google.cloud.storage.Storage.BlobGetOption;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.Storage.BlobTargetOption;
import com.google.cloud.storage.spi.v1.RpcBatch;
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.annotations.VisibleForTesting;
import java.util.Map;
/**
* A batch of operations to be submitted to Google Cloud Storage using a single RPC request.
*
* <p>Example of using a batch request to delete, update and get a blob:
*
* <pre>{@code
* StorageBatch batch = storage.batch();
* BlobId firstBlob = BlobId.of("bucket", "blob1"));
* BlobId secondBlob = BlobId.of("bucket", "blob2"));
* batch.delete(firstBlob).notify(new BatchResult.Callback<Boolean, StorageException>() {
* public void success(Boolean result) {
* // deleted successfully
* }
*
* public void error(StorageException exception) {
* // delete failed
* }
* });
* batch.update(BlobInfo.builder(secondBlob).contentType("text/plain").build());
* StorageBatchResult<Blob> result = batch.get(secondBlob);
* batch.submit();
* Blob blob = result.get(); // returns get result or throws StorageException
* }</pre>
*/
public class StorageBatch {
private final RpcBatch batch;
private final StorageRpc storageRpc;
private final StorageOptions options;
StorageBatch(StorageOptions options) {
this.options = options;
this.storageRpc = options.getStorageRpcV1();
this.batch = storageRpc.createBatch();
}
@VisibleForTesting
Object getBatch() {
return batch;
}
@VisibleForTesting
StorageRpc getStorageRpc() {
return storageRpc;
}
@VisibleForTesting
StorageOptions getOptions() {
return options;
}
/**
* Adds a request representing the "delete blob" operation to this batch. Calling {@link
* StorageBatchResult#get()} on the return value yields {@code true} upon successful deletion,
* {@code false} if the blob was not found, or throws a {@link StorageException} if the operation
* failed.
*/
public StorageBatchResult<Boolean> delete(
String bucket, String blob, BlobSourceOption... options) {
return delete(BlobId.of(bucket, blob), options);
}
/**
* Adds a request representing the "delete blob" operation to this batch. Calling {@link
* StorageBatchResult#get()} on the return value yields {@code true} upon successful deletion,
* {@code false} if the blob was not found, or throws a {@link StorageException} if the operation
* failed.
*/
public StorageBatchResult<Boolean> delete(BlobId blob, BlobSourceOption... options) {
StorageBatchResult<Boolean> result = new StorageBatchResult<>();
RpcBatch.Callback<Void> callback = createDeleteCallback(result);
Map<StorageRpc.Option, ?> optionMap = StorageImpl.optionMap(blob, options);
batch.addDelete(blob.toPb(), callback, optionMap);
return result;
}
/**
* Adds a request representing the "update blob" operation to this batch. The {@code options} can
* be used in the same way as for {@link Storage#update(BlobInfo, BlobTargetOption...)}. Calling
* {@link StorageBatchResult#get()} on the return value yields the updated {@link Blob} if
* successful, or throws a {@link StorageException} if the operation failed.
*/
public StorageBatchResult<Blob> update(BlobInfo blobInfo, BlobTargetOption... options) {
StorageBatchResult<Blob> result = new StorageBatchResult<>();
RpcBatch.Callback<StorageObject> callback = createUpdateCallback(this.options, result);
Map<StorageRpc.Option, ?> optionMap = StorageImpl.optionMap(blobInfo, options);
batch.addPatch(blobInfo.toPb(), callback, optionMap);
return result;
}
/**
* Adds a request representing the "get blob" operation to this batch. The {@code options} can be
* used in the same way as for {@link Storage#get(BlobId, BlobGetOption...)}. Calling {@link
* StorageBatchResult#get()} on the return value yields the requested {@link Blob} if successful,
* {@code null} if no such blob exists, or throws a {@link StorageException} if the operation
* failed.
*/
public StorageBatchResult<Blob> get(String bucket, String blob, BlobGetOption... options) {
return get(BlobId.of(bucket, blob), options);
}
/**
* Adds a request representing the "get blob" operation to this batch. The {@code options} can be
* used in the same way as for {@link Storage#get(BlobId, BlobGetOption...)}. Calling {@link
* StorageBatchResult#get()} on the return value yields the requested {@link Blob} if successful,
* {@code null} if no such blob exists, or throws a {@link StorageException} if the operation
* failed.
*/
public StorageBatchResult<Blob> get(BlobId blob, BlobGetOption... options) {
StorageBatchResult<Blob> result = new StorageBatchResult<>();
RpcBatch.Callback<StorageObject> callback = createGetCallback(this.options, result);
Map<StorageRpc.Option, ?> optionMap = StorageImpl.optionMap(blob, options);
batch.addGet(blob.toPb(), callback, optionMap);
return result;
}
/** Submits this batch for processing using a single RPC request. */
public void submit() {
batch.submit();
}
private RpcBatch.Callback<Void> createDeleteCallback(final StorageBatchResult<Boolean> result) {
return new RpcBatch.Callback<Void>() {
@Override
public void onSuccess(Void response) {
result.success(true);
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
StorageException serviceException = new StorageException(googleJsonError);
if (serviceException.getCode() == HTTP_NOT_FOUND) {
result.success(false);
} else {
result.error(serviceException);
}
}
};
}
private RpcBatch.Callback<StorageObject> createGetCallback(
final StorageOptions serviceOptions, final StorageBatchResult<Blob> result) {
return new RpcBatch.Callback<StorageObject>() {
@Override
public void onSuccess(StorageObject response) {
result.success(
response == null ? null : Blob.fromPb(serviceOptions.getService(), response));
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
StorageException serviceException = new StorageException(googleJsonError);
if (serviceException.getCode() == HTTP_NOT_FOUND) {
result.success(null);
} else {
result.error(serviceException);
}
}
};
}
private RpcBatch.Callback<StorageObject> createUpdateCallback(
final StorageOptions serviceOptions, final StorageBatchResult<Blob> result) {
return new RpcBatch.Callback<StorageObject>() {
@Override
public void onSuccess(StorageObject response) {
result.success(
response == null ? null : Blob.fromPb(serviceOptions.getService(), response));
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new StorageException(googleJsonError));
}
};
}
}