Skip to content

Commit 588f71d

Browse files
committed
---
yaml --- r: 6397 b: refs/heads/tswast-patch-1 c: 6753c54 h: refs/heads/master i: 6395: ef45402
1 parent 61d0732 commit 588f71d

13 files changed

Lines changed: 141 additions & 210 deletions

File tree

[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: ea36a9e62f6179c557f7f788c37189f9aa3c107d
60+
refs/heads/tswast-patch-1: 6753c541b99e947d0af13b6e4d88ce65cd65a4c7
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-core/src/main/java/com/google/gcloud/BasePage.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,38 @@
1818

1919
import java.io.Serializable;
2020
import java.util.Collections;
21-
import java.util.Iterator;
2221
import java.util.Objects;
2322

2423
/**
2524
* Base implementation for Google Cloud paginated results.
2625
*/
27-
public class BasePage<T extends Serializable> implements Page<T>, Serializable {
26+
public class BasePage<T> implements Page<T>, Serializable {
2827

29-
private static final long serialVersionUID = -6937287874908527950L;
28+
private static final long serialVersionUID = 3914827379823557934L;
3029

3130
private final String cursor;
3231
private final Iterable<T> results;
3332
private final NextPageFetcher<T> pageFetcher;
3433

35-
public interface NextPageFetcher<T extends Serializable> extends Serializable {
34+
public interface NextPageFetcher<T> extends Serializable {
3635
Page<T> nextPage();
3736
}
3837

38+
/**
39+
* Creates a {@code BasePage} object. In order for the object to be serializable the {@code
40+
* results} parameter must be serializable.
41+
*/
3942
public BasePage(NextPageFetcher<T> pageFetcher, String cursor, Iterable<T> results) {
4043
this.pageFetcher = pageFetcher;
4144
this.cursor = cursor;
4245
this.results = results;
4346
}
4447

48+
@Override
49+
public Iterable<T> values() {
50+
return results == null ? Collections.EMPTY_LIST : results;
51+
}
52+
4553
@Override
4654
public String nextPageCursor() {
4755
return cursor;
@@ -55,11 +63,6 @@ public Page<T> nextPage() {
5563
return pageFetcher.nextPage();
5664
}
5765

58-
@Override
59-
public Iterator<T> iterator() {
60-
return results == null ? Collections.<T>emptyIterator() : results.iterator();
61-
}
62-
6366
@Override
6467
public int hashCode() {
6568
return Objects.hash(cursor, results);

branches/tswast-patch-1/gcloud-java-core/src/main/java/com/google/gcloud/Page.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,25 @@
1818

1919
/**
2020
* Interface for Google Cloud paginated results.
21+
*
22+
* <p>
23+
* A typical {@code Page} usage:
24+
* <pre> {@code
25+
* Page<T> page = ...; // get a Page<T> instance
26+
* while (page != null) {
27+
* for (T value : page.values()) {
28+
* // do something with value
29+
* }
30+
* page = page.nextPage();
31+
* }
32+
* }</pre>
2133
*/
22-
public interface Page<T> extends Iterable<T> {
34+
public interface Page<T> {
35+
36+
/**
37+
* Returns the values contained in this page.
38+
*/
39+
Iterable<T> values();
2340

2441
/**
2542
* Returns the cursor for the nextPage or {@code null} if no more results.

branches/tswast-patch-1/gcloud-java-core/src/test/java/com/google/gcloud/BasePageTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.junit.Assert.assertEquals;
2020

2121
import com.google.common.collect.ImmutableList;
22-
import com.google.gcloud.BasePage;
2322

2423
import org.junit.Test;
2524

@@ -42,7 +41,6 @@ public BasePage<String> nextPage() {
4241
BasePage<String> result = new BasePage<>(fetcher, "c", values);
4342
assertEquals(nextResult, result.nextPage());
4443
assertEquals("c", result.nextPageCursor());
45-
assertEquals(values, ImmutableList.copyOf(result.iterator()));
46-
44+
assertEquals(values, ImmutableList.copyOf(result.values().iterator()));
4745
}
4846
}

branches/tswast-patch-1/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java

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

1919
import com.google.gcloud.AuthCredentials;
2020
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
21+
import com.google.gcloud.Page;
2122
import com.google.gcloud.RetryParams;
2223
import com.google.gcloud.spi.StorageRpc.Tuple;
2324
import com.google.gcloud.storage.Blob;
@@ -213,8 +214,12 @@ String parse(String... args) {
213214
public void run(Storage storage, String bucketName) {
214215
if (bucketName == null) {
215216
// list buckets
216-
for (BucketInfo b : storage.list()) {
217-
System.out.println(b);
217+
Page<BucketInfo> bucketPage = storage.list();
218+
while (bucketPage != null) {
219+
for (BucketInfo b : bucketPage.values()) {
220+
System.out.println(b);
221+
}
222+
bucketPage = bucketPage.nextPage();
218223
}
219224
} else {
220225
// list a bucket's blobs
@@ -223,8 +228,12 @@ public void run(Storage storage, String bucketName) {
223228
System.out.println("No such bucket");
224229
return;
225230
}
226-
for (Blob b : bucket.list()) {
227-
System.out.println(b.info());
231+
Page<Blob> blobPage = bucket.list();
232+
while (blobPage != null) {
233+
for (Blob b : blobPage.values()) {
234+
System.out.println(b.info());
235+
}
236+
blobPage = blobPage.nextPage();
228237
}
229238
}
230239
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import com.google.common.base.Function;
2223
import com.google.common.base.MoreObjects;
24+
import com.google.common.collect.Iterators;
25+
import com.google.gcloud.BasePage;
2326
import com.google.gcloud.Page;
2427
import com.google.gcloud.storage.Storage.BlobSourceOption;
2528
import com.google.gcloud.storage.Storage.BlobTargetOption;
2629
import com.google.gcloud.storage.Storage.BlobWriteOption;
2730
import com.google.gcloud.storage.Storage.BucketSourceOption;
2831
import com.google.gcloud.storage.Storage.BucketTargetOption;
29-
import java.io.InputStream;
3032

33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
import java.io.ObjectInputStream;
36+
import java.io.Serializable;
3137
import java.util.ArrayList;
3238
import java.util.Collections;
39+
import java.util.Iterator;
3340
import java.util.List;
3441
import java.util.Objects;
3542

@@ -47,6 +54,71 @@ public final class Bucket {
4754
private final Storage storage;
4855
private final BucketInfo info;
4956

57+
private static class BlobPageFetcher implements BasePage.NextPageFetcher<Blob> {
58+
59+
private static final long serialVersionUID = 3221100177471323801L;
60+
61+
private final StorageOptions options;
62+
private final Page<BlobInfo> infoPage;
63+
64+
BlobPageFetcher(StorageOptions options, Page<BlobInfo> infoPage) {
65+
this.options = options;
66+
this.infoPage = infoPage;
67+
}
68+
69+
@Override
70+
public Page<Blob> nextPage() {
71+
Page<BlobInfo> nextInfoPage = infoPage.nextPage();
72+
return new BasePage<Blob>(new BlobPageFetcher(options, nextInfoPage),
73+
nextInfoPage.nextPageCursor(), new LazyBlobIterable(options, nextInfoPage.values()));
74+
}
75+
}
76+
77+
private static class LazyBlobIterable implements Iterable<Blob>, Serializable {
78+
79+
private static final long serialVersionUID = -3092290247725378832L;
80+
81+
private final StorageOptions options;
82+
private Iterable<BlobInfo> infoIterable;
83+
private transient Storage storage;
84+
85+
public LazyBlobIterable(StorageOptions options, Iterable<BlobInfo> infoIterable) {
86+
this.options = options;
87+
this.infoIterable = infoIterable;
88+
this.storage = options.service();
89+
}
90+
91+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
92+
in.defaultReadObject();
93+
this.storage = options.service();
94+
}
95+
96+
@Override
97+
public Iterator<Blob> iterator() {
98+
return Iterators.transform(infoIterable.iterator(), new Function<BlobInfo, Blob>() {
99+
@Override
100+
public Blob apply(BlobInfo blobInfo) {
101+
return new Blob(storage, blobInfo);
102+
}
103+
});
104+
}
105+
106+
@Override
107+
public int hashCode() {
108+
return Objects.hash(options, infoIterable);
109+
}
110+
111+
@Override
112+
public boolean equals(Object obj) {
113+
if (!(obj instanceof LazyBlobIterable)) {
114+
return false;
115+
}
116+
LazyBlobIterable other = (LazyBlobIterable) obj;
117+
return Objects.equals(options, other.options)
118+
&& Objects.equals(infoIterable, other.infoIterable);
119+
}
120+
}
121+
50122
/**
51123
* Constructs a {@code Bucket} object for the provided {@code BucketInfo}. The storage service is
52124
* used to issue requests.
@@ -136,7 +208,10 @@ public boolean delete(BucketSourceOption... options) {
136208
* @throws StorageException upon failure
137209
*/
138210
public Page<Blob> list(Storage.BlobListOption... options) {
139-
return new BlobPage(storage, storage.list(info.name(), options));
211+
Page<BlobInfo> infoPage = storage.list(info.name(), options);
212+
StorageOptions storageOptions = storage.options();
213+
return new BasePage<Blob>(new BlobPageFetcher(storageOptions, infoPage),
214+
infoPage.nextPageCursor(), new LazyBlobIterable(storageOptions, infoPage.values()));
140215
}
141216

142217
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public DeleteBucketTask(Storage storage, String bucket) {
195195
@Override
196196
public Boolean call() throws Exception {
197197
while (true) {
198-
for (BlobInfo info : storage.list(bucket)) {
198+
for (BlobInfo info : storage.list(bucket).values()) {
199199
storage.delete(bucket, info.name());
200200
}
201201
try {

0 commit comments

Comments
 (0)