Skip to content

Commit

Permalink
Minor refactoring bigquery functional objects
Browse files Browse the repository at this point in the history
- Make Builder public
- Make Dataset.builder package scope
- Add toAndFromPb test
- Remove static get methods
  • Loading branch information
mziccard committed Feb 3, 2016
1 parent b0c0bb7 commit 94533b2
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {

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

/**
* Lists the tables in the dataset. This method returns partial information on each table
* ({@link TableInfo#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
* ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
* is part of {@link Table#definition()}). To get complete information use either
* {@link #getTable(TableId, TableOption...)} or
* {@link #getTable(String, String, TableOption...)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class Dataset extends DatasetInfo {
private final BigQueryOptions options;
private transient BigQuery bigquery;

static final class Builder extends DatasetInfo.Builder {
public static final class Builder extends DatasetInfo.Builder {

private final BigQuery bigquery;
private final DatasetInfo.BuilderImpl infoBuilder;
Expand Down Expand Up @@ -134,20 +134,6 @@ public Dataset build() {
this.options = bigquery.options();
}

/**
* Creates a {@code Dataset} object for the provided dataset's user-defined id. Performs an RPC
* call to get the latest dataset information.
*
* @param bigquery the BigQuery service used for issuing requests
* @param dataset dataset's user-defined id
* @param options dataset options
* @return the {@code Dataset} object or {@code null} if not found
* @throws BigQueryException upon failure
*/
public static Dataset get(BigQuery bigquery, String dataset, BigQuery.DatasetOption... options) {
return bigquery.getDataset(dataset, options);
}

/**
* Checks if this dataset exists.
*
Expand All @@ -167,7 +153,7 @@ public boolean exists() {
* @throws BigQueryException upon failure
*/
public Dataset reload(BigQuery.DatasetOption... options) {
return Dataset.get(bigquery, datasetId().dataset(), options);
return bigquery.getDataset(datasetId().dataset(), options);
}

/**
Expand Down Expand Up @@ -234,14 +220,11 @@ public BigQuery bigquery() {
return bigquery;
}

public static Builder builder(BigQuery bigquery, DatasetId datasetId) {
static Builder builder(BigQuery bigquery, DatasetId datasetId) {
return new Builder(bigquery).datasetId(datasetId);
}

/**
* Returns a builder for a {@code DatasetInfo} object given it's user-defined id.
*/
public static Builder builder(BigQuery bigquery, String datasetId) {
static Builder builder(BigQuery bigquery, String datasetId) {
return builder(bigquery, DatasetId.of(datasetId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public static Builder builder(String datasetId) {
}

/**
* Returns a builder for the DatasetInfo object given it's project and user-defined id.
* Returns a builder for the DatasetInfo object given it's user-defined project and dataset ids.
*/
public static Builder builder(String projectId, String datasetId) {
return builder(DatasetId.of(projectId, datasetId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class Job extends JobInfo {
private final BigQueryOptions options;
private transient BigQuery bigquery;

static final class Builder extends JobInfo.Builder {
public static final class Builder extends JobInfo.Builder {

private final BigQuery bigquery;
private final JobInfo.BuilderImpl infoBuilder;
Expand Down Expand Up @@ -112,20 +112,6 @@ public Job build() {
this.options = bigquery.options();
}

/**
* Creates a {@code Job} object for the provided job's user-defined id. Performs an RPC call to
* get the latest job information.
*
* @param bigquery the BigQuery service used for issuing requests
* @param job job's id, either user-defined or picked by the BigQuery service
* @param options job options
* @return the {@code Job} object or {@code null} if not found
* @throws BigQueryException upon failure
*/
public static Job get(BigQuery bigquery, String job, BigQuery.JobOption... options) {
return bigquery.getJob(job, options);
}

/**
* Checks if this job exists.
*
Expand Down Expand Up @@ -164,7 +150,7 @@ public boolean isDone() {
* @throws BigQueryException upon failure
*/
public Job reload(BigQuery.JobOption... options) {
return Job.get(bigquery, jobId().job(), options);
return bigquery.getJob(jobId().job(), options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class Table extends TableInfo {
private final BigQueryOptions options;
private transient BigQuery bigquery;

static class Builder extends TableInfo.Builder {
public static class Builder extends TableInfo.Builder {

private final BigQuery bigquery;
private final TableInfo.BuilderImpl infoBuilder;
Expand Down Expand Up @@ -129,36 +129,6 @@ public Table build() {
this.options = bigquery.options();
}

/**
* Creates a {@code Table} object for the provided dataset and table's user-defined ids. Performs
* an RPC call to get the latest table information.
*
* @param bigquery the BigQuery service used for issuing requests
* @param dataset the dataset's user-defined id
* @param table the table's user-defined id
* @param options table options
* @return the {@code Table} object or {@code null} if not found
* @throws BigQueryException upon failure
*/
public static Table get(BigQuery bigquery, String dataset, String table,
BigQuery.TableOption... options) {
return get(bigquery, TableId.of(dataset, table), options);
}

/**
* Creates a {@code Table} object for the provided table identity. Performs an RPC call to get the
* latest table information.
*
* @param bigquery the BigQuery service used for issuing requests
* @param table the table's identity
* @param options table options
* @return the {@code Table} object or {@code null} if not found
* @throws BigQueryException upon failure
*/
public static Table get(BigQuery bigquery, TableId table, BigQuery.TableOption... options) {
return bigquery.getTable(table, options);
}

/**
* Checks if this table exists.
*
Expand All @@ -177,7 +147,7 @@ public boolean exists() {
* @throws BigQueryException upon failure
*/
public Table reload(BigQuery.TableOption... options) {
return Table.get(bigquery, tableId(), options);
return bigquery.getTable(tableId(), options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

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

@Test
public void testStaticGet() throws Exception {
initializeExpectedDataset(3);
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset())).andReturn(expectedDataset);
replay(bigquery);
Dataset loadedDataset = Dataset.get(bigquery, DATASET_INFO.datasetId().dataset());
compareDataset(expectedDataset, loadedDataset);
}

@Test
public void testStaticGetNull() throws Exception {
public void testBigquery() {
initializeExpectedDataset(1);
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset())).andReturn(null);
replay(bigquery);
assertNull(Dataset.get(bigquery, DATASET_INFO.datasetId().dataset()));
assertSame(serviceMockReturnsOptions, expectedDataset.bigquery());
}

@Test
public void testStaticGetWithOptions() throws Exception {
initializeExpectedDataset(3);
expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset(), BigQuery.DatasetOption.fields()))
.andReturn(expectedDataset);
public void testToAndFromPb() {
initializeExpectedDataset(4);
replay(bigquery);
Dataset loadedDataset =
Dataset.get(bigquery, DATASET_INFO.datasetId().dataset(), BigQuery.DatasetOption.fields());
compareDataset(expectedDataset, loadedDataset);
compareDataset(expectedDataset,
Dataset.fromPb(serviceMockReturnsOptions, expectedDataset.toPb()));
}

private void compareDataset(Dataset expected, Dataset value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,17 @@ public void testCancel() throws Exception {
}

@Test
public void testGet() throws Exception {
initializeExpectedJob(3);
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(expectedJob);
replay(bigquery);
Job loadedJob = Job.get(bigquery, JOB_INFO.jobId().job());
compareJob(expectedJob, loadedJob);
}

@Test
public void testGetNull() throws Exception {
public void testBigquery() {
initializeExpectedJob(1);
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(null);
replay(bigquery);
Job loadedJob = Job.get(bigquery, JOB_INFO.jobId().job());
assertNull(loadedJob);
}

@Test
public void testGetWithOptions() throws Exception {
initializeExpectedJob(3);
expect(bigquery.getJob(JOB_INFO.jobId().job(), BigQuery.JobOption.fields()))
.andReturn(expectedJob);
replay(bigquery);
Job loadedJob = Job.get(bigquery, JOB_INFO.jobId().job(), BigQuery.JobOption.fields());
compareJob(expectedJob, loadedJob);
assertSame(serviceMockReturnsOptions, expectedJob.bigquery());
}

@Test
public void testBigquery() {
initializeExpectedJob(1);
public void testToAndFromPb() {
initializeExpectedJob(4);
replay(bigquery);
assertSame(serviceMockReturnsOptions, expectedJob.bigquery());
compareJob(expectedJob, Job.fromPb(serviceMockReturnsOptions, expectedJob.toPb()));
}

private void compareJob(Job expected, Job value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,65 +370,17 @@ public void testExtractDataUris() throws Exception {
}

@Test
public void testGetFromId() throws Exception {
initializeExpectedTable(3);
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(expectedTable);
replay(bigquery);
Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId());
compareTable(expectedTable, loadedTable);
}

@Test
public void testGetFromStrings() throws Exception {
initializeExpectedTable(3);
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(expectedTable);
replay(bigquery);
Table loadedTable = Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table());
compareTable(expectedTable, loadedTable);
}

@Test
public void testGetFromIdNull() throws Exception {
initializeExpectedTable(1);
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
replay(bigquery);
assertNull(Table.get(bigquery, TABLE_ID1));
}

@Test
public void testGetFromStringsNull() throws Exception {
public void testBigquery() {
initializeExpectedTable(1);
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
replay(bigquery);
assertNull(Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table()));
}

@Test
public void testGetFromIdWithOptions() throws Exception {
initializeExpectedTable(3);
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
.andReturn(expectedTable);
replay(bigquery);
Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId(), BigQuery.TableOption.fields());
compareTable(expectedTable, loadedTable);
}

@Test
public void testGetFromStringsWithOptions() throws Exception {
initializeExpectedTable(3);
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
.andReturn(expectedTable);
replay(bigquery);
Table loadedTable =
Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table(), BigQuery.TableOption.fields());
compareTable(expectedTable, loadedTable);
assertSame(serviceMockReturnsOptions, expectedTable.bigquery());
}

@Test
public void testBigquery() {
initializeExpectedTable(1);
public void testToAndFromPb() {
initializeExpectedTable(4);
replay(bigquery);
assertSame(serviceMockReturnsOptions, expectedTable.bigquery());
compareTable(expectedTable, Table.fromPb(serviceMockReturnsOptions, expectedTable.toPb()));
}

private void compareTable(Table expected, Table value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public void run(BigQuery bigquery, JobId jobId) {
private abstract static class CreateTableAction extends BigQueryAction<TableInfo> {
@Override
void run(BigQuery bigquery, TableInfo table) throws Exception {
TableInfo createTable = bigquery.create(table);
Table createTable = bigquery.create(table);
System.out.println("Created table:");
System.out.println(createTable.toString());
}
Expand Down

0 comments on commit 94533b2

Please sign in to comment.