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 #ifndef DATA_TRANSFORMER_H 16 #define DATA_TRANSFORMER_H 17 #ifdef RELATIONAL_STORE 18 19 #include <optional> 20 #include <vector> 21 #include "data_value.h" 22 #include "db_types.h" 23 #include "relational_schema_object.h" 24 25 namespace DistributedDB { 26 using RowData = std::vector<DataValue>; 27 using OptRowData = std::vector<DataValue>; 28 29 struct LogInfo { 30 int64_t dataKey = -1; 31 std::string device; 32 std::string originDev; 33 TimeStamp timestamp = 0; 34 TimeStamp wTimeStamp = 0; 35 uint64_t flag = 0; 36 Key hashKey; // primary key hash value 37 }; 38 39 struct RowDataWithLog { 40 LogInfo logInfo; 41 RowData rowData; 42 }; 43 44 struct OptRowDataWithLog { 45 LogInfo logInfo; 46 OptRowData optionalData; 47 }; 48 49 struct TableDataWithLog { 50 std::string tableName; 51 std::vector<RowDataWithLog> dataList; 52 }; 53 54 struct OptTableDataWithLog { 55 std::string tableName; 56 std::vector<OptRowDataWithLog> dataList; 57 }; 58 59 class DataTransformer { 60 public: 61 static int TransformTableData(const TableDataWithLog &tableDataWithLog, 62 const std::vector<FieldInfo> &fieldInfoList, std::vector<DataItem> &dataItems); 63 static int TransformDataItem(const std::vector<DataItem> &dataItems, const std::vector<FieldInfo> &remoteFieldInfo, 64 const std::vector<FieldInfo> &localFieldInfo, OptTableDataWithLog &tableDataWithLog); 65 66 static int SerializeDataItem(const RowDataWithLog &data, const std::vector<FieldInfo> &fieldInfo, 67 DataItem &dataItem); 68 static int DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data, 69 const std::vector<FieldInfo> &remoteFieldInfo); 70 static void ReduceMapping(const std::vector<FieldInfo> &remoteFieldInfo, 71 const std::vector<FieldInfo> &localFieldInfo); 72 73 private: 74 static int SerializeValue(Value &value, const RowData &rowData, const std::vector<FieldInfo> &fieldInfoList); 75 static int DeSerializeValue(const Value &value, OptRowData &optionalData, 76 const std::vector<FieldInfo> &remoteFieldInfo); 77 78 static uint32_t CalDataValueLength(const DataValue &dataValue); 79 }; 80 } 81 82 #endif 83 #endif // DATA_TRANSFORMER_H