Skip to content

Commit 16d3c81

Browse files
committed
opt in 0308~0310
1 parent 39bc4cc commit 16d3c81

File tree

11 files changed

+431
-200
lines changed

11 files changed

+431
-200
lines changed

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/impl/ObObj.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,30 @@
2222
import com.alipay.oceanbase.rpc.util.ObVString;
2323
import io.netty.buffer.ByteBuf;
2424

25+
import java.util.concurrent.ConcurrentLinkedQueue;
26+
import java.util.concurrent.atomic.AtomicInteger;
27+
2528
public class ObObj implements ObSimplePayload {
2629

2730
private static ObObj MAX_OBJECT;
2831
private static ObObj MIN_OBJECT;
32+
private static ObObj NULL_OBJECT;
2933
private static long MAX_OBJECT_VALUE = -2L;
3034
private static long MIN_OBJECT_VALUE = -3L;
3135

3236
static {
3337
MAX_OBJECT = new ObObj(ObObjType.ObExtendType.getDefaultObjMeta(), MAX_OBJECT_VALUE);
3438
MIN_OBJECT = new ObObj(ObObjType.ObExtendType.getDefaultObjMeta(), MIN_OBJECT_VALUE);
39+
NULL_OBJECT = new ObObj(ObObjType.ObNullType.getDefaultObjMeta(), null);
40+
}
41+
private static final int OBJ_POOL_SIZE = 100000;
42+
private static final AtomicInteger CURRENT_INDEX = new AtomicInteger(0);
43+
private static final ConcurrentLinkedQueue<ObObj> OBJ_POOL = new ConcurrentLinkedQueue<>();
44+
static {
45+
// 初始化对象池
46+
for (int i = 0; i < OBJ_POOL_SIZE; i++) {
47+
OBJ_POOL.offer(new ObObj());
48+
}
3549
}
3650

3751
/*
@@ -158,14 +172,33 @@ public void setValue(Object value) {
158172
* Get instance.
159173
*/
160174
public static ObObj getInstance(Object value) {
161-
ObObjMeta meta = ObObjType.defaultObjMeta(value);
175+
ObObjType type = ObObjType.valueOfType(value);
176+
ObObjMeta meta = null;
177+
if (type == ObObjType.ObVarcharType) {
178+
meta = ObObjMetaPool.varchrObjMeta;
179+
} else {
180+
meta = ObObjType.defaultObjMeta(value);
181+
}
182+
ObObj obj = OBJ_POOL.poll();
183+
if (obj == null) {
184+
// 如果池为空,创建新对象
185+
obj = new ObObj();
186+
}
187+
// 初始化对象
188+
obj.setMeta(meta);
162189
if (value instanceof ObObj) {
163-
return new ObObj(meta, ((ObObj) value).getValue());
190+
obj.setValue(((ObObj) value).getValue());
164191
} else {
165-
return new ObObj(meta, value);
192+
obj.setValue(value);
166193
}
194+
return obj;
167195
}
168196

197+
public static ObObj getNullObject() {
198+
return NULL_OBJECT;
199+
}
200+
201+
169202
/*
170203
* Get max.
171204
*/

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/impl/ObObjType.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
import java.time.*;
2727
import java.util.*;
2828

29+
class ObObjMetaPool {
30+
public static ObObjMeta varchrObjMeta = null;
31+
static {
32+
varchrObjMeta = new ObObjMeta(ObObjType.ObVarcharType, ObCollationLevel.CS_LEVEL_EXPLICIT,
33+
ObCollationType.CS_TYPE_UTF8MB4_GENERAL_CI, (byte) 10);
34+
}
35+
}
36+
37+
2938
public enum ObObjType {
3039

3140
ObNullType(0) {

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/impl/ObTableObjType.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ public void decode(ByteBuf buf, ObObj obj) {
207207
private static Map<Integer, ObTableObjType> valueMap = new HashMap<Integer, ObTableObjType>();
208208
// mapping from ObTableObjType to ObObjType
209209
private static Map<ObTableObjType, ObObjType> tableObjTypeMap = new HashMap<>();
210-
// mapping from ObObjType to ObTableObjType
211-
private static Map<ObObjType, ObTableObjType> objTableTypeMap = new HashMap<>();
212210

213211
ObTableObjType(int value) {
214212
this.value = value;
@@ -220,38 +218,46 @@ public void decode(ByteBuf buf, ObObj obj) {
220218
}
221219
}
222220

223-
static {
224-
objTableTypeMap.put(ObObjType.ObNullType, ObTableObjType.ObTableNullType);
225-
objTableTypeMap.put(ObObjType.ObTinyIntType, ObTableObjType.ObTableTinyIntType);
226-
objTableTypeMap.put(ObObjType.ObSmallIntType, ObTableObjType.ObTableSmallIntType);
227-
objTableTypeMap.put(ObObjType.ObInt32Type, ObTableObjType.ObTableInt32Type);
228-
objTableTypeMap.put(ObObjType.ObInt64Type, ObTableObjType.ObTableInt64Type);
229-
objTableTypeMap.put(ObObjType.ObVarcharType, ObTableObjType.ObTableVarcharType);
230-
objTableTypeMap.put(ObObjType.ObDoubleType, ObTableObjType.ObTableDoubleType);
231-
objTableTypeMap.put(ObObjType.ObFloatType, ObTableObjType.ObTableFloatType);
232-
objTableTypeMap.put(ObObjType.ObTimestampType, ObTableObjType.ObTableTimestampType);
233-
objTableTypeMap.put(ObObjType.ObDateTimeType, ObTableObjType.ObTableDateTimeType);
234-
objTableTypeMap.put(ObObjType.ObExtendType, null);
235-
objTableTypeMap.put(ObObjType.ObCharType, ObTableObjType.ObTableCharType);
236-
}
237-
238221
public static ObTableObjType getTableObjType(ObObj obj) {
239222
ObObjType objType = obj.getMeta().getType();
240223
ObCollationType objCsType = obj.getMeta().getCsType();
241-
ObTableObjType tableObjType = objTableTypeMap.get(objType);
242-
if (objType == ObObjType.ObVarcharType && objCsType == ObCollationType.CS_TYPE_BINARY) {
243-
tableObjType = ObTableObjType.ObTableVarbinaryType;
224+
if (objType == ObObjType.ObNullType) {
225+
// only for GET operation default value
226+
return ObTableNullType;
227+
} else if (objType == ObObjType.ObTinyIntType) {
228+
return ObTableTinyIntType;
229+
} else if (objType == ObObjType.ObSmallIntType) {
230+
return ObTableObjType.ObTableSmallIntType;
231+
} else if (objType == ObObjType.ObInt32Type) {
232+
return ObTableObjType.ObTableInt32Type;
233+
} else if (objType == ObObjType.ObInt64Type) {
234+
return ObTableObjType.ObTableInt64Type;
235+
} else if (objType == ObObjType.ObVarcharType) {
236+
if (objCsType == ObCollationType.CS_TYPE_BINARY) {
237+
return ObTableObjType.ObTableVarbinaryType;
238+
} else {
239+
return ObTableObjType.ObTableVarcharType;
240+
}
241+
} else if (objType == ObObjType.ObDoubleType) {
242+
return ObTableObjType.ObTableDoubleType;
243+
} else if (objType == ObObjType.ObFloatType) {
244+
return ObTableObjType.ObTableFloatType;
245+
} else if (objType == ObObjType.ObTimestampType) {
246+
return ObTableObjType.ObTableTimestampType;
247+
} else if (objType == ObObjType.ObDateTimeType) {
248+
return ObTableObjType.ObTableDateTimeType;
244249
} else if (objType == ObObjType.ObExtendType) {
245250
if (obj.isMinObj()) {
246-
tableObjType = ObTableObjType.ObTableMinType;
251+
return ObTableObjType.ObTableMinType;
247252
} else if (obj.isMaxObj()) {
248-
tableObjType = ObTableObjType.ObTableMaxType;
253+
return ObTableObjType.ObTableMaxType;
249254
}
250-
} else if (tableObjType == null) {
251-
throw new IllegalArgumentException("Cannot get ObTableObjType, invalid ob obj type: " + objType);
255+
} else if (objType == ObObjType.ObCharType) {
256+
return ObTableObjType.ObTableCharType;
252257
}
253258

254-
return tableObjType;
259+
throw new IllegalArgumentException("cannot get ObTableObjType, invalid ob obj type: "
260+
+ objType.getClass().getName());
255261
}
256262

257263
static {

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/impl/execute/ObTableLSOperation.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class ObTableLSOperation extends AbstractPayload {
4747

4848
private static final int LS_ID_SIZE = 8;
4949
private static final long INVALID_LS_ID = -1;
50+
private boolean isHbase = false;
5051

5152
/*
5253
OB_UNIS_DEF_SERIALIZE(ObTableLSOp,
@@ -131,18 +132,25 @@ public void encode(ObByteBuf buf) {
131132

132133
// 3. encode table id
133134
Serialization.encodeVi64(buf, tableId);
135+
if (isHbase) {
136+
buf.writeBytes(StaticHbaseColumns.RowkeyColumSize);
137+
buf.writeBytes(StaticHbaseColumns.getKQTBytes());
138+
buf.writeBytes(StaticHbaseColumns.VColumSize);
139+
buf.writeBytes(StaticHbaseColumns.getVBytes());
140+
} else {
141+
// 4. encode rowKey names
142+
Serialization.encodeVi64(buf, rowKeyNames.size());
143+
for (String rowKeyName : rowKeyNames) {
144+
Serialization.encodeVString(buf, rowKeyName);
145+
}
134146

135-
// 4. encode rowKey names
136-
Serialization.encodeVi64(buf, rowKeyNames.size());
137-
for (String rowKeyName : rowKeyNames) {
138-
Serialization.encodeVString(buf, rowKeyName);
147+
// 5. encode properties names
148+
Serialization.encodeVi64(buf, propertiesNames.size());
149+
for (String propertyName : propertiesNames) {
150+
Serialization.encodeVString(buf, propertyName);
151+
}
139152
}
140153

141-
// 5. encode properties names
142-
Serialization.encodeVi64(buf, propertiesNames.size());
143-
for (String propertyName : propertiesNames) {
144-
Serialization.encodeVString(buf, propertyName);
145-
}
146154

147155
// 6. encode option flag
148156
Serialization.encodeVi64(buf, optionFlag.getValue());
@@ -212,16 +220,23 @@ public long getPayloadContentSize() {
212220
for (ObTableTabletOp operation : tabletOperations) {
213221
payloadContentSize += operation.getPayloadSize();
214222
}
223+
if (isHbase) {
224+
payloadContentSize += StaticHbaseColumns.RowkeyColumSize.length;
225+
payloadContentSize += StaticHbaseColumns.getKQTBytes().length;
226+
payloadContentSize += StaticHbaseColumns.VColumSize.length;
227+
payloadContentSize += StaticHbaseColumns.getVBytes().length;
228+
} else {
229+
payloadContentSize += Serialization.getNeedBytes(rowKeyNames.size());
230+
for (String rowKeyName : rowKeyNames) {
231+
payloadContentSize += Serialization.getNeedBytes(rowKeyName);
232+
}
215233

216-
payloadContentSize += Serialization.getNeedBytes(rowKeyNames.size());
217-
for (String rowKeyName : rowKeyNames) {
218-
payloadContentSize += Serialization.getNeedBytes(rowKeyName);
234+
payloadContentSize += Serialization.getNeedBytes(propertiesNames.size());
235+
for (String propertyName : propertiesNames) {
236+
payloadContentSize += Serialization.getNeedBytes(propertyName);
237+
}
219238
}
220239

221-
payloadContentSize += Serialization.getNeedBytes(propertiesNames.size());
222-
for (String propertyName : propertiesNames) {
223-
payloadContentSize += Serialization.getNeedBytes(propertyName);
224-
}
225240

226241
this.payLoadContentSize = payloadContentSize + LS_ID_SIZE + Serialization.getNeedBytes(optionFlag.getValue())
227242
+ Serialization.getNeedBytes(tableName) + Serialization.getNeedBytes(tableId);
@@ -368,10 +383,18 @@ public void prepareColumnNamesBitMap() {
368383
}
369384

370385
public void prepare() {
371-
this.collectColumnNamesIdxMap();
372-
this.beforeOption();
373-
this.prepareOption();
374-
this.prepareColumnNamesBitMap();
386+
if (isHbase) {
387+
this.rowKeyNames.add("K");
388+
this.rowKeyNames.add("Q");
389+
this.rowKeyNames.add("T");
390+
this.propertiesNames.add("V");
391+
this.setIsSamePropertiesNames(true);
392+
} else {
393+
this.collectColumnNamesIdxMap();
394+
this.beforeOption();
395+
this.prepareOption();
396+
this.prepareColumnNamesBitMap();
397+
}
375398
}
376399

377400
public long getLsId() {
@@ -382,4 +405,12 @@ public void setTableName(String tableName) {
382405
this.tableName = tableName;
383406
}
384407

408+
public boolean isHbase() {
409+
return isHbase;
410+
}
411+
412+
public void setHbase(boolean hbase) {
413+
isHbase = hbase;
414+
}
415+
385416
}

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/impl/execute/ObTableSingleOpEntity.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ public class ObTableSingleOpEntity extends AbstractPayload {
4242
private List<ObObj> propertiesValues = new ArrayList<>();
4343

4444
private boolean ignoreEncodePropertiesColumnNames = false;
45-
45+
private boolean isHbase = false;
4646
public ObTableSingleOpEntity() {}
4747

48+
public boolean isHbase() {
49+
return isHbase;
50+
}
51+
52+
public void setHbase(boolean hbase) {
53+
isHbase = hbase;
54+
}
55+
4856
/*
4957
* Encode.
5058
*/
@@ -108,11 +116,17 @@ public void encode(ObByteBuf buf) {
108116
encodeHeader(buf);
109117

110118
// 1. encode rowKey bitmap
111-
Serialization.encodeVi64(buf, rowKeyBitLen);
112-
for (byte b : rowKeyBitMap) {
113-
Serialization.encodeI8(buf, b);
119+
if (isHbase) {
120+
Serialization.encodeVi64(buf, 3L);
121+
Serialization.encodeI8(buf, (byte) 0b00000111);
122+
} else {
123+
Serialization.encodeVi64(buf, rowKeyBitLen);
124+
for (byte b : rowKeyBitMap) {
125+
Serialization.encodeI8(buf, b);
126+
}
114127
}
115128

129+
116130
// 2. encode rowkey
117131
Serialization.encodeVi64(buf, rowkey.size());
118132
for (ObObj obj : rowkey) {
@@ -122,6 +136,9 @@ public void encode(ObByteBuf buf) {
122136
// 3. encode property bitmap
123137
if (ignoreEncodePropertiesColumnNames) {
124138
Serialization.encodeVi64(buf,0L);
139+
} else if (isHbase) {
140+
Serialization.encodeVi64(buf, 1);
141+
Serialization.encodeI8(buf, (byte) 0b00000001);
125142
} else {
126143
Serialization.encodeVi64(buf, propertiesBitLen);
127144
for (byte b : propertiesBitMap) {
@@ -198,9 +215,13 @@ public Object decode(ByteBuf buf) {
198215
public long getPayloadContentSize() {
199216
if (this.payLoadContentSize == -1) {
200217
long payloadContentSize = 0;
201-
202-
payloadContentSize += Serialization.getNeedBytes(rowKeyBitLen);
203-
payloadContentSize += rowKeyBitMap.length;
218+
if (isHbase) {
219+
payloadContentSize += Serialization.getNeedBytes(3L);
220+
payloadContentSize += 1;
221+
} else {
222+
payloadContentSize += Serialization.getNeedBytes(rowKeyBitLen);
223+
payloadContentSize += rowKeyBitMap.length;
224+
}
204225

205226
payloadContentSize += Serialization.getNeedBytes(rowkey.size());
206227
for (ObObj obj : rowkey) {
@@ -209,6 +230,9 @@ public long getPayloadContentSize() {
209230

210231
if (ignoreEncodePropertiesColumnNames) {
211232
payloadContentSize += Serialization.getNeedBytes(0L);
233+
} else if (isHbase) {
234+
payloadContentSize += Serialization.getNeedBytes(1L);
235+
payloadContentSize += 1;
212236
} else {
213237
payloadContentSize += Serialization.getNeedBytes(propertiesBitLen);
214238
payloadContentSize += propertiesBitMap.length;

0 commit comments

Comments
 (0)