Skip to content

Commit d75f1f4

Browse files
committed
Minor fixes to bigquery functional classes
- Remove periods from javadoc return paragraphs - Add options to load methods - Implement reload using load - Document that reload can return null - Add code snippet to Job.isDone - Better unit tests
1 parent 5e734ee commit d75f1f4

File tree

6 files changed

+148
-38
lines changed

6 files changed

+148
-38
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ public Dataset(BigQuery bigquery, DatasetInfo info) {
127127
*
128128
* @param bigquery the BigQuery service used for issuing requests
129129
* @param dataset dataset's user-defined id
130-
* @return the {@code Dataset} object or {@code null} if not found.
130+
* @param options dataset options
131+
* @return the {@code Dataset} object or {@code null} if not found
131132
* @throws BigQueryException upon failure
132133
*/
133-
public static Dataset load(BigQuery bigquery, String dataset) {
134-
DatasetInfo info = bigquery.getDataset(dataset);
134+
public static Dataset load(BigQuery bigquery, String dataset, BigQuery.DatasetOption... options) {
135+
DatasetInfo info = bigquery.getDataset(dataset, options);
135136
return info != null ? new Dataset(bigquery, info) : null;
136137
}
137138

@@ -145,22 +146,23 @@ public DatasetInfo info() {
145146
/**
146147
* Checks if this dataset exists.
147148
*
148-
* @return {@code true} if this dataset exists, {@code false} otherwise.
149+
* @return {@code true} if this dataset exists, {@code false} otherwise
149150
* @throws BigQueryException upon failure
150151
*/
151152
public boolean exists() {
152153
return bigquery.getDataset(info.datasetId(), BigQuery.DatasetOption.fields()) != null;
153154
}
154155

155156
/**
156-
* Fetches current dataset's latest information.
157+
* Fetches current dataset's latest information. Returns {@code null} if the dataset does not
158+
* exist.
157159
*
158160
* @param options dataset options
159-
* @return a {@code Dataset} object with latest information.
161+
* @return a {@code Dataset} object with latest information or {@code null} if not found
160162
* @throws BigQueryException upon failure
161163
*/
162164
public Dataset reload(BigQuery.DatasetOption... options) {
163-
return new Dataset(bigquery, bigquery.getDataset(info.datasetId(), options));
165+
return Dataset.load(bigquery, info.datasetId().dataset(), options);
164166
}
165167

166168
/**
@@ -170,7 +172,7 @@ public Dataset reload(BigQuery.DatasetOption... options) {
170172
* @param datasetInfo new dataset's information. User-defined id must match the one of the current
171173
* dataset
172174
* @param options dataset options
173-
* @return a {@code Dataset} object with updated information.
175+
* @return a {@code Dataset} object with updated information
174176
* @throws BigQueryException upon failure
175177
*/
176178
public Dataset update(DatasetInfo datasetInfo, BigQuery.DatasetOption... options) {
@@ -221,7 +223,7 @@ public Table get(String table, BigQuery.TableOption... options) {
221223
* @param table the table's user-defined id
222224
* @param schema the table's schema
223225
* @param options options for table creation
224-
* @return a {@code Table} object for the created table.
226+
* @return a {@code Table} object for the created table
225227
* @throws BigQueryException upon failure
226228
*/
227229
public Table create(String table, Schema schema, BigQuery.TableOption... options) {
@@ -236,7 +238,7 @@ public Table create(String table, Schema schema, BigQuery.TableOption... options
236238
* @param query the query used to generate the table
237239
* @param functions user-defined functions that can be used by the query
238240
* @param options options for table creation
239-
* @return a {@code Table} object for the created table.
241+
* @return a {@code Table} object for the created table
240242
* @throws BigQueryException upon failure
241243
*/
242244
public Table create(String table, String query, List<UserDefinedFunction> functions,
@@ -252,7 +254,7 @@ public Table create(String table, String query, List<UserDefinedFunction> functi
252254
* @param table the table's user-defined id
253255
* @param query the query used to generate the table
254256
* @param options options for table creation
255-
* @return a {@code Table} object for the created table.
257+
* @return a {@code Table} object for the created table
256258
* @throws BigQueryException upon failure
257259
*/
258260
public Table create(String table, String query, BigQuery.TableOption... options) {
@@ -266,7 +268,7 @@ public Table create(String table, String query, BigQuery.TableOption... options)
266268
* @param table the table's user-defined id
267269
* @param configuration data format, location and other properties of an external table
268270
* @param options options for table creation
269-
* @return a {@code Table} object for the created table.
271+
* @return a {@code Table} object for the created table
270272
* @throws BigQueryException upon failure
271273
*/
272274
public Table create(String table, ExternalDataConfiguration configuration,

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ public Job(BigQuery bigquery, JobInfo info) {
4343
}
4444

4545
/**
46-
* Creates a {@code Job} object for the provided job's user-defined id. Performs an RPC call
47-
* to get the latest job information.
46+
* Creates a {@code Job} object for the provided job's user-defined id. Performs an RPC call to
47+
* get the latest job information.
4848
*
4949
* @param bigquery the BigQuery service used for issuing requests
5050
* @param job job's id, either user-defined or picked by the BigQuery service
51-
* @return the {@code Job} object or {@code null} if not found.
51+
* @param options job options
52+
* @return the {@code Job} object or {@code null} if not found
5253
* @throws BigQueryException upon failure
5354
*/
54-
public static Job load(BigQuery bigquery, String job) {
55-
JobInfo info = bigquery.getJob(job);
55+
public static Job load(BigQuery bigquery, String job, BigQuery.JobOption... options) {
56+
JobInfo info = bigquery.getJob(job, options);
5657
return info != null ? new Job(bigquery, info) : null;
5758
}
5859

@@ -66,18 +67,26 @@ public JobInfo info() {
6667
/**
6768
* Checks if this job exists.
6869
*
69-
* @return {@code true} if this job exists, {@code false} otherwise.
70+
* @return {@code true} if this job exists, {@code false} otherwise
7071
* @throws BigQueryException upon failure
7172
*/
7273
public boolean exists() {
7374
return bigquery.getJob(info.jobId(), BigQuery.JobOption.fields()) != null;
7475
}
7576

7677
/**
77-
* Checks if this job has completed its execution, either failing or succeeding.
78+
* Checks if this job has completed its execution, either failing or succeeding. If the job does
79+
* not exist this method returns {@code false}. To correctly wait for job's completion check that
80+
* the job exists first, using {@link #exists()}:
81+
* <pre> {@code
82+
* if (job.exists()) {
83+
* while(!job.isDone()) {
84+
* Thread.sleep(1000L);
85+
* }
86+
* }}</pre>
7887
*
7988
* @return {@code true} if this job is in {@link JobStatus.State#DONE} state, {@code false} if the
80-
* state is not {@link JobStatus.State#DONE} or the job does not exist.
89+
* state is not {@link JobStatus.State#DONE} or the job does not exist
8190
* @throws BigQueryException upon failure
8291
*/
8392
public boolean isDone() {
@@ -87,21 +96,21 @@ public boolean isDone() {
8796
}
8897

8998
/**
90-
* Fetches current job's latest information.
99+
* Fetches current job's latest information. Returns {@code null} if the job does not exist.
91100
*
92101
* @param options job options
93-
* @return a {@code Job} object with latest information.
102+
* @return a {@code Job} object with latest information or {@code null} if not found
94103
* @throws BigQueryException upon failure
95104
*/
96105
public Job reload(BigQuery.JobOption... options) {
97-
return new Job(bigquery, bigquery.getJob(info.jobId(), options));
106+
return Job.load(bigquery, info.jobId().job(), options);
98107
}
99108

100109
/**
101110
* Sends a job cancel request.
102111
*
103112
* @return {@code true} if cancel request was sent successfully, {@code false} if job was not
104-
* found.
113+
* found
105114
* @throws BigQueryException upon failure
106115
*/
107116
public boolean cancel() {

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ public Table(BigQuery bigquery, BaseTableInfo info) {
5757
* @param bigquery the BigQuery service used for issuing requests
5858
* @param dataset the dataset's user-defined id
5959
* @param table the table's user-defined id
60-
* @return the {@code Table} object or {@code null} if not found.
60+
* @param options table options
61+
* @return the {@code Table} object or {@code null} if not found
6162
* @throws BigQueryException upon failure
6263
*/
63-
public static Table load(BigQuery bigquery, String dataset, String table) {
64-
return load(bigquery, TableId.of(dataset, table));
64+
public static Table load(BigQuery bigquery, String dataset, String table,
65+
BigQuery.TableOption... options) {
66+
return load(bigquery, TableId.of(dataset, table), options);
6567
}
6668

6769
/**
@@ -70,11 +72,12 @@ public static Table load(BigQuery bigquery, String dataset, String table) {
7072
*
7173
* @param bigquery the BigQuery service used for issuing requests
7274
* @param table the table's identity
73-
* @return the {@code Table} object or {@code null} if not found.
75+
* @param options table options
76+
* @return the {@code Table} object or {@code null} if not found
7477
* @throws BigQueryException upon failure
7578
*/
76-
public static Table load(BigQuery bigquery, TableId table) {
77-
BaseTableInfo info = bigquery.getTable(table);
79+
public static Table load(BigQuery bigquery, TableId table, BigQuery.TableOption... options) {
80+
BaseTableInfo info = bigquery.getTable(table, options);
7881
return info != null ? new Table(bigquery, info) : null;
7982
}
8083

@@ -88,22 +91,22 @@ public BaseTableInfo info() {
8891
/**
8992
* Checks if this table exists.
9093
*
91-
* @return {@code true} if this table exists, {@code false} otherwise.
94+
* @return {@code true} if this table exists, {@code false} otherwise
9295
* @throws BigQueryException upon failure
9396
*/
9497
public boolean exists() {
9598
return bigquery.getTable(info.tableId(), BigQuery.TableOption.fields()) != null;
9699
}
97100

98101
/**
99-
* Fetches current table's latest information.
102+
* Fetches current table's latest information. Returns {@code null} if the table does not exist.
100103
*
101104
* @param options table options
102-
* @return a {@code Table} object with latest information.
105+
* @return a {@code Table} object with latest information or {@code null} if not found
103106
* @throws BigQueryException upon failure
104107
*/
105108
public Table reload(BigQuery.TableOption... options) {
106-
return new Table(bigquery, bigquery.getTable(info.tableId(), options));
109+
return Table.load(bigquery, info.tableId(), options);
107110
}
108111

109112
/**
@@ -113,7 +116,7 @@ public Table reload(BigQuery.TableOption... options) {
113116
* @param tableInfo new table's information. Dataset's and table's user-defined ids must match the
114117
* ones of the current table
115118
* @param options dataset options
116-
* @return a {@code Table} object with updated information.
119+
* @return a {@code Table} object with updated information
117120
* @throws BigQueryException upon failure
118121
*/
119122
public Table update(BaseTableInfo tableInfo, BigQuery.TableOption... options) {
@@ -127,7 +130,7 @@ public Table update(BaseTableInfo tableInfo, BigQuery.TableOption... options) {
127130
/**
128131
* Deletes this table.
129132
*
130-
* @return {@code true} if table was deleted, {@code false} if it was not found.
133+
* @return {@code true} if table was deleted, {@code false} if it was not found
131134
* @throws BigQueryException upon failure
132135
*/
133136
public boolean delete() {

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,31 @@ public void testExists_False() throws Exception {
8888
@Test
8989
public void testReload() throws Exception {
9090
DatasetInfo updatedInfo = DATASET_INFO.toBuilder().description("Description").build();
91-
expect(bigquery.getDataset(DATASET_ID)).andReturn(updatedInfo);
91+
expect(bigquery.getDataset(DATASET_ID.dataset())).andReturn(updatedInfo);
9292
replay(bigquery);
9393
Dataset updatedDataset = dataset.reload();
9494
assertSame(bigquery, updatedDataset.bigquery());
9595
assertEquals(updatedInfo, updatedDataset.info());
9696
}
9797

98+
@Test
99+
public void testReloadNull() throws Exception {
100+
expect(bigquery.getDataset(DATASET_ID.dataset())).andReturn(null);
101+
replay(bigquery);
102+
assertNull(dataset.reload());
103+
}
104+
105+
@Test
106+
public void testReloadWithOptions() throws Exception {
107+
DatasetInfo updatedInfo = DATASET_INFO.toBuilder().description("Description").build();
108+
expect(bigquery.getDataset(DATASET_ID.dataset(), BigQuery.DatasetOption.fields()))
109+
.andReturn(updatedInfo);
110+
replay(bigquery);
111+
Dataset updatedDataset = dataset.reload(BigQuery.DatasetOption.fields());
112+
assertSame(bigquery, updatedDataset.bigquery());
113+
assertEquals(updatedInfo, updatedDataset.info());
114+
}
115+
98116
@Test
99117
public void testUpdate() throws Exception {
100118
DatasetInfo updatedInfo = DATASET_INFO.toBuilder().description("Description").build();
@@ -194,4 +212,15 @@ public void testLoadNull() throws Exception {
194212
replay(bigquery);
195213
assertNull(Dataset.load(bigquery, DATASET_INFO.datasetId().dataset()));
196214
}
215+
216+
@Test
217+
public void testLoadWithOptions() throws Exception {
218+
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset(), BigQuery.DatasetOption.fields()))
219+
.andReturn(DATASET_INFO);
220+
replay(bigquery);
221+
Dataset loadedDataset = Dataset.load(bigquery, DATASET_INFO.datasetId().dataset(),
222+
BigQuery.DatasetOption.fields());
223+
assertNotNull(loadedDataset);
224+
assertEquals(DATASET_INFO, loadedDataset.info());
225+
}
197226
}

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,31 @@ public void testIsDone_NotExists() throws Exception {
109109
@Test
110110
public void testReload() throws Exception {
111111
JobInfo updatedInfo = JOB_INFO.toBuilder().etag("etag").build();
112-
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(updatedInfo);
112+
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(updatedInfo);
113113
replay(bigquery);
114114
Job updatedJob = job.reload();
115115
assertSame(bigquery, updatedJob.bigquery());
116116
assertEquals(updatedInfo, updatedJob.info());
117117
}
118118

119+
@Test
120+
public void testReloadNull() throws Exception {
121+
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(null);
122+
replay(bigquery);
123+
assertNull(job.reload());
124+
}
125+
126+
@Test
127+
public void testReloadWithOptions() throws Exception {
128+
JobInfo updatedInfo = JOB_INFO.toBuilder().etag("etag").build();
129+
expect(bigquery.getJob(JOB_INFO.jobId().job(), BigQuery.JobOption.fields()))
130+
.andReturn(updatedInfo);
131+
replay(bigquery);
132+
Job updatedJob = job.reload(BigQuery.JobOption.fields());
133+
assertSame(bigquery, updatedJob.bigquery());
134+
assertEquals(updatedInfo, updatedJob.info());
135+
}
136+
119137
@Test
120138
public void testCancel() throws Exception {
121139
expect(bigquery.cancel(JOB_INFO.jobId())).andReturn(true);
@@ -138,4 +156,14 @@ public void testLoadNull() throws Exception {
138156
replay(bigquery);
139157
assertNull(Job.load(bigquery, JOB_INFO.jobId().job()));
140158
}
159+
160+
@Test
161+
public void testLoadWithOptions() throws Exception {
162+
expect(bigquery.getJob(JOB_INFO.jobId().job(), BigQuery.JobOption.fields()))
163+
.andReturn(JOB_INFO);
164+
replay(bigquery);
165+
Job loadedJob = Job.load(bigquery, JOB_INFO.jobId().job(), BigQuery.JobOption.fields());
166+
assertNotNull(loadedJob);
167+
assertEquals(JOB_INFO, loadedJob.info());
168+
}
141169
}

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ public void testReload() throws Exception {
121121
assertEquals(updatedInfo, updatedTable.info());
122122
}
123123

124+
@Test
125+
public void testReloadNull() throws Exception {
126+
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
127+
replay(bigquery);
128+
assertNull(table.reload());
129+
}
130+
131+
@Test
132+
public void testReloadWithOptions() throws Exception {
133+
TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
134+
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
135+
.andReturn(updatedInfo);
136+
replay(bigquery);
137+
Table updatedTable = table.reload(BigQuery.TableOption.fields());
138+
assertSame(bigquery, updatedTable.bigquery());
139+
assertEquals(updatedInfo, updatedTable.info());
140+
}
141+
124142
@Test
125143
public void testDelete() throws Exception {
126144
expect(bigquery.delete(TABLE_INFO.tableId())).andReturn(true);
@@ -240,4 +258,25 @@ public void testLoadFromStringsNull() throws Exception {
240258
replay(bigquery);
241259
assertNull(Table.load(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table()));
242260
}
261+
262+
@Test
263+
public void testLoadFromIdWithOptions() throws Exception {
264+
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
265+
.andReturn(TABLE_INFO);
266+
replay(bigquery);
267+
Table loadedTable = Table.load(bigquery, TABLE_INFO.tableId(), BigQuery.TableOption.fields());
268+
assertNotNull(loadedTable);
269+
assertEquals(TABLE_INFO, loadedTable.info());
270+
}
271+
272+
@Test
273+
public void testLoadFromStringsWithOptions() throws Exception {
274+
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
275+
.andReturn(TABLE_INFO);
276+
replay(bigquery);
277+
Table loadedTable =
278+
Table.load(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table(), BigQuery.TableOption.fields());
279+
assertNotNull(loadedTable);
280+
assertEquals(TABLE_INFO, loadedTable.info());
281+
}
243282
}

0 commit comments

Comments
 (0)