/
Options.java
505 lines (427 loc) · 14.4 KB
/
Options.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* Copyright 2017 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
*
* http://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.spanner;
import com.google.common.base.Preconditions;
import com.google.spanner.v1.RequestOptions.Priority;
import java.io.Serializable;
import java.util.Objects;
/** Specifies options for various spanner operations */
public final class Options implements Serializable {
private static final long serialVersionUID = 8067099123096783941L;
/**
* Priority for an RPC invocation. The default priority is {@link #HIGH}. This enum can be used to
* set a lower priority for a specific RPC invocation.
*/
public enum RpcPriority {
LOW(Priority.PRIORITY_LOW),
MEDIUM(Priority.PRIORITY_MEDIUM),
HIGH(Priority.PRIORITY_HIGH);
private final Priority proto;
private RpcPriority(Priority proto) {
this.proto = Preconditions.checkNotNull(proto);
}
}
/** Marker interface to mark options applicable to both Read and Query operations */
public interface ReadAndQueryOption extends ReadOption, QueryOption {}
/** Marker interface to mark options applicable to read operation */
public interface ReadOption {}
/** Marker interface to mark options applicable to Read, Query, Update and Write operations */
public interface ReadQueryUpdateTransactionOption
extends ReadOption, QueryOption, UpdateOption, TransactionOption {}
/** Marker interface to mark options applicable to query operation. */
public interface QueryOption {}
/** Marker interface to mark options applicable to write operations */
public interface TransactionOption {}
/** Marker interface to mark options applicable to update operation. */
public interface UpdateOption {}
/** Marker interface to mark options applicable to list operations in admin API. */
public interface ListOption {}
/** Specifying this instructs the transaction to request {@link CommitStats} from the backend. */
public static TransactionOption commitStats() {
return COMMIT_STATS_OPTION;
}
/**
* Specifying this will cause the read to yield at most this many rows. This should be greater
* than 0.
*/
public static ReadOption limit(long limit) {
Preconditions.checkArgument(limit > 0, "Limit should be greater than 0");
return new LimitOption(limit);
}
/**
* Specifying this will allow the client to prefetch up to {@code prefetchChunks} {@code
* PartialResultSet} chunks for read and query. The data size of each chunk depends on the server
* implementation but a good rule of thumb is that each chunk will be up to 1 MiB. Larger values
* reduce the likelihood of blocking while consuming results at the cost of greater memory
* consumption. {@code prefetchChunks} should be greater than 0. To get good performance choose a
* value that is large enough to allow buffering of chunks for an entire row. Apart from the
* buffered chunks, there can be at most one more row buffered in the client.
*/
public static ReadAndQueryOption prefetchChunks(int prefetchChunks) {
Preconditions.checkArgument(prefetchChunks > 0, "prefetchChunks should be greater than 0");
return new FlowControlOption(prefetchChunks);
}
public static ReadAndQueryOption bufferRows(int bufferRows) {
Preconditions.checkArgument(bufferRows > 0, "bufferRows should be greater than 0");
return new BufferRowsOption(bufferRows);
}
/** Specifies the priority to use for the RPC. */
public static ReadQueryUpdateTransactionOption priority(RpcPriority priority) {
return new PriorityOption(priority);
}
/**
* Specifying this will cause the reads, queries, updates and writes operations statistics
* collection to be grouped by tag.
*/
public static ReadQueryUpdateTransactionOption tag(String name) {
return new TagOption(name);
}
/**
* Specifying this will cause the list operations to fetch at most this many records in a page.
*/
public static ListOption pageSize(int pageSize) {
return new PageSizeOption(pageSize);
}
/**
* Specifying this will cause the list operation to start fetching the record from this onwards.
*/
public static ListOption pageToken(String pageToken) {
return new PageTokenOption(pageToken);
}
/**
* Specifying this will cause the given filter to be applied to the list operation. List
* operations that support this options are:
*
* <ul>
* <li>{@link InstanceAdminClient#listInstances}
* </ul>
*
* If this option is passed to any other list operation, it will throw an
* IllegalArgumentException.
*
* @param filter An expression for filtering the results of the request. Filter rules are case
* insensitive. Some examples of using filters are:
* <ul>
* <li>name:* The entity has a name.
* <li>name:Howl The entity's name contains "howl".
* <li>name:HOWL Equivalent to above.
* <li>NAME:howl Equivalent to above.
* <li>labels.env:* The entity has the label env.
* <li>labels.env:dev The entity has a label env whose value contains "dev".
* <li>name:howl labels.env:dev The entity's name contains "howl" and it has the label env
* whose value contains "dev".
* </ul>
*/
public static ListOption filter(String filter) {
return new FilterOption(filter);
}
/** Option to request {@link CommitStats} for read/write transactions. */
static final class CommitStatsOption extends InternalOption implements TransactionOption {
@Override
void appendToOptions(Options options) {
options.withCommitStats = true;
}
}
static final CommitStatsOption COMMIT_STATS_OPTION = new CommitStatsOption();
/** Option pertaining to flow control. */
static final class FlowControlOption extends InternalOption implements ReadAndQueryOption {
final int prefetchChunks;
FlowControlOption(int prefetchChunks) {
this.prefetchChunks = prefetchChunks;
}
@Override
void appendToOptions(Options options) {
options.prefetchChunks = prefetchChunks;
}
}
static final class BufferRowsOption extends InternalOption implements ReadAndQueryOption {
final int bufferRows;
BufferRowsOption(int bufferRows) {
this.bufferRows = bufferRows;
}
@Override
void appendToOptions(Options options) {
options.bufferRows = bufferRows;
}
}
static final class PriorityOption extends InternalOption
implements ReadQueryUpdateTransactionOption {
private final RpcPriority priority;
PriorityOption(RpcPriority priority) {
this.priority = priority;
}
@Override
void appendToOptions(Options options) {
options.priority = priority;
}
}
static final class TagOption extends InternalOption implements ReadQueryUpdateTransactionOption {
private final String tag;
TagOption(String tag) {
this.tag = tag;
}
@Override
void appendToOptions(Options options) {
options.tag = tag;
}
}
private boolean withCommitStats;
private Long limit;
private Integer prefetchChunks;
private Integer bufferRows;
private Integer pageSize;
private String pageToken;
private String filter;
private RpcPriority priority;
private String tag;
// Construction is via factory methods below.
private Options() {}
boolean withCommitStats() {
return withCommitStats;
}
boolean hasLimit() {
return limit != null;
}
long limit() {
return limit;
}
boolean hasPrefetchChunks() {
return prefetchChunks != null;
}
int prefetchChunks() {
return prefetchChunks;
}
boolean hasBufferRows() {
return bufferRows != null;
}
int bufferRows() {
return bufferRows;
}
boolean hasPageSize() {
return pageSize != null;
}
int pageSize() {
return pageSize;
}
boolean hasPageToken() {
return pageToken != null;
}
String pageToken() {
return pageToken;
}
boolean hasFilter() {
return filter != null;
}
String filter() {
return filter;
}
boolean hasPriority() {
return priority != null;
}
Priority priority() {
return priority == null ? null : priority.proto;
}
boolean hasTag() {
return tag != null;
}
String tag() {
return tag;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
if (withCommitStats) {
b.append("withCommitStats: ").append(withCommitStats).append(' ');
}
if (limit != null) {
b.append("limit: ").append(limit).append(' ');
}
if (prefetchChunks != null) {
b.append("prefetchChunks: ").append(prefetchChunks).append(' ');
}
if (pageSize != null) {
b.append("pageSize: ").append(pageSize).append(' ');
}
if (pageToken != null) {
b.append("pageToken: ").append(pageToken).append(' ');
}
if (filter != null) {
b.append("filter: ").append(filter).append(' ');
}
if (priority != null) {
b.append("priority: ").append(priority).append(' ');
}
if (tag != null) {
b.append("tag: ").append(tag).append(' ');
}
return b.toString();
}
@Override
// Since Options mandates checking hasXX() before XX() is called, the equals & hashCode look more
// complicated than usual.
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Options that = (Options) o;
return Objects.equals(withCommitStats, that.withCommitStats)
&& (!hasLimit() && !that.hasLimit()
|| hasLimit() && that.hasLimit() && Objects.equals(limit(), that.limit()))
&& (!hasPrefetchChunks() && !that.hasPrefetchChunks()
|| hasPrefetchChunks()
&& that.hasPrefetchChunks()
&& Objects.equals(prefetchChunks(), that.prefetchChunks()))
&& (!hasBufferRows() && !that.hasBufferRows()
|| hasBufferRows()
&& that.hasBufferRows()
&& Objects.equals(bufferRows(), that.bufferRows()))
&& (!hasPageSize() && !that.hasPageSize()
|| hasPageSize() && that.hasPageSize() && Objects.equals(pageSize(), that.pageSize()))
&& Objects.equals(pageToken(), that.pageToken())
&& Objects.equals(filter(), that.filter())
&& Objects.equals(priority(), that.priority())
&& Objects.equals(tag(), that.tag());
}
@Override
public int hashCode() {
int result = 31;
if (withCommitStats) {
result = 31 * result + 1231;
}
if (limit != null) {
result = 31 * result + limit.hashCode();
}
if (prefetchChunks != null) {
result = 31 * result + prefetchChunks.hashCode();
}
if (bufferRows != null) {
result = 31 * result + bufferRows.hashCode();
}
if (pageSize != null) {
result = 31 * result + pageSize.hashCode();
}
if (pageToken != null) {
result = 31 * result + pageToken.hashCode();
}
if (filter != null) {
result = 31 * result + filter.hashCode();
}
if (priority != null) {
result = 31 * result + priority.hashCode();
}
if (tag != null) {
result = 31 * result + tag.hashCode();
}
return result;
}
static Options fromReadOptions(ReadOption... options) {
Options readOptions = new Options();
for (ReadOption option : options) {
if (option instanceof InternalOption) {
((InternalOption) option).appendToOptions(readOptions);
}
}
return readOptions;
}
static Options fromQueryOptions(QueryOption... options) {
Options readOptions = new Options();
for (QueryOption option : options) {
if (option instanceof InternalOption) {
((InternalOption) option).appendToOptions(readOptions);
}
}
return readOptions;
}
static Options fromUpdateOptions(UpdateOption... options) {
Options updateOptions = new Options();
for (UpdateOption option : options) {
if (option instanceof InternalOption) {
((InternalOption) option).appendToOptions(updateOptions);
}
}
return updateOptions;
}
static Options fromTransactionOptions(TransactionOption... options) {
Options transactionOptions = new Options();
for (TransactionOption option : options) {
if (option instanceof InternalOption) {
((InternalOption) option).appendToOptions(transactionOptions);
}
}
return transactionOptions;
}
static Options fromListOptions(ListOption... options) {
Options listOptions = new Options();
for (ListOption option : options) {
if (option instanceof InternalOption) {
((InternalOption) option).appendToOptions(listOptions);
}
}
return listOptions;
}
private abstract static class InternalOption {
abstract void appendToOptions(Options options);
}
static class LimitOption extends InternalOption implements ReadOption {
private final long limit;
LimitOption(long limit) {
this.limit = limit;
}
@Override
void appendToOptions(Options options) {
options.limit = limit;
}
}
static class PageSizeOption extends InternalOption implements ListOption {
private final int pageSize;
PageSizeOption(int pageSize) {
this.pageSize = pageSize;
}
@Override
void appendToOptions(Options options) {
options.pageSize = pageSize;
}
}
static class PageTokenOption extends InternalOption implements ListOption {
private final String pageToken;
PageTokenOption(String pageToken) {
this.pageToken = pageToken;
}
@Override
void appendToOptions(Options options) {
options.pageToken = pageToken;
}
}
static class FilterOption extends InternalOption implements ListOption {
private final String filter;
FilterOption(String filter) {
this.filter = filter;
}
@Override
void appendToOptions(Options options) {
options.filter = filter;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof FilterOption)) return false;
return Objects.equals(filter, ((FilterOption) o).filter);
}
}
}