Skip to content

Commit cbdfecc

Browse files
author
Ajay Kannan
committed
Refactor page fetchers
1 parent 8d37c32 commit cbdfecc

File tree

4 files changed

+91
-100
lines changed

4 files changed

+91
-100
lines changed

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

+38-40
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.gcloud.ExceptionHandler.Interceptor;
3939
import com.google.gcloud.Page;
4040
import com.google.gcloud.PageImpl;
41+
import com.google.gcloud.PageImpl.NextPageFetcher;
4142
import com.google.gcloud.RetryHelper;
4243
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
4344
import com.google.gcloud.spi.BigQueryRpc;
@@ -66,39 +67,20 @@ public RetryResult beforeEval(Exception exception) {
6667
return Interceptor.RetryResult.CONTINUE_EVALUATION;
6768
}
6869
};
69-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
70+
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
7071
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
7172

72-
private abstract static class BasePageFetcher<T> implements PageImpl.NextPageFetcher<T> {
73+
private static class DatasetPageFetcher implements NextPageFetcher<DatasetInfo> {
7374

74-
private static final long serialVersionUID = -338124488600215401L;
75-
76-
protected final Map<BigQueryRpc.Option, ?> requestOptions;
77-
protected final BigQueryOptions serviceOptions;
78-
79-
BasePageFetcher(BigQueryOptions serviceOptions, String cursor,
80-
Map<BigQueryRpc.Option, ?> optionMap) {
81-
this.serviceOptions = serviceOptions;
82-
ImmutableMap.Builder<BigQueryRpc.Option, Object> builder = ImmutableMap.builder();
83-
if (cursor != null) {
84-
builder.put(BigQueryRpc.Option.PAGE_TOKEN, cursor);
85-
}
86-
for (Map.Entry<BigQueryRpc.Option, ?> option : optionMap.entrySet()) {
87-
if (option.getKey() != BigQueryRpc.Option.PAGE_TOKEN) {
88-
builder.put(option.getKey(), option.getValue());
89-
}
90-
}
91-
this.requestOptions = builder.build();
92-
}
93-
}
94-
95-
private static class DatasetPageFetcher extends BasePageFetcher<DatasetInfo> {
96-
97-
private static final long serialVersionUID = 3030824397616608646L;
75+
private static final long serialVersionUID = -3057564042439021278L;
76+
private final Map<BigQueryRpc.Option, ?> requestOptions;
77+
private final BigQueryOptions serviceOptions;
9878

9979
DatasetPageFetcher(BigQueryOptions serviceOptions, String cursor,
10080
Map<BigQueryRpc.Option, ?> optionMap) {
101-
super(serviceOptions, cursor, optionMap);
81+
this.requestOptions =
82+
PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
83+
this.serviceOptions = serviceOptions;
10284
}
10385

10486
@Override
@@ -107,14 +89,18 @@ public Page<DatasetInfo> nextPage() {
10789
}
10890
}
10991

110-
private static class TablePageFetcher extends BasePageFetcher<BaseTableInfo> {
92+
private static class TablePageFetcher implements NextPageFetcher<BaseTableInfo> {
11193

112-
private static final long serialVersionUID = 5908129355985236115L;
94+
private static final long serialVersionUID = 8611248840504201187L;
95+
private final Map<BigQueryRpc.Option, ?> requestOptions;
96+
private final BigQueryOptions serviceOptions;
11397
private final String dataset;
11498

11599
TablePageFetcher(String dataset, BigQueryOptions serviceOptions, String cursor,
116100
Map<BigQueryRpc.Option, ?> optionMap) {
117-
super(serviceOptions, cursor, optionMap);
101+
this.requestOptions =
102+
PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
103+
this.serviceOptions = serviceOptions;
118104
this.dataset = dataset;
119105
}
120106

@@ -124,13 +110,17 @@ public Page<BaseTableInfo> nextPage() {
124110
}
125111
}
126112

127-
private static class JobPageFetcher extends BasePageFetcher<JobInfo> {
113+
private static class JobPageFetcher implements NextPageFetcher<JobInfo> {
128114

129-
private static final long serialVersionUID = -4984845360519279880L;
115+
private static final long serialVersionUID = 8536533282558245472L;
116+
private final Map<BigQueryRpc.Option, ?> requestOptions;
117+
private final BigQueryOptions serviceOptions;
130118

131119
JobPageFetcher(BigQueryOptions serviceOptions, String cursor,
132120
Map<BigQueryRpc.Option, ?> optionMap) {
133-
super(serviceOptions, cursor, optionMap);
121+
this.requestOptions =
122+
PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
123+
this.serviceOptions = serviceOptions;
134124
}
135125

136126
@Override
@@ -139,14 +129,18 @@ public Page<JobInfo> nextPage() {
139129
}
140130
}
141131

142-
private static class TableDataPageFetcher extends BasePageFetcher<List<FieldValue>> {
132+
private static class TableDataPageFetcher implements NextPageFetcher<List<FieldValue>> {
143133

144-
private static final long serialVersionUID = 1281938239570262432L;
134+
private static final long serialVersionUID = -8501991114794410114L;
135+
private final Map<BigQueryRpc.Option, ?> requestOptions;
136+
private final BigQueryOptions serviceOptions;
145137
private final TableId table;
146138

147139
TableDataPageFetcher(TableId table, BigQueryOptions serviceOptions, String cursor,
148140
Map<BigQueryRpc.Option, ?> optionMap) {
149-
super(serviceOptions, cursor, optionMap);
141+
this.requestOptions =
142+
PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
143+
this.serviceOptions = serviceOptions;
150144
this.table = table;
151145
}
152146

@@ -156,15 +150,19 @@ public Page<List<FieldValue>> nextPage() {
156150
}
157151
}
158152

159-
private static class QueryResultsPageFetcherImpl extends BasePageFetcher<List<FieldValue>>
160-
implements QueryResult.QueryResultsPageFetcher {
153+
private static class QueryResultsPageFetcherImpl
154+
implements NextPageFetcher<List<FieldValue>>, QueryResult.QueryResultsPageFetcher {
161155

162-
private static final long serialVersionUID = 6713948754731557486L;
156+
private static final long serialVersionUID = -9198905840550459803L;
157+
private final Map<BigQueryRpc.Option, ?> requestOptions;
158+
private final BigQueryOptions serviceOptions;
163159
private final JobId job;
164160

165161
QueryResultsPageFetcherImpl(JobId job, BigQueryOptions serviceOptions, String cursor,
166162
Map<BigQueryRpc.Option, ?> optionMap) {
167-
super(serviceOptions, cursor, optionMap);
163+
this.requestOptions =
164+
PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
165+
this.serviceOptions = serviceOptions;
168166
this.job = job;
169167
}
170168

gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package com.google.gcloud;
1818

1919
import com.google.common.collect.AbstractIterator;
20+
import com.google.common.collect.ImmutableMap;
2021

2122
import java.io.Serializable;
2223
import java.util.Collections;
2324
import java.util.Iterator;
25+
import java.util.Map;
2426
import java.util.Objects;
2527

2628
/**
@@ -85,7 +87,7 @@ public Iterable<T> values() {
8587

8688
@Override
8789
public Iterator<T> iterateAll() {
88-
return new PageIterator<T>(this);
90+
return new PageIterator<>(this);
8991
}
9092

9193
@Override
@@ -115,4 +117,28 @@ public boolean equals(Object obj) {
115117
return Objects.equals(cursor, other.cursor)
116118
&& Objects.equals(results, other.results);
117119
}
120+
121+
/**
122+
* Utility method to construct the options map for the next page request.
123+
*
124+
* @param <T> the value type that the page holds. Instances of {@code T} should be
125+
* {@code Serializable}
126+
* @param pageTokenOption the key for the next page cursor option in the options map
127+
* @param cursor the cursor for the next page
128+
* @param optionMap the previous options map
129+
* @return the options map for the next page request
130+
*/
131+
public static <T> Map<T, Object> nextRequestOptions(
132+
T pageTokenOption, String cursor, Map<T, ?> optionMap) {
133+
ImmutableMap.Builder<T, Object> builder = ImmutableMap.builder();
134+
if (cursor != null) {
135+
builder.put(pageTokenOption, cursor);
136+
}
137+
for (Map.Entry<T, ?> option : optionMap.entrySet()) {
138+
if (!Objects.equals(option.getKey(), pageTokenOption)) {
139+
builder.put(option.getKey(), option.getValue());
140+
}
141+
}
142+
return builder.build();
143+
}
118144
}

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java

+9-29
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
import com.google.gcloud.ExceptionHandler.Interceptor;
3030
import com.google.gcloud.Page;
3131
import com.google.gcloud.PageImpl;
32+
import com.google.gcloud.PageImpl.NextPageFetcher;
3233
import com.google.gcloud.RetryHelper.RetryHelperException;
3334
import com.google.gcloud.spi.ResourceManagerRpc;
3435
import com.google.gcloud.spi.ResourceManagerRpc.Tuple;
3536

36-
import java.io.Serializable;
3737
import java.util.Map;
3838
import java.util.concurrent.Callable;
3939

@@ -58,7 +58,7 @@ public RetryResult beforeEval(Exception exception) {
5858
return Interceptor.RetryResult.CONTINUE_EVALUATION;
5959
}
6060
};
61-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
61+
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
6262
.abortOn(RuntimeException.class)
6363
.interceptor(EXCEPTION_HANDLER_INTERCEPTOR)
6464
.build();
@@ -117,37 +117,17 @@ public com.google.api.services.cloudresourcemanager.model.Project call() {
117117
}
118118
}
119119

120-
private abstract static class BasePageFetcher<T extends Serializable>
121-
implements PageImpl.NextPageFetcher<T> {
120+
private static class ProjectPageFetcher implements NextPageFetcher<ProjectInfo> {
122121

123-
private static final long serialVersionUID = -5560906434575940205L;
124-
125-
protected final Map<ResourceManagerRpc.Option, ?> requestOptions;
126-
protected final ResourceManagerOptions serviceOptions;
127-
128-
BasePageFetcher(ResourceManagerOptions serviceOptions, String cursor,
129-
Map<ResourceManagerRpc.Option, ?> optionMap) {
130-
this.serviceOptions = serviceOptions;
131-
ImmutableMap.Builder<ResourceManagerRpc.Option, Object> builder = ImmutableMap.builder();
132-
if (cursor != null) {
133-
builder.put(ResourceManagerRpc.Option.PAGE_TOKEN, cursor);
134-
}
135-
for (Map.Entry<ResourceManagerRpc.Option, ?> option : optionMap.entrySet()) {
136-
if (option.getKey() != ResourceManagerRpc.Option.PAGE_TOKEN) {
137-
builder.put(option.getKey(), option.getValue());
138-
}
139-
}
140-
this.requestOptions = builder.build();
141-
}
142-
}
143-
144-
private static class ProjectPageFetcher extends BasePageFetcher<ProjectInfo> {
145-
146-
private static final long serialVersionUID = -533306655445189098L;
122+
private static final long serialVersionUID = 2158209410430566961L;
123+
private final Map<ResourceManagerRpc.Option, ?> requestOptions;
124+
private final ResourceManagerOptions serviceOptions;
147125

148126
ProjectPageFetcher(ResourceManagerOptions serviceOptions, String cursor,
149127
Map<ResourceManagerRpc.Option, ?> optionMap) {
150-
super(serviceOptions, cursor, optionMap);
128+
this.requestOptions =
129+
PageImpl.nextRequestOptions(ResourceManagerRpc.Option.PAGE_TOKEN, cursor, optionMap);
130+
this.serviceOptions = serviceOptions;
151131
}
152132

153133
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

+17-30
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.google.gcloud.ExceptionHandler.Interceptor;
4949
import com.google.gcloud.Page;
5050
import com.google.gcloud.PageImpl;
51+
import com.google.gcloud.PageImpl.NextPageFetcher;
5152
import com.google.gcloud.RetryHelper.RetryHelperException;
5253
import com.google.gcloud.spi.StorageRpc;
5354
import com.google.gcloud.spi.StorageRpc.RewriteResponse;
@@ -92,7 +93,7 @@ public RetryResult beforeEval(Exception exception) {
9293
return Interceptor.RetryResult.CONTINUE_EVALUATION;
9394
}
9495
};
95-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
96+
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
9697
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
9798
private static final byte[] EMPTY_BYTE_ARRAY = {};
9899
private static final String EMPTY_BYTE_ARRAY_MD5 = "1B2M2Y8AsgTpgAmY7PhCfg==";
@@ -209,36 +210,18 @@ public BlobInfo get(BlobId blob) {
209210
return get(blob, new BlobGetOption[0]);
210211
}
211212

212-
private abstract static class BasePageFetcher<T extends Serializable>
213-
implements PageImpl.NextPageFetcher<T> {
213+
private static class BucketPageFetcher implements NextPageFetcher<BucketInfo> {
214214

215-
private static final long serialVersionUID = 8236329004030295223L;
216-
protected final Map<StorageRpc.Option, ?> requestOptions;
217-
protected final StorageOptions serviceOptions;
215+
private static final long serialVersionUID = 5850406828803613729L;
216+
private final Map<StorageRpc.Option, ?> requestOptions;
217+
private final StorageOptions serviceOptions;
218218

219-
BasePageFetcher(StorageOptions serviceOptions, String cursor,
219+
BucketPageFetcher(
220+
StorageOptions serviceOptions, String cursor,
220221
Map<StorageRpc.Option, ?> optionMap) {
222+
this.requestOptions =
223+
PageImpl.nextRequestOptions(StorageRpc.Option.PAGE_TOKEN, cursor, optionMap);
221224
this.serviceOptions = serviceOptions;
222-
ImmutableMap.Builder<StorageRpc.Option, Object> builder = ImmutableMap.builder();
223-
if (cursor != null) {
224-
builder.put(StorageRpc.Option.PAGE_TOKEN, cursor);
225-
}
226-
for (Map.Entry<StorageRpc.Option, ?> option : optionMap.entrySet()) {
227-
if (option.getKey() != StorageRpc.Option.PAGE_TOKEN) {
228-
builder.put(option.getKey(), option.getValue());
229-
}
230-
}
231-
this.requestOptions = builder.build();
232-
}
233-
}
234-
235-
private static class BucketPageFetcher extends BasePageFetcher<BucketInfo> {
236-
237-
private static final long serialVersionUID = -5490616010200159174L;
238-
239-
BucketPageFetcher(StorageOptions serviceOptions, String cursor,
240-
Map<StorageRpc.Option, ?> optionMap) {
241-
super(serviceOptions, cursor, optionMap);
242225
}
243226

244227
@Override
@@ -247,14 +230,18 @@ public Page<BucketInfo> nextPage() {
247230
}
248231
}
249232

250-
private static class BlobPageFetcher extends BasePageFetcher<BlobInfo> {
233+
private static class BlobPageFetcher implements NextPageFetcher<BlobInfo> {
251234

252-
private static final long serialVersionUID = -5490616010200159174L;
235+
private static final long serialVersionUID = 81807334445874098L;
236+
private final Map<StorageRpc.Option, ?> requestOptions;
237+
private final StorageOptions serviceOptions;
253238
private final String bucket;
254239

255240
BlobPageFetcher(String bucket, StorageOptions serviceOptions, String cursor,
256241
Map<StorageRpc.Option, ?> optionMap) {
257-
super(serviceOptions, cursor, optionMap);
242+
this.requestOptions =
243+
PageImpl.nextRequestOptions(StorageRpc.Option.PAGE_TOKEN, cursor, optionMap);
244+
this.serviceOptions = serviceOptions;
258245
this.bucket = bucket;
259246
}
260247

0 commit comments

Comments
 (0)