|
17 | 17 |
|
18 | 18 | package org.tikv.common.codec; |
19 | 19 |
|
20 | | -import java.util.ArrayList; |
21 | | -import java.util.HashMap; |
22 | | -import java.util.List; |
23 | 20 | import org.tikv.common.handle.Handle; |
24 | 21 | import org.tikv.common.meta.TiColumnInfo; |
25 | 22 | import org.tikv.common.meta.TiIndexColumn; |
|
28 | 25 | import org.tikv.common.row.ObjectRowImpl; |
29 | 26 | import org.tikv.common.row.Row; |
30 | 27 |
|
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | + |
31 | 32 | public class TableCodecV2 { |
32 | 33 |
|
33 | | - /** |
34 | | - * New Row Format: Reference |
35 | | - * https://github.com/pingcap/tidb/blob/952d1d7541a8e86be0af58f5b7e3d5e982bab34e/docs/design/2018-07-19-row-format.md |
36 | | - * |
37 | | - * <p>- version, flag, numOfNotNullCols, numOfNullCols, notNullCols, nullCols, notNullOffsets, |
38 | | - * notNullValues |
39 | | - */ |
40 | | - protected static byte[] encodeRow( |
41 | | - List<TiColumnInfo> columnInfos, Object[] values, boolean isPkHandle) { |
42 | | - RowEncoderV2 encoder = new RowEncoderV2(); |
43 | | - List<TiColumnInfo> columnInfoList = new ArrayList<>(); |
44 | | - List<Object> valueList = new ArrayList<>(); |
45 | | - for (int i = 0; i < columnInfos.size(); i++) { |
46 | | - TiColumnInfo col = columnInfos.get(i); |
47 | | - // skip pk is handle case |
48 | | - if (col.isPrimaryKey() && isPkHandle) { |
49 | | - continue; |
50 | | - } |
51 | | - columnInfoList.add(col); |
52 | | - valueList.add(values[i]); |
| 34 | + /** |
| 35 | + * New Row Format: Reference |
| 36 | + * https://github.com/pingcap/tidb/blob/952d1d7541a8e86be0af58f5b7e3d5e982bab34e/docs/design/2018-07-19-row-format.md |
| 37 | + * |
| 38 | + * <p>- version, flag, numOfNotNullCols, numOfNullCols, notNullCols, nullCols, notNullOffsets, |
| 39 | + * notNullValues |
| 40 | + */ |
| 41 | + protected static byte[] encodeRow( |
| 42 | + List<TiColumnInfo> columnInfos, Object[] values, boolean isPkHandle) { |
| 43 | + RowEncoderV2 encoder = new RowEncoderV2(); |
| 44 | + List<TiColumnInfo> columnInfoList = new ArrayList<>(); |
| 45 | + List<Object> valueList = new ArrayList<>(); |
| 46 | + for (int i = 0; i < columnInfos.size(); i++) { |
| 47 | + TiColumnInfo col = columnInfos.get(i); |
| 48 | + // skip pk is handle case |
| 49 | + if (col.isPrimaryKey() && isPkHandle) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + columnInfoList.add(col); |
| 53 | + valueList.add(values[i]); |
| 54 | + } |
| 55 | + return encoder.encode(columnInfoList, valueList); |
53 | 56 | } |
54 | | - return encoder.encode(columnInfoList, valueList); |
55 | | - } |
56 | 57 |
|
57 | | - protected static Object[] decodeObjects(byte[] value, Handle handle, TiTableInfo tableInfo) { |
58 | | - if (handle == null && tableInfo.isPkHandle()) { |
59 | | - throw new IllegalArgumentException("when pk is handle, handle cannot be null"); |
60 | | - } |
61 | | - int colSize = tableInfo.getColumns().size(); |
62 | | - // decode bytes to Map<ColumnID, Data> |
63 | | - HashMap<Long, Object> decodedDataMap = new HashMap<>(colSize); |
64 | | - org.tikv.common.codec.RowV2 rowV2 = org.tikv.common.codec.RowV2.createNew(value); |
| 58 | + protected static Object[] decodeObjects(byte[] value, Handle handle, TiTableInfo tableInfo) { |
| 59 | + if (handle == null && tableInfo.isPkHandle()) { |
| 60 | + throw new IllegalArgumentException("when pk is handle, handle cannot be null"); |
| 61 | + } |
| 62 | + int colSize = tableInfo.getColumns().size(); |
| 63 | + // decode bytes to Map<ColumnID, Data> |
| 64 | + HashMap<Long, Object> decodedDataMap = new HashMap<>(colSize); |
| 65 | + org.tikv.common.codec.RowV2 rowV2 = org.tikv.common.codec.RowV2.createNew(value); |
65 | 66 |
|
66 | | - TiIndexInfo pk = tableInfo.getPrimaryKey(); |
| 67 | + TiIndexInfo pk = tableInfo.getPrimaryKey(); |
67 | 68 |
|
68 | | - if (pk != null) { |
69 | | - List<TiColumnInfo> cols = new ArrayList<>(); |
70 | | - for (TiIndexColumn indexColumn : pk.getIndexColumns()) { |
71 | | - TiColumnInfo col = tableInfo.getColumn(indexColumn.getOffset()); |
72 | | - cols.add(col); |
73 | | - } |
74 | | - if (tableInfo.isPkHandle()) { |
75 | | - assert cols.size() == 1; |
76 | | - decodedDataMap.put(cols.get(0).getId(), handle.data()[0]); |
77 | | - } |
78 | | - if (tableInfo.isCommonHandle()) { |
79 | | - for (int i = 0; i < cols.size(); i++) { |
80 | | - decodedDataMap.put(cols.get(i).getId(), handle.data()[i]); |
| 69 | + if (pk != null) { |
| 70 | + List<TiColumnInfo> cols = new ArrayList<>(); |
| 71 | + for (TiIndexColumn indexColumn : pk.getIndexColumns()) { |
| 72 | + TiColumnInfo col = tableInfo.getColumn(indexColumn.getOffset()); |
| 73 | + cols.add(col); |
| 74 | + } |
| 75 | + if (tableInfo.isPkHandle()) { |
| 76 | + assert cols.size() == 1; |
| 77 | + decodedDataMap.put(cols.get(0).getId(), handle.data()[0]); |
| 78 | + } |
| 79 | + if (tableInfo.isCommonHandle()) { |
| 80 | + for (int i = 0; i < cols.size(); i++) { |
| 81 | + decodedDataMap.put(cols.get(i).getId(), handle.data()[i]); |
| 82 | + } |
| 83 | + } |
81 | 84 | } |
82 | | - } |
83 | | - } |
84 | 85 |
|
85 | | - for (TiColumnInfo col : tableInfo.getColumns()) { |
86 | | - if (decodedDataMap.containsKey(col.getId())) { |
87 | | - continue; |
88 | | - } else if (col.isPrimaryKey() && tableInfo.isPkHandle()) { |
89 | | - decodedDataMap.put(col.getId(), handle); |
90 | | - continue; |
91 | | - } |
92 | | - RowV2.ColIDSearchResult searchResult = rowV2.findColID(col.getId()); |
93 | | - if (searchResult.isNull) { |
94 | | - // current col is null, nothing should be added to decodedMap |
95 | | - continue; |
96 | | - } |
97 | | - if (!searchResult.notFound) { |
98 | | - // corresponding column should be found |
99 | | - assert (searchResult.idx != -1); |
100 | | - byte[] colData = rowV2.getData(searchResult.idx); |
101 | | - Object d = RowDecoderV2.decodeCol(colData, col.getType()); |
102 | | - decodedDataMap.put(col.getId(), d); |
103 | | - } |
104 | | - } |
105 | | - Object[] res = new Object[colSize]; |
| 86 | + for (TiColumnInfo col : tableInfo.getColumns()) { |
| 87 | + if (decodedDataMap.containsKey(col.getId())) { |
| 88 | + continue; |
| 89 | + } else if (col.isPrimaryKey() && tableInfo.isPkHandle()) { |
| 90 | + decodedDataMap.put(col.getId(), handle.data()); |
| 91 | + continue; |
| 92 | + } |
| 93 | + RowV2.ColIDSearchResult searchResult = rowV2.findColID(col.getId()); |
| 94 | + if (searchResult.isNull) { |
| 95 | + // current col is null, nothing should be added to decodedMap |
| 96 | + continue; |
| 97 | + } |
| 98 | + if (!searchResult.notFound) { |
| 99 | + // corresponding column should be found |
| 100 | + assert (searchResult.idx != -1); |
| 101 | + byte[] colData = rowV2.getData(searchResult.idx); |
| 102 | + Object d = RowDecoderV2.decodeCol(colData, col.getType()); |
| 103 | + decodedDataMap.put(col.getId(), d); |
| 104 | + } |
| 105 | + } |
| 106 | + Object[] res = new Object[colSize]; |
106 | 107 |
|
107 | | - // construct Row with Map<ColumnID, Data> & handle |
108 | | - for (int i = 0; i < colSize; i++) { |
109 | | - // skip pk is handle case |
110 | | - TiColumnInfo col = tableInfo.getColumn(i); |
111 | | - res[i] = decodedDataMap.get(col.getId()); |
| 108 | + // construct Row with Map<ColumnID, Data> & handle |
| 109 | + for (int i = 0; i < colSize; i++) { |
| 110 | + // skip pk is handle case |
| 111 | + TiColumnInfo col = tableInfo.getColumn(i); |
| 112 | + res[i] = decodedDataMap.get(col.getId()); |
| 113 | + } |
| 114 | + return res; |
112 | 115 | } |
113 | | - return res; |
114 | | - } |
115 | 116 |
|
116 | | - protected static Row decodeRow(byte[] value, Handle handle, TiTableInfo tableInfo) { |
117 | | - return ObjectRowImpl.create(decodeObjects(value, handle, tableInfo)); |
118 | | - } |
| 117 | + protected static Row decodeRow(byte[] value, Handle handle, TiTableInfo tableInfo) { |
| 118 | + return ObjectRowImpl.create(decodeObjects(value, handle, tableInfo)); |
| 119 | + } |
119 | 120 | } |
0 commit comments