Skip to content

Commit

Permalink
Refactor BasePageFetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Jan 5, 2016
1 parent 823f0de commit 5628d7f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.gcloud.ExceptionHandler.Interceptor;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.PageImpl.BasePageFetcher;
import com.google.gcloud.RetryHelper;
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
import com.google.gcloud.spi.BigQueryRpc;
Expand Down Expand Up @@ -69,108 +70,93 @@ public RetryResult beforeEval(Exception exception) {
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();

private abstract static class BasePageFetcher<T> implements PageImpl.NextPageFetcher<T> {
private static class DatasetPageFetcher extends BasePageFetcher<DatasetInfo, BigQueryOptions> {

private static final long serialVersionUID = -338124488600215401L;

protected final Map<BigQueryRpc.Option, ?> requestOptions;
protected final BigQueryOptions serviceOptions;

BasePageFetcher(BigQueryOptions serviceOptions, String cursor,
Map<BigQueryRpc.Option, ?> optionMap) {
this.serviceOptions = serviceOptions;
ImmutableMap.Builder<BigQueryRpc.Option, Object> builder = ImmutableMap.builder();
if (cursor != null) {
builder.put(BigQueryRpc.Option.PAGE_TOKEN, cursor);
}
for (Map.Entry<BigQueryRpc.Option, ?> option : optionMap.entrySet()) {
if (option.getKey() != BigQueryRpc.Option.PAGE_TOKEN) {
builder.put(option.getKey(), option.getValue());
}
}
this.requestOptions = builder.build();
}
}

private static class DatasetPageFetcher extends BasePageFetcher<DatasetInfo> {

private static final long serialVersionUID = 3030824397616608646L;
private static final long serialVersionUID = 8049465230453759547L;

DatasetPageFetcher(BigQueryOptions serviceOptions, String cursor,
Map<BigQueryRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
}

@SuppressWarnings("unchecked")
@Override
public Page<DatasetInfo> nextPage() {
return listDatasets(serviceOptions, requestOptions);
return listDatasets(serviceOptions, (Map<BigQueryRpc.Option, ?>) requestOptions);
}
}

private static class TablePageFetcher extends BasePageFetcher<BaseTableInfo> {
private static class TablePageFetcher extends BasePageFetcher<BaseTableInfo, BigQueryOptions> {

private static final long serialVersionUID = 5908129355985236115L;
private static final long serialVersionUID = 6542604186608591265L;
private final String dataset;

TablePageFetcher(String dataset, BigQueryOptions serviceOptions, String cursor,
Map<BigQueryRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.dataset = dataset;
}

@SuppressWarnings("unchecked")
@Override
public Page<BaseTableInfo> nextPage() {
return listTables(dataset, serviceOptions, requestOptions);
return listTables(dataset, serviceOptions, (Map<BigQueryRpc.Option, ?>) requestOptions);
}
}

private static class JobPageFetcher extends BasePageFetcher<JobInfo> {
private static class JobPageFetcher extends BasePageFetcher<JobInfo, BigQueryOptions> {

private static final long serialVersionUID = -4984845360519279880L;
private static final long serialVersionUID = 1983484324002958994L;

JobPageFetcher(BigQueryOptions serviceOptions, String cursor,
Map<BigQueryRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
}

@SuppressWarnings("unchecked")
@Override
public Page<JobInfo> nextPage() {
return listJobs(serviceOptions, requestOptions);
return listJobs(serviceOptions, (Map<BigQueryRpc.Option, ?>) requestOptions);
}
}

private static class TableDataPageFetcher extends BasePageFetcher<List<FieldValue>> {
private static class TableDataPageFetcher
extends BasePageFetcher<List<FieldValue>, BigQueryOptions> {

private static final long serialVersionUID = 1281938239570262432L;
private static final long serialVersionUID = 7767953898927019188L;
private final TableId table;

TableDataPageFetcher(TableId table, BigQueryOptions serviceOptions, String cursor,
Map<BigQueryRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.table = table;
}

@SuppressWarnings("unchecked")
@Override
public Page<List<FieldValue>> nextPage() {
return listTableData(table, serviceOptions, requestOptions);
return listTableData(table, serviceOptions, (Map<BigQueryRpc.Option, ?>) requestOptions);
}
}

private static class QueryResultsPageFetcherImpl extends BasePageFetcher<List<FieldValue>>
private static class QueryResultsPageFetcherImpl
extends BasePageFetcher<List<FieldValue>, BigQueryOptions>
implements QueryResult.QueryResultsPageFetcher {

private static final long serialVersionUID = 6713948754731557486L;
private static final long serialVersionUID = 6377034420634949203L;
private final JobId job;

QueryResultsPageFetcherImpl(JobId job, BigQueryOptions serviceOptions, String cursor,
Map<BigQueryRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.job = job;
}

@SuppressWarnings("unchecked")
@Override
public QueryResult nextPage() {
return getQueryResults(job, serviceOptions, requestOptions).result();
return getQueryResults(job, serviceOptions, (Map<BigQueryRpc.Option, ?>) requestOptions)
.result();
}
}

Expand Down
33 changes: 33 additions & 0 deletions gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package com.google.gcloud;

import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableMap;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
Expand All @@ -38,6 +40,37 @@ public interface NextPageFetcher<T> extends Serializable {
Page<T> nextPage();
}

/**
* Base class for page fetchers.
*
* @param <T> the value type that the page holds. {@code T} should implement
* {@code java.io.Serializable}
* @param <O> the service options type
*/
@SuppressWarnings("rawtypes")
public abstract static class BasePageFetcher<T, O extends ServiceOptions>
implements NextPageFetcher<T> {

private static final long serialVersionUID = -3333056232729661990L;
protected final Map<?, ?> requestOptions;
protected final O serviceOptions;

protected BasePageFetcher(O serviceOptions, Enum<?> pageTokenOption,
String cursor, Map<?, ?> optionMap) {
this.serviceOptions = serviceOptions;
ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
if (cursor != null) {
builder.put(pageTokenOption, cursor);
}
for (Map.Entry<?, ?> option : optionMap.entrySet()) {
if (option.getKey() != pageTokenOption) {
builder.put(option.getKey(), option.getValue());
}
}
this.requestOptions = builder.build();
}
}

static class PageIterator<T> extends AbstractIterator<T> {

private Iterator<T> currentPageIterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import com.google.gcloud.ExceptionHandler.Interceptor;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.PageImpl.BasePageFetcher;
import com.google.gcloud.RetryHelper.RetryHelperException;
import com.google.gcloud.spi.ResourceManagerRpc;
import com.google.gcloud.spi.ResourceManagerRpc.Tuple;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -117,42 +117,20 @@ public com.google.api.services.cloudresourcemanager.model.Project call() {
}
}

private abstract static class BasePageFetcher<T extends Serializable>
implements PageImpl.NextPageFetcher<T> {

private static final long serialVersionUID = -5560906434575940205L;

protected final Map<ResourceManagerRpc.Option, ?> requestOptions;
protected final ResourceManagerOptions serviceOptions;

BasePageFetcher(ResourceManagerOptions serviceOptions, String cursor,
Map<ResourceManagerRpc.Option, ?> optionMap) {
this.serviceOptions = serviceOptions;
ImmutableMap.Builder<ResourceManagerRpc.Option, Object> builder = ImmutableMap.builder();
if (cursor != null) {
builder.put(ResourceManagerRpc.Option.PAGE_TOKEN, cursor);
}
for (Map.Entry<ResourceManagerRpc.Option, ?> option : optionMap.entrySet()) {
if (option.getKey() != ResourceManagerRpc.Option.PAGE_TOKEN) {
builder.put(option.getKey(), option.getValue());
}
}
this.requestOptions = builder.build();
}
}

private static class ProjectPageFetcher extends BasePageFetcher<ProjectInfo> {
private static class ProjectPageFetcher
extends BasePageFetcher<ProjectInfo, ResourceManagerOptions> {

private static final long serialVersionUID = -533306655445189098L;

ProjectPageFetcher(ResourceManagerOptions serviceOptions, String cursor,
Map<ResourceManagerRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, ResourceManagerRpc.Option.PAGE_TOKEN, cursor, optionMap);
}

@SuppressWarnings("unchecked")
@Override
public Page<ProjectInfo> nextPage() {
return listProjects(serviceOptions, requestOptions);
return listProjects(serviceOptions, (Map<ResourceManagerRpc.Option, ?>) requestOptions);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.gcloud.ExceptionHandler.Interceptor;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.PageImpl.BasePageFetcher;
import com.google.gcloud.RetryHelper.RetryHelperException;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.RewriteResponse;
Expand Down Expand Up @@ -209,58 +210,37 @@ public BlobInfo get(BlobId blob) {
return get(blob, new BlobGetOption[0]);
}

private abstract static class BasePageFetcher<T extends Serializable>
implements PageImpl.NextPageFetcher<T> {
private static class BucketPageFetcher extends BasePageFetcher<BucketInfo, StorageOptions> {

private static final long serialVersionUID = 8236329004030295223L;
protected final Map<StorageRpc.Option, ?> requestOptions;
protected final StorageOptions serviceOptions;

BasePageFetcher(StorageOptions serviceOptions, String cursor,
Map<StorageRpc.Option, ?> optionMap) {
this.serviceOptions = serviceOptions;
ImmutableMap.Builder<StorageRpc.Option, Object> builder = ImmutableMap.builder();
if (cursor != null) {
builder.put(StorageRpc.Option.PAGE_TOKEN, cursor);
}
for (Map.Entry<StorageRpc.Option, ?> option : optionMap.entrySet()) {
if (option.getKey() != StorageRpc.Option.PAGE_TOKEN) {
builder.put(option.getKey(), option.getValue());
}
}
this.requestOptions = builder.build();
}
}

private static class BucketPageFetcher extends BasePageFetcher<BucketInfo> {

private static final long serialVersionUID = -5490616010200159174L;
private static final long serialVersionUID = -2456686089489944533L;

BucketPageFetcher(StorageOptions serviceOptions, String cursor,
Map<StorageRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, StorageRpc.Option.PAGE_TOKEN, cursor, optionMap);
}

@SuppressWarnings("unchecked")
@Override
public Page<BucketInfo> nextPage() {
return listBuckets(serviceOptions, requestOptions);
return listBuckets(serviceOptions, (Map<StorageRpc.Option, ?>) requestOptions);
}
}

private static class BlobPageFetcher extends BasePageFetcher<BlobInfo> {
private static class BlobPageFetcher extends BasePageFetcher<BlobInfo, StorageOptions> {

private static final long serialVersionUID = -5490616010200159174L;
private static final long serialVersionUID = 3841753010333660702L;
private final String bucket;

BlobPageFetcher(String bucket, StorageOptions serviceOptions, String cursor,
Map<StorageRpc.Option, ?> optionMap) {
super(serviceOptions, cursor, optionMap);
super(serviceOptions, StorageRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.bucket = bucket;
}

@SuppressWarnings("unchecked")
@Override
public Page<BlobInfo> nextPage() {
return listBlobs(bucket, serviceOptions, requestOptions);
return listBlobs(bucket, serviceOptions, (Map<StorageRpc.Option, ?>) requestOptions);
}
}

Expand Down

0 comments on commit 5628d7f

Please sign in to comment.