Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BasePageFetcher #517

Merged
merged 1 commit into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.NextPageFetcher;
import com.google.gcloud.RetryHelper;
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
import com.google.gcloud.spi.BigQueryRpc;
Expand Down Expand Up @@ -69,36 +70,17 @@ 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 implements NextPageFetcher<DatasetInfo> {

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 = -3057564042439021278L;
private final Map<BigQueryRpc.Option, ?> requestOptions;
private final BigQueryOptions serviceOptions;

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

@Override
Expand All @@ -107,14 +89,18 @@ public Page<DatasetInfo> nextPage() {
}
}

private static class TablePageFetcher extends BasePageFetcher<BaseTableInfo> {
private static class TablePageFetcher implements NextPageFetcher<BaseTableInfo> {

private static final long serialVersionUID = 5908129355985236115L;
private static final long serialVersionUID = 8611248840504201187L;
private final Map<BigQueryRpc.Option, ?> requestOptions;
private final BigQueryOptions serviceOptions;
private final String dataset;

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

Expand All @@ -124,13 +110,17 @@ public Page<BaseTableInfo> nextPage() {
}
}

private static class JobPageFetcher extends BasePageFetcher<JobInfo> {
private static class JobPageFetcher implements NextPageFetcher<JobInfo> {

private static final long serialVersionUID = -4984845360519279880L;
private static final long serialVersionUID = 8536533282558245472L;
private final Map<BigQueryRpc.Option, ?> requestOptions;
private final BigQueryOptions serviceOptions;

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

@Override
Expand All @@ -139,14 +129,18 @@ public Page<JobInfo> nextPage() {
}
}

private static class TableDataPageFetcher extends BasePageFetcher<List<FieldValue>> {
private static class TableDataPageFetcher implements NextPageFetcher<List<FieldValue>> {

private static final long serialVersionUID = 1281938239570262432L;
private static final long serialVersionUID = -8501991114794410114L;
private final Map<BigQueryRpc.Option, ?> requestOptions;
private final BigQueryOptions serviceOptions;
private final TableId table;

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

Expand All @@ -156,15 +150,19 @@ public Page<List<FieldValue>> nextPage() {
}
}

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

private static final long serialVersionUID = 6713948754731557486L;
private static final long serialVersionUID = -9198905840550459803L;
private final Map<BigQueryRpc.Option, ?> requestOptions;
private final BigQueryOptions serviceOptions;
private final JobId job;

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

Expand Down
28 changes: 27 additions & 1 deletion 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 Down Expand Up @@ -85,7 +87,7 @@ public Iterable<T> values() {

@Override
public Iterator<T> iterateAll() {
return new PageIterator<T>(this);
return new PageIterator<>(this);
}

@Override
Expand Down Expand Up @@ -115,4 +117,28 @@ public boolean equals(Object obj) {
return Objects.equals(cursor, other.cursor)
&& Objects.equals(results, other.results);
}

/**
* Utility method to construct the options map for the next page request.
*
* @param <T> the value type that the page holds. Instances of {@code T} should be
* {@code Serializable}
* @param pageTokenOption the key for the next page cursor option in the options map
* @param cursor the cursor for the next page
* @param optionMap the previous options map
* @return the options map for the next page request
*/
public static <T> Map<T, Object> nextRequestOptions(
T pageTokenOption, String cursor, Map<T, ?> optionMap) {
ImmutableMap.Builder<T, Object> builder = ImmutableMap.builder();
if (cursor != null) {
builder.put(pageTokenOption, cursor);
}
for (Map.Entry<T, ?> option : optionMap.entrySet()) {
if (!Objects.equals(option.getKey(), pageTokenOption)) {
builder.put(option.getKey(), option.getValue());
}
}
return builder.build();
}
}
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.NextPageFetcher;
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,37 +117,17 @@ public com.google.api.services.cloudresourcemanager.model.Project call() {
}
}

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

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 final long serialVersionUID = -533306655445189098L;
private static final long serialVersionUID = 2158209410430566961L;
private final Map<ResourceManagerRpc.Option, ?> requestOptions;
private final ResourceManagerOptions serviceOptions;

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

@Override
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.NextPageFetcher;
import com.google.gcloud.RetryHelper.RetryHelperException;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.RewriteResponse;
Expand Down Expand Up @@ -209,36 +210,18 @@ 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 implements NextPageFetcher<BucketInfo> {

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

BasePageFetcher(StorageOptions serviceOptions, String cursor,
BucketPageFetcher(
StorageOptions serviceOptions, String cursor,
Map<StorageRpc.Option, ?> optionMap) {
this.requestOptions =
PageImpl.nextRequestOptions(StorageRpc.Option.PAGE_TOKEN, cursor, 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;

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

@Override
Expand All @@ -247,14 +230,18 @@ public Page<BucketInfo> nextPage() {
}
}

private static class BlobPageFetcher extends BasePageFetcher<BlobInfo> {
private static class BlobPageFetcher implements NextPageFetcher<BlobInfo> {

private static final long serialVersionUID = -5490616010200159174L;
private static final long serialVersionUID = 81807334445874098L;
private final Map<StorageRpc.Option, ?> requestOptions;
private final StorageOptions serviceOptions;
private final String bucket;

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

Expand Down