-
Notifications
You must be signed in to change notification settings - Fork 81
/
Query.java
445 lines (392 loc) · 14.6 KB
/
Query.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.models;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.ReadRowsRequest;
import com.google.bigtable.v2.RowFilter;
import com.google.bigtable.v2.RowRange;
import com.google.bigtable.v2.RowSet;
import com.google.cloud.bigtable.data.v2.internal.ByteStringComparator;
import com.google.cloud.bigtable.data.v2.internal.NameUtil;
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
import com.google.cloud.bigtable.data.v2.internal.RowSetUtil;
import com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.SortedSet;
import javax.annotation.Nonnull;
/** A simple wrapper to construct a query for the ReadRows RPC. */
public final class Query implements Serializable {
private static final long serialVersionUID = -316972783499434755L;
// bigtable can server the largest filter size of 20KB.
private static final int MAX_FILTER_SIZE = 20 * 1024;
private final String tableId;
private transient ReadRowsRequest.Builder builder = ReadRowsRequest.newBuilder();
/**
* Constructs a new Query object for the specified table id. The table id will be combined with
* the instance name specified in the {@link
* com.google.cloud.bigtable.data.v2.BigtableDataSettings}.
*/
public static Query create(String tableId) {
return new Query(tableId);
}
private Query(String tableId) {
this.tableId = tableId;
}
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
builder = ReadRowsRequest.newBuilder().mergeFrom(input);
}
private void writeObject(ObjectOutputStream output) throws IOException {
output.defaultWriteObject();
builder.build().writeTo(output);
}
/** Adds a key to looked up */
public Query rowKey(String key) {
Preconditions.checkNotNull(key, "Key can't be null.");
return rowKey(ByteString.copyFromUtf8(key));
}
/** Adds a key to looked up */
public Query rowKey(ByteString key) {
Preconditions.checkNotNull(key, "Key can't be null.");
builder.getRowsBuilder().addRowKeys(key);
return this;
}
public Query prefix(ByteString prefix) {
return range(ByteStringRange.prefix(prefix));
}
public Query prefix(String prefix) {
return range(ByteStringRange.prefix(prefix));
}
/**
* Adds a range to be looked up.
*
* @param start The beginning of the range (inclusive). Can be null to represent negative
* infinity.
* @param end The end of the range (exclusive). Can be null to represent positive infinity.
*/
public Query range(String start, String end) {
return range(wrapKey(start), wrapKey(end));
}
/**
* Adds a range to be looked up.
*
* @param start The beginning of the range (inclusive). Can be null to represent negative
* infinity.
* @param end The end of the range (exclusive). Can be null to represent positive infinity.
*/
public Query range(ByteString start, ByteString end) {
RowRange.Builder rangeBuilder = RowRange.newBuilder();
if (start != null) {
rangeBuilder.setStartKeyClosed(start);
}
if (end != null) {
rangeBuilder.setEndKeyOpen(end);
}
builder.getRowsBuilder().addRowRanges(rangeBuilder.build());
return this;
}
/** Adds a range to be looked up. */
public Query range(ByteStringRange range) {
RowRange.Builder rangeBuilder = RowRange.newBuilder();
switch (range.getStartBound()) {
case OPEN:
rangeBuilder.setStartKeyOpen(range.getStart());
break;
case CLOSED:
rangeBuilder.setStartKeyClosed(range.getStart());
break;
case UNBOUNDED:
rangeBuilder.clearStartKey();
break;
default:
throw new IllegalStateException("Unknown start bound: " + range.getStartBound());
}
switch (range.getEndBound()) {
case OPEN:
rangeBuilder.setEndKeyOpen(range.getEnd());
break;
case CLOSED:
rangeBuilder.setEndKeyClosed(range.getEnd());
break;
case UNBOUNDED:
rangeBuilder.clearEndKey();
break;
default:
throw new IllegalStateException("Unknown end bound: " + range.getEndBound());
}
builder.getRowsBuilder().addRowRanges(rangeBuilder.build());
return this;
}
/**
* Sets the filter to apply to each row. Only one filter can be set at a time. To use multiple
* filters, please use {@link Filters#interleave()} or {@link Filters#chain()}.
*/
public Query filter(Filters.Filter filter) {
Preconditions.checkNotNull(filter, "filter can't be null");
RowFilter rowFilter = filter.toProto();
Preconditions.checkArgument(
rowFilter.getSerializedSize() < MAX_FILTER_SIZE, "filter size can't be more than 20KB");
builder.setFilter(rowFilter);
return this;
}
/** Limits the number of rows that can be returned */
public Query limit(long limit) {
Preconditions.checkArgument(limit > 0, "Limit must be greater than 0.");
builder.setRowsLimit(limit);
return this;
}
/**
* Return rows in reverse order.
*
* <p>The row will be streamed in reverse lexiographic order of the keys. The row key ranges are
* still expected to be oriented the same way as forwards. ie [a,c] where a <= c. The row content
* will remain unchanged from the ordering forward scans. This is particularly useful to get the
* last N records before a key:
*
* <pre>{@code
* query
* .range(ByteStringRange.unbounded().endOpen("key"))
* .limit(10)
* .reversed(true)
* }</pre>
*/
public Query reversed(boolean enable) {
builder.setReversed(enable);
return this;
}
/**
* Split this query into multiple queries that can be evenly distributed across Bigtable nodes and
* be run in parallel. This method takes the results from {@link
* com.google.cloud.bigtable.data.v2.BigtableDataClient#sampleRowKeysAsync(String)} to divide this
* query into a set of disjoint queries that logically combine into form this query.
*
* <p>Expected Usage:
*
* <pre>{@code
* List<KeyOffset> keyOffsets = dataClient.sampleRowKeysAsync("my-table").get();
* List<Query> queryShards = myQuery.shard(keyOffsets);
* List<ApiFuture<List<Row>>> futures = new ArrayList();
* for (Query subQuery : queryShards) {
* futures.add(dataClient.readRowsCallable().all().futureCall(subQuery));
* }
* List<List<Row>> results = ApiFutures.allAsList(futures).get();
* }</pre>
*/
public List<Query> shard(List<KeyOffset> sampledRowKeys) {
Preconditions.checkState(builder.getRowsLimit() == 0, "Can't shard query with row limits");
ImmutableSortedSet.Builder<ByteString> splitPoints =
ImmutableSortedSet.orderedBy(ByteStringComparator.INSTANCE);
for (KeyOffset keyOffset : sampledRowKeys) {
if (!keyOffset.getKey().isEmpty()) {
splitPoints.add(keyOffset.getKey());
}
}
return shard(splitPoints.build());
}
/**
* Split this query into multiple queries that logically combine into this query. This is intended
* to be used by map reduce style frameworks like Beam to split a query across multiple workers.
*
* <p>Expected Usage:
*
* <pre>{@code
* List<ByteString> splitPoints = ...;
* List<Query> queryShards = myQuery.shard(splitPoints);
* List<ApiFuture<List<Row>>> futures = new ArrayList();
* for (Query subQuery : queryShards) {
* futures.add(dataClient.readRowsCallable().all().futureCall(subQuery));
* }
* List<List<Row>> results = ApiFutures.allAsList(futures).get();
* }</pre>
*/
public List<Query> shard(SortedSet<ByteString> splitPoints) {
Preconditions.checkState(builder.getRowsLimit() == 0, "Can't shard a query with a row limit");
List<RowSet> shardedRowSets = RowSetUtil.shard(builder.getRows(), splitPoints);
List<Query> shards = Lists.newArrayListWithCapacity(shardedRowSets.size());
for (RowSet rowSet : shardedRowSets) {
Query queryShard = new Query(tableId);
queryShard.builder.mergeFrom(this.builder.build());
queryShard.builder.setRows(rowSet);
shards.add(queryShard);
}
return shards;
}
/**
* Create a query paginator that'll split the query into smaller chunks.
*
* <p>Example usage:
*
* <pre>{@code
* Query query = Query.create(...).range("a", "z");
* Query.QueryPaginator paginator = query.createQueryPaginator(100);
* ByteString lastSeenRowKey = ByteString.EMPTY;
* do {
* List<Row> rows = client.readRowsCallable().all().call(paginator.getNextQuery());
* for (Row row : rows) {
* // do some processing
* lastSeenRow = row;
* }
* } while (paginator.advance(lastSeenRowKey));
* }</pre>
*/
@BetaApi("This surface is stable yet it might be removed in the future.")
public QueryPaginator createPaginator(int pageSize) {
return new QueryPaginator(this, pageSize);
}
/** Get the minimal range that encloses all of the row keys and ranges in this Query. */
public ByteStringRange getBound() {
return RowSetUtil.getBound(builder.getRows());
}
/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
@InternalApi
public ReadRowsRequest toProto(RequestContext requestContext) {
String tableName =
NameUtil.formatTableName(
requestContext.getProjectId(), requestContext.getInstanceId(), tableId);
return builder
.setTableName(tableName)
.setAppProfileId(requestContext.getAppProfileId())
.build();
}
/**
* Wraps the protobuf {@link ReadRowsRequest}.
*
* <p>WARNING: Please note that the project id & instance id in the table name will be overwritten
* by the configuration in the BigtableDataClient.
*/
public static Query fromProto(@Nonnull ReadRowsRequest request) {
Preconditions.checkArgument(request != null, "ReadRowsRequest must not be null");
Query query = new Query(NameUtil.extractTableIdFromTableName(request.getTableName()));
query.builder = request.toBuilder();
return query;
}
public Query clone() {
Query query = Query.create(tableId);
query.builder = this.builder.clone();
return query;
}
private static ByteString wrapKey(String key) {
if (key == null) {
return null;
}
return ByteString.copyFromUtf8(key);
}
/**
* A Query Paginator that will split a query into small chunks. See {@link
* Query#createPaginator(int)} for example usage.
*/
@BetaApi("This surface is stable yet it might be removed in the future.")
public static class QueryPaginator {
private final boolean hasOverallLimit;
private long remainingRows;
private Query query;
private final int pageSize;
private ByteString prevSplitPoint;
QueryPaginator(@Nonnull Query query, int pageSize) {
this.hasOverallLimit = query.builder.getRowsLimit() > 0;
this.remainingRows = query.builder.getRowsLimit();
this.query = query.limit(pageSize);
if (hasOverallLimit) {
remainingRows -= pageSize;
}
this.pageSize = pageSize;
this.prevSplitPoint = ByteString.EMPTY;
}
/** Return the next query. */
public Query getNextQuery() {
return query;
}
/**
* Construct the next query. Return true if there are more queries to return. False if we've
* read everything.
*/
public boolean advance(@Nonnull ByteString lastSeenRowKey) {
Preconditions.checkNotNull(
lastSeenRowKey, "lastSeenRowKey cannot be null, use ByteString.EMPTY instead.");
// Full table scans don't have ranges or limits. Running the query again will return an empty
// list when we reach the end of the table. lastSeenRowKey won't be updated in this case, and
// we can break out of the loop.
if (lastSeenRowKey.equals(prevSplitPoint)) {
return false;
}
this.prevSplitPoint = lastSeenRowKey;
// Set the query limit. If the original limit is set, return false if the new
// limit is <= 0 to avoid returning more rows than intended.
if (hasOverallLimit && remainingRows <= 0) {
return false;
}
if (hasOverallLimit) {
query.limit(Math.min(this.pageSize, remainingRows));
remainingRows -= pageSize;
} else {
query.limit(pageSize);
}
// Split the row ranges / row keys. Return false if there's nothing
// left on the right of the split point.
RowSet remaining =
RowSetUtil.erase(query.builder.getRows(), lastSeenRowKey, !query.builder.getReversed());
if (remaining == null) {
return false;
}
query.builder.setRows(remaining);
return true;
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Query query = (Query) o;
return Objects.equal(tableId, query.tableId)
&& Objects.equal(builder.getRows(), query.builder.getRows())
&& Objects.equal(builder.getFilter(), query.builder.getFilter())
&& Objects.equal(builder.getRowsLimit(), query.builder.getRowsLimit());
}
@Override
public int hashCode() {
return Objects.hashCode(
tableId, builder.getRows(), builder.getFilter(), builder.getRowsLimit());
}
@Override
public String toString() {
ReadRowsRequest request = builder.build();
return MoreObjects.toStringHelper(this)
.add("tableId", tableId)
.add("keys", request.getRows().getRowKeysList())
.add("ranges", request.getRows().getRowRangesList())
.add("filter", request.getFilter())
.add("limit", request.getRowsLimit())
.toString();
}
}