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 "cloud/cloud_store_types.h" 22 #include "data_value.h" 23 #include "db_types.h" 24 #include "relational_schema_object.h" 25 26 namespace DistributedDB { 27 using RowData = std::vector<DataValue>; 28 using OptRowData = std::vector<DataValue>; 29 30 struct LogInfo { 31 int64_t dataKey = -1; 32 std::string device; 33 std::string originDev; 34 Timestamp timestamp = 0; 35 Timestamp wTimestamp = 0; 36 uint64_t flag = 0; 37 Key hashKey; // primary key hash value 38 std::string cloudGid; // use for sync with cloud 39 std::string sharingResource; // use for cloud share data 40 std::string version; // use for conflict check 41 uint32_t status = static_cast<uint32_t>(LockStatus::UNLOCK); // record lock status 42 bool isNeedUpdateAsset = false; 43 uint64_t cloud_flag = 0; // use for kv cloud log table 44 }; 45 46 enum class LogInfoFlag : uint32_t { 47 FLAG_CLOUD = 0x0, // same as device sync 48 FLAG_DELETE = 0x1, 49 FLAG_LOCAL = 0x2, 50 FLAG_FORCE_PUSH_IGNORE = 0x4, // use in RDB 51 FLAG_LOGIC_DELETE = 0x8, // use in RDB 52 FLAG_WAIT_COMPENSATED_SYNC = 0x10, 53 FLAG_DEVICE_CLOUD_INCONSISTENCY = 0x20, 54 FLAG_KV_FORCE_PUSH_IGNORE = 0x40, 55 FLAG_KV_LOGIC_DELETE = 0x80, 56 FLAG_CLOUD_WRITE = 0x100, 57 FLAG_SYSTEM_RECORD = 0x200, 58 FLAG_UPLOAD_FINISHED = 0x400, 59 FLAG_LOGIC_DELETE_FOR_LOGOUT = 0x800, 60 FLAG_ASSET_DOWNLOADING_FOR_ASYNC = 0x1000, 61 FLAG_LOGIN_USER = 0x2000, // same hash key, login user's data 62 }; 63 64 struct RowDataWithLog { 65 LogInfo logInfo; 66 RowData rowData; 67 }; 68 69 struct OptRowDataWithLog { 70 LogInfo logInfo; 71 OptRowData optionalData; 72 }; 73 74 struct TableDataWithLog { 75 std::string tableName; 76 std::vector<RowDataWithLog> dataList; 77 }; 78 79 struct OptTableDataWithLog { 80 std::string tableName; 81 std::vector<OptRowDataWithLog> dataList; 82 }; 83 84 // use for cloud sync 85 struct DataInfoWithLog { 86 LogInfo logInfo; 87 VBucket primaryKeys; 88 }; 89 90 struct UpdateRecordFlagStruct { 91 std::string tableName; 92 bool isRecordConflict; 93 bool isInconsistency; 94 }; 95 96 class DataTransformer { 97 public: 98 static int TransformTableData(const TableDataWithLog &tableDataWithLog, 99 const std::vector<FieldInfo> &fieldInfoList, std::vector<DataItem> &dataItems); 100 static int TransformDataItem(const std::vector<DataItem> &dataItems, const std::vector<FieldInfo> &remoteFieldInfo, 101 OptTableDataWithLog &tableDataWithLog); 102 103 static int SerializeDataItem(const RowDataWithLog &data, const std::vector<FieldInfo> &fieldInfo, 104 DataItem &dataItem); 105 static int DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data, 106 const std::vector<FieldInfo> &remoteFieldInfo); 107 108 static uint32_t CalDataValueLength(const DataValue &dataValue); 109 static int DeserializeDataValue(DataValue &dataValue, Parcel &parcel); 110 static int SerializeDataValue(const DataValue &dataValue, Parcel &parcel); 111 112 private: 113 static int SerializeValue(Value &value, const RowData &rowData, const std::vector<FieldInfo> &fieldInfoList); 114 static int DeSerializeValue(const Value &value, OptRowData &optionalData, 115 const std::vector<FieldInfo> &remoteFieldInfo); 116 }; 117 } 118 119 #endif 120 #endif // DATA_TRANSFORMER_H 121