Skip to content

Commit b4df900

Browse files
committed
opt for ObObj encodeSize and ObTableObjType
1 parent 7aae1ba commit b4df900

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
import com.alipay.oceanbase.rpc.util.ObByteBuf;
2222
import com.alipay.oceanbase.rpc.util.ObVString;
2323
import io.netty.buffer.ByteBuf;
24+
import static com.alipay.oceanbase.rpc.protocol.payload.impl.ObTableObjType.ObTableInvalidType;
2425

2526
public class ObObj implements ObSimplePayload {
2627

2728
private static ObObj MAX_OBJECT;
2829
private static ObObj MIN_OBJECT;
2930
private static long MAX_OBJECT_VALUE = -2L;
3031
private static long MIN_OBJECT_VALUE = -3L;
32+
private int encodeSizeCache = -1; // use for cache encodeSize to avoid calculate repeatedly
33+
private ObTableObjType tableObjTypeCache = ObTableInvalidType;
3134

3235
static {
3336
MAX_OBJECT = new ObObj(ObObjType.ObExtendType.getDefaultObjMeta(), MAX_OBJECT_VALUE);
@@ -50,6 +53,23 @@ public ObObj(ObObjMeta meta, Object value) {
5053
setValue(value);
5154
}
5255

56+
public int getEncodeSizeCache() {
57+
return encodeSizeCache;
58+
}
59+
60+
public void setEncodeSizeCache(int encodeSizeCache) {
61+
this.encodeSizeCache = encodeSizeCache;
62+
}
63+
64+
public ObTableObjType getTableObjType() {
65+
return tableObjTypeCache;
66+
}
67+
68+
public void setTableObjType(ObTableObjType tableObjType) {
69+
this.tableObjTypeCache = tableObjType;
70+
}
71+
72+
5373
// 1. 序列化 meta
5474
private ObObjMeta meta;
5575
// 2. 序列化 valueLength 还是 nmb_desc_?

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,25 @@ public void decode(ByteBuf buf, ObObj obj) {
217217
}
218218

219219
public static ObTableObjType getTableObjType(ObObj obj) {
220-
ObObjType objType = obj.getMeta().getType();
221-
ObCollationType objCsType = obj.getMeta().getCsType();
222-
ObTableObjType tableObjType = objTableTypeMap.get(objType);
223-
if (objType == ObObjType.ObVarcharType && objCsType == ObCollationType.CS_TYPE_BINARY) {
224-
tableObjType = ObTableObjType.ObTableVarbinaryType;
225-
} else if (objType == ObObjType.ObExtendType) {
226-
if (obj.isMinObj()) {
227-
tableObjType = ObTableObjType.ObTableMinType;
228-
} else if (obj.isMaxObj()) {
229-
tableObjType = ObTableObjType.ObTableMaxType;
220+
ObTableObjType tableObjType = null;
221+
if (obj.getTableObjType() == ObTableInvalidType) {
222+
ObObjType objType = obj.getMeta().getType();
223+
ObCollationType objCsType = obj.getMeta().getCsType();
224+
tableObjType = objTableTypeMap.get(objType);
225+
if (objType == ObObjType.ObVarcharType && objCsType == ObCollationType.CS_TYPE_BINARY) {
226+
tableObjType = ObTableObjType.ObTableVarbinaryType;
227+
} else if (objType == ObObjType.ObExtendType) {
228+
if (obj.isMinObj()) {
229+
tableObjType = ObTableObjType.ObTableMinType;
230+
} else if (obj.isMaxObj()) {
231+
tableObjType = ObTableObjType.ObTableMaxType;
232+
}
233+
} else if (tableObjType == null) {
234+
throw new IllegalArgumentException("Cannot get ObTableObjType, invalid ob obj type: " + objType);
230235
}
231-
} else if (tableObjType == null) {
232-
throw new IllegalArgumentException("Cannot get ObTableObjType, invalid ob obj type: " + objType);
236+
obj.setTableObjType(tableObjType);
237+
} else {
238+
tableObjType = obj.getTableObjType();
233239
}
234240

235241
return tableObjType;
@@ -305,8 +311,15 @@ public void decode(ByteBuf buf, ObObj obj) {
305311
}
306312

307313
public int getEncodedSize(ObObj obj) {
308-
ObObjType objType = obj.getMeta().getType();
309-
return DEFAULT_TABLE_OBJ_TYPE_SIZE + objType.getEncodedSize(obj.getValue());
314+
int encodeSize = 0;
315+
if (obj.getEncodeSizeCache() == -1) {
316+
ObObjType objType = obj.getMeta().getType();
317+
encodeSize = DEFAULT_TABLE_OBJ_TYPE_SIZE + objType.getEncodedSize(obj.getValue());
318+
obj.setEncodeSizeCache(encodeSize);
319+
} else {
320+
encodeSize = obj.getEncodeSizeCache();
321+
}
322+
return encodeSize;
310323
}
311324

312325
public byte[] encodeWithMeta(ObObj obj) {

0 commit comments

Comments
 (0)