Skip to content

Commit

Permalink
Fix minor errors
Browse files Browse the repository at this point in the history
- Add argument checks to bigquery operation options
- Remove retry helper in insertAll
- Fix javadoc errors, add javadoc to operation options
- Better unit tests
- Rename test in OptionTest
- Add bigquery operation options to SerializationTest
- Other minor
  • Loading branch information
mziccard committed Dec 16, 2015
1 parent e1dc3af commit afdc2dd
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.gcloud.bigquery;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
Expand All @@ -26,6 +29,7 @@

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* An interface for Google Cloud BigQuery.
Expand All @@ -34,11 +38,17 @@
*/
public interface BigQuery extends Service<BigQueryOptions> {

/**
* Fields of a BigQuery Dataset resource.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets#resource">Dataset
* Resource</a>
*/
enum DatasetField {
ACCESS("access"),
CREATION_TIME("creationTime"),
DATASET_REFERENCE("datasetReference"),
DEFAULT_TABLE_EXPIRATION_MS("defaultTableLifetime"),
DEFAULT_TABLE_EXPIRATION_MS("defaultTableExpirationMsS"),
DESCRIPTION("description"),
ETAG("etag"),
FRIENDLY_NAME("friendlyName"),
Expand All @@ -58,15 +68,21 @@ public String selector() {
}

static String selector(DatasetField... fields) {
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
fieldStrings.add(DATASET_REFERENCE.selector());
for (DatasetField field : fields) {
fieldStrings.add(field.selector());
}
return com.google.common.base.Joiner.on(',').join(fieldStrings);
return Joiner.on(',').join(fieldStrings);
}
}

/**
* Fields of a BigQuery Table resource.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#resource">Table
* Resource</a>
*/
enum TableField {
CREATION_TIME("creationTime"),
DESCRIPTION("description"),
Expand Down Expand Up @@ -97,16 +113,22 @@ public String selector() {
}

static String selector(TableField... fields) {
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
fieldStrings.add(TABLE_REFERENCE.selector());
fieldStrings.add(TYPE.selector());
for (TableField field : fields) {
fieldStrings.add(field.selector());
}
return com.google.common.base.Joiner.on(',').join(fieldStrings);
return Joiner.on(',').join(fieldStrings);
}
}

/**
* Fields of a BigQuery Job resource.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#resource">Job Resource
* </a>
*/
enum JobField {
CONFIGURATION("configuration"),
ETAG("etag"),
Expand Down Expand Up @@ -134,7 +156,7 @@ static String selector(JobField... fields) {
for (JobField field : fields) {
fieldStrings.add(field.selector());
}
return com.google.common.base.Joiner.on(',').join(fieldStrings);
return Joiner.on(',').join(fieldStrings);
}
}

Expand Down Expand Up @@ -184,7 +206,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) {

/**
* Returns an option to specify the dataset's fields to be returned by the RPC call. If this
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields}) can
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields} can
* be used to specify only the fields of interest. {@link DatasetInfo#datasetId()} is always
* returned, even if not specified.
*/
Expand Down Expand Up @@ -228,6 +250,7 @@ private TableListOption(BigQueryRpc.Option option, Object value) {
* Returns an option to specify the maximum number of tables to be returned.
*/
public static TableListOption maxResults(long maxResults) {
checkArgument(maxResults >= 0);
return new TableListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
}

Expand All @@ -252,7 +275,7 @@ private TableOption(BigQueryRpc.Option option, Object value) {

/**
* Returns an option to specify the table's fields to be returned by the RPC call. If this
* option is not provided all table's fields are returned. {@code TableOption.fields}) can be
* option is not provided all table's fields are returned. {@code TableOption.fields} can be
* used to specify only the fields of interest. {@link BaseTableInfo#tableId()} and
* {@link BaseTableInfo#type()} are always returned, even if not specified.
*/
Expand All @@ -276,6 +299,7 @@ private TableDataListOption(BigQueryRpc.Option option, Object value) {
* Returns an option to specify the maximum number of rows to be returned.
*/
public static TableDataListOption maxResults(long maxResults) {
checkArgument(maxResults >= 0);
return new TableDataListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
}

Expand All @@ -291,6 +315,7 @@ public static TableDataListOption startPageToken(String pageToken) {
* data.
*/
public static TableDataListOption startIndex(long index) {
checkArgument(index >= 0);
return new TableDataListOption(BigQueryRpc.Option.START_INDEX, index);
}
}
Expand All @@ -314,14 +339,14 @@ public static JobListOption allUsers() {
}

/**
* Returns an option to list only jobs that match the provided filters.
* Returns an option to list only jobs that match the provided state filters.
*/
public static JobListOption stateFilter(JobStatus.State... stateFilters) {
List<String> stringFilters = Lists.transform(ImmutableList.copyOf(stateFilters),
new Function<JobStatus.State, String>() {
@Override
public String apply(JobStatus.State state) {
return state.toString().toLowerCase();
return state.name().toLowerCase();
}
});
return new JobListOption(BigQueryRpc.Option.STATE_FILTER, stringFilters);
Expand All @@ -331,6 +356,7 @@ public String apply(JobStatus.State state) {
* Returns an option to specify the maximum number of jobs to be returned.
*/
public static JobListOption maxResults(long maxResults) {
checkArgument(maxResults >= 0);
return new JobListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
}

Expand All @@ -343,7 +369,7 @@ public static JobListOption startPageToken(String pageToken) {

/**
* Returns an option to specify the job's fields to be returned by the RPC call. If this option
* is not provided all job's fields are returned. {@code JobOption.fields()}) can be used to
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
* specify only the fields of interest. {@link JobInfo#jobId()}, {@link JobStatus#state()},
* {@link JobStatus#error()} as well as type-specific configuration (e.g.
* {@link QueryJobInfo#query()} for Query Jobs) are always returned, even if not specified.
Expand All @@ -370,7 +396,7 @@ private JobOption(BigQueryRpc.Option option, Object value) {

/**
* Returns an option to specify the job's fields to be returned by the RPC call. If this option
* is not provided all job's fields are returned. {@code JobOption.fields}) can be used to
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
* specify only the fields of interest. {@link JobInfo#jobId()} as well as type-specific
* configuration (e.g. {@link QueryJobInfo#query()} for Query Jobs) are always returned, even if
* not specified.
Expand All @@ -395,21 +421,23 @@ private QueryResultsOption(BigQueryRpc.Option option, Object value) {
* Returns an option to specify the maximum number of rows to be returned.
*/
public static QueryResultsOption maxResults(long maxResults) {
checkArgument(maxResults >= 0);
return new QueryResultsOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
}

/**
* Returns an option to specify the page token from which to start listing query results.
* Returns an option to specify the page token from which to start getting query results.
*/
public static QueryResultsOption startPageToken(String pageToken) {
return new QueryResultsOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}

/**
* Returns an option that sets the zero-based index of the row from which to start listing query
* Returns an option that sets the zero-based index of the row from which to start getting query
* results.
*/
public static QueryResultsOption startIndex(Long startIndex) {
public static QueryResultsOption startIndex(long startIndex) {
checkArgument(startIndex >= 0);
return new QueryResultsOption(BigQueryRpc.Option.START_INDEX, startIndex);
}

Expand All @@ -418,7 +446,8 @@ public static QueryResultsOption startIndex(Long startIndex) {
* before returning. Default is 10 seconds. If the timeout passes before the job completes,
* {@link QueryResponse#jobComplete()} will be {@code false}.
*/
public static QueryResultsOption maxWaitTime(Long maxWaitTime) {
public static QueryResultsOption maxWaitTime(long maxWaitTime) {
checkArgument(maxWaitTime >= 0);
return new QueryResultsOption(BigQueryRpc.Option.TIMEOUT, maxWaitTime);
}
}
Expand Down Expand Up @@ -460,7 +489,7 @@ public static QueryResultsOption maxWaitTime(Long maxWaitTime) {

/**
* Lists the project's datasets. This method returns partial information on each dataset
* ({@link DatasetInfo#datasetId()} ()}, {@link DatasetInfo#friendlyName()} and
* ({@link DatasetInfo#datasetId()}, {@link DatasetInfo#friendlyName()} and
* {@link DatasetInfo#id()}). To get complete information use either
* {@link #getDataset(String, DatasetOption...)} or
* {@link #getDataset(DatasetId, DatasetOption...)}.
Expand Down Expand Up @@ -592,15 +621,16 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
JobInfo getJob(JobId jobId, JobOption... options) throws BigQueryException;

/**
* Lists the dataset's tables.
* Lists the jobs.
*
* @throws BigQueryException upon failure
*/
Page<JobInfo> listJobs(JobListOption... options) throws BigQueryException;

/**
* Sends a job cancel request. This call will return immediately, and the client will need to poll
* for the job status to see if the cancel completed successfully.
* Sends a job cancel request. This call will return immediately. The job status can then be
* checked using either {@link #getJob(JobId, JobOption...)} or
* {@link #getJob(String, JobOption...)}).
*
* @return {@code true} if cancel was requested successfully, {@code false} if the job was not
* found
Expand All @@ -609,9 +639,9 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
boolean cancel(String jobId) throws BigQueryException;

/**
* Sends a job cancel request. This call will return immediately. The client will need to poll
* for the job status using either {@link #getJob(JobId, JobOption...)} or
* {@link #getJob(String, JobOption...)}) to see if the cancel operation completed successfully.
* Sends a job cancel request. This call will return immediately. The job status can then be
* checked using either {@link #getJob(JobId, JobOption...)} or
* {@link #getJob(String, JobOption...)}).
*
* @return {@code true} if cancel was requested successfully, {@code false} if the job was not
* found
Expand All @@ -620,14 +650,14 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
boolean cancel(JobId tableId) throws BigQueryException;

/**
* Runs the query associated to the request.
* Runs the query associated with the request.
*
* @throws BigQueryException upon failure
*/
QueryResponse query(QueryRequest request) throws BigQueryException;

/**
* Returns results of the query associated to the provided job.
* Returns results of the query associated with the provided job.
*
* @throws BigQueryException upon failure
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public Boolean call() {
}

@Override
public boolean delete(final String datasetId, final String tableId) throws BigQueryException {
public boolean delete(String datasetId, String tableId) throws BigQueryException {
return delete(TableId.of(datasetId, tableId));
}

Expand Down Expand Up @@ -420,16 +420,8 @@ public Rows apply(RowToInsert rowToInsert) {
}
});
requestPb.setRows(rowsPb);
try {
return InsertAllResponse.fromPb(runWithRetries(new Callable<TableDataInsertAllResponse>() {
@Override
public TableDataInsertAllResponse call() {
return bigQueryRpc.insertAll(tableId.dataset(), tableId.table(), requestPb);
}
}, options().retryParams(), EXCEPTION_HANDLER));
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
}
return InsertAllResponse.fromPb(
bigQueryRpc.insertAll(tableId.dataset(), tableId.table(), requestPb));
}

@Override
Expand Down Expand Up @@ -475,7 +467,7 @@ public List<FieldValue> apply(TableRow rowPb) {
}

@Override
public JobInfo getJob(final String jobId, JobOption... options) throws BigQueryException {
public JobInfo getJob(String jobId, JobOption... options) throws BigQueryException {
return getJob(JobId.of(jobId), options);
}

Expand Down Expand Up @@ -575,7 +567,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
@Override
public QueryResponse getQueryResults(JobId job, QueryResultsOption... options)
throws BigQueryException {
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
return getQueryResults(job, options(), optionsMap);
}

Expand Down
Loading

0 comments on commit afdc2dd

Please sign in to comment.