Skip to content

Commit 94533b2

Browse files
committed
Minor refactoring bigquery functional objects
- Make Builder public - Make Dataset.builder package scope - Add toAndFromPb test - Remove static get methods
1 parent b0c0bb7 commit 94533b2

9 files changed

Lines changed: 29 additions & 171 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
559559

560560
/**
561561
* Lists the tables in the dataset. This method returns partial information on each table
562-
* ({@link TableInfo#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
562+
* ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
563563
* is part of {@link Table#definition()}). To get complete information use either
564564
* {@link #getTable(TableId, TableOption...)} or
565565
* {@link #getTable(String, String, TableOption...)}.
@@ -570,7 +570,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
570570

571571
/**
572572
* Lists the tables in the dataset. This method returns partial information on each table
573-
* ({@link TableInfo#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
573+
* ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
574574
* is part of {@link Table#definition()}). To get complete information use either
575575
* {@link #getTable(TableId, TableOption...)} or
576576
* {@link #getTable(String, String, TableOption...)}.

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class Dataset extends DatasetInfo {
4141
private final BigQueryOptions options;
4242
private transient BigQuery bigquery;
4343

44-
static final class Builder extends DatasetInfo.Builder {
44+
public static final class Builder extends DatasetInfo.Builder {
4545

4646
private final BigQuery bigquery;
4747
private final DatasetInfo.BuilderImpl infoBuilder;
@@ -134,20 +134,6 @@ public Dataset build() {
134134
this.options = bigquery.options();
135135
}
136136

137-
/**
138-
* Creates a {@code Dataset} object for the provided dataset's user-defined id. Performs an RPC
139-
* call to get the latest dataset information.
140-
*
141-
* @param bigquery the BigQuery service used for issuing requests
142-
* @param dataset dataset's user-defined id
143-
* @param options dataset options
144-
* @return the {@code Dataset} object or {@code null} if not found
145-
* @throws BigQueryException upon failure
146-
*/
147-
public static Dataset get(BigQuery bigquery, String dataset, BigQuery.DatasetOption... options) {
148-
return bigquery.getDataset(dataset, options);
149-
}
150-
151137
/**
152138
* Checks if this dataset exists.
153139
*
@@ -167,7 +153,7 @@ public boolean exists() {
167153
* @throws BigQueryException upon failure
168154
*/
169155
public Dataset reload(BigQuery.DatasetOption... options) {
170-
return Dataset.get(bigquery, datasetId().dataset(), options);
156+
return bigquery.getDataset(datasetId().dataset(), options);
171157
}
172158

173159
/**
@@ -234,14 +220,11 @@ public BigQuery bigquery() {
234220
return bigquery;
235221
}
236222

237-
public static Builder builder(BigQuery bigquery, DatasetId datasetId) {
223+
static Builder builder(BigQuery bigquery, DatasetId datasetId) {
238224
return new Builder(bigquery).datasetId(datasetId);
239225
}
240226

241-
/**
242-
* Returns a builder for a {@code DatasetInfo} object given it's user-defined id.
243-
*/
244-
public static Builder builder(BigQuery bigquery, String datasetId) {
227+
static Builder builder(BigQuery bigquery, String datasetId) {
245228
return builder(bigquery, DatasetId.of(datasetId));
246229
}
247230

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public static Builder builder(String datasetId) {
460460
}
461461

462462
/**
463-
* Returns a builder for the DatasetInfo object given it's project and user-defined id.
463+
* Returns a builder for the DatasetInfo object given it's user-defined project and dataset ids.
464464
*/
465465
public static Builder builder(String projectId, String datasetId) {
466466
return builder(DatasetId.of(projectId, datasetId));

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class Job extends JobInfo {
3737
private final BigQueryOptions options;
3838
private transient BigQuery bigquery;
3939

40-
static final class Builder extends JobInfo.Builder {
40+
public static final class Builder extends JobInfo.Builder {
4141

4242
private final BigQuery bigquery;
4343
private final JobInfo.BuilderImpl infoBuilder;
@@ -112,20 +112,6 @@ public Job build() {
112112
this.options = bigquery.options();
113113
}
114114

115-
/**
116-
* Creates a {@code Job} object for the provided job's user-defined id. Performs an RPC call to
117-
* get the latest job information.
118-
*
119-
* @param bigquery the BigQuery service used for issuing requests
120-
* @param job job's id, either user-defined or picked by the BigQuery service
121-
* @param options job options
122-
* @return the {@code Job} object or {@code null} if not found
123-
* @throws BigQueryException upon failure
124-
*/
125-
public static Job get(BigQuery bigquery, String job, BigQuery.JobOption... options) {
126-
return bigquery.getJob(job, options);
127-
}
128-
129115
/**
130116
* Checks if this job exists.
131117
*
@@ -164,7 +150,7 @@ public boolean isDone() {
164150
* @throws BigQueryException upon failure
165151
*/
166152
public Job reload(BigQuery.JobOption... options) {
167-
return Job.get(bigquery, jobId().job(), options);
153+
return bigquery.getJob(jobId().job(), options);
168154
}
169155

170156
/**

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

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class Table extends TableInfo {
4242
private final BigQueryOptions options;
4343
private transient BigQuery bigquery;
4444

45-
static class Builder extends TableInfo.Builder {
45+
public static class Builder extends TableInfo.Builder {
4646

4747
private final BigQuery bigquery;
4848
private final TableInfo.BuilderImpl infoBuilder;
@@ -129,36 +129,6 @@ public Table build() {
129129
this.options = bigquery.options();
130130
}
131131

132-
/**
133-
* Creates a {@code Table} object for the provided dataset and table's user-defined ids. Performs
134-
* an RPC call to get the latest table information.
135-
*
136-
* @param bigquery the BigQuery service used for issuing requests
137-
* @param dataset the dataset's user-defined id
138-
* @param table the table's user-defined id
139-
* @param options table options
140-
* @return the {@code Table} object or {@code null} if not found
141-
* @throws BigQueryException upon failure
142-
*/
143-
public static Table get(BigQuery bigquery, String dataset, String table,
144-
BigQuery.TableOption... options) {
145-
return get(bigquery, TableId.of(dataset, table), options);
146-
}
147-
148-
/**
149-
* Creates a {@code Table} object for the provided table identity. Performs an RPC call to get the
150-
* latest table information.
151-
*
152-
* @param bigquery the BigQuery service used for issuing requests
153-
* @param table the table's identity
154-
* @param options table options
155-
* @return the {@code Table} object or {@code null} if not found
156-
* @throws BigQueryException upon failure
157-
*/
158-
public static Table get(BigQuery bigquery, TableId table, BigQuery.TableOption... options) {
159-
return bigquery.getTable(table, options);
160-
}
161-
162132
/**
163133
* Checks if this table exists.
164134
*
@@ -177,7 +147,7 @@ public boolean exists() {
177147
* @throws BigQueryException upon failure
178148
*/
179149
public Table reload(BigQuery.TableOption... options) {
180-
return Table.get(bigquery, tableId(), options);
150+
return bigquery.getTable(tableId(), options);
181151
}
182152

183153
/**

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.junit.Assert.assertFalse;
2828
import static org.junit.Assert.assertNotNull;
2929
import static org.junit.Assert.assertNull;
30+
import static org.junit.Assert.assertSame;
3031
import static org.junit.Assert.assertTrue;
3132

3233
import com.google.common.collect.ImmutableList;
@@ -335,31 +336,18 @@ public void testCreateTableWithOptions() throws Exception {
335336
}
336337

337338
@Test
338-
public void testStaticGet() throws Exception {
339-
initializeExpectedDataset(3);
340-
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset())).andReturn(expectedDataset);
341-
replay(bigquery);
342-
Dataset loadedDataset = Dataset.get(bigquery, DATASET_INFO.datasetId().dataset());
343-
compareDataset(expectedDataset, loadedDataset);
344-
}
345-
346-
@Test
347-
public void testStaticGetNull() throws Exception {
339+
public void testBigquery() {
348340
initializeExpectedDataset(1);
349-
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset())).andReturn(null);
350341
replay(bigquery);
351-
assertNull(Dataset.get(bigquery, DATASET_INFO.datasetId().dataset()));
342+
assertSame(serviceMockReturnsOptions, expectedDataset.bigquery());
352343
}
353344

354345
@Test
355-
public void testStaticGetWithOptions() throws Exception {
356-
initializeExpectedDataset(3);
357-
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset(), BigQuery.DatasetOption.fields()))
358-
.andReturn(expectedDataset);
346+
public void testToAndFromPb() {
347+
initializeExpectedDataset(4);
359348
replay(bigquery);
360-
Dataset loadedDataset =
361-
Dataset.get(bigquery, DATASET_INFO.datasetId().dataset(), BigQuery.DatasetOption.fields());
362-
compareDataset(expectedDataset, loadedDataset);
349+
compareDataset(expectedDataset,
350+
Dataset.fromPb(serviceMockReturnsOptions, expectedDataset.toPb()));
363351
}
364352

365353
private void compareDataset(Dataset expected, Dataset value) {

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -223,38 +223,17 @@ public void testCancel() throws Exception {
223223
}
224224

225225
@Test
226-
public void testGet() throws Exception {
227-
initializeExpectedJob(3);
228-
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(expectedJob);
229-
replay(bigquery);
230-
Job loadedJob = Job.get(bigquery, JOB_INFO.jobId().job());
231-
compareJob(expectedJob, loadedJob);
232-
}
233-
234-
@Test
235-
public void testGetNull() throws Exception {
226+
public void testBigquery() {
236227
initializeExpectedJob(1);
237-
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(null);
238-
replay(bigquery);
239-
Job loadedJob = Job.get(bigquery, JOB_INFO.jobId().job());
240-
assertNull(loadedJob);
241-
}
242-
243-
@Test
244-
public void testGetWithOptions() throws Exception {
245-
initializeExpectedJob(3);
246-
expect(bigquery.getJob(JOB_INFO.jobId().job(), BigQuery.JobOption.fields()))
247-
.andReturn(expectedJob);
248228
replay(bigquery);
249-
Job loadedJob = Job.get(bigquery, JOB_INFO.jobId().job(), BigQuery.JobOption.fields());
250-
compareJob(expectedJob, loadedJob);
229+
assertSame(serviceMockReturnsOptions, expectedJob.bigquery());
251230
}
252231

253232
@Test
254-
public void testBigquery() {
255-
initializeExpectedJob(1);
233+
public void testToAndFromPb() {
234+
initializeExpectedJob(4);
256235
replay(bigquery);
257-
assertSame(serviceMockReturnsOptions, expectedJob.bigquery());
236+
compareJob(expectedJob, Job.fromPb(serviceMockReturnsOptions, expectedJob.toPb()));
258237
}
259238

260239
private void compareJob(Job expected, Job value) {

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

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -370,65 +370,17 @@ public void testExtractDataUris() throws Exception {
370370
}
371371

372372
@Test
373-
public void testGetFromId() throws Exception {
374-
initializeExpectedTable(3);
375-
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(expectedTable);
376-
replay(bigquery);
377-
Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId());
378-
compareTable(expectedTable, loadedTable);
379-
}
380-
381-
@Test
382-
public void testGetFromStrings() throws Exception {
383-
initializeExpectedTable(3);
384-
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(expectedTable);
385-
replay(bigquery);
386-
Table loadedTable = Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table());
387-
compareTable(expectedTable, loadedTable);
388-
}
389-
390-
@Test
391-
public void testGetFromIdNull() throws Exception {
392-
initializeExpectedTable(1);
393-
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
394-
replay(bigquery);
395-
assertNull(Table.get(bigquery, TABLE_ID1));
396-
}
397-
398-
@Test
399-
public void testGetFromStringsNull() throws Exception {
373+
public void testBigquery() {
400374
initializeExpectedTable(1);
401-
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
402375
replay(bigquery);
403-
assertNull(Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table()));
404-
}
405-
406-
@Test
407-
public void testGetFromIdWithOptions() throws Exception {
408-
initializeExpectedTable(3);
409-
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
410-
.andReturn(expectedTable);
411-
replay(bigquery);
412-
Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId(), BigQuery.TableOption.fields());
413-
compareTable(expectedTable, loadedTable);
414-
}
415-
416-
@Test
417-
public void testGetFromStringsWithOptions() throws Exception {
418-
initializeExpectedTable(3);
419-
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
420-
.andReturn(expectedTable);
421-
replay(bigquery);
422-
Table loadedTable =
423-
Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table(), BigQuery.TableOption.fields());
424-
compareTable(expectedTable, loadedTable);
376+
assertSame(serviceMockReturnsOptions, expectedTable.bigquery());
425377
}
426378

427379
@Test
428-
public void testBigquery() {
429-
initializeExpectedTable(1);
380+
public void testToAndFromPb() {
381+
initializeExpectedTable(4);
430382
replay(bigquery);
431-
assertSame(serviceMockReturnsOptions, expectedTable.bigquery());
383+
compareTable(expectedTable, Table.fromPb(serviceMockReturnsOptions, expectedTable.toPb()));
432384
}
433385

434386
private void compareTable(Table expected, Table value) {

gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public void run(BigQuery bigquery, JobId jobId) {
395395
private abstract static class CreateTableAction extends BigQueryAction<TableInfo> {
396396
@Override
397397
void run(BigQuery bigquery, TableInfo table) throws Exception {
398-
TableInfo createTable = bigquery.create(table);
398+
Table createTable = bigquery.create(table);
399399
System.out.println("Created table:");
400400
System.out.println(createTable.toString());
401401
}

0 commit comments

Comments
 (0)