-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPut.java
More file actions
174 lines (153 loc) · 5.31 KB
/
Put.java
File metadata and controls
174 lines (153 loc) · 5.31 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
/*-
* #%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.exception.ObTableException;
import com.alipay.oceanbase.rpc.exception.ObTableUnexpectedException;
import com.alipay.oceanbase.rpc.filter.ObTableFilter;
import com.alipay.oceanbase.rpc.mutation.result.MutationResult;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.ObTableOperation;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.ObTableOperationType;
import com.alipay.oceanbase.rpc.table.api.Table;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
public class Put extends Mutation<Put> {
private LinkedHashSet<String> columns = null;
private List<Object> values = null;
/*
* default constructor
*/
public Put() {
super();
columns = new LinkedHashSet<String>();
values = new ArrayList<Object>();
}
/*
* construct with ObTableClient and String
*/
public Put(Table client, String tableName) {
super(client, tableName);
columns = new LinkedHashSet<String>();
values = new ArrayList<Object>();
}
/*
* set the Row Key of mutation with Row and keep scan range
*/
@Override
public Put setRowKey(Row rowKey) {
return setRowKeyOnly(rowKey);
}
/*
* set the Row Key of mutation with ColumnValues and keep scan range
*/
@Override
public Put setRowKey(ColumnValue... rowKey) {
return setRowKeyOnly(rowKey);
}
/*
* add filter into mutation (use QueryAndMutate) and scan range
*/
public Put setFilter(ObTableFilter filter) throws Exception {
return setFilterOnly(filter);
}
/*
* get operation type
*/
public ObTableOperationType getOperationType() {
return ObTableOperationType.PUT;
}
/*
* add mutated Row
*/
public Put addMutateRow(Row rows) {
if (null == rows) {
throw new IllegalArgumentException("Invalid null rowKey set into Put");
} else if (0 == rows.getMap().size()) {
throw new IllegalArgumentException("input row key should not be empty");
}
// set mutate row into Put
for (Map.Entry<String, Object> entry : rows.getMap().entrySet()) {
columns.add(entry.getKey());
values.add(entry.getValue());
}
return this;
}
/*
* get the mutated columns' name
*/
public String[] getColumns() {
return columns.toArray(new String[columns.size()]);
}
/*
* get the mutated columns' value
*/
public Object[] getValues() {
return values.toArray();
}
/*
* add mutated ColumnValues
*/
public Put addMutateColVal(ColumnValue... columnValues) throws Exception {
if (null == columnValues) {
throw new IllegalArgumentException("Invalid null columnValues set into Put");
}
// set mutate row into Put
for (ColumnValue columnValue : columnValues) {
if (columns.contains(columnValue.getColumnName())) {
throw new ObTableException("Duplicate column in Row Key");
}
columns.add(columnValue.getColumnName());
values.add(columnValue.getValue());
}
return this;
}
/*
* Remove rowkey from mutateColval
*/
public Put removeRowkeyFromMutateColval() {
removeRowkeyFromMutateColval(this.columns, this.values, this.rowKeyNames);
return this;
}
/*
* execute
*/
public MutationResult execute() throws Exception {
if (null == getTableName()) {
throw new ObTableException("table name is null");
} else if (null == getClient()) {
throw new ObTableException("client is null");
}
if (null == getQuery()) {
// simple Put, without filter
return new MutationResult(((ObTableClient) getClient()).putWithResult(getTableName(),
getRowKey(), getKeyRanges(), columns.toArray(new String[columns.size()]),
values.toArray()));
} else {
if (checkMutationWithFilter()) {
// QueryAndPut
ObTableOperation operation = ObTableOperation.getInstance(ObTableOperationType.PUT,
getRowKey(), columns.toArray(new String[columns.size()]), values.toArray());
return new MutationResult(((ObTableClient) getClient()).mutationWithFilter(
getQuery(), getRowKey(), getKeyRanges(), operation, true));
} else {
throw new ObTableUnexpectedException("should set filter and scan range both");
}
}
}
}