Skip to content

Commit b4a731a

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 2519 b: refs/heads/update-datastore c: f995b12 h: refs/heads/master i: 2517: 2338a0c 2515: e9414ea 2511: d2b14c4
1 parent 658436d commit b4a731a

55 files changed

Lines changed: 1420 additions & 470 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3
66
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b
77
refs/heads/resource-manager: ebf4adc5ee835cd2086c4ac5b4e78d01a5a005a7
8-
refs/heads/update-datastore: a5607774aee109dea6de6202a8f9ffb6652bcb6f
8+
refs/heads/update-datastore: f995b125709679a31b275cba879e8f63749ac207
99
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444
1010
refs/tags/v0.0.10: 207ebd2a3472fddee69fe1298eb90429e3306efd
1111
refs/tags/v0.0.11: ffbfba48a6426ff63c08ff2117e58681f251fbf2

branches/update-datastore/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ If you are using Maven, add this to your pom.xml file
2525
<dependency>
2626
<groupId>com.google.gcloud</groupId>
2727
<artifactId>gcloud-java</artifactId>
28-
<version>0.0.10</version>
28+
<version>0.0.12</version>
2929
</dependency>
3030
```
3131
If you are using Gradle, add this to your dependencies
3232
```Groovy
33-
compile 'com.google.gcloud:gcloud-java:jar:0.0.10'
33+
compile 'com.google.gcloud:gcloud-java:jar:0.0.12'
3434
```
3535
If you are using SBT, add this to your dependencies
3636
```Scala
37-
libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.0.10"
37+
libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.0.12"
3838
```
3939

4040
Example Applications
@@ -121,7 +121,7 @@ import com.google.gcloud.datastore.Entity;
121121
import com.google.gcloud.datastore.Key;
122122
import com.google.gcloud.datastore.KeyFactory;
123123
124-
Datastore datastore = DatastoreOptions.getDefaultInstance().service();
124+
Datastore datastore = DatastoreOptions.defaultInstance().service();
125125
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
126126
Key key = keyFactory.newKey(keyName);
127127
Entity entity = datastore.get(key);
@@ -164,8 +164,7 @@ import com.google.gcloud.storage.StorageOptions;
164164
import java.nio.ByteBuffer;
165165
import java.nio.channels.WritableByteChannel;
166166
167-
StorageOptions options = StorageOptions.builder().projectId("project").build();
168-
Storage storage = options.service();
167+
Storage storage = StorageOptions.defaultInstance().service();
169168
BlobId blobId = BlobId.of("bucket", "blob_name");
170169
Blob blob = Blob.load(storage, blobId);
171170
if (blob == null) {

branches/update-datastore/RELEASING.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ This script takes an optional argument denoting the new version. By default, if
1010
2. Create a PR to update the pom.xml version.
1111
The PR should look something like [#225](https://github.com/GoogleCloudPlatform/gcloud-java/pull/225). After this PR is merged into GoogleCloudPlatform/gcloud-java, Travis CI will push a new website to GoogleCloudPlatform/gh-pages, push a new artifact to the Maven Central Repository, and update versions in the README files.
1212

13-
3. Create a release on Github manually.
13+
3. Before moving on, verify that the artifacts have successfully been pushed to the Maven Central Repository. Open Travis CI, click the ["Build History" tab](https://travis-ci.org/GoogleCloudPlatform/gcloud-java/builds), and open the second build's logs for Step 2's PR. Be sure that you are not opening the "Pull Request" build logs. When the build finishes, scroll to the end of the log and verify that the artifacts were successfully staged and deployed. You can also search for `gcloud-java` on the [Sonatype website](https://oss.sonatype.org/#nexus-search;quick~gcloud-java) and check the latest version number. If the deployment didn't succeed because of a flaky test, rerun the build.
14+
15+
4. Create a release on Github manually.
1416
Go to the [releases page](https://github.com/GoogleCloudPlatform/gcloud-java/releases) and click "Draft a new release." Use `vX.Y.Z` as the "Tag Version" and `X.Y.Z` as the "Release Title", where `X.Y.Z` is the release version as listed in the `pom.xml` files.
1517

16-
4. Run `utilities/update_pom_version.sh` again (to include "-SNAPSHOT" in the project version).
18+
5. Run `utilities/update_pom_version.sh` again (to include "-SNAPSHOT" in the project version).
1719
As mentioned before, there is an optional version argument. By default, the script will update the version from "X.Y.Z" to "X.Y.Z+1-SNAPSHOT". Suppose a different version is desired, for example X+1.0.0-SNAPSHOT. Then the appropriate command to run would be `utilities/update_pom_version.sh X+1.0.0-SNAPSHOT`.
1820

19-
5. Create and merge in another PR to reflect the updated project version. For an example of what this PR should look like, see [#227](https://github.com/GoogleCloudPlatform/gcloud-java/pull/227).
21+
6. Create and merge in another PR to reflect the updated project version. For an example of what this PR should look like, see [#227](https://github.com/GoogleCloudPlatform/gcloud-java/pull/227).
2022

2123
### To push a snapshot version
2224

branches/update-datastore/gcloud-java-core/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ If you are using Maven, add this to your pom.xml file
1717
<dependency>
1818
<groupId>com.google.gcloud</groupId>
1919
<artifactId>gcloud-java-core</artifactId>
20-
<version>0.0.10</version>
20+
<version>0.0.12</version>
2121
</dependency>
2222
```
2323
If you are using Gradle, add this to your dependencies
2424
```Groovy
25-
compile 'com.google.gcloud:gcloud-java-core:jar:0.0.10'
25+
compile 'com.google.gcloud:gcloud-java-core:jar:0.0.12'
2626
```
2727
If you are using SBT, add this to your dependencies
2828
```Scala
29-
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.10"
29+
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.12"
3030
```
3131

3232
Java Versions

branches/update-datastore/gcloud-java-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.google.gcloud</groupId>
1313
<artifactId>gcloud-java-pom</artifactId>
14-
<version>0.0.11-SNAPSHOT</version>
14+
<version>0.0.13-SNAPSHOT</version>
1515
</parent>
1616
<properties>
1717
<site.installationModule>gcloud-java-core</site.installationModule>

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.api.client.json.jackson.JacksonFactory;
2525
import com.google.auth.http.HttpCredentialsAdapter;
2626
import com.google.auth.oauth2.GoogleCredentials;
27+
import com.google.auth.oauth2.ServiceAccountCredentials;
2728

2829
import java.io.IOException;
2930
import java.io.InputStream;
@@ -119,7 +120,7 @@ public RestorableState<AuthCredentials> capture() {
119120
}
120121
}
121122

122-
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
123+
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {
123124

124125
private GoogleCredentials googleCredentials;
125126

@@ -159,7 +160,16 @@ public boolean equals(Object obj) {
159160
@Override
160161
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
161162
Set<String> scopes) {
162-
return new HttpCredentialsAdapter(googleCredentials);
163+
return new HttpCredentialsAdapter(googleCredentials.createScoped(scopes));
164+
}
165+
166+
public ServiceAccountAuthCredentials toServiceAccountCredentials() {
167+
if (googleCredentials instanceof ServiceAccountCredentials) {
168+
ServiceAccountCredentials credentials = (ServiceAccountCredentials) googleCredentials;
169+
return new ServiceAccountAuthCredentials(credentials.getClientEmail(),
170+
credentials.getPrivateKey());
171+
}
172+
return null;
163173
}
164174

165175
@Override

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ void verifyCaller(Callable<?> callable) {
231231
}
232232
}
233233

234-
public Set<Class<? extends Exception>> getRetriableExceptions() {
234+
public Set<Class<? extends Exception>> retriableExceptions() {
235235
return retriableExceptions;
236236
}
237237

238-
public Set<Class<? extends Exception>> getNonRetriableExceptions() {
238+
public Set<Class<? extends Exception>> nonRetriableExceptions() {
239239
return nonRetriableExceptions;
240240
}
241241

@@ -262,7 +262,7 @@ boolean shouldRetry(Exception ex) {
262262
/**
263263
* Returns an instance which retry any checked exception and abort on any runtime exception.
264264
*/
265-
public static ExceptionHandler getDefaultInstance() {
265+
public static ExceptionHandler defaultInstance() {
266266
return DEFAULT_INSTANCE;
267267
}
268268

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/Page.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,30 @@
1616

1717
package com.google.gcloud;
1818

19+
import java.util.Iterator;
20+
1921
/**
2022
* Interface for Google Cloud paginated results.
2123
*
2224
* <p>
23-
* A typical {@code Page} usage:
25+
* Use {@code Page} to iterate through all values (also in next pages):
26+
* <pre> {@code
27+
* Page<T> page = ...; // get a Page<T> instance
28+
* Iterator<T> iterator = page.iterateAll();
29+
* while (iterator.hasNext()) {
30+
* T value = iterator.next();
31+
* // do something with value
32+
* }}</pre>
33+
* <p>
34+
* Or handle pagination explicitly:
2435
* <pre> {@code
2536
* Page<T> page = ...; // get a Page<T> instance
2637
* while (page != null) {
2738
* for (T value : page.values()) {
2839
* // do something with value
2940
* }
3041
* page = page.nextPage();
31-
* }
32-
* }</pre>
42+
* }}</pre>
3343
*/
3444
public interface Page<T> {
3545

@@ -38,6 +48,12 @@ public interface Page<T> {
3848
*/
3949
Iterable<T> values();
4050

51+
/**
52+
* Returns an iterator for all values, possibly also in the next pages. Once current page's values
53+
* are traversed the iterator fetches next page, if any.
54+
*/
55+
Iterator<T> iterateAll();
56+
4157
/**
4258
* Returns the cursor for the nextPage or {@code null} if no more results.
4359
*/

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package com.google.gcloud;
1818

19+
import com.google.common.collect.AbstractIterator;
20+
1921
import java.io.Serializable;
2022
import java.util.Collections;
23+
import java.util.Iterator;
2124
import java.util.Objects;
2225

2326
/**
@@ -35,6 +38,29 @@ public interface NextPageFetcher<T> extends Serializable {
3538
Page<T> nextPage();
3639
}
3740

41+
static class PageIterator<T> extends AbstractIterator<T> {
42+
43+
private Iterator<T> currentPageIterator;
44+
private Page<T> currentPage;
45+
46+
PageIterator(Page<T> currentPage) {
47+
this.currentPageIterator = currentPage.values().iterator();
48+
this.currentPage = currentPage;
49+
}
50+
51+
@Override
52+
protected T computeNext() {
53+
while (!currentPageIterator.hasNext()) {
54+
currentPage = currentPage.nextPage();
55+
if (currentPage == null) {
56+
return endOfData();
57+
}
58+
currentPageIterator = currentPage.values().iterator();
59+
}
60+
return currentPageIterator.next();
61+
}
62+
}
63+
3864
/**
3965
* Creates a {@code PageImpl} object. In order for the object to be serializable the {@code
4066
* results} parameter must be serializable.
@@ -50,6 +76,11 @@ public Iterable<T> values() {
5076
return results == null ? Collections.EMPTY_LIST : results;
5177
}
5278

79+
@Override
80+
public Iterator<T> iterateAll() {
81+
return new PageIterator<T>(this);
82+
}
83+
5384
@Override
5485
public String nextPageCursor() {
5586
return cursor;

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/RetryHelper.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ private V doRetry() throws RetryHelperException {
194194
}
195195
exception = e;
196196
}
197-
if (attemptNumber >= params.getRetryMaxAttempts()
198-
|| attemptNumber >= params.getRetryMinAttempts()
199-
&& stopwatch.elapsed(MILLISECONDS) >= params.getTotalRetryPeriodMillis()) {
197+
if (attemptNumber >= params.retryMaxAttempts()
198+
|| attemptNumber >= params.retryMinAttempts()
199+
&& stopwatch.elapsed(MILLISECONDS) >= params.totalRetryPeriodMillis()) {
200200
throw new RetriesExhaustedException(this + ": Too many failures, giving up", exception);
201201
}
202202
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
@@ -215,9 +215,9 @@ private V doRetry() throws RetryHelperException {
215215

216216
@VisibleForTesting
217217
static long getSleepDuration(RetryParams retryParams, int attemptsSoFar) {
218-
long initialDelay = retryParams.getInitialRetryDelayMillis();
219-
double backoffFactor = retryParams.getRetryDelayBackoffFactor();
220-
long maxDelay = retryParams.getMaxRetryDelayMillis();
218+
long initialDelay = retryParams.initialRetryDelayMillis();
219+
double backoffFactor = retryParams.retryDelayBackoffFactor();
220+
long maxDelay = retryParams.maxRetryDelayMillis();
221221
long retryDelay = getExponentialValue(initialDelay, backoffFactor, maxDelay, attemptsSoFar);
222222
return (long) ((random() / 2.0 + .75) * retryDelay);
223223
}
@@ -228,8 +228,8 @@ private static long getExponentialValue(long initialDelay, double backoffFactor,
228228
}
229229

230230
public static <V> V runWithRetries(Callable<V> callable) throws RetryHelperException {
231-
return runWithRetries(callable, RetryParams.getDefaultInstance(),
232-
ExceptionHandler.getDefaultInstance());
231+
return runWithRetries(callable, RetryParams.defaultInstance(),
232+
ExceptionHandler.defaultInstance());
233233
}
234234

235235
public static <V> V runWithRetries(Callable<V> callable, RetryParams params,

0 commit comments

Comments
 (0)