From 6636e23cac4c18a9fcc4a826aecc531294a564c0 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 4 Nov 2015 17:04:25 -0800 Subject: [PATCH 1/7] Create BaseServiceException in gcloud-java-core --- .../google/gcloud/BaseServiceException.java | 51 +++++++++++++++++++ .../gcloud/BaseServiceExceptionTest.java | 45 ++++++++++++++++ .../gcloud/datastore/DatastoreException.java | 50 +++++++++--------- .../datastore/DatastoreExceptionTest.java | 12 ++--- .../gcloud/datastore/DatastoreTest.java | 4 +- .../gcloud/storage/StorageException.java | 24 ++------- 6 files changed, 135 insertions(+), 51 deletions(-) create mode 100644 gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/BaseServiceExceptionTest.java diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java new file mode 100644 index 000000000000..45c047d4710a --- /dev/null +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -0,0 +1,51 @@ +/* + * 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; + +/** + * Base service exception. + */ +public class BaseServiceException extends RuntimeException { + + private static final long serialVersionUID = 5028833760039966178L; + + private final int code; + private final boolean retryable; + + public BaseServiceException(int code, String message, boolean retryable) { + super(message); + this.code = code; + this.retryable = retryable; + } + + public BaseServiceException(int code, String message, boolean retryable, Exception cause) { + super(message, cause); + this.code = code; + this.retryable = retryable; + } + + /** + * Returns the code associated with this exception. + */ + public int code() { + return code; + } + + public boolean retryable() { + return retryable; + } +} diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseServiceExceptionTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseServiceExceptionTest.java new file mode 100644 index 000000000000..f30fd3abfb79 --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseServiceExceptionTest.java @@ -0,0 +1,45 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Tests for {@link BaseServiceException}. + */ +public class BaseServiceExceptionTest { + + private final int code = 1; + private final String message = "some message"; + private final boolean retryable = true; + + @Test + public void testBaseServiceException() { + BaseServiceException serviceException = new BaseServiceException(code, message, retryable); + assertEquals(serviceException.code(), code); + assertEquals(serviceException.getMessage(), message); + assertEquals(serviceException.getCause(), null); + + Exception cause = new RuntimeException(); + serviceException = new BaseServiceException(code, message, retryable, cause); + assertEquals(serviceException.code(), code); + assertEquals(serviceException.getMessage(), message); + assertEquals(serviceException.getCause(), cause); + } +} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java index 562578a26428..ecbdd57b6bfb 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java @@ -18,6 +18,7 @@ import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableMap; +import com.google.gcloud.BaseServiceException; import com.google.gcloud.RetryHelper; import com.google.gcloud.RetryHelper.RetryHelperException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; @@ -26,21 +27,21 @@ import java.util.HashMap; import java.util.Map; -public class DatastoreException extends RuntimeException { +public class DatastoreException extends BaseServiceException { - private static final long serialVersionUID = 8170357898917041899L; - private static final ImmutableMap REASON_TO_CODE; - private static final ImmutableMap HTTP_TO_CODE; + private static final long serialVersionUID = -2336749234060754893L; + private static final ImmutableMap REASON_TO_CODE; + private static final ImmutableMap HTTP_TO_CODE; - private final Code code; + private final ErrorInfo errorInfo; /** - * An error code to represent the failure. + * Represent metadata about {@link DatastoreException}s. * * @see Google Cloud * Datastore error codes */ - public enum Code { + public enum ErrorInfo { ABORTED(Reason.ABORTED), DEADLINE_EXCEEDED(Reason.DEADLINE_EXCEEDED), @@ -57,11 +58,11 @@ public enum Code { private final String description; private final int httpStatus; - Code(Reason reason) { + ErrorInfo(Reason reason) { this(reason.retryable(), reason.description(), reason.httpStatus()); } - Code(boolean retryable, String description, int httpStatus) { + ErrorInfo(boolean retryable, String description, int httpStatus) { this.retryable = retryable; this.description = description; this.httpStatus = httpStatus; @@ -89,9 +90,9 @@ DatastoreException translate(DatastoreRpcException exception, String message) { } static { - ImmutableMap.Builder builder = ImmutableMap.builder(); - Map httpCodes = new HashMap<>(); - for (Code code : Code.values()) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + Map httpCodes = new HashMap<>(); + for (ErrorInfo code : ErrorInfo.values()) { builder.put(code.name(), code); httpCodes.put(code.httpStatus(), code); } @@ -99,20 +100,21 @@ DatastoreException translate(DatastoreRpcException exception, String message) { HTTP_TO_CODE = ImmutableMap.copyOf(httpCodes); } - public DatastoreException(Code code, String message, Exception cause) { - super(MoreObjects.firstNonNull(message, code.description), cause); - this.code = code; + public DatastoreException(ErrorInfo errorInfo, String message, Exception cause) { + super(errorInfo.httpStatus(), MoreObjects.firstNonNull(message, errorInfo.description), + errorInfo.retryable(), cause); + this.errorInfo = errorInfo; } - public DatastoreException(Code code, String message) { - this(code, message, null); + public DatastoreException(ErrorInfo errorInfo, String message) { + this(errorInfo, message, null); } /** * Returns the code associated with this exception. */ - public Code code() { - return code; + public ErrorInfo errorInfo() { + return errorInfo; } static DatastoreException translateAndThrow(RetryHelperException ex) { @@ -122,7 +124,7 @@ static DatastoreException translateAndThrow(RetryHelperException ex) { if (ex instanceof RetryHelper.RetryInterruptedException) { RetryHelper.RetryInterruptedException.propagate(); } - throw new DatastoreException(Code.UNKNOWN, ex.getMessage(), ex); + throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex); } /** @@ -133,9 +135,9 @@ static DatastoreException translateAndThrow(RetryHelperException ex) { */ static DatastoreException translateAndThrow(DatastoreRpcException exception) { String message = exception.getMessage(); - Code code = REASON_TO_CODE.get(exception.reason()); + ErrorInfo code = REASON_TO_CODE.get(exception.reason()); if (code == null) { - code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), Code.UNKNOWN); + code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), ErrorInfo.UNKNOWN); } throw code.translate(exception, message); } @@ -147,10 +149,10 @@ static DatastoreException translateAndThrow(DatastoreRpcException exception) { * @throws DatastoreException every time */ static DatastoreException throwInvalidRequest(String massage, Object... params) { - throw new DatastoreException(Code.FAILED_PRECONDITION, String.format(massage, params)); + throw new DatastoreException(ErrorInfo.FAILED_PRECONDITION, String.format(massage, params)); } static DatastoreException propagateUserException(Exception ex) { - throw new DatastoreException(Code.UNKNOWN, ex.getMessage(), ex); + throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java index a64a3531c19d..20c2f742579a 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import com.google.gcloud.datastore.DatastoreException.Code; +import com.google.gcloud.datastore.DatastoreException.ErrorInfo; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; @@ -30,14 +30,14 @@ public class DatastoreExceptionTest { @Test public void testCode() throws Exception { for (Reason reason : Reason.values()) { - Code code = Code.valueOf(reason.name()); + ErrorInfo code = ErrorInfo.valueOf(reason.name()); assertEquals(reason.retryable(), code.retryable()); assertEquals(reason.description(), code.description()); assertEquals(reason.httpStatus(), code.httpStatus()); } - DatastoreException exception = new DatastoreException(Code.ABORTED, "bla"); - assertEquals(Code.ABORTED, exception.code()); + DatastoreException exception = new DatastoreException(ErrorInfo.ABORTED, "bla"); + assertEquals(ErrorInfo.ABORTED, exception.errorInfo()); } @Test @@ -47,7 +47,7 @@ public void testTranslateAndThrow() throws Exception { DatastoreException.translateAndThrow(new DatastoreRpcException(reason)); fail("Exception expected"); } catch (DatastoreException ex) { - assertEquals(reason.name(), ex.code().name()); + assertEquals(reason.name(), ex.errorInfo().name()); } } } @@ -58,7 +58,7 @@ public void testThrowInvalidRequest() throws Exception { DatastoreException.throwInvalidRequest("message %s %d", "a", 1); fail("Exception expected"); } catch (DatastoreException ex) { - assertEquals(Code.FAILED_PRECONDITION, ex.code()); + assertEquals(ErrorInfo.FAILED_PRECONDITION, ex.errorInfo()); assertEquals("message a 1", ex.getMessage()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index f639ca3fdac0..9b42c99b0da1 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -197,7 +197,7 @@ public void testTransactionWithRead() { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { - assertEquals(DatastoreException.Code.ABORTED, expected.code()); + assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo()); } } @@ -225,7 +225,7 @@ public void testTransactionWithQuery() { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { - assertEquals(DatastoreException.Code.ABORTED, expected.code()); + assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo()); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java index e354e3a6d427..c1075ae28c8b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java @@ -16,8 +16,10 @@ package com.google.gcloud.storage; +import com.google.gcloud.BaseServiceException; import com.google.gcloud.RetryHelper; import com.google.gcloud.RetryHelper.RetryHelperException; +import com.google.gcloud.RetryHelper.RetryInterruptedException; /** * Storage service exception. @@ -25,29 +27,13 @@ * @see Google Cloud * Storage error codes */ -public class StorageException extends RuntimeException { +public class StorageException extends BaseServiceException { - private static final long serialVersionUID = -3748432005065428084L; + private static final long serialVersionUID = 8088235105953640145L; private static final int UNKNOWN_CODE = -1; - private final int code; - private final boolean retryable; - public StorageException(int code, String message, boolean retryable) { - super(message); - this.code = code; - this.retryable = retryable; - } - - /** - * Returns the code associated with this exception. - */ - public int code() { - return code; - } - - public boolean retryable() { - return retryable; + super(code, message, retryable); } /** From b57a13d6d0e44a7bb5374bf9950551d5e4d5a23b Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 4 Nov 2015 19:06:58 -0800 Subject: [PATCH 2/7] Rename enum, fix docs, and make accessors package private --- .../google/gcloud/BaseServiceException.java | 5 +- .../gcloud/datastore/DatastoreException.java | 75 +++++++++---------- .../datastore/DatastoreExceptionTest.java | 20 ++--- .../gcloud/datastore/DatastoreTest.java | 4 +- 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java index 45c047d4710a..cd0933426756 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -17,7 +17,7 @@ package com.google.gcloud; /** - * Base service exception. + * Base class for all service exceptions. */ public class BaseServiceException extends RuntimeException { @@ -45,6 +45,9 @@ public int code() { return code; } + /** + * Returns {@code true} when it is safe to retry the operation that caused this exception. + */ public boolean retryable() { return retryable; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java index ecbdd57b6bfb..dded1d11875e 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java @@ -30,18 +30,18 @@ public class DatastoreException extends BaseServiceException { private static final long serialVersionUID = -2336749234060754893L; - private static final ImmutableMap REASON_TO_CODE; - private static final ImmutableMap HTTP_TO_CODE; + private static final ImmutableMap REASON_TO_ERROR; + private static final ImmutableMap HTTP_TO_ERROR; - private final ErrorInfo errorInfo; + private final DatastoreError error; /** - * Represent metadata about {@link DatastoreException}s. + * Represents Datastore errors. * * @see Google Cloud * Datastore error codes */ - public enum ErrorInfo { + public enum DatastoreError { ABORTED(Reason.ABORTED), DEADLINE_EXCEEDED(Reason.DEADLINE_EXCEEDED), @@ -58,29 +58,25 @@ public enum ErrorInfo { private final String description; private final int httpStatus; - ErrorInfo(Reason reason) { + DatastoreError(Reason reason) { this(reason.retryable(), reason.description(), reason.httpStatus()); } - ErrorInfo(boolean retryable, String description, int httpStatus) { + DatastoreError(boolean retryable, String description, int httpStatus) { this.retryable = retryable; this.description = description; this.httpStatus = httpStatus; } - public String description() { + String description() { return description; } - public int httpStatus() { + int httpStatus() { return httpStatus; } - /** - * Returns {@code true} if this exception is transient and the same request could be retried. - * For any retry it is highly recommended to apply an exponential backoff. - */ - public boolean retryable() { + boolean retryable() { return retryable; } @@ -90,31 +86,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) { } static { - ImmutableMap.Builder builder = ImmutableMap.builder(); - Map httpCodes = new HashMap<>(); - for (ErrorInfo code : ErrorInfo.values()) { - builder.put(code.name(), code); - httpCodes.put(code.httpStatus(), code); + ImmutableMap.Builder builder = ImmutableMap.builder(); + Map httpCodes = new HashMap<>(); + for (DatastoreError error : DatastoreError.values()) { + builder.put(error.name(), error); + httpCodes.put(error.httpStatus(), error); } - REASON_TO_CODE = builder.build(); - HTTP_TO_CODE = ImmutableMap.copyOf(httpCodes); + REASON_TO_ERROR = builder.build(); + HTTP_TO_ERROR = ImmutableMap.copyOf(httpCodes); } - public DatastoreException(ErrorInfo errorInfo, String message, Exception cause) { - super(errorInfo.httpStatus(), MoreObjects.firstNonNull(message, errorInfo.description), - errorInfo.retryable(), cause); - this.errorInfo = errorInfo; + public DatastoreException(DatastoreError error, String message, Exception cause) { + super(error.httpStatus(), MoreObjects.firstNonNull(message, error.description), + error.retryable(), cause); + this.error = error; } - public DatastoreException(ErrorInfo errorInfo, String message) { - this(errorInfo, message, null); + public DatastoreException(DatastoreError error, String message) { + this(error, message, null); } /** - * Returns the code associated with this exception. + * Returns the DatastoreError associated with this exception. */ - public ErrorInfo errorInfo() { - return errorInfo; + public DatastoreError datastoreError() { + return error; } static DatastoreException translateAndThrow(RetryHelperException ex) { @@ -124,35 +120,36 @@ static DatastoreException translateAndThrow(RetryHelperException ex) { if (ex instanceof RetryHelper.RetryInterruptedException) { RetryHelper.RetryInterruptedException.propagate(); } - throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex); + throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex); } /** - * Translate DatastoreException to DatastoreException based on their + * Translate DatastoreRpcExceptions to DatastoreExceptions based on their * HTTP error codes. This method will always throw a new DatastoreException. * * @throws DatastoreException every time */ static DatastoreException translateAndThrow(DatastoreRpcException exception) { String message = exception.getMessage(); - ErrorInfo code = REASON_TO_CODE.get(exception.reason()); - if (code == null) { - code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), ErrorInfo.UNKNOWN); + DatastoreError error = REASON_TO_ERROR.get(exception.reason()); + if (error == null) { + error = MoreObjects.firstNonNull( + HTTP_TO_ERROR.get(exception.httpStatus()), DatastoreError.UNKNOWN); } - throw code.translate(exception, message); + throw error.translate(exception, message); } /** - * Throw a DatastoreException with {@code FAILED_PRECONDITION} code and the {@code message} + * Throw a DatastoreException with {@code FAILED_PRECONDITION} error and the {@code message} * in a nested exception. * * @throws DatastoreException every time */ static DatastoreException throwInvalidRequest(String massage, Object... params) { - throw new DatastoreException(ErrorInfo.FAILED_PRECONDITION, String.format(massage, params)); + throw new DatastoreException(DatastoreError.FAILED_PRECONDITION, String.format(massage, params)); } static DatastoreException propagateUserException(Exception ex) { - throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex); + throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java index 20c2f742579a..9ad836b15a4e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import com.google.gcloud.datastore.DatastoreException.ErrorInfo; +import com.google.gcloud.datastore.DatastoreException.DatastoreError; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; @@ -28,16 +28,16 @@ public class DatastoreExceptionTest { @Test - public void testCode() throws Exception { + public void testDatastoreError() throws Exception { for (Reason reason : Reason.values()) { - ErrorInfo code = ErrorInfo.valueOf(reason.name()); - assertEquals(reason.retryable(), code.retryable()); - assertEquals(reason.description(), code.description()); - assertEquals(reason.httpStatus(), code.httpStatus()); + DatastoreError error = DatastoreError.valueOf(reason.name()); + assertEquals(reason.retryable(), error.retryable()); + assertEquals(reason.description(), error.description()); + assertEquals(reason.httpStatus(), error.httpStatus()); } - DatastoreException exception = new DatastoreException(ErrorInfo.ABORTED, "bla"); - assertEquals(ErrorInfo.ABORTED, exception.errorInfo()); + DatastoreException exception = new DatastoreException(DatastoreError.ABORTED, "bla"); + assertEquals(DatastoreError.ABORTED, exception.datastoreError()); } @Test @@ -47,7 +47,7 @@ public void testTranslateAndThrow() throws Exception { DatastoreException.translateAndThrow(new DatastoreRpcException(reason)); fail("Exception expected"); } catch (DatastoreException ex) { - assertEquals(reason.name(), ex.errorInfo().name()); + assertEquals(reason.name(), ex.datastoreError().name()); } } } @@ -58,7 +58,7 @@ public void testThrowInvalidRequest() throws Exception { DatastoreException.throwInvalidRequest("message %s %d", "a", 1); fail("Exception expected"); } catch (DatastoreException ex) { - assertEquals(ErrorInfo.FAILED_PRECONDITION, ex.errorInfo()); + assertEquals(DatastoreError.FAILED_PRECONDITION, ex.datastoreError()); assertEquals("message a 1", ex.getMessage()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 9b42c99b0da1..6afd40927264 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -197,7 +197,7 @@ public void testTransactionWithRead() { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { - assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo()); + assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError()); } } @@ -225,7 +225,7 @@ public void testTransactionWithQuery() { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { - assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo()); + assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError()); } } From 73d379ba5afd65fa0a112093f48eace1e5acb931 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 5 Nov 2015 16:56:16 +0100 Subject: [PATCH 3/7] Add sbt/gradle dependencies to READMEs, change update_docs_version.sh --- README.md | 10 +++++++++- gcloud-java-core/README.md | 10 +++++++++- gcloud-java-datastore/README.md | 10 +++++++++- gcloud-java-examples/README.md | 10 +++++++++- gcloud-java-storage/README.md | 10 +++++++++- gcloud-java/README.md | 10 +++++++++- utilities/update_docs_version.sh | 2 ++ 7 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1b9867fd198f..85a3ef7993f8 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This client supports the following Google Cloud Platform services: Quickstart ---------- -Add this to your pom.xml file +If you are using Maven, add this to your pom.xml file ```xml com.google.gcloud @@ -28,6 +28,14 @@ Add this to your pom.xml file 0.0.10 ``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.gcloud:gcloud-java:jar:0.0.10' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.0.10" +``` Example Applications -------------------- diff --git a/gcloud-java-core/README.md b/gcloud-java-core/README.md index 2a3be300f4ac..032127540d19 100644 --- a/gcloud-java-core/README.md +++ b/gcloud-java-core/README.md @@ -12,7 +12,7 @@ This module provides common functionality required by service-specific modules o Quickstart ---------- -Add this to your pom.xml file +If you are using Maven, add this to your pom.xml file ```xml com.google.gcloud @@ -20,6 +20,14 @@ Add this to your pom.xml file 0.0.10 ``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.gcloud:gcloud-java-core:jar:0.0.10' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.10" +``` Java Versions ------------- diff --git a/gcloud-java-datastore/README.md b/gcloud-java-datastore/README.md index bbcdd9d8857c..8915f2d37a55 100644 --- a/gcloud-java-datastore/README.md +++ b/gcloud-java-datastore/README.md @@ -15,7 +15,7 @@ Java idiomatic client for [Google Cloud Datastore] (https://cloud.google.com/dat Quickstart ---------- -Add this to your pom.xml file +If you are using Maven, add this to your pom.xml file ```xml com.google.gcloud @@ -23,6 +23,14 @@ Add this to your pom.xml file 0.0.10 ``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.gcloud:gcloud-java-datastore:jar:0.0.10' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.0.10" +``` Example Application -------------------- diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 366acd5de929..9afe16a2b1a5 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -12,7 +12,7 @@ Examples for gcloud-java (Java idiomatic client for [Google Cloud Platform][clou Quickstart ---------- -Add this to your pom.xml file +If you are using Maven, add this to your pom.xml file ```xml com.google.gcloud @@ -20,6 +20,14 @@ Add this to your pom.xml file 0.0.10 ``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.gcloud:gcloud-java-examples:jar:0.0.10' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.0.10" +``` To run examples from your command line: diff --git a/gcloud-java-storage/README.md b/gcloud-java-storage/README.md index 717fd1f1f3e4..8722da76cec4 100644 --- a/gcloud-java-storage/README.md +++ b/gcloud-java-storage/README.md @@ -15,7 +15,7 @@ Java idiomatic client for [Google Cloud Storage] (https://cloud.google.com/stora Quickstart ---------- -Add this to your pom.xml file +If you are using Maven, add this to your pom.xml file ```xml com.google.gcloud @@ -23,6 +23,14 @@ Add this to your pom.xml file 0.0.10 ``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.gcloud:gcloud-java-storage:jar:0.0.10' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.0.10" +``` Example Application ------------------- diff --git a/gcloud-java/README.md b/gcloud-java/README.md index 7e2eee84a8c4..baa1e5c53b1c 100644 --- a/gcloud-java/README.md +++ b/gcloud-java/README.md @@ -20,7 +20,7 @@ This client supports the following Google Cloud Platform services: Quickstart ---------- -Add this to your pom.xml file +If you are using Maven, add this to your pom.xml file ```xml com.google.gcloud @@ -28,6 +28,14 @@ Add this to your pom.xml file 0.0.10 ``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.gcloud:gcloud-java:jar:0.0.10' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.0.10" +``` Java Versions ------------- diff --git a/utilities/update_docs_version.sh b/utilities/update_docs_version.sh index d7e7bdbfb830..4b1641a0bd81 100755 --- a/utilities/update_docs_version.sh +++ b/utilities/update_docs_version.sh @@ -14,6 +14,8 @@ if [ "${RELEASED_VERSION##*-}" != "SNAPSHOT" ]; then for item in ${module_folders[*]} do sed -ri "s/[0-9]+\.[0-9]+\.[0-9]+<\/version>/${RELEASED_VERSION}<\/version>/g" ${item}/README.md + sed -ri "s/:[0-9]+\.[0-9]+\.[0-9]+'/:${RELEASED_VERSION}'/g" ${item}/README.md + sed -ri "s/\"[0-9]+\.[0-9]+\.[0-9]+\"/\"${RELEASED_VERSION}\"/g" ${item}/README.md done git add README.md */README.md From 2203b9188f89a1a410b86888d8c5f7369fb22465 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 4 Nov 2015 18:31:22 -0800 Subject: [PATCH 4/7] Run coveralls for PRs as well --- utilities/after_success.sh | 56 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/utilities/after_success.sh b/utilities/after_success.sh index 26405bcd9db3..05ab5fb373d6 100755 --- a/utilities/after_success.sh +++ b/utilities/after_success.sh @@ -7,35 +7,37 @@ source ./utilities/integration_test_env.sh echo "Travis branch: " ${TRAVIS_BRANCH} echo "Travis pull request: " ${TRAVIS_PULL_REQUEST} echo "Travis JDK version: " ${TRAVIS_JDK_VERSION} -if [ "${TRAVIS_JDK_VERSION}" == "oraclejdk7" -a "${TRAVIS_BRANCH}" == "master" -a "${TRAVIS_PULL_REQUEST}" == "false" ]; then - mvn cobertura:cobertura coveralls:report - SITE_VERSION=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -Ev '(^\[|\w+:)') - if [ "${SITE_VERSION##*-}" != "SNAPSHOT" ]; then - # Deploy site if not a SNAPSHOT - git config --global user.name "travis-ci" - git config --global user.email "travis@travis-ci.org" - git clone --branch gh-pages --single-branch https://github.com/GoogleCloudPlatform/gcloud-java/ tmp_gh-pages - mkdir -p tmp_gh-pages/$SITE_VERSION - mvn site -DskipTests=true - mvn site:stage -DtopSiteURL=http://googlecloudplatform.github.io/gcloud-java/site/${SITE_VERSION}/ - cd tmp_gh-pages - cp -r ../target/staging/$SITE_VERSION/* $SITE_VERSION/ - sed -i "s/{{SITE_VERSION}}/$SITE_VERSION/g" ${SITE_VERSION}/index.html # Update "Quickstart with Maven" to reflect version change - git add $SITE_VERSION - echo "" > index.html - git add index.html - echo "" > apidocs/index.html - git add apidocs/index.html - git commit -m "Added a new site for version $SITE_VERSION and updated the root directory's redirect." - git config --global push.default simple - git push --quiet "https://${CI_DEPLOY_USERNAME}:${CI_DEPLOY_PASSWORD}@github.com/GoogleCloudPlatform/gcloud-java.git" > /dev/null 2>&1 +if [ "${TRAVIS_JDK_VERSION}" == "oraclejdk7" -a "${TRAVIS_BRANCH}" == "master" ]; then + mvn clean cobertura:cobertura coveralls:report + if [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then + SITE_VERSION=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -Ev '(^\[|\w+:)') + if [ "${SITE_VERSION##*-}" != "SNAPSHOT" ]; then + # Deploy site if not a SNAPSHOT + git config --global user.name "travis-ci" + git config --global user.email "travis@travis-ci.org" + git clone --branch gh-pages --single-branch https://github.com/GoogleCloudPlatform/gcloud-java/ tmp_gh-pages + mkdir -p tmp_gh-pages/$SITE_VERSION + mvn site -DskipTests=true + mvn site:stage -DtopSiteURL=http://googlecloudplatform.github.io/gcloud-java/site/${SITE_VERSION}/ + cd tmp_gh-pages + cp -r ../target/staging/$SITE_VERSION/* $SITE_VERSION/ + sed -i "s/{{SITE_VERSION}}/$SITE_VERSION/g" ${SITE_VERSION}/index.html # Update "Quickstart with Maven" to reflect version change + git add $SITE_VERSION + echo "" > index.html + git add index.html + echo "" > apidocs/index.html + git add apidocs/index.html + git commit -m "Added a new site for version $SITE_VERSION and updated the root directory's redirect." + git config --global push.default simple + git push --quiet "https://${CI_DEPLOY_USERNAME}:${CI_DEPLOY_PASSWORD}@github.com/GoogleCloudPlatform/gcloud-java.git" > /dev/null 2>&1 - cd .. - utilities/update_docs_version.sh # Update version in READMEs - mvn clean deploy --settings ~/.m2/settings.xml -P sign-deploy - else - mvn clean deploy -DskipTests=true -Dgpg.skip=true --settings ~/.m2/settings.xml + cd .. + utilities/update_docs_version.sh # Update version in READMEs + mvn clean deploy --settings ~/.m2/settings.xml -P sign-deploy + else + mvn clean deploy -DskipTests=true -Dgpg.skip=true --settings ~/.m2/settings.xml + fi fi else echo "Not deploying artifacts. This is only done with non-pull-request commits to master branch with Oracle Java 7 builds." From fa9348edd3316ad230a8c93a2f858f8cb364a6b0 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Thu, 5 Nov 2015 16:19:53 -0800 Subject: [PATCH 5/7] update location where we look for config file when using gcloud SDK to get project ID --- .../java/com/google/gcloud/ServiceOptions.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 1be1f16115ad..898897833287 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -30,6 +30,7 @@ import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; @@ -387,8 +388,18 @@ protected static String googleCloudProjectId() { } else { configDir = new File(System.getProperty("user.home"), ".config/gcloud"); } - try (BufferedReader reader = - new BufferedReader(new FileReader(new File(configDir, "properties")))) { + FileReader fileReader; + try { + fileReader = new FileReader(new File(configDir, "configurations/config_default")); + } catch (FileNotFoundException newConfigFileNotFoundEx) { + try { + fileReader = new FileReader(new File(configDir, "properties")); + } catch (FileNotFoundException oldConfigFileNotFoundEx) { + // return null if we can't find config file + return null; + } + } + try (BufferedReader reader = new BufferedReader(fileReader)) { String line; String section = null; Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$"); From 81744f7ffe7cb838154341018e80fd4b8deabd64 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 5 Nov 2015 15:34:42 +0100 Subject: [PATCH 6/7] Create packages for bigquery and outline spi layer --- gcloud-java-bigquery/README.md | 86 +++++++++++ gcloud-java-bigquery/pom.xml | 50 ++++++ .../com/google/gcloud/bigquery/Bigquery.java | 29 ++++ .../gcloud/bigquery/BigqueryException.java | 54 +++++++ .../gcloud/bigquery/BigqueryFactory.java | 26 ++++ .../gcloud/bigquery/BigqueryOptions.java | 112 ++++++++++++++ .../google/gcloud/bigquery/package-info.java | 27 ++++ .../com/google/gcloud/spi/BigqueryRpc.java | 142 ++++++++++++++++++ .../google/gcloud/spi/BigqueryRpcFactory.java | 26 ++++ 9 files changed, 552 insertions(+) create mode 100644 gcloud-java-bigquery/README.md create mode 100644 gcloud-java-bigquery/pom.xml create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md new file mode 100644 index 000000000000..17ae3a267b0e --- /dev/null +++ b/gcloud-java-bigquery/README.md @@ -0,0 +1,86 @@ +Google Cloud Java Client for BigQuery +==================================== + +Java idiomatic client for [Google Cloud BigQuery] (https://cloud.google.com/bigquery). + +[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java) +[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master) + + +- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/) ++ + +> Note: This client is a work-in-progress, and may occasionally +> make backwards-incompatible changes. + +Quickstart +---------- +Add this to your pom.xml file + + + +Example Application +------------------- + + + +Authentication +-------------- + +See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README. + +About Google Cloud BigQuery +-------------------------- + +[Google Cloud BigQuery][cloud-bigquery] is a fully managed, NoOps, low cost data analytics service. +Data can be streamed into BigQuery at millions of rows per second to enable real-time analysis. +With BigQuery you can easily deploy Petabyte-scale Databases. + +Be sure to activate the Google Cloud BigQuery API on the Developer's Console to use BigQuery from your project. + +See the ``gcloud-java`` API [bigquery documentation][bigquery-api] to learn how to interact +with Google Cloud BigQuery using this Client Library. + +Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must [supply credentials](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) and a project ID if running this snippet elsewhere. + + + +Java Versions +------------- + +Java 7 or above is required for using this client. + +Testing +------- + + + +Versioning +---------- + +This library follows [Semantic Versioning] (http://semver.org/). + +It is currently in major version zero (``0.y.z``), which means that anything +may change at any time and the public API should not be considered +stable. + +Contributing +------------ + +Contributions to this library are always welcome and highly encouraged. + +See [CONTRIBUTING] for more information on how to get started. + +License +------- + +Apache 2.0 - See [LICENSE] for more information. + + +[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md +[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE +[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-bigquery +[cloud-platform]: https://cloud.google.com/ + +[cloud-bigquery]: https://cloud.google.com/bigquery/ +[bigquery-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/bigquery/package-summary.html \ No newline at end of file diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml new file mode 100644 index 000000000000..f4d459ab82f7 --- /dev/null +++ b/gcloud-java-bigquery/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + com.google.gcloud + gcloud-java-bigquery + jar + GCloud Java bigquery + + Java idiomatic client for Google Cloud BigQuery. + + + com.google.gcloud + gcloud-java-pom + 0.0.11-SNAPSHOT + + + gcloud-java-bigquery + + + + ${project.groupId} + gcloud-java-core + ${project.version} + + + com.google.apis + google-api-services-bigquery + v2-rev244-1.20.0 + compile + + + com.google.guava + guava-jdk5 + + + + + junit + junit + 4.12 + test + + + org.easymock + easymock + 3.3 + test + + + diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java new file mode 100644 index 000000000000..558e18a5e3e4 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java @@ -0,0 +1,29 @@ +/* + * 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.bigquery; + +import com.google.gcloud.Service; + +/** + * An interface for Google Cloud BigQuery. + * + * @see Google Cloud BigQuery + */ +public interface Bigquery extends Service { + + // TODO(mziccard) add missing methods +} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java new file mode 100644 index 000000000000..fbfb895da6a3 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java @@ -0,0 +1,54 @@ +/* + * 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.bigquery; + +import com.google.gcloud.BaseServiceException; +import com.google.gcloud.RetryHelper.RetryHelperException; +import com.google.gcloud.RetryHelper.RetryInterruptedException; + +/** + * BigQuery service exception. + * + * @see Google Cloud + * BigQuery error codes + */ +public class BigqueryException extends BaseServiceException { + + private static final long serialVersionUID = -5504832700512784654L; + public static final int UNKNOWN_CODE = -1; + + public BigqueryException(int code, String message, boolean retryable) { + super(code, message, retryable); + } + + /** + * Translate RetryHelperException to the BigqueryException that caused the error. This method will + * always throw an exception. + * + * @throws BigqueryException when {@code ex} was caused by a {@code BigqueryException} + * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} + */ + static BigqueryException translateAndThrow(RetryHelperException ex) { + if (ex.getCause() instanceof BigqueryException) { + throw (BigqueryException) ex.getCause(); + } + if (ex instanceof RetryInterruptedException) { + RetryInterruptedException.propagate(); + } + throw new BigqueryException(UNKNOWN_CODE, ex.getMessage(), false); + } +} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java new file mode 100644 index 000000000000..04454052edec --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java @@ -0,0 +1,26 @@ +/* + * 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.bigquery; + + +import com.google.gcloud.ServiceFactory; + +/** + * An interface for BigQuery factories. + */ +public interface BigqueryFactory extends ServiceFactory { +} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java new file mode 100644 index 000000000000..74b6dba6c3c8 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java @@ -0,0 +1,112 @@ +/* + * 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.bigquery; + +import com.google.common.collect.ImmutableSet; +import com.google.gcloud.ServiceOptions; +import com.google.gcloud.spi.DefaultBigqueryRpc; +import com.google.gcloud.spi.BigqueryRpc; +import com.google.gcloud.spi.BigqueryRpcFactory; + +import java.util.Set; + +public class BigqueryOptions extends ServiceOptions { + + private static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"; + private static final Set SCOPES = ImmutableSet.of(BIGQUERY_SCOPE); + private static final long serialVersionUID = -215981591481708043L; + + public static class DefaultBigqueryFactory implements BigqueryFactory { + + private static final BigqueryFactory INSTANCE = new DefaultBigqueryFactory(); + + @Override + public Bigquery create(BigqueryOptions options) { + // TODO(mziccard) return new BigqueryImpl(options); + return null; + } + } + + public static class DefaultBigqueryRpcFactory implements BigqueryRpcFactory { + + private static final BigqueryRpcFactory INSTANCE = new DefaultBigqueryRpcFactory(); + + @Override + public BigqueryRpc create(BigqueryOptions options) { + // TODO(mziccard) return new DefaultBigqueryRpc(options); + return null; + } + } + + public static class Builder extends + ServiceOptions.Builder { + + private Builder() { + } + + private Builder(BigqueryOptions options) { + super(options); + } + + @Override + public BigqueryOptions build() { + return new BigqueryOptions(this); + } + } + + private BigqueryOptions(Builder builder) { + super(BigqueryFactory.class, BigqueryRpcFactory.class, builder); + } + + @Override + protected BigqueryFactory defaultServiceFactory() { + return DefaultBigqueryFactory.INSTANCE; + } + + @Override + protected BigqueryRpcFactory defaultRpcFactory() { + return DefaultBigqueryRpcFactory.INSTANCE; + } + + @Override + protected Set scopes() { + return SCOPES; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public int hashCode() { + return baseHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BigqueryOptions)) { + return false; + } + BigqueryOptions other = (BigqueryOptions) obj; + return baseEquals(other); + } + + public static Builder builder() { + return new Builder(); + } +} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java new file mode 100644 index 000000000000..4acaa40ca851 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java @@ -0,0 +1,27 @@ +/* + * 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. + */ + +/** + * A client to Google Cloud BigQuery. + * + *

A simple usage example: + *

{@code
+ * //TODO(mziccard): add code example
+ * }
+ * + * @see Google Cloud BigQuery + */ +package com.google.gcloud.bigquery; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java new file mode 100644 index 000000000000..9e4e486422d6 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java @@ -0,0 +1,142 @@ +/* + * 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.bigquery.model.Dataset; +import com.google.api.services.bigquery.model.DatasetReference; +import com.google.api.services.bigquery.model.GetQueryResultsResponse; +import com.google.api.services.bigquery.model.Job; +import com.google.api.services.bigquery.model.JobReference; +import com.google.api.services.bigquery.model.QueryRequest; +import com.google.api.services.bigquery.model.QueryResponse; +import com.google.api.services.bigquery.model.Table; +import com.google.api.services.bigquery.model.TableDataInsertAllRequest; +import com.google.api.services.bigquery.model.TableDataInsertAllResponse; +import com.google.api.services.bigquery.model.TableReference; +import com.google.api.services.bigquery.model.TableRow; +import com.google.gcloud.bigquery.BigqueryException; + +import java.util.Map; + +public interface BigqueryRpc { + + // These options are part of the Google Cloud BigQuery query parameters + enum Option { + QUOTA_USER("quotaUser"), + USER_IP("userIp"), + FIELDS("fields"), + DELETE_CONTENTS("deleteContents"), + ALL_DATASETS("all"), + ALL_USERS("allUsers"), + MAX_RESULTS("maxResults"), + PAGE_TOKEN("pageToken"), + START_INDEX("startIndex"), + STATE_FILTER("stateFilter"), + TIMEOUT("timeOut"); + + private final String value; + + Option(String value) { + this.value = value; + } + + public String value() { + return value; + } + + @SuppressWarnings("unchecked") + T get(Map options) { + return (T) options.get(this); + } + + String getString(Map options) { + return get(options); + } + + Long getLong(Map options) { + return get(options); + } + + Boolean getBoolean(Map options) { + return get(options); + } + } + + class Tuple { + + private final X x; + private final Y y; + + private Tuple(X x, Y y) { + this.x = x; + this.y = y; + } + + public static Tuple of(X x, Y y) { + return new Tuple<>(x, y); + } + + public X x() { + return x; + } + + public Y y() { + return y; + } + } + + Dataset getDataset(String datasetId, Map options) throws BigqueryException; + + Tuple> listDatasets(Map options) throws BigqueryException; + + Dataset create(Dataset dataset, Map options) throws BigqueryException; + + boolean deleteDataset(String datasetId, Map options) throws BigqueryException; + + Dataset patch(Dataset dataset, Map options) throws BigqueryException; + + Table getTable(String datasetId, String tableId, Map options) throws BigqueryException; + + Tuple> listTables(String dataset, Map options) + throws BigqueryException; + + Table create(String dataset, Table table, Map options) throws BigqueryException; + + boolean deleteTable(String datasetId, String tableId, Map options) + throws BigqueryException; + + Table patch(Table table, Map options) throws BigqueryException; + + TableDataInsertAllResponse insertAll(TableReference table, TableDataInsertAllRequest request, + Map options) throws BigqueryException; + + Tuple> listTableData(String datasetId, String tableId, + Map options) throws BigqueryException; + + Job getJob(String jobId, Map options) throws BigqueryException; + + Tuple> listJobs(Map options) throws BigqueryException; + + Job create(Job job, Map options) throws BigqueryException; + + boolean cancel(String jobId, Map options) throws BigqueryException; + + GetQueryResultsResponse getQueryResults(JobReference job, Map options) + throws BigqueryException; + + QueryResponse query(QueryRequest request, Map options) throws BigqueryException; +} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java new file mode 100644 index 000000000000..09c2d836127f --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java @@ -0,0 +1,26 @@ +/* + * 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.gcloud.bigquery.BigqueryOptions; + +/** + * An interface for BigQuery RPC factory. + * Implementation will be loaded via {@link java.util.ServiceLoader}. + */ +public interface BigqueryRpcFactory extends ServiceRpcFactory { +} From eeae71f5f1dfd9bef1e716ccec0d3f4ca0e1597b Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 9 Nov 2015 16:17:22 +0100 Subject: [PATCH 7/7] Rename Bigquery to BigQuery across bigquery module --- .../bigquery/{Bigquery.java => BigQuery.java} | 2 +- ...yException.java => BigQueryException.java} | 16 +++---- ...queryFactory.java => BigQueryFactory.java} | 2 +- ...queryOptions.java => BigQueryOptions.java} | 42 +++++++++--------- .../{BigqueryRpc.java => BigQueryRpc.java} | 43 +++++++++---------- ...pcFactory.java => BigQueryRpcFactory.java} | 4 +- 6 files changed, 54 insertions(+), 55 deletions(-) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{Bigquery.java => BigQuery.java} (93%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{BigqueryException.java => BigQueryException.java} (74%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{BigqueryFactory.java => BigQueryFactory.java} (89%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{BigqueryOptions.java => BigQueryOptions.java} (61%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/{BigqueryRpc.java => BigQueryRpc.java} (80%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/{BigqueryRpcFactory.java => BigQueryRpcFactory.java} (84%) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java similarity index 93% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 558e18a5e3e4..28fb33dcc58c 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Bigquery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -23,7 +23,7 @@ * * @see Google Cloud BigQuery */ -public interface Bigquery extends Service { +public interface BigQuery extends Service { // TODO(mziccard) add missing methods } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java similarity index 74% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java index fbfb895da6a3..020917762fa3 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryException.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java @@ -26,29 +26,29 @@ * @see Google Cloud * BigQuery error codes */ -public class BigqueryException extends BaseServiceException { +public class BigQueryException extends BaseServiceException { private static final long serialVersionUID = -5504832700512784654L; public static final int UNKNOWN_CODE = -1; - public BigqueryException(int code, String message, boolean retryable) { + public BigQueryException(int code, String message, boolean retryable) { super(code, message, retryable); } /** - * Translate RetryHelperException to the BigqueryException that caused the error. This method will + * Translate RetryHelperException to the BigQueryException that caused the error. This method will * always throw an exception. * - * @throws BigqueryException when {@code ex} was caused by a {@code BigqueryException} + * @throws BigQueryException when {@code ex} was caused by a {@code BigQueryException} * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ - static BigqueryException translateAndThrow(RetryHelperException ex) { - if (ex.getCause() instanceof BigqueryException) { - throw (BigqueryException) ex.getCause(); + static BigQueryException translateAndThrow(RetryHelperException ex) { + if (ex.getCause() instanceof BigQueryException) { + throw (BigQueryException) ex.getCause(); } if (ex instanceof RetryInterruptedException) { RetryInterruptedException.propagate(); } - throw new BigqueryException(UNKNOWN_CODE, ex.getMessage(), false); + throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), false); } } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryFactory.java similarity index 89% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryFactory.java index 04454052edec..2fc98125f4be 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryFactory.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryFactory.java @@ -22,5 +22,5 @@ /** * An interface for BigQuery factories. */ -public interface BigqueryFactory extends ServiceFactory { +public interface BigQueryFactory extends ServiceFactory { } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java similarity index 61% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java index 74b6dba6c3c8..59a4b3229f68 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigqueryOptions.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java @@ -18,68 +18,68 @@ import com.google.common.collect.ImmutableSet; import com.google.gcloud.ServiceOptions; -import com.google.gcloud.spi.DefaultBigqueryRpc; -import com.google.gcloud.spi.BigqueryRpc; -import com.google.gcloud.spi.BigqueryRpcFactory; +import com.google.gcloud.spi.DefaultBigQueryRpc; +import com.google.gcloud.spi.BigQueryRpc; +import com.google.gcloud.spi.BigQueryRpcFactory; import java.util.Set; -public class BigqueryOptions extends ServiceOptions { +public class BigQueryOptions extends ServiceOptions { private static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"; private static final Set SCOPES = ImmutableSet.of(BIGQUERY_SCOPE); private static final long serialVersionUID = -215981591481708043L; - public static class DefaultBigqueryFactory implements BigqueryFactory { + public static class DefaultBigqueryFactory implements BigQueryFactory { - private static final BigqueryFactory INSTANCE = new DefaultBigqueryFactory(); + private static final BigQueryFactory INSTANCE = new DefaultBigqueryFactory(); @Override - public Bigquery create(BigqueryOptions options) { + public BigQuery create(BigQueryOptions options) { // TODO(mziccard) return new BigqueryImpl(options); return null; } } - public static class DefaultBigqueryRpcFactory implements BigqueryRpcFactory { + public static class DefaultBigQueryRpcFactory implements BigQueryRpcFactory { - private static final BigqueryRpcFactory INSTANCE = new DefaultBigqueryRpcFactory(); + private static final BigQueryRpcFactory INSTANCE = new DefaultBigQueryRpcFactory(); @Override - public BigqueryRpc create(BigqueryOptions options) { + public BigQueryRpc create(BigQueryOptions options) { // TODO(mziccard) return new DefaultBigqueryRpc(options); return null; } } public static class Builder extends - ServiceOptions.Builder { + ServiceOptions.Builder { private Builder() { } - private Builder(BigqueryOptions options) { + private Builder(BigQueryOptions options) { super(options); } @Override - public BigqueryOptions build() { - return new BigqueryOptions(this); + public BigQueryOptions build() { + return new BigQueryOptions(this); } } - private BigqueryOptions(Builder builder) { - super(BigqueryFactory.class, BigqueryRpcFactory.class, builder); + private BigQueryOptions(Builder builder) { + super(BigQueryFactory.class, BigQueryRpcFactory.class, builder); } @Override - protected BigqueryFactory defaultServiceFactory() { + protected BigQueryFactory defaultServiceFactory() { return DefaultBigqueryFactory.INSTANCE; } @Override - protected BigqueryRpcFactory defaultRpcFactory() { - return DefaultBigqueryRpcFactory.INSTANCE; + protected BigQueryRpcFactory defaultRpcFactory() { + return DefaultBigQueryRpcFactory.INSTANCE; } @Override @@ -99,10 +99,10 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (!(obj instanceof BigqueryOptions)) { + if (!(obj instanceof BigQueryOptions)) { return false; } - BigqueryOptions other = (BigqueryOptions) obj; + BigQueryOptions other = (BigQueryOptions) obj; return baseEquals(other); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java similarity index 80% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java index 9e4e486422d6..7cce35ab3eb9 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java @@ -17,7 +17,6 @@ package com.google.gcloud.spi; import com.google.api.services.bigquery.model.Dataset; -import com.google.api.services.bigquery.model.DatasetReference; import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.Job; import com.google.api.services.bigquery.model.JobReference; @@ -28,11 +27,11 @@ import com.google.api.services.bigquery.model.TableDataInsertAllResponse; import com.google.api.services.bigquery.model.TableReference; import com.google.api.services.bigquery.model.TableRow; -import com.google.gcloud.bigquery.BigqueryException; +import com.google.gcloud.bigquery.BigQueryException; import java.util.Map; -public interface BigqueryRpc { +public interface BigQueryRpc { // These options are part of the Google Cloud BigQuery query parameters enum Option { @@ -46,7 +45,7 @@ enum Option { PAGE_TOKEN("pageToken"), START_INDEX("startIndex"), STATE_FILTER("stateFilter"), - TIMEOUT("timeOut"); + TIMEOUT("timeoutMs"); private final String value; @@ -99,44 +98,44 @@ public Y y() { } } - Dataset getDataset(String datasetId, Map options) throws BigqueryException; + Dataset getDataset(String datasetId, Map options) throws BigQueryException; - Tuple> listDatasets(Map options) throws BigqueryException; + Tuple> listDatasets(Map options) throws BigQueryException; - Dataset create(Dataset dataset, Map options) throws BigqueryException; + Dataset create(Dataset dataset, Map options) throws BigQueryException; - boolean deleteDataset(String datasetId, Map options) throws BigqueryException; + boolean deleteDataset(String datasetId, Map options) throws BigQueryException; - Dataset patch(Dataset dataset, Map options) throws BigqueryException; + Dataset patch(Dataset dataset, Map options) throws BigQueryException; - Table getTable(String datasetId, String tableId, Map options) throws BigqueryException; + Table getTable(String datasetId, String tableId, Map options) throws BigQueryException; Tuple> listTables(String dataset, Map options) - throws BigqueryException; + throws BigQueryException; - Table create(String dataset, Table table, Map options) throws BigqueryException; + Table create(Table table, Map options) throws BigQueryException; boolean deleteTable(String datasetId, String tableId, Map options) - throws BigqueryException; + throws BigQueryException; - Table patch(Table table, Map options) throws BigqueryException; + Table patch(Table table, Map options) throws BigQueryException; TableDataInsertAllResponse insertAll(TableReference table, TableDataInsertAllRequest request, - Map options) throws BigqueryException; + Map options) throws BigQueryException; Tuple> listTableData(String datasetId, String tableId, - Map options) throws BigqueryException; + Map options) throws BigQueryException; - Job getJob(String jobId, Map options) throws BigqueryException; + Job getJob(String jobId, Map options) throws BigQueryException; - Tuple> listJobs(Map options) throws BigqueryException; + Tuple> listJobs(Map options) throws BigQueryException; - Job create(Job job, Map options) throws BigqueryException; + Job create(Job job, Map options) throws BigQueryException; - boolean cancel(String jobId, Map options) throws BigqueryException; + boolean cancel(String jobId, Map options) throws BigQueryException; GetQueryResultsResponse getQueryResults(JobReference job, Map options) - throws BigqueryException; + throws BigQueryException; - QueryResponse query(QueryRequest request, Map options) throws BigqueryException; + QueryResponse query(QueryRequest request, Map options) throws BigQueryException; } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpcFactory.java similarity index 84% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpcFactory.java index 09c2d836127f..2706868756a5 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigqueryRpcFactory.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpcFactory.java @@ -16,11 +16,11 @@ package com.google.gcloud.spi; -import com.google.gcloud.bigquery.BigqueryOptions; +import com.google.gcloud.bigquery.BigQueryOptions; /** * An interface for BigQuery RPC factory. * Implementation will be loaded via {@link java.util.ServiceLoader}. */ -public interface BigqueryRpcFactory extends ServiceRpcFactory { +public interface BigQueryRpcFactory extends ServiceRpcFactory { }