Skip to content

Commit afdc2dd

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 afdc2dd

File tree

5 files changed

+139
-53
lines changed

5 files changed

+139
-53
lines changed

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

+55-25
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
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;
@@ -26,6 +29,7 @@
2629

2730
import java.util.HashSet;
2831
import java.util.List;
32+
import java.util.Set;
2933

3034
/**
3135
* An interface for Google Cloud BigQuery.
@@ -34,11 +38,17 @@
3438
*/
3539
public interface BigQuery extends Service<BigQueryOptions> {
3640

41+
/**
42+
* Fields of a BigQuery Dataset resource.
43+
*
44+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets#resource">Dataset
45+
* Resource</a>
46+
*/
3747
enum DatasetField {
3848
ACCESS("access"),
3949
CREATION_TIME("creationTime"),
4050
DATASET_REFERENCE("datasetReference"),
41-
DEFAULT_TABLE_EXPIRATION_MS("defaultTableLifetime"),
51+
DEFAULT_TABLE_EXPIRATION_MS("defaultTableExpirationMsS"),
4252
DESCRIPTION("description"),
4353
ETAG("etag"),
4454
FRIENDLY_NAME("friendlyName"),
@@ -58,15 +68,21 @@ public String selector() {
5868
}
5969

6070
static String selector(DatasetField... fields) {
61-
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
71+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
6272
fieldStrings.add(DATASET_REFERENCE.selector());
6373
for (DatasetField field : fields) {
6474
fieldStrings.add(field.selector());
6575
}
66-
return com.google.common.base.Joiner.on(',').join(fieldStrings);
76+
return Joiner.on(',').join(fieldStrings);
6777
}
6878
}
6979

80+
/**
81+
* Fields of a BigQuery Table resource.
82+
*
83+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#resource">Table
84+
* Resource</a>
85+
*/
7086
enum TableField {
7187
CREATION_TIME("creationTime"),
7288
DESCRIPTION("description"),
@@ -97,16 +113,22 @@ public String selector() {
97113
}
98114

99115
static String selector(TableField... fields) {
100-
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
116+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2);
101117
fieldStrings.add(TABLE_REFERENCE.selector());
102118
fieldStrings.add(TYPE.selector());
103119
for (TableField field : fields) {
104120
fieldStrings.add(field.selector());
105121
}
106-
return com.google.common.base.Joiner.on(',').join(fieldStrings);
122+
return Joiner.on(',').join(fieldStrings);
107123
}
108124
}
109125

126+
/**
127+
* Fields of a BigQuery Job resource.
128+
*
129+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#resource">Job Resource
130+
* </a>
131+
*/
110132
enum JobField {
111133
CONFIGURATION("configuration"),
112134
ETAG("etag"),
@@ -134,7 +156,7 @@ static String selector(JobField... fields) {
134156
for (JobField field : fields) {
135157
fieldStrings.add(field.selector());
136158
}
137-
return com.google.common.base.Joiner.on(',').join(fieldStrings);
159+
return Joiner.on(',').join(fieldStrings);
138160
}
139161
}
140162

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

185207
/**
186208
* 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
209+
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields} can
188210
* be used to specify only the fields of interest. {@link DatasetInfo#datasetId()} is always
189211
* returned, even if not specified.
190212
*/
@@ -228,6 +250,7 @@ private TableListOption(BigQueryRpc.Option option, Object value) {
228250
* Returns an option to specify the maximum number of tables to be returned.
229251
*/
230252
public static TableListOption maxResults(long maxResults) {
253+
checkArgument(maxResults >= 0);
231254
return new TableListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
232255
}
233256

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

253276
/**
254277
* 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
278+
* option is not provided all table's fields are returned. {@code TableOption.fields} can be
256279
* used to specify only the fields of interest. {@link BaseTableInfo#tableId()} and
257280
* {@link BaseTableInfo#type()} are always returned, even if not specified.
258281
*/
@@ -276,6 +299,7 @@ private TableDataListOption(BigQueryRpc.Option option, Object value) {
276299
* Returns an option to specify the maximum number of rows to be returned.
277300
*/
278301
public static TableDataListOption maxResults(long maxResults) {
302+
checkArgument(maxResults >= 0);
279303
return new TableDataListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
280304
}
281305

@@ -291,6 +315,7 @@ public static TableDataListOption startPageToken(String pageToken) {
291315
* data.
292316
*/
293317
public static TableDataListOption startIndex(long index) {
318+
checkArgument(index >= 0);
294319
return new TableDataListOption(BigQueryRpc.Option.START_INDEX, index);
295320
}
296321
}
@@ -314,14 +339,14 @@ public static JobListOption allUsers() {
314339
}
315340

316341
/**
317-
* Returns an option to list only jobs that match the provided filters.
342+
* Returns an option to list only jobs that match the provided state filters.
318343
*/
319344
public static JobListOption stateFilter(JobStatus.State... stateFilters) {
320345
List<String> stringFilters = Lists.transform(ImmutableList.copyOf(stateFilters),
321346
new Function<JobStatus.State, String>() {
322347
@Override
323348
public String apply(JobStatus.State state) {
324-
return state.toString().toLowerCase();
349+
return state.name().toLowerCase();
325350
}
326351
});
327352
return new JobListOption(BigQueryRpc.Option.STATE_FILTER, stringFilters);
@@ -331,6 +356,7 @@ public String apply(JobStatus.State state) {
331356
* Returns an option to specify the maximum number of jobs to be returned.
332357
*/
333358
public static JobListOption maxResults(long maxResults) {
359+
checkArgument(maxResults >= 0);
334360
return new JobListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
335361
}
336362

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

344370
/**
345371
* 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
372+
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
347373
* specify only the fields of interest. {@link JobInfo#jobId()}, {@link JobStatus#state()},
348374
* {@link JobStatus#error()} as well as type-specific configuration (e.g.
349375
* {@link QueryJobInfo#query()} for Query Jobs) are always returned, even if not specified.
@@ -370,7 +396,7 @@ private JobOption(BigQueryRpc.Option option, Object value) {
370396

371397
/**
372398
* 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
399+
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
374400
* specify only the fields of interest. {@link JobInfo#jobId()} as well as type-specific
375401
* configuration (e.g. {@link QueryJobInfo#query()} for Query Jobs) are always returned, even if
376402
* not specified.
@@ -395,21 +421,23 @@ private QueryResultsOption(BigQueryRpc.Option option, Object value) {
395421
* Returns an option to specify the maximum number of rows to be returned.
396422
*/
397423
public static QueryResultsOption maxResults(long maxResults) {
424+
checkArgument(maxResults >= 0);
398425
return new QueryResultsOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
399426
}
400427

401428
/**
402-
* Returns an option to specify the page token from which to start listing query results.
429+
* Returns an option to specify the page token from which to start getting query results.
403430
*/
404431
public static QueryResultsOption startPageToken(String pageToken) {
405432
return new QueryResultsOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
406433
}
407434

408435
/**
409-
* Returns an option that sets the zero-based index of the row from which to start listing query
436+
* Returns an option that sets the zero-based index of the row from which to start getting query
410437
* results.
411438
*/
412-
public static QueryResultsOption startIndex(Long startIndex) {
439+
public static QueryResultsOption startIndex(long startIndex) {
440+
checkArgument(startIndex >= 0);
413441
return new QueryResultsOption(BigQueryRpc.Option.START_INDEX, startIndex);
414442
}
415443

@@ -418,7 +446,8 @@ public static QueryResultsOption startIndex(Long startIndex) {
418446
* before returning. Default is 10 seconds. If the timeout passes before the job completes,
419447
* {@link QueryResponse#jobComplete()} will be {@code false}.
420448
*/
421-
public static QueryResultsOption maxWaitTime(Long maxWaitTime) {
449+
public static QueryResultsOption maxWaitTime(long maxWaitTime) {
450+
checkArgument(maxWaitTime >= 0);
422451
return new QueryResultsOption(BigQueryRpc.Option.TIMEOUT, maxWaitTime);
423452
}
424453
}
@@ -460,7 +489,7 @@ public static QueryResultsOption maxWaitTime(Long maxWaitTime) {
460489

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

594623
/**
595-
* Lists the dataset's tables.
624+
* Lists the jobs.
596625
*
597626
* @throws BigQueryException upon failure
598627
*/
599628
Page<JobInfo> listJobs(JobListOption... options) throws BigQueryException;
600629

601630
/**
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.
631+
* Sends a job cancel request. This call will return immediately. The job status can then be
632+
* checked using either {@link #getJob(JobId, JobOption...)} or
633+
* {@link #getJob(String, JobOption...)}).
604634
*
605635
* @return {@code true} if cancel was requested successfully, {@code false} if the job was not
606636
* found
@@ -609,9 +639,9 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
609639
boolean cancel(String jobId) throws BigQueryException;
610640

611641
/**
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.
642+
* Sends a job cancel request. This call will return immediately. The job status can then be
643+
* checked using either {@link #getJob(JobId, JobOption...)} or
644+
* {@link #getJob(String, JobOption...)}).
615645
*
616646
* @return {@code true} if cancel was requested successfully, {@code false} if the job was not
617647
* found
@@ -620,14 +650,14 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
620650
boolean cancel(JobId tableId) throws BigQueryException;
621651

622652
/**
623-
* Runs the query associated to the request.
653+
* Runs the query associated with the request.
624654
*
625655
* @throws BigQueryException upon failure
626656
*/
627657
QueryResponse query(QueryRequest request) throws BigQueryException;
628658

629659
/**
630-
* Returns results of the query associated to the provided job.
660+
* Returns results of the query associated with the provided job.
631661
*
632662
* @throws BigQueryException upon failure
633663
*/

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

+5-13
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public Boolean call() {
300300
}
301301

302302
@Override
303-
public boolean delete(final String datasetId, final String tableId) throws BigQueryException {
303+
public boolean delete(String datasetId, String tableId) throws BigQueryException {
304304
return delete(TableId.of(datasetId, tableId));
305305
}
306306

@@ -420,16 +420,8 @@ public Rows apply(RowToInsert rowToInsert) {
420420
}
421421
});
422422
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-
}
423+
return InsertAllResponse.fromPb(
424+
bigQueryRpc.insertAll(tableId.dataset(), tableId.table(), requestPb));
433425
}
434426

435427
@Override
@@ -475,7 +467,7 @@ public List<FieldValue> apply(TableRow rowPb) {
475467
}
476468

477469
@Override
478-
public JobInfo getJob(final String jobId, JobOption... options) throws BigQueryException {
470+
public JobInfo getJob(String jobId, JobOption... options) throws BigQueryException {
479471
return getJob(JobId.of(jobId), options);
480472
}
481473

@@ -575,7 +567,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
575567
@Override
576568
public QueryResponse getQueryResults(JobId job, QueryResultsOption... options)
577569
throws BigQueryException {
578-
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
570+
Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
579571
return getQueryResults(job, options(), optionsMap);
580572
}
581573

0 commit comments

Comments
 (0)