Skip to content

Commit

Permalink
Merge pull request #574 from GoogleCloudPlatform/master
Browse files Browse the repository at this point in the history
Merge to bring the branch up to speed
  • Loading branch information
mderka committed Jan 22, 2016
2 parents 7493f1a + 3f7626d commit 1c69b05
Show file tree
Hide file tree
Showing 73 changed files with 3,284 additions and 1,782 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static Entity fromPb(Access access) {
}
// Unreachable
throw new BigQueryException(BigQueryException.UNKNOWN_CODE,
"Unrecognized access configuration", false);
"Unrecognized access configuration");
}
}

Expand Down Expand Up @@ -370,22 +370,11 @@ Access toPb() {
}
}

/**
* Build an ACL for an {@code entity} and a {@code role}.
*/
public Acl(Entity entity, Role role) {
private Acl(Entity entity, Role role) {
this.entity = checkNotNull(entity);
this.role = role;
}

/**
* Build an ACL for a view entity.
*/
public Acl(View view) {
this.entity = checkNotNull(view);
this.role = null;
}

/**
* Returns the entity for this ACL.
*/
Expand All @@ -400,6 +389,23 @@ public Role role() {
return role;
}

/**
* Returns an Acl object.
*
* @param entity the entity for this ACL object
* @param role the role to associate to the {@code entity} object
*/
public static Acl of(Entity entity, Role role) {
return new Acl(entity, role);
}

/**
* Returns an Acl object for a view entity.
*/
public static Acl of(View view) {
return new Acl(view, null);
}

@Override
public int hashCode() {
return Objects.hash(entity, role);
Expand Down Expand Up @@ -432,7 +438,7 @@ Access toPb() {
}

static Acl fromPb(Access access) {
return new Acl(Entity.fromPb(access),
return Acl.of(Entity.fromPb(access),
access.getRole() != null ? Role.valueOf(access.getRole()) : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public static QueryResultsOption startIndex(long startIndex) {
/**
* Returns an option that sets how long to wait for the query to complete, in milliseconds,
* before returning. Default is 10 seconds. If the timeout passes before the job completes,
* {@link QueryResponse#jobComplete()} will be {@code false}.
* {@link QueryResponse#jobCompleted()} will be {@code false}.
*/
public static QueryResultsOption maxWaitTime(long maxWaitTime) {
checkArgument(maxWaitTime >= 0);
Expand Down Expand Up @@ -662,4 +662,12 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
* @throws BigQueryException upon failure
*/
QueryResponse getQueryResults(JobId job, QueryResultsOption... options) throws BigQueryException;

/**
* Returns a channel to write data to be inserted into a BigQuery table. Data format and other
* options can be configured using the {@link LoadConfiguration} parameter.
*
* @throws BigQueryException upon failure
*/
TableDataWriteChannel writer(LoadConfiguration loadConfiguration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

package com.google.gcloud.bigquery;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.common.collect.ImmutableSet;
import com.google.gcloud.BaseServiceException;
import com.google.gcloud.RetryHelper.RetryHelperException;
import com.google.gcloud.RetryHelper.RetryInterruptedException;

import java.io.IOException;
import java.util.Set;

/**
* BigQuery service exception.
*
Expand All @@ -28,17 +34,31 @@
*/
public class BigQueryException extends BaseServiceException {

private static final long serialVersionUID = -5504832700512784654L;
public static final int UNKNOWN_CODE = -1;
// see: https://cloud.google.com/bigquery/troubleshooting-errors
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(
new Error(500, null),
new Error(502, null),
new Error(503, null),
new Error(504, null));
private static final long serialVersionUID = -5006625989225438209L;

private final BigQueryError error;

public BigQueryException(int code, String message, boolean retryable) {
this(code, message, retryable, null);
public BigQueryException(int code, String message) {
this(code, message, null);
}

public BigQueryException(int code, String message, boolean retryable, BigQueryError error) {
super(code, message, retryable);
public BigQueryException(int code, String message, BigQueryError error) {
super(code, message, error != null ? error.reason() : null, true);
this.error = error;
}

public BigQueryException(IOException exception) {
super(exception, true);
BigQueryError error = null;
if (reason() != null) {
error = new BigQueryError(reason(), location(), getMessage(), debugInfo());
}
this.error = error;
}

Expand All @@ -50,20 +70,20 @@ public BigQueryError error() {
return error;
}

@Override
protected Set<Error> retryableErrors() {
return RETRYABLE_ERRORS;
}

/**
* 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);
static BaseServiceException translateAndThrow(RetryHelperException ex) {
BaseServiceException.translateAndPropagateIfPossible(ex);
throw new BigQueryException(UNKNOWN_CODE, ex.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gcloud.BaseService;
import com.google.gcloud.ExceptionHandler;
import com.google.gcloud.ExceptionHandler.Interceptor;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.PageImpl.NextPageFetcher;
Expand All @@ -49,27 +47,6 @@

final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery {

private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {

private static final long serialVersionUID = -7478333733015750774L;

@Override
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
return Interceptor.RetryResult.CONTINUE_EVALUATION;
}

@Override
public RetryResult beforeEval(Exception exception) {
if (exception instanceof BigQueryException) {
boolean retriable = ((BigQueryException) exception).retryable();
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
}
return Interceptor.RetryResult.CONTINUE_EVALUATION;
}
};
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();

private static class DatasetPageFetcher implements NextPageFetcher<DatasetInfo> {

private static final long serialVersionUID = -3057564042439021278L;
Expand Down Expand Up @@ -537,10 +514,10 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
QueryResponse.Builder builder = QueryResponse.builder();
JobId completeJobId = JobId.fromPb(results.getJobReference());
builder.jobId(completeJobId);
builder.jobComplete(results.getJobComplete());
builder.jobCompleted(results.getJobComplete());
List<TableRow> rowsPb = results.getRows();
if (results.getJobComplete()) {
builder.jobComplete(true);
builder.jobCompleted(true);
QueryResult.Builder resultBuilder = transformQueryResults(completeJobId, rowsPb,
results.getPageToken(), options(), ImmutableMap.<BigQueryRpc.Option, Object>of());
resultBuilder.totalBytesProcessed(results.getTotalBytesProcessed());
Expand Down Expand Up @@ -584,7 +561,7 @@ public GetQueryResultsResponse call() {
JobId completeJobId = JobId.fromPb(results.getJobReference());
builder.jobId(completeJobId);
builder.etag(results.getEtag());
builder.jobComplete(results.getJobComplete());
builder.jobCompleted(results.getJobComplete());
List<TableRow> rowsPb = results.getRows();
if (results.getJobComplete()) {
QueryResult.Builder resultBuilder = transformQueryResults(completeJobId, rowsPb,
Expand Down Expand Up @@ -619,6 +596,10 @@ private static QueryResult.Builder transformQueryResults(JobId jobId, List<Table
.results(transformTableData(rowsPb));
}

public TableDataWriteChannel writer(LoadConfiguration loadConfiguration) {
return new TableDataWriteChannel(options(), setProjectId(loadConfiguration));
}

private Map<BigQueryRpc.Option, ?> optionMap(Option... options) {
Map<BigQueryRpc.Option, Object> optionMap = Maps.newEnumMap(BigQueryRpc.Option.class);
for (Option option : options) {
Expand All @@ -640,7 +621,7 @@ private DatasetInfo setProjectId(DatasetInfo dataset) {
if (viewReferencePb.getProjectId() == null) {
viewReferencePb.setProjectId(options().projectId());
}
acls.add(new Acl(new Acl.View(TableId.fromPb(viewReferencePb))));
acls.add(Acl.of(new Acl.View(TableId.fromPb(viewReferencePb))));
} else {
acls.add(acl);
}
Expand Down Expand Up @@ -698,8 +679,7 @@ public TableId apply(TableId tableId) {
if (job instanceof LoadJobInfo) {
LoadJobInfo loadJob = (LoadJobInfo) job;
LoadJobInfo.Builder loadBuilder = loadJob.toBuilder();
loadBuilder.destinationTable(setProjectId(loadJob.destinationTable()));
return loadBuilder.build();
return loadBuilder.configuration(setProjectId(loadJob.configuration())).build();
}
return job;
}
Expand All @@ -711,4 +691,10 @@ private QueryRequest setProjectId(QueryRequest request) {
}
return builder.build();
}

private LoadConfiguration setProjectId(LoadConfiguration configuration) {
LoadConfiguration.Builder builder = configuration.toBuilder();
builder.destinationTable(setProjectId(configuration.destinationTable()));
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public Builder destinationTable(TableId destinationTable) {
/**
* Sets whether the job is allowed to create new tables.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.link">
* Jobs: Link Configuration</a>
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition">
* Create Disposition</a>
*/
public Builder createDisposition(CreateDisposition createDisposition) {
this.createDisposition = createDisposition;
Expand All @@ -106,8 +106,8 @@ public Builder createDisposition(CreateDisposition createDisposition) {
/**
* Sets the action that should occur if the destination table already exists.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.link">
* Jobs: Link Configuration</a>
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition">
* Write Disposition</a>
*/
public Builder writeDisposition(WriteDisposition writeDisposition) {
this.writeDisposition = writeDisposition;
Expand Down Expand Up @@ -145,8 +145,8 @@ public TableId destinationTable() {
/**
* Returns whether the job is allowed to create new tables.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy">
* Jobs: Copy Configuration</a>
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition">
* Create Disposition</a>
*/
public CreateDisposition createDisposition() {
return this.createDisposition;
Expand All @@ -155,8 +155,8 @@ public CreateDisposition createDisposition() {
/**
* Returns the action that should occur if the destination table already exists.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy">
* Jobs: Copy Configuration</a>
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition">
* Write Disposition</a>
*/
public WriteDisposition writeDisposition() {
return writeDisposition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.api.services.bigquery.model.Table;
import com.google.common.base.MoreObjects.ToStringHelper;

import java.util.Objects;

/**
* Google BigQuery External Table information. BigQuery's external tables are tables whose data
* reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are
Expand Down Expand Up @@ -103,6 +105,17 @@ ToStringHelper toStringHelper() {
return super.toStringHelper().add("configuration", configuration);
}

@Override
public boolean equals(Object obj) {
return obj instanceof ExternalTableInfo
&& Objects.equals(toPb(), ((ExternalTableInfo) obj).toPb());
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), configuration);
}

@Override
Table toPb() {
Table tablePb = super.toPb();
Expand Down
Loading

0 comments on commit 1c69b05

Please sign in to comment.