Skip to content

Commit 72fdbe0

Browse files
committed
---
yaml --- r: 6275 b: refs/heads/tswast-patch-1 c: b1918ca h: refs/heads/master i: 6273: 5bcff1d 6271: acae946
1 parent 22dbfa7 commit 72fdbe0

3 files changed

Lines changed: 221 additions & 1 deletion

File tree

  • branches/tswast-patch-1/gcloud-java-storage/src

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: fc3a2d74dc5f3daf5d8c660e6fba571b55bda585
60+
refs/heads/tswast-patch-1: b1918ca205078c37b249d4367d3d8b584e922ca0
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.gcloud.storage.Blob.BlobSourceOption.convert;
2222

23+
import com.google.common.base.Function;
24+
import com.google.common.collect.Lists;
2325
import com.google.gcloud.spi.StorageRpc;
2426
import com.google.gcloud.storage.Storage.BlobTargetOption;
2527
import com.google.gcloud.storage.Storage.CopyRequest;
2628
import com.google.gcloud.storage.Storage.SignUrlOption;
2729

2830
import java.net.URL;
31+
import java.util.Arrays;
32+
import java.util.Collections;
33+
import java.util.List;
2934
import java.util.Objects;
3035

3136
/**
@@ -256,4 +261,88 @@ public URL signUrl(long expirationTimeInSeconds, SignUrlOption... options) {
256261
public Storage storage() {
257262
return storage;
258263
}
264+
265+
/**
266+
* Gets the requested blobs. If {@code infos.length == 0} an empty list is returned. If
267+
* {@code infos.length > 1} a batch request is used to fetch blobs.
268+
*
269+
* @param storage the storage service used to issue the request
270+
* @param infos the blobs to get
271+
* @return a list of {@code Blob} objects. If a blob does not exist the corresponding item in the
272+
* list is {@code null}.
273+
*/
274+
public static List<Blob> get(final Storage storage, BlobInfo... infos) {
275+
checkNotNull(storage);
276+
checkNotNull(infos);
277+
int length = infos.length;
278+
switch (length) {
279+
case 0:
280+
return Collections.emptyList();
281+
case 1:
282+
return Collections.singletonList(
283+
new Blob(storage, storage.get(infos[0].bucket(), infos[0].name())));
284+
default:
285+
return Lists.transform(
286+
storage.get(infos[0], infos[1], Arrays.copyOfRange(infos, 2, length)),
287+
new Function<BlobInfo, Blob>() {
288+
@Override
289+
public Blob apply(BlobInfo f) {
290+
return f != null ? new Blob(storage, f) : null;
291+
}
292+
});
293+
}
294+
}
295+
296+
/**
297+
* Updates the requested blobs. If {@code infos.length == 0} an empty list is returned. If
298+
* {@code infos.length > 1} a batch request is used to update blobs.
299+
*
300+
* @param storage the storage service used to issue the request
301+
* @param infos the blobs to update
302+
* @return a list of {@code Blob} objects. If a blob does not exist the corresponding item in the
303+
* list is {@code null}.
304+
*/
305+
public static List<Blob> update(final Storage storage, BlobInfo... infos) {
306+
checkNotNull(storage);
307+
checkNotNull(infos);
308+
int length = infos.length;
309+
switch (length) {
310+
case 0:
311+
return Collections.emptyList();
312+
case 1:
313+
return Collections.singletonList(new Blob(storage, storage.update(infos[0])));
314+
default:
315+
return Lists.transform(
316+
storage.update(infos[0], infos[1], Arrays.copyOfRange(infos, 2, length)),
317+
new Function<BlobInfo, Blob>() {
318+
@Override
319+
public Blob apply(BlobInfo f) {
320+
return f != null ? new Blob(storage, f) : null;
321+
}
322+
});
323+
}
324+
}
325+
326+
/**
327+
* Deletes the requested blobs. If {@code infos.length == 0} an empty list is returned. If
328+
* {@code infos.length > 1} a batch request is used to delete blobs.
329+
*
330+
* @param storage the storage service used to issue the request
331+
* @param infos the blobs to delete
332+
* @return a list of booleans. If a blob has been deleted the corresponding item in the list is
333+
* {@code true}. If deletion failed the item is {@code false}.
334+
*/
335+
public static List<Boolean> delete(Storage storage, BlobInfo... infos) {
336+
checkNotNull(storage);
337+
checkNotNull(infos);
338+
int length = infos.length;
339+
switch (length) {
340+
case 0:
341+
return Collections.emptyList();
342+
case 1:
343+
return Collections.singletonList(storage.delete(infos[0].bucket(), infos[0].name()));
344+
default:
345+
return storage.delete(infos[0], infos[1], Arrays.copyOfRange(infos, 2, length));
346+
}
347+
}
259348
}

branches/tswast-patch-1/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,25 @@
2525
import static org.junit.Assert.assertArrayEquals;
2626
import static org.junit.Assert.assertEquals;
2727
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertNull;
2829
import static org.junit.Assert.assertSame;
2930
import static org.junit.Assert.assertTrue;
3031

32+
import com.google.api.client.util.Lists;
3133
import com.google.gcloud.storage.Storage.CopyRequest;
3234
import org.easymock.Capture;
3335
import org.junit.After;
3436
import org.junit.Before;
3537
import org.junit.Test;
3638
import java.net.URL;
39+
import java.util.Arrays;
40+
import java.util.List;
3741

3842
public class BlobTest {
3943

4044
private static final BlobInfo BLOB_INFO = BlobInfo.of("b", "n");
45+
private static final BlobInfo[] BLOB_INFO_ARRAY = {BlobInfo.of("b1", "n1"),
46+
BlobInfo.of("b2", "n2"), BlobInfo.of("b3", "n3")};
4147

4248
private Storage storage;
4349
private Blob blob;
@@ -159,4 +165,129 @@ public void testSignUrl() throws Exception {
159165
replay(storage);
160166
assertEquals(url, blob.signUrl(100));
161167
}
168+
169+
@Test
170+
public void testGetNone() throws Exception {
171+
replay(storage);
172+
assertTrue(Blob.get(storage).isEmpty());
173+
}
174+
175+
@Test
176+
public void testGetOne() throws Exception {
177+
expect(storage.get(BLOB_INFO.bucket(), BLOB_INFO.name())).andReturn(BLOB_INFO);
178+
replay(storage);
179+
List<Blob> result = Blob.get(storage, BLOB_INFO);
180+
assertEquals(1, result.size());
181+
assertEquals(BLOB_INFO, result.get(0).info());
182+
}
183+
184+
@Test
185+
public void testGetSome() throws Exception {
186+
List<BlobInfo> blobInfoList = Arrays.asList(BLOB_INFO_ARRAY);
187+
expect(storage.get(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1],
188+
Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList);
189+
replay(storage);
190+
List<Blob> result = Blob.get(storage, BLOB_INFO_ARRAY);
191+
assertEquals(blobInfoList.size(), result.size());
192+
for (int i = 0; i < blobInfoList.size(); i++) {
193+
assertEquals(blobInfoList.get(i), result.get(i).info());
194+
}
195+
}
196+
197+
@Test
198+
public void testGetSomeNull() throws Exception {
199+
List<BlobInfo> blobInfoList = Arrays.asList(BLOB_INFO_ARRAY[0], null, BLOB_INFO_ARRAY[2]);
200+
expect(storage.get(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1],
201+
Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList);
202+
replay(storage);
203+
List<Blob> result = Blob.get(storage, BLOB_INFO_ARRAY);
204+
assertEquals(blobInfoList.size(), result.size());
205+
for (int i = 0; i < blobInfoList.size(); i++) {
206+
if (blobInfoList.get(i) != null) {
207+
assertEquals(blobInfoList.get(i), result.get(i).info());
208+
} else {
209+
assertNull(result.get(i));
210+
}
211+
}
212+
}
213+
214+
@Test
215+
public void testUpdateNone() throws Exception {
216+
replay(storage);
217+
assertTrue(Blob.update(storage).isEmpty());
218+
}
219+
220+
@Test
221+
public void testUpdateOne() throws Exception {
222+
BlobInfo updatedBlob = BLOB_INFO.toBuilder().contentType("content").build();
223+
expect(storage.update(BLOB_INFO)).andReturn(updatedBlob);
224+
replay(storage);
225+
List<Blob> result = Blob.update(storage, BLOB_INFO);
226+
assertEquals(1, result.size());
227+
assertEquals(updatedBlob, result.get(0).info());
228+
}
229+
230+
@Test
231+
public void testUpdateSome() throws Exception {
232+
List<BlobInfo> blobInfoList = Lists.newArrayListWithCapacity(BLOB_INFO_ARRAY.length);
233+
for (BlobInfo info : BLOB_INFO_ARRAY) {
234+
blobInfoList.add(info.toBuilder().contentType("content").build());
235+
}
236+
expect(storage.update(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1],
237+
Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList);
238+
replay(storage);
239+
List<Blob> result = Blob.update(storage, BLOB_INFO_ARRAY);
240+
assertEquals(blobInfoList.size(), result.size());
241+
for (int i = 0; i < blobInfoList.size(); i++) {
242+
assertEquals(blobInfoList.get(i), result.get(i).info());
243+
}
244+
}
245+
246+
@Test
247+
public void testUpdateSomeNull() throws Exception {
248+
List<BlobInfo> blobInfoList = Arrays.asList(
249+
BLOB_INFO_ARRAY[0].toBuilder().contentType("content").build(), null,
250+
BLOB_INFO_ARRAY[2].toBuilder().contentType("content").build());
251+
expect(storage.update(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1],
252+
Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList);
253+
replay(storage);
254+
List<Blob> result = Blob.update(storage, BLOB_INFO_ARRAY);
255+
assertEquals(blobInfoList.size(), result.size());
256+
for (int i = 0; i < blobInfoList.size(); i++) {
257+
if (blobInfoList.get(i) != null) {
258+
assertEquals(blobInfoList.get(i), result.get(i).info());
259+
} else {
260+
assertNull(result.get(i));
261+
}
262+
}
263+
}
264+
265+
@Test
266+
public void testDeleteNone() throws Exception {
267+
replay(storage);
268+
assertTrue(Blob.delete(storage).isEmpty());
269+
}
270+
271+
@Test
272+
public void testDeleteOne() throws Exception {
273+
expect(storage.delete(BLOB_INFO.bucket(), BLOB_INFO.name())).andReturn(true);
274+
replay(storage);
275+
List<Boolean> result = Blob.delete(storage, BLOB_INFO);
276+
assertEquals(1, result.size());
277+
assertTrue(result.get(0));
278+
}
279+
280+
@Test
281+
public void testDeleteSome() throws Exception {
282+
List<Boolean> deleleResultList = Arrays.asList(true, true, true);
283+
expect(storage.delete(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1],
284+
Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length)))
285+
.andReturn(deleleResultList);
286+
replay(storage);
287+
List<Boolean> result = Blob.delete(storage, BLOB_INFO_ARRAY);
288+
assertEquals(deleleResultList.size(), result.size());
289+
for (int i = 0; i < deleleResultList.size(); i++) {
290+
assertEquals(deleleResultList.get(i), result.get(i));
291+
}
292+
}
162293
}

0 commit comments

Comments
 (0)