forked from oceanbase/obkv-table-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObTableGetTest.java
More file actions
312 lines (280 loc) · 12.2 KB
/
ObTableGetTest.java
File metadata and controls
312 lines (280 loc) · 12.2 KB
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
/*-
* #%L
* com.oceanbase:obkv-table-client
* %%
* Copyright (C) 2021 - 2025 OceanBase
* %%
* OBKV Table Client Framework is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* #L%
*/
package com.alipay.oceanbase.rpc;
import com.alipay.oceanbase.rpc.exception.ObTableException;
import com.alipay.oceanbase.rpc.get.Get;
import com.alipay.oceanbase.rpc.get.result.GetResult;
import com.alipay.oceanbase.rpc.mutation.BatchOperation;
import com.alipay.oceanbase.rpc.mutation.result.BatchOperationResult;
import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal;
import static com.alipay.oceanbase.rpc.mutation.MutationFactory.row;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/*
CREATE TABLE IF NOT EXISTS `test_get` (
`c1` varchar(20) NOT NULL,
`c2` varchar(20) NOT NULL,
`c3` varchar(20) DEFAULT NULL,
PRIMARY KEY (`c1`, `c2`)
) PARTITION BY KEY(`c1`) PARTITIONS 3;
*/
public class ObTableGetTest {
ObTableClient client;
public static String tableName = "test_get";
@Before
public void setup() throws Exception {
final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient();
obTableClient.init();
this.client = obTableClient;
}
/* test empty result */
@Test
public void testSingleGet1() throws Exception {
// expect empty result
Get get = client.get(tableName);
Map<String, Object> res = get
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1", "c2")
.execute();
Assert.assertNotNull(res);
Assert.assertEquals(null, res.get("c1"));
}
/* test select column */
@Test
public void testSingleGet2() throws Exception {
try {
// insert
client.insertOrUpdate(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.addMutateColVal(colVal("c3", "c3_val")).execute();
// select c1,c2
Map<String, Object> res = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1", "c2")
.execute();
Assert.assertNotNull(res);
Assert.assertEquals("c1_val", res.get("c1"));
Assert.assertEquals("c2_val", res.get("c2"));
// get with empty select columns
res = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).execute();
Assert.assertNotNull(res);
Assert.assertEquals("c1_val", res.get("c1"));
Assert.assertEquals("c2_val", res.get("c2"));
// select c1
res = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1")
.execute();
Assert.assertNotNull(res);
Assert.assertEquals("c1_val", res.get("c1"));
Assert.assertEquals(null, res.get("c2"));
// select c2
res = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c2")
.execute();
Assert.assertNotNull(res);
Assert.assertEquals("c2_val", res.get("c2"));
Assert.assertEquals(null, res.get("c1"));
} finally {
client.delete(tableName).setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.execute();
}
}
/* test rowKey */
@Test
public void testSingleGet3() {
ObTableException thrown = assertThrows(
ObTableException.class,
() -> {
client.get(tableName).select("c1", "c2").execute(); // not set rowKey
}
);
System.out.println(thrown.getMessage());
assertTrue(thrown.getMessage().contains("RowKey or scan range is null"));
}
/* test tableName */
@Test
public void testSingleGet4() {
ObTableException thrown = assertThrows(
ObTableException.class,
() -> {
client.get("my_not_exist_table")
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.select("c1", "c2")
.execute();
}
);
System.out.println(thrown.getMessage());
assertTrue(thrown.getMessage().contains("table not exist"));
}
/* test partition key */
@Test
public void testSingleGet5() {
ObTableException thrown = assertThrows(
ObTableException.class,
() -> {
client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"))) // only set one rowKey column
.select("c1", "c2")
.execute();
}
);
System.out.println(thrown.getMessage());
assertTrue(thrown.getMessage().contains("[-4007][OB_NOT_SUPPORTED][operation is not supported to partially fill rowkey columns not supported]"));
}
/* test empty result */
@Test
public void testBatchGet1() throws Exception {
// expect empty result
BatchOperation batch = client.batchOperation(tableName);
Get get = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1", "c2");
batch.addOperation(get);
BatchOperationResult res = batch.execute();
Assert.assertNotNull(res);
Assert.assertEquals(null, res.get(0).getOperationRow().get("c1"));
Assert.assertEquals(null, res.get(0).getOperationRow().get("c2"));
}
/* test select column */
@Test
public void testBatchGet2() throws Exception {
try {
// insert
client.insertOrUpdate(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.addMutateColVal(colVal("c3", "c3_val")).execute();
// select c1,c2
BatchOperation batch1 = client.batchOperation(tableName);
Get get = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1", "c2");
batch1.addOperation(get);
BatchOperationResult res = batch1.execute();
Assert.assertNotNull(res);
Assert.assertEquals("c1_val", res.get(0).getOperationRow().get("c1"));
Assert.assertEquals("c2_val", res.get(0).getOperationRow().get("c2"));
// get with empty select columns(has bug in observer)
// BatchOperation batch2 = client.batchOperation(tableName);
// get = client.get(tableName).setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")));
// batch2.addOperation(get);
// res = batch2.execute();
// Assert.assertNotNull(res);
// Assert.assertEquals("c1_val", res.get(0).getOperationRow().get("c1"));
// Assert.assertEquals("c2_val", res.get(0).getOperationRow().get("c2"));
// select c1
BatchOperation batch3 = client.batchOperation(tableName);
get = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1");
batch3.addOperation(get);
res = batch3.execute();
Assert.assertNotNull(res);
Assert.assertEquals("c1_val", res.get(0).getOperationRow().get("c1"));
Assert.assertEquals(null, res.get(0).getOperationRow().get("c2"));
// select c2
BatchOperation batch4 = client.batchOperation(tableName);
get = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c2");
batch4.addOperation(get);
res = batch4.execute();
Assert.assertNotNull(res);
Assert.assertEquals("c2_val", res.get(0).getOperationRow().get("c2"));
Assert.assertEquals(null, res.get(0).getOperationRow().get("c1"));
} finally {
client.delete(tableName).setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.execute();
}
}
/* test rowKey */
@Test
public void testBatchGet3() throws Exception {
// not set rowKey
IllegalArgumentException thrown = assertThrows(
IllegalArgumentException.class,
() -> {
Get get = client.get(tableName).select("c1", "c2");
client.batchOperation(tableName).addOperation(get).execute();
}
);
System.out.println(thrown.getMessage());
assertTrue(thrown.getMessage().contains("RowKey is null"));
}
/* test tableName */
@Test
public void testBatchGet4() {
ObTableException thrown = assertThrows(
ObTableException.class,
() -> {
String tableName = "my_not_exist_table";
Get get = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.select("c1", "c2");
client.batchOperation(tableName).addOperation(get).execute();
}
);
System.out.println(thrown.getMessage());
assertTrue(thrown.getMessage().contains("table not exist"));
}
/* test partition key */
@Test
public void testBatchGet5() {
ObTableException thrown = assertThrows(
ObTableException.class,
() -> {
Get get = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"))) // only set one rowKey
.select("c1", "c2");
client.batchOperation(tableName).addOperation(get).execute();
}
);
System.out.println(thrown.getMessage());
assertTrue(thrown.getMessage().contains("[-4007][OB_NOT_SUPPORTED][operation is not supported to partially fill rowkey columns not supported]"));
}
/* test same Get */
@Test
public void testBatchGet6() throws Exception {
try {
// insert
client.insertOrUpdate(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.addMutateColVal(colVal("c3", "c3_val")).execute();
// select c1,c2
BatchOperation batch = client.batchOperation(tableName);
Get get1 = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1", "c2", "c3");
Get get2 = client.get(tableName)
.setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val"))).select("c1", "c2", "c3");
batch.addOperation(get1, get2);
BatchOperationResult res = batch.execute();
Assert.assertNotNull(res);
List<Object> getResults = res.getResults();
assertEquals(2, getResults.size());
for (Object result : getResults) {
GetResult getResult = (GetResult) result;
Map<String, Object> row = getResult.getOperationRow().getMap();
assertEquals("c1_val", row.get("c1"));
assertEquals("c2_val", row.get("c2"));
assertEquals("c3_val", row.get("c3"));
}
} finally {
client.delete(tableName).setRowKey(row(colVal("c1", "c1_val"), colVal("c2", "c2_val")))
.execute();
}
}
}