Skip to content

Commit d676636

Browse files
committed
Fix minor errors
- 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
1 parent e1dc3af commit d676636

5 files changed

Lines changed: 140 additions & 56 deletions

File tree

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717
package com.google.gcloud.bigquery;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
20+
1921
import com.google.common.base.Function;
22+
import com.google.common.base.Joiner;
2023
import com.google.common.collect.ImmutableList;
2124
import com.google.common.collect.Lists;
2225
import com.google.common.collect.Sets;
2326
import com.google.gcloud.Page;
2427
import com.google.gcloud.Service;
2528
import com.google.gcloud.spi.BigQueryRpc;
2629

27-
import java.util.HashSet;
2830
import java.util.List;
31+
import java.util.Set;
2932

3033
/**
3134
* An interface for Google Cloud BigQuery.
@@ -34,11 +37,17 @@
3437
*/
3538
public interface BigQuery extends Service<BigQueryOptions> {
3639

40+
/**
41+
* Fields of a BigQuery Dataset resource.
42+
*
43+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets#resource">Dataset
44+
* Resource</a>
45+
*/
3746
enum DatasetField {
3847
ACCESS("access"),
3948
CREATION_TIME("creationTime"),
4049
DATASET_REFERENCE("datasetReference"),
41-
DEFAULT_TABLE_EXPIRATION_MS("defaultTableLifetime"),
50+
DEFAULT_TABLE_EXPIRATION_MS("defaultTableExpirationMsS"),
4251
DESCRIPTION("description"),
4352
ETAG("etag"),
4453
FRIENDLY_NAME("friendlyName"),
@@ -58,15 +67,21 @@ public String selector() {
5867
}
5968

6069
static String selector(DatasetField... fields) {
61-
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
70+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
6271
fieldStrings.add(DATASET_REFERENCE.selector());
6372
for (DatasetField field : fields) {
6473
fieldStrings.add(field.selector());
6574
}
66-
return com.google.common.base.Joiner.on(',').join(fieldStrings);
75+
return Joiner.on(',').join(fieldStrings);
6776
}
6877
}
6978

79+
/**
80+
* Fields of a BigQuery Table resource.
81+
*
82+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#resource">Table
83+
* Resource</a>
84+
*/
7085
enum TableField {
7186
CREATION_TIME("creationTime"),
7287
DESCRIPTION("description"),
@@ -97,16 +112,22 @@ public String selector() {
97112
}
98113

99114
static String selector(TableField... fields) {
100-
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
115+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
101116
fieldStrings.add(TABLE_REFERENCE.selector());
102117
fieldStrings.add(TYPE.selector());
103118
for (TableField field : fields) {
104119
fieldStrings.add(field.selector());
105120
}
106-
return com.google.common.base.Joiner.on(',').join(fieldStrings);
121+
return Joiner.on(',').join(fieldStrings);
107122
}
108123
}
109124

125+
/**
126+
* Fields of a BigQuery Job resource.
127+
*
128+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#resource">Job Resource
129+
* </a>
130+
*/
110131
enum JobField {
111132
CONFIGURATION("configuration"),
112133
ETAG("etag"),
@@ -128,13 +149,13 @@ public String selector() {
128149
}
129150

130151
static String selector(JobField... fields) {
131-
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
152+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
132153
fieldStrings.add(JOB_REFERENCE.selector());
133154
fieldStrings.add(CONFIGURATION.selector());
134155
for (JobField field : fields) {
135156
fieldStrings.add(field.selector());
136157
}
137-
return com.google.common.base.Joiner.on(',').join(fieldStrings);
158+
return Joiner.on(',').join(fieldStrings);
138159
}
139160
}
140161

@@ -184,7 +205,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) {
184205

185206
/**
186207
* Returns an option to specify the dataset's fields to be returned by the RPC call. If this
187-
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields}) can
208+
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields} can
188209
* be used to specify only the fields of interest. {@link DatasetInfo#datasetId()} is always
189210
* returned, even if not specified.
190211
*/
@@ -228,6 +249,7 @@ private TableListOption(BigQueryRpc.Option option, Object value) {
228249
* Returns an option to specify the maximum number of tables to be returned.
229250
*/
230251
public static TableListOption maxResults(long maxResults) {
252+
checkArgument(maxResults >= 0);
231253
return new TableListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
232254
}
233255

@@ -252,7 +274,7 @@ private TableOption(BigQueryRpc.Option option, Object value) {
252274

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

@@ -291,6 +314,7 @@ public static TableDataListOption startPageToken(String pageToken) {
291314
* data.
292315
*/
293316
public static TableDataListOption startIndex(long index) {
317+
checkArgument(index >= 0);
294318
return new TableDataListOption(BigQueryRpc.Option.START_INDEX, index);
295319
}
296320
}
@@ -314,14 +338,14 @@ public static JobListOption allUsers() {
314338
}
315339

316340
/**
317-
* Returns an option to list only jobs that match the provided filters.
341+
* Returns an option to list only jobs that match the provided state filters.
318342
*/
319343
public static JobListOption stateFilter(JobStatus.State... stateFilters) {
320344
List<String> stringFilters = Lists.transform(ImmutableList.copyOf(stateFilters),
321345
new Function<JobStatus.State, String>() {
322346
@Override
323347
public String apply(JobStatus.State state) {
324-
return state.toString().toLowerCase();
348+
return state.name().toLowerCase();
325349
}
326350
});
327351
return new JobListOption(BigQueryRpc.Option.STATE_FILTER, stringFilters);
@@ -331,6 +355,7 @@ public String apply(JobStatus.State state) {
331355
* Returns an option to specify the maximum number of jobs to be returned.
332356
*/
333357
public static JobListOption maxResults(long maxResults) {
358+
checkArgument(maxResults >= 0);
334359
return new JobListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
335360
}
336361

@@ -343,7 +368,7 @@ public static JobListOption startPageToken(String pageToken) {
343368

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

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

401427
/**
402-
* Returns an option to specify the page token from which to start listing query results.
428+
* Returns an option to specify the page token from which to start getting query results.
403429
*/
404430
public static QueryResultsOption startPageToken(String pageToken) {
405431
return new QueryResultsOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
406432
}
407433

408434
/**
409-
* Returns an option that sets the zero-based index of the row from which to start listing query
435+
* Returns an option that sets the zero-based index of the row from which to start getting query
410436
* results.
411437
*/
412-
public static QueryResultsOption startIndex(Long startIndex) {
438+
public static QueryResultsOption startIndex(long startIndex) {
439+
checkArgument(startIndex >= 0);
413440
return new QueryResultsOption(BigQueryRpc.Option.START_INDEX, startIndex);
414441
}
415442

@@ -418,7 +445,8 @@ public static QueryResultsOption startIndex(Long startIndex) {
418445
* before returning. Default is 10 seconds. If the timeout passes before the job completes,
419446
* {@link QueryResponse#jobComplete()} will be {@code false}.
420447
*/
421-
public static QueryResultsOption maxWaitTime(Long maxWaitTime) {
448+
public static QueryResultsOption maxWaitTime(long maxWaitTime) {
449+
checkArgument(maxWaitTime >= 0);
422450
return new QueryResultsOption(BigQueryRpc.Option.TIMEOUT, maxWaitTime);
423451
}
424452
}
@@ -460,7 +488,7 @@ public static QueryResultsOption maxWaitTime(Long maxWaitTime) {
460488

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

594622
/**
595-
* Lists the dataset's tables.
623+
* Lists the jobs.
596624
*
597625
* @throws BigQueryException upon failure
598626
*/
599627
Page<JobInfo> listJobs(JobListOption... options) throws BigQueryException;
600628

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

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

622651
/**
623-
* Runs the query associated to the request.
652+
* Runs the query associated with the request.
624653
*
625654
* @throws BigQueryException upon failure
626655
*/
627656
QueryResponse query(QueryRequest request) throws BigQueryException;
628657

629658
/**
630-
* Returns results of the query associated to the provided job.
659+
* Returns results of the query associated with the provided job.
631660
*
632661
* @throws BigQueryException upon failure
633662
*/

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.api.services.bigquery.model.Table;
2626
import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
2727
import com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows;
28-
import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
2928
import com.google.api.services.bigquery.model.TableReference;
3029
import com.google.api.services.bigquery.model.TableRow;
3130
import com.google.common.base.Function;
@@ -300,7 +299,7 @@ public Boolean call() {
300299
}
301300

302301
@Override
303-
public boolean delete(final String datasetId, final String tableId) throws BigQueryException {
302+
public boolean delete(String datasetId, String tableId) throws BigQueryException {
304303
return delete(TableId.of(datasetId, tableId));
305304
}
306305

@@ -420,16 +419,8 @@ public Rows apply(RowToInsert rowToInsert) {
420419
}
421420
});
422421
requestPb.setRows(rowsPb);
423-
try {
424-
return InsertAllResponse.fromPb(runWithRetries(new Callable<TableDataInsertAllResponse>() {
425-
@Override
426-
public TableDataInsertAllResponse call() {
427-
return bigQueryRpc.insertAll(tableId.dataset(), tableId.table(), requestPb);
428-
}
429-
}, options().retryParams(), EXCEPTION_HANDLER));
430-
} catch (RetryHelper.RetryHelperException e) {
431-
throw BigQueryException.translateAndThrow(e);
432-
}
422+
return InsertAllResponse.fromPb(
423+
bigQueryRpc.insertAll(tableId.dataset(), tableId.table(), requestPb));
433424
}
434425

435426
@Override
@@ -475,7 +466,7 @@ public List<FieldValue> apply(TableRow rowPb) {
475466
}
476467

477468
@Override
478-
public JobInfo getJob(final String jobId, JobOption... options) throws BigQueryException {
469+
public JobInfo getJob(String jobId, JobOption... options) throws BigQueryException {
479470
return getJob(JobId.of(jobId), options);
480471
}
481472

@@ -575,7 +566,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
575566
@Override
576567
public QueryResponse getQueryResults(JobId job, QueryResultsOption... options)
577568
throws BigQueryException {
578-
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
569+
Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
579570
return getQueryResults(job, options(), optionsMap);
580571
}
581572

0 commit comments

Comments
 (0)