-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathBatchOperation.java
More file actions
393 lines (364 loc) · 16.3 KB
/
BatchOperation.java
File metadata and controls
393 lines (364 loc) · 16.3 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
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
/*-
* #%L
* OBKV Table Client Framework
* %%
* Copyright (C) 2021 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.mutation;
import com.alipay.oceanbase.rpc.ObTableClient;
import com.alipay.oceanbase.rpc.checkandmutate.CheckAndInsUp;
import com.alipay.oceanbase.rpc.exception.FeatureNotSupportedException;
import com.alipay.oceanbase.rpc.exception.ObTableException;
import com.alipay.oceanbase.rpc.get.Get;
import com.alipay.oceanbase.rpc.mutation.result.BatchOperationResult;
import com.alipay.oceanbase.rpc.protocol.payload.impl.ObObj;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.ObTableEntityType;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.ObTableOperationType;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.mutate.ObTableQueryAndMutate;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.query.ObTableQuery;
import com.alipay.oceanbase.rpc.queryandmutate.QueryAndMutate;
import com.alipay.oceanbase.rpc.table.ObTableClientLSBatchOpsImpl;
import com.alipay.oceanbase.rpc.table.api.Table;
import com.alipay.oceanbase.rpc.table.api.TableBatchOps;
import com.alipay.oceanbase.rpc.table.api.TableQuery;
import com.alipay.oceanbase.rpc.ObGlobal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BatchOperation {
private String tableName;
private Table client;
boolean withResult;
private List<Object> operations;
boolean isAtomic = false;
boolean returnOneResult = false;
boolean hasCheckAndInsUp = false;
boolean hasGet = false;
boolean serverCanRetry = false;
boolean needTabletId = false;
ObTableOperationType lastType = ObTableOperationType.INVALID;
boolean isSameType = true;
protected ObTableEntityType entityType = ObTableEntityType.KV;
/*
* default constructor
*/
public BatchOperation() {
tableName = null;
client = null;
withResult = false;
operations = new ArrayList<>();
}
/*
* construct with client and table name
*/
public BatchOperation(Table client, String tableName) {
this.tableName = tableName;
this.client = client;
withResult = false;
operations = new ArrayList<>();
}
/*
* set client
*/
public BatchOperation setClient(Table client) {
this.client = client;
return this;
}
/*
* set table
*/
public BatchOperation setTable(String tableName) {
this.tableName = tableName;
return this;
}
/*
* add queries
*/
public BatchOperation addOperation(TableQuery... queries) {
boolean isHBaseQuery = false;
if (queries != null && queries.length > 0) {
isHBaseQuery = queries[0].getObTableQuery().isHbaseQuery();
}
if (isHBaseQuery) {
if (isSameType && lastType != ObTableOperationType.INVALID
&& lastType != ObTableOperationType.SCAN) {
isSameType = false;
}
lastType = ObTableOperationType.SCAN;
} else {
if (isSameType && lastType != ObTableOperationType.INVALID
&& lastType != ObTableOperationType.GET) {
isSameType = false;
}
lastType = ObTableOperationType.GET;
}
this.operations.addAll(Arrays.asList(queries));
return this;
}
public BatchOperation addOperation(QueryAndMutate qm) {
lastType = ObTableOperationType.QUERY_AND_MUTATE;
this.operations.add(qm);
return this;
}
/*
* add get
*/
public BatchOperation addOperation(Get... gets) {
if (isSameType && lastType != ObTableOperationType.INVALID
&& lastType != ObTableOperationType.GET) {
isSameType = false;
}
lastType = ObTableOperationType.GET;
this.operations.addAll(Arrays.asList(gets));
return this;
}
/*
* add mutations
*/
public BatchOperation addOperation(Mutation... mutations) {
for (int i = 0; i < mutations.length; i++) {
if (isSameType && lastType != ObTableOperationType.INVALID
&& lastType != mutations[i].getOperationType()) {
isSameType = false;
}
lastType = mutations[i].getOperationType();
}
this.operations.addAll(Arrays.asList(mutations));
return this;
}
/*
* add mutations
*/
public BatchOperation addOperation(List<Mutation> mutations) {
for (int i = 0; i < mutations.size(); i++) {
if (isSameType && lastType != ObTableOperationType.INVALID
&& lastType != mutations.get(i).getOperationType()) {
isSameType = false;
}
lastType = mutations.get(i).getOperationType();
}
this.operations.addAll(mutations);
return this;
}
/*
* add CheckAndInsUp
*/
public BatchOperation addOperation(CheckAndInsUp... insUps) {
if (isSameType && lastType != ObTableOperationType.INVALID
&& lastType != ObTableOperationType.CHECK_AND_INSERT_UP) {
isSameType = false;
}
lastType = ObTableOperationType.CHECK_AND_INSERT_UP;
this.operations.addAll(Arrays.asList(insUps));
this.hasCheckAndInsUp = true;
return this;
}
public void setEntityType(ObTableEntityType entityType) {
this.entityType = entityType;
}
public void setServerCanRetry(boolean canRetry) {
this.serverCanRetry = canRetry;
}
public void setNeedTabletId(boolean needTabletId) {
this.needTabletId = needTabletId;
}
public BatchOperation setIsAtomic(boolean isAtomic) {
this.isAtomic = isAtomic;
return this;
}
public BatchOperation setReturnOneResult(boolean returnOneResult) {
this.returnOneResult = returnOneResult;
return this;
}
@SuppressWarnings("unchecked")
public BatchOperationResult execute() throws Exception {
if (returnOneResult && !ObGlobal.isReturnOneResultSupport()) {
throw new FeatureNotSupportedException(
"returnOneResult is not supported in this Observer version ["
+ ObGlobal.obVsnString() + "]");
} else if (returnOneResult
&& !(isSameType && (lastType == ObTableOperationType.INSERT
|| lastType == ObTableOperationType.PUT
|| lastType == ObTableOperationType.REPLACE || lastType == ObTableOperationType.DEL))) {
throw new IllegalArgumentException(
"returnOneResult only support multi-insert/put/replace/del");
}
if (hasCheckAndInsUp || ObGlobal.isLsOpSupport()) {
return executeWithLSBatchOp();
} else {
return executeWithNormalBatchOp();
}
}
private BatchOperationResult executeWithNormalBatchOp() throws Exception {
if (tableName == null || tableName.isEmpty()) {
throw new IllegalArgumentException("table name is null");
}
TableBatchOps batchOps = client.batch(tableName);
batchOps.setEntityType(entityType);
boolean hasSetRowkeyElement = false;
for (Object operation : operations) {
if (operation instanceof Mutation) {
Mutation mutation = (Mutation) operation;
if (!hasSetRowkeyElement && mutation.getRowKeyNames() != null) {
List<String> rowKeyNames = mutation.getRowKeyNames();
((ObTableClient) client).addRowKeyElement(tableName,
rowKeyNames.toArray(new String[0]));
hasSetRowkeyElement = true;
}
ObTableOperationType type = mutation.getOperationType();
switch (type) {
case GET:
throw new IllegalArgumentException("Invalid type in batch operation, "
+ type);
case INSERT:
((Insert) mutation).removeRowkeyFromMutateColval();
batchOps.insert(((Insert) mutation).getRowKeyValues()
.toArray(new Object[0]), ((Insert) mutation).getColumns(),
((Insert) mutation).getValues());
break;
case DEL:
batchOps.delete(((Delete) mutation).getRowKeyValues()
.toArray(new Object[0]));
break;
case UPDATE:
((Update) mutation).removeRowkeyFromMutateColval();
batchOps.update(((Update) mutation).getRowKeyValues()
.toArray(new Object[0]), ((Update) mutation).getColumns(),
((Update) mutation).getValues());
break;
case INSERT_OR_UPDATE:
((InsertOrUpdate) mutation).removeRowkeyFromMutateColval();
batchOps.insertOrUpdate(((InsertOrUpdate) mutation).getRowKeyValues()
.toArray(new Object[0]), ((InsertOrUpdate) mutation).getColumns(),
((InsertOrUpdate) mutation).getValues());
break;
case REPLACE:
((Replace) mutation).removeRowkeyFromMutateColval();
batchOps.replace(
((Replace) mutation).getRowKeyValues().toArray(new Object[0]),
((Replace) mutation).getColumns(), ((Replace) mutation).getValues());
break;
case INCREMENT:
((Increment) mutation).removeRowkeyFromMutateColval();
batchOps.increment(
((Increment) mutation).getRowKeyValues().toArray(new Object[0]),
((Increment) mutation).getColumns(),
((Increment) mutation).getValues(), withResult);
break;
case APPEND:
((Append) mutation).removeRowkeyFromMutateColval();
batchOps.append(((Append) mutation).getRowKeyValues()
.toArray(new Object[0]), ((Append) mutation).getColumns(),
((Append) mutation).getValues(), withResult);
break;
case PUT:
((Put) mutation).removeRowkeyFromMutateColval();
batchOps.put(((Put) mutation).getRowKeyValues().toArray(new Object[0]),
((Put) mutation).getColumns(), ((Put) mutation).getValues());
break;
default:
throw new ObTableException("unknown operation type " + type);
}
} else if (operation instanceof TableQuery) {
TableQuery query = (TableQuery) operation;
batchOps.get(query.getRowKey().getValues(),
query.getSelectColumns().toArray((new String[0])));
} else if (operation instanceof Get) {
Get get = (Get) operation;
if (get.getRowKey() == null) {
throw new IllegalArgumentException("RowKey is null in Get operation");
}
batchOps.get(get.getRowKey().getValues(), get.getSelectColumns());
} else {
throw new ObTableException("unknown operation " + operation);
}
}
batchOps.setAtomicOperation(isAtomic);
batchOps.setReturnOneResult(returnOneResult);
return new BatchOperationResult(batchOps.executeWithResult());
}
private BatchOperationResult executeWithLSBatchOp() throws Exception {
if (tableName == null || tableName.isEmpty()) {
throw new IllegalArgumentException("table name is null");
}
ObTableClientLSBatchOpsImpl batchOps;
boolean hasSetRowkeyElement = false;
int checkAndInsUpCnt = 0;
if (client instanceof ObTableClient) {
batchOps = new ObTableClientLSBatchOpsImpl(tableName, (ObTableClient) client);
batchOps.setEntityType(entityType);
batchOps.setServerCanRetry(serverCanRetry);
batchOps.setNeedTabletId(needTabletId);
for (Object operation : operations) {
if (operation instanceof CheckAndInsUp) {
checkAndInsUpCnt++;
CheckAndInsUp checkAndInsUp = (CheckAndInsUp) operation;
batchOps.addOperation(checkAndInsUp);
List<String> rowKeyNames = checkAndInsUp.getInsUp().getRowKeyNames();
if (!hasSetRowkeyElement && rowKeyNames != null) {
((ObTableClient) client).addRowKeyElement(tableName,
rowKeyNames.toArray(new String[0]));
hasSetRowkeyElement = true;
}
} else if (operation instanceof Mutation) {
Mutation mutation = (Mutation) operation;
if (((ObTableClient) client).getRunningMode() == ObTableClient.RunningMode.HBASE) {
negateHbaseTimestamp(mutation);
}
batchOps.addOperation(mutation);
if (!hasSetRowkeyElement && mutation.getRowKeyNames() != null) {
List<String> rowKeyNames = mutation.getRowKeyNames();
((ObTableClient) client).addRowKeyElement(tableName,
rowKeyNames.toArray(new String[0]));
hasSetRowkeyElement = true;
}
} else if (operation instanceof Get) {
Get get = (Get) operation;
if (get.getRowKey() == null) {
throw new IllegalArgumentException("RowKey is null in Get operation");
}
batchOps.addOperation(get);
} else if (operation instanceof TableQuery) {
TableQuery query = (TableQuery) operation;
batchOps.addOperation(query);
} else if (operation instanceof QueryAndMutate) {
QueryAndMutate qm = (QueryAndMutate) operation;
batchOps.addOperation(qm);
} else {
throw new IllegalArgumentException(
"The operations in batch must be all checkAndInsUp or all non-checkAndInsUp");
}
}
} else {
throw new IllegalArgumentException(
"execute batch using ObTable directly is not supported");
}
if (checkAndInsUpCnt > 0 && checkAndInsUpCnt != operations.size()) {
throw new IllegalArgumentException(
"Can not mix checkAndInsUP and other types operation in batch");
}
batchOps.setReturningAffectedEntity(withResult);
batchOps.setReturnOneResult(returnOneResult);
batchOps.setAtomicOperation(isAtomic);
return new BatchOperationResult(batchOps.executeWithResult());
}
private void negateHbaseTimestamp(Mutation mutation) {
Row rowKey = mutation.getRowKey();
if (rowKey == null || rowKey.size() != 3) {
throw new IllegalArgumentException("hbase rowkey length must be 3");
} else {
long ts = (long) ObTableClient.getRowKeyValue(mutation, 2);
ObTableClient.setRowKeyValue(mutation, 2, -ts);
}
}
}