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 <vector> 20 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 std::string cloudGid; // use for sync with cloud 38 }; 39 40 struct RowDataWithLog { 41 LogInfo logInfo; 42 RowData rowData; 43 }; 44 45 struct OptRowDataWithLog { 46 LogInfo logInfo; 47 OptRowData optionalData; 48 }; 49 50 struct TableDataWithLog { 51 std::string tableName; 52 std::vector<RowDataWithLog> dataList; 53 }; 54 55 struct OptTableDataWithLog { 56 std::string tableName; 57 std::vector<OptRowDataWithLog> dataList; 58 }; 59 60 // use for cloud sync 61 struct DataInfoWithLog { 62 LogInfo logInfo; 63 VBucket primaryKeys; 64 }; 65 66 class DataTransformer { 67 public: 68 static int TransformTableData(const TableDataWithLog &tableDataWithLog, 69 const std::vector<FieldInfo> &fieldInfoList, std::vector<DataItem> &dataItems); 70 static int TransformDataItem(const std::vector<DataItem> &dataItems, const std::vector<FieldInfo> &remoteFieldInfo, 71 OptTableDataWithLog &tableDataWithLog); 72 73 static int SerializeDataItem(const RowDataWithLog &data, const std::vector<FieldInfo> &fieldInfo, 74 DataItem &dataItem); 75 static int DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data, 76 const std::vector<FieldInfo> &remoteFieldInfo); 77 78 static uint32_t CalDataValueLength(const DataValue &dataValue); 79 static int DeserializeDataValue(DataValue &dataValue, Parcel &parcel); 80 static int SerializeDataValue(const DataValue &dataValue, Parcel &parcel); 81 82 private: 83 static int SerializeValue(Value &value, const RowData &rowData, const std::vector<FieldInfo> &fieldInfoList); 84 static int DeSerializeValue(const Value &value, OptRowData &optionalData, 85 const std::vector<FieldInfo> &remoteFieldInfo); 86 }; 87 } 88 89 #endif 90 #endif // DATA_TRANSFORMER_H