1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifdef RELATIONAL_STORE
16 #include "data_transformer.h"
17
18 #include "db_common.h"
19 #include "db_errno.h"
20 #include "log_print.h"
21 #include "parcel.h"
22 #include "relational_schema_object.h"
23
24 namespace DistributedDB {
TransformTableData(const TableDataWithLog & tableDataWithLog,const std::vector<FieldInfo> & fieldInfoList,std::vector<DataItem> & dataItems)25 int DataTransformer::TransformTableData(const TableDataWithLog &tableDataWithLog,
26 const std::vector<FieldInfo> &fieldInfoList, std::vector<DataItem> &dataItems)
27 {
28 if (tableDataWithLog.dataList.empty()) {
29 return E_OK;
30 }
31 for (const RowDataWithLog& data : tableDataWithLog.dataList) {
32 DataItem dataItem;
33 int errCode = SerializeDataItem(data, fieldInfoList, dataItem);
34 if (errCode != E_OK) {
35 return errCode;
36 }
37 dataItems.push_back(std::move(dataItem));
38 }
39 return E_OK;
40 }
41
TransformDataItem(const std::vector<DataItem> & dataItems,const std::vector<FieldInfo> & remoteFieldInfo,const std::vector<FieldInfo> & localFieldInfo,OptTableDataWithLog & tableDataWithLog)42 int DataTransformer::TransformDataItem(const std::vector<DataItem> &dataItems,
43 const std::vector<FieldInfo> &remoteFieldInfo, const std::vector<FieldInfo> &localFieldInfo,
44 OptTableDataWithLog &tableDataWithLog)
45 {
46 if (dataItems.empty()) {
47 return E_OK;
48 }
49 std::vector<int> indexMapping;
50 ReduceMapping(remoteFieldInfo, localFieldInfo);
51 for (const DataItem &dataItem : dataItems) {
52 OptRowDataWithLog dataWithLog;
53 int errCode = DeSerializeDataItem(dataItem, dataWithLog, remoteFieldInfo);
54 if (errCode != E_OK) {
55 return errCode;
56 }
57 tableDataWithLog.dataList.push_back(std::move(dataWithLog));
58 }
59 return E_OK;
60 }
61
SerializeDataItem(const RowDataWithLog & data,const std::vector<FieldInfo> & fieldInfo,DataItem & dataItem)62 int DataTransformer::SerializeDataItem(const RowDataWithLog &data,
63 const std::vector<FieldInfo> &fieldInfo, DataItem &dataItem)
64 {
65 int errCode = SerializeValue(dataItem.value, data.rowData, fieldInfo);
66 if (errCode != E_OK) {
67 return errCode;
68 }
69 const LogInfo &logInfo = data.logInfo;
70 dataItem.timeStamp = logInfo.timestamp;
71 dataItem.dev = logInfo.device;
72 dataItem.origDev = logInfo.originDev;
73 dataItem.writeTimeStamp = logInfo.wTimeStamp;
74 dataItem.flag = logInfo.flag;
75 dataItem.hashKey = logInfo.hashKey;
76 return E_OK;
77 }
78
DeSerializeDataItem(const DataItem & dataItem,OptRowDataWithLog & data,const std::vector<FieldInfo> & remoteFieldInfo)79 int DataTransformer::DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data,
80 const std::vector<FieldInfo> &remoteFieldInfo)
81 {
82 if ((dataItem.flag & DataItem::DELETE_FLAG) == 0 &&
83 (dataItem.flag & DataItem::REMOTE_DEVICE_DATA_MISS_QUERY) == 0) {
84 int errCode = DeSerializeValue(dataItem.value, data.optionalData, remoteFieldInfo);
85 if (errCode != E_OK) {
86 return errCode;
87 }
88 }
89
90 LogInfo &logInfo = data.logInfo;
91 logInfo.timestamp = dataItem.timeStamp;
92 logInfo.device = dataItem.dev;
93 logInfo.originDev = dataItem.origDev;
94 logInfo.wTimeStamp = dataItem.writeTimeStamp;
95 logInfo.flag = dataItem.flag;
96 logInfo.hashKey = dataItem.hashKey;
97 return E_OK;
98 }
99
CalDataValueLength(const DataValue & dataValue)100 uint32_t DataTransformer::CalDataValueLength(const DataValue &dataValue)
101 {
102 static std::map<StorageType, uint32_t> lengthMap = {
103 { StorageType::STORAGE_TYPE_NULL, Parcel::GetUInt32Len()},
104 { StorageType::STORAGE_TYPE_INTEGER, Parcel::GetInt64Len()},
105 { StorageType::STORAGE_TYPE_REAL, Parcel::GetDoubleLen()}
106 };
107 if (lengthMap.find(dataValue.GetType()) != lengthMap.end()) {
108 return lengthMap[dataValue.GetType()];
109 }
110 if (dataValue.GetType() != StorageType::STORAGE_TYPE_BLOB &&
111 dataValue.GetType() != StorageType::STORAGE_TYPE_TEXT) {
112 return 0u;
113 }
114 uint32_t length = 0;
115 switch (dataValue.GetType()) {
116 case StorageType::STORAGE_TYPE_BLOB:
117 case StorageType::STORAGE_TYPE_TEXT:
118 (void)dataValue.GetBlobLength(length);
119 length = Parcel::GetEightByteAlign(length);
120 length += Parcel::GetUInt32Len(); // record data length
121 break;
122 default:
123 break;
124 }
125 return length;
126 }
127
ReduceMapping(const std::vector<FieldInfo> & remoteFieldInfo,const std::vector<FieldInfo> & localFieldInfo)128 void DataTransformer::ReduceMapping(const std::vector<FieldInfo> &remoteFieldInfo,
129 const std::vector<FieldInfo> &localFieldInfo)
130 {
131 std::map<std::string, int> fieldMap;
132 for (int i = 0; i < static_cast<int>(remoteFieldInfo.size()); ++i) {
133 const auto &fieldInfo = remoteFieldInfo[i];
134 fieldMap[fieldInfo.GetFieldName()] = i;
135 }
136 }
137
138 namespace {
SerializeNullValue(const DataValue & dataValue,Parcel & parcel)139 int SerializeNullValue(const DataValue &dataValue, Parcel &parcel)
140 {
141 return parcel.WriteUInt32(0u);
142 }
143
DeSerializeNullValue(DataValue & dataValue,Parcel & parcel)144 int DeSerializeNullValue(DataValue &dataValue, Parcel &parcel)
145 {
146 uint32_t dataLength = -1;
147 (void)parcel.ReadUInt32(dataLength);
148 if (parcel.IsError() || dataLength != 0) {
149 return -E_PARSE_FAIL;
150 }
151 dataValue.ResetValue();
152 return E_OK;
153 }
154
SerializeIntValue(const DataValue & dataValue,Parcel & parcel)155 int SerializeIntValue(const DataValue &dataValue, Parcel &parcel)
156 {
157 int64_t val = 0;
158 (void)dataValue.GetInt64(val);
159 return parcel.WriteInt64(val);
160 }
161
DeSerializeIntValue(DataValue & dataValue,Parcel & parcel)162 int DeSerializeIntValue(DataValue &dataValue, Parcel &parcel)
163 {
164 int64_t val = 0;
165 (void)parcel.ReadInt64(val);
166 if (parcel.IsError()) {
167 return -E_PARSE_FAIL;
168 }
169 dataValue = val;
170 return E_OK;
171 }
172
SerializeDoubleValue(const DataValue & dataValue,Parcel & parcel)173 int SerializeDoubleValue(const DataValue &dataValue, Parcel &parcel)
174 {
175 double val = 0;
176 (void)dataValue.GetDouble(val);
177 return parcel.WriteDouble(val);
178 }
179
DeSerializeDoubleValue(DataValue & dataValue,Parcel & parcel)180 int DeSerializeDoubleValue(DataValue &dataValue, Parcel &parcel)
181 {
182 double val = 0;
183 (void)parcel.ReadDouble(val);
184 if (parcel.IsError()) {
185 return -E_PARSE_FAIL;
186 }
187 dataValue = val;
188 return E_OK;
189 }
190
SerializeBlobValue(const DataValue & dataValue,Parcel & parcel)191 int SerializeBlobValue(const DataValue &dataValue, Parcel &parcel)
192 {
193 Blob val;
194 (void)dataValue.GetBlob(val);
195 uint32_t size = val.GetSize();
196 if (size == 0) {
197 return SerializeNullValue(dataValue, parcel);
198 }
199 int errCode = parcel.WriteUInt32(size);
200 if (errCode != E_OK) {
201 return errCode;
202 }
203 return parcel.WriteBlob(reinterpret_cast<const char *>(val.GetData()), size);
204 }
205
DeSerializeBlobByType(DataValue & dataValue,Parcel & parcel,StorageType type)206 int DeSerializeBlobByType(DataValue &dataValue, Parcel &parcel, StorageType type)
207 {
208 uint32_t blobLength = 0;
209 (void)parcel.ReadUInt32(blobLength);
210 if (blobLength == 0) {
211 dataValue.ResetValue();
212 return E_OK;
213 }
214 if (blobLength >= DBConstant::MAX_VALUE_SIZE || parcel.IsError()) { // One blob cannot be over one value size.
215 return -E_PARSE_FAIL;
216 }
217 auto array = new (std::nothrow) char[blobLength]();
218 if (array == nullptr) {
219 return -E_OUT_OF_MEMORY;
220 }
221 (void)parcel.ReadBlob(array, blobLength);
222 if (parcel.IsError()) {
223 delete []array;
224 return -E_PARSE_FAIL;
225 }
226 int errCode = -E_NOT_SUPPORT;
227 if (type == StorageType::STORAGE_TYPE_TEXT) {
228 errCode = dataValue.SetText(reinterpret_cast<const uint8_t *>(array), blobLength);
229 } else if (type == StorageType::STORAGE_TYPE_BLOB) {
230 Blob val;
231 errCode = val.WriteBlob(reinterpret_cast<const uint8_t *>(array), blobLength);
232 if (errCode == E_OK) {
233 errCode = dataValue.SetBlob(val);
234 }
235 }
236 delete []array;
237 return errCode;
238 }
239
DeSerializeBlobValue(DataValue & dataValue,Parcel & parcel)240 int DeSerializeBlobValue(DataValue &dataValue, Parcel &parcel)
241 {
242 return DeSerializeBlobByType(dataValue, parcel, StorageType::STORAGE_TYPE_BLOB);
243 }
244
SerializeTextValue(const DataValue & dataValue,Parcel & parcel)245 int SerializeTextValue(const DataValue &dataValue, Parcel &parcel)
246 {
247 return SerializeBlobValue(dataValue, parcel);
248 }
249
DeSerializeTextValue(DataValue & dataValue,Parcel & parcel)250 int DeSerializeTextValue(DataValue &dataValue, Parcel &parcel)
251 {
252 return DeSerializeBlobByType(dataValue, parcel, StorageType::STORAGE_TYPE_TEXT);
253 }
254
SerializeDataValue(const DataValue & dataValue,Parcel & parcel)255 int SerializeDataValue(const DataValue &dataValue, Parcel &parcel)
256 {
257 static const std::function<int(const DataValue&, Parcel&)> funcs[] = {
258 SerializeNullValue, SerializeIntValue,
259 SerializeDoubleValue, SerializeTextValue, SerializeBlobValue,
260 };
261 StorageType type = dataValue.GetType();
262 parcel.WriteUInt32(static_cast<uint32_t>(type));
263 if (type < StorageType::STORAGE_TYPE_NULL || type > StorageType::STORAGE_TYPE_BLOB) {
264 LOGE("Cannot serialize %u", type);
265 return -E_NOT_SUPPORT;
266 }
267 return funcs[static_cast<uint32_t>(type) - 1](dataValue, parcel);
268 }
269
DeserializeDataValue(DataValue & dataValue,Parcel & parcel)270 int DeserializeDataValue(DataValue &dataValue, Parcel &parcel)
271 {
272 static const std::function<int(DataValue&, Parcel&)> funcs[] = {
273 DeSerializeNullValue, DeSerializeIntValue,
274 DeSerializeDoubleValue, DeSerializeTextValue, DeSerializeBlobValue,
275 };
276 uint32_t type = 0;
277 parcel.ReadUInt32(type);
278 if (type < static_cast<uint32_t>(StorageType::STORAGE_TYPE_NULL) ||
279 type > static_cast<uint32_t>(StorageType::STORAGE_TYPE_BLOB)) {
280 LOGE("Cannot deserialize %u", type);
281 return -E_PARSE_FAIL;
282 }
283 return funcs[type - 1](dataValue, parcel);
284 }
285 }
286
SerializeValue(Value & value,const RowData & rowData,const std::vector<FieldInfo> & fieldInfoList)287 int DataTransformer::SerializeValue(Value &value, const RowData &rowData, const std::vector<FieldInfo> &fieldInfoList)
288 {
289 if (rowData.size() != fieldInfoList.size()) {
290 LOGE("[DataTransformer][SerializeValue] unequal field counts!");
291 return -E_INVALID_ARGS;
292 }
293
294 uint32_t totalLength = Parcel::GetUInt64Len(); // first record field count
295 for (uint32_t i = 0; i < rowData.size(); ++i) {
296 const auto &dataValue = rowData[i];
297 totalLength += Parcel::GetUInt32Len(); // For save the dataValue's type.
298 uint32_t dataLength = CalDataValueLength(dataValue);
299 totalLength += dataLength;
300 }
301 value.resize(totalLength);
302 if (value.size() != totalLength) {
303 return -E_OUT_OF_MEMORY;
304 }
305 Parcel parcel(value.data(), value.size());
306 (void)parcel.WriteUInt64(rowData.size());
307 for (const auto &dataValue : rowData) {
308 int errCode = SerializeDataValue(dataValue, parcel);
309 if (errCode != E_OK) {
310 value.clear();
311 return errCode;
312 }
313 }
314 return E_OK;
315 }
316
DeSerializeValue(const Value & value,OptRowData & optionalData,const std::vector<FieldInfo> & remoteFieldInfo)317 int DataTransformer::DeSerializeValue(const Value &value, OptRowData &optionalData,
318 const std::vector<FieldInfo> &remoteFieldInfo)
319 {
320 Parcel parcel(const_cast<uint8_t *>(value.data()), value.size());
321 uint64_t fieldCount = 0;
322 (void)parcel.ReadUInt64(fieldCount);
323 if (fieldCount > DBConstant::MAX_COLUMN || parcel.IsError()) {
324 return -E_PARSE_FAIL;
325 }
326 for (size_t i = 0; i < fieldCount; ++i) {
327 DataValue dataValue;
328 int errCode = DeserializeDataValue(dataValue, parcel);
329 if (errCode != E_OK) {
330 LOGD("[DataTransformer][DeSerializeValue] deSerialize failed");
331 return errCode;
332 }
333 optionalData.push_back(std::move(dataValue));
334 }
335 return E_OK;
336 }
337 } // namespace DistributedDB
338 #endif