/
QueryTest.java
330 lines (286 loc) · 12.2 KB
/
QueryTest.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
/*
* 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 static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
import static com.google.common.truth.Truth.assertThat;
import com.google.bigtable.v2.ReadRowsRequest;
import com.google.bigtable.v2.ReadRowsRequest.Builder;
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.models.Range.ByteStringRange;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import com.google.protobuf.ByteString;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.SortedSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class QueryTest {
private static final String PROJECT_ID = "fake-project";
private static final String INSTANCE_ID = "fake-instance";
private static final String TABLE_ID = "fake-table";
private static final String APP_PROFILE_ID = "fake-profile-id";
private RequestContext requestContext;
@Rule public ExpectedException expect = ExpectedException.none();
@Before
public void setUp() {
requestContext = RequestContext.create(PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
}
@Test
public void requestContextTest() {
Query query = Query.create(TABLE_ID);
ReadRowsRequest proto = query.toProto(requestContext);
assertThat(proto).isEqualTo(expectedProtoBuilder().build());
}
@Test
public void rowKeysTest() {
Query query =
Query.create(TABLE_ID)
.rowKey("simple-string")
.rowKey(ByteString.copyFromUtf8("byte-string"));
ReadRowsRequest.Builder expectedProto = expectedProtoBuilder();
expectedProto
.getRowsBuilder()
.addRowKeys(ByteString.copyFromUtf8("simple-string"))
.addRowKeys(ByteString.copyFromUtf8("byte-string"));
ReadRowsRequest actualProto = query.toProto(requestContext);
assertThat(actualProto).isEqualTo(expectedProto.build());
}
@Test
public void rowRangeTest() {
Query query =
Query.create(TABLE_ID)
.range("simple-begin", "simple-end")
.range(ByteString.copyFromUtf8("byte-begin"), ByteString.copyFromUtf8("byte-end"))
.range(ByteStringRange.create("range-begin", "range-end"));
Builder expectedProto = expectedProtoBuilder();
expectedProto
.getRowsBuilder()
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("simple-begin"))
.setEndKeyOpen(ByteString.copyFromUtf8("simple-end")))
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("byte-begin"))
.setEndKeyOpen(ByteString.copyFromUtf8("byte-end")))
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("range-begin"))
.setEndKeyOpen(ByteString.copyFromUtf8("range-end")));
ReadRowsRequest actualProto = query.toProto(requestContext);
assertThat(actualProto).isEqualTo(expectedProto.build());
}
@Test
public void filterTestWithExceptions() {
Exception actualException = null;
try {
Query.create(TABLE_ID).filter(null);
} catch (Exception ex) {
actualException = ex;
}
assertThat(actualException).isInstanceOf(NullPointerException.class);
actualException = null;
int maxFilterSize = 20 * 1024;
ByteString largeValue = ByteString.copyFrom(new byte[maxFilterSize + 1]);
try {
Query.create(TABLE_ID).filter(FILTERS.value().exactMatch(largeValue));
} catch (Exception ex) {
actualException = ex;
}
assertThat(actualException).hasMessageThat().contains("filter size can't be more than 20KB");
}
@Test
public void filterTest() {
Query query = Query.create(TABLE_ID).filter(FILTERS.key().regex(".*"));
Builder expectedProto =
expectedProtoBuilder()
.setFilter(RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")));
ReadRowsRequest actualProto = query.toProto(requestContext);
assertThat(actualProto).isEqualTo(expectedProto.build());
}
@Test
public void limitTest() {
Query query = Query.create(TABLE_ID).limit(10);
Builder expectedProto = expectedProtoBuilder().setRowsLimit(10);
ReadRowsRequest actualProto = query.toProto(requestContext);
assertThat(actualProto).isEqualTo(expectedProto.build());
}
@Test
public void serializationTest() throws IOException, ClassNotFoundException {
Query expected = Query.create(TABLE_ID).filter(FILTERS.key().regex(".*"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(expected);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
Query actual = (Query) ois.readObject();
assertThat(actual.toProto(requestContext)).isEqualTo(expected.toProto(requestContext));
}
@Test
public void shardTestSplitPoints() {
Query query = Query.create(TABLE_ID).range("a", "z");
SortedSet<ByteString> splitPoints =
ImmutableSortedSet.orderedBy(ByteStringComparator.INSTANCE)
.add(ByteString.copyFromUtf8("j"))
.build();
List<Query> subQueries = query.shard(splitPoints);
assertThat(subQueries).hasSize(2);
assertThat(subQueries.get(0).toProto(requestContext))
.isEqualTo(
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setRows(
RowSet.newBuilder()
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("a"))
.setEndKeyClosed(ByteString.copyFromUtf8("j"))))
.build());
assertThat(subQueries.get(1).toProto(requestContext))
.isEqualTo(
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setRows(
RowSet.newBuilder()
.addRowRanges(
RowRange.newBuilder()
.setStartKeyOpen(ByteString.copyFromUtf8("j"))
.setEndKeyOpen(ByteString.copyFromUtf8("z"))))
.build());
}
@Test
public void shardTestKeyOffsets() {
Query query = Query.create(TABLE_ID).range("a", "z");
List<KeyOffset> keyOffsets =
ImmutableList.of(
KeyOffset.create(ByteString.copyFromUtf8("j"), 10),
KeyOffset.create(ByteString.EMPTY, 100));
List<Query> subQueries = query.shard(keyOffsets);
assertThat(subQueries).hasSize(2);
assertThat(subQueries.get(0).toProto(requestContext))
.isEqualTo(
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setRows(
RowSet.newBuilder()
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("a"))
.setEndKeyClosed(ByteString.copyFromUtf8("j"))))
.build());
assertThat(subQueries.get(1).toProto(requestContext))
.isEqualTo(
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setRows(
RowSet.newBuilder()
.addRowRanges(
RowRange.newBuilder()
.setStartKeyOpen(ByteString.copyFromUtf8("j"))
.setEndKeyOpen(ByteString.copyFromUtf8("z"))))
.build());
}
private static ReadRowsRequest.Builder expectedProtoBuilder() {
return ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID);
}
@Test
public void testFromProto() {
ReadRowsRequest request =
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setFilter(RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")))
.setRows(
RowSet.newBuilder()
.addRowKeys(ByteString.copyFromUtf8("row-key"))
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("j"))
.setEndKeyClosed(ByteString.copyFromUtf8("z"))))
.build();
Query query = Query.fromProto(request);
assertThat(query.toProto(requestContext)).isEqualTo(request);
}
@Test(expected = IllegalArgumentException.class)
public void testFromProtoWithEmptyTableId() {
Query.fromProto(ReadRowsRequest.getDefaultInstance());
expect.expect(IllegalArgumentException.class);
expect.expectMessage("Invalid table name:");
}
@Test
public void testEquality() {
Query request =
Query.create(TABLE_ID)
.rowKey("row-key")
.range("a", "z")
.limit(3)
.filter(FILTERS.family().exactMatch("test"));
// Query#toProto should not change the Query instance state
request.toProto(requestContext);
assertThat(request)
.isEqualTo(
Query.create(TABLE_ID)
.rowKey("row-key")
.range("a", "z")
.limit(3)
.filter(FILTERS.family().exactMatch("test")));
assertThat(Query.create(TABLE_ID).rowKey("row-key"))
.isNotEqualTo(Query.create(TABLE_ID).rowKey("row-key-1"));
assertThat(Query.create(TABLE_ID).range("a", "z"))
.isNotEqualTo(Query.create(TABLE_ID).range("a", "s"));
assertThat(Query.create(TABLE_ID).filter(FILTERS.family().regex("test")))
.isNotEqualTo(Query.create(TABLE_ID).filter(FILTERS.family().exactMatch("test-one")));
assertThat(Query.create(TABLE_ID).limit(4)).isNotEqualTo(Query.create(TABLE_ID).limit(5));
}
@Test
public void testClone() {
Query query = Query.create(TABLE_ID).filter(FILTERS.key().regex("temp")).limit(10);
ReadRowsRequest request =
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setRowsLimit(10)
.setFilter(
RowFilter.newBuilder()
.setRowKeyRegexFilter(ByteString.copyFromUtf8("temp"))
.build())
.build();
Query clonedReq = query.clone();
assertThat(clonedReq).isEqualTo(query);
assertThat(clonedReq.toProto(requestContext)).isEqualTo(request);
}
}