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 FLAG_CLOUD_UPDATE_LOCAL = 0x4000, // 1 indicates an update on the cloud side, and 0 indicates data inserted on the 63 // cloud side or data operated locally 64 FLAG_KNOWLEDGE_INVERTED_WRITE = 0x8000, // knowledge process written to inverted table 65 FLAG_KNOWLEDGE_VECTOR_WRITE = 0x10000, // knowledge process written to vector table 66 }; 67 68 struct RowDataWithLog { 69 LogInfo logInfo; 70 RowData rowData; 71 }; 72 73 struct OptRowDataWithLog { 74 LogInfo logInfo; 75 OptRowData optionalData; 76 }; 77 78 struct TableDataWithLog { 79 std::string tableName; 80 std::vector<RowDataWithLog> dataList; 81 }; 82 83 struct OptTableDataWithLog { 84 std::string tableName; 85 std::vector<OptRowDataWithLog> dataList; 86 }; 87 88 // use for cloud sync 89 struct DataInfoWithLog { 90 LogInfo logInfo; 91 VBucket primaryKeys; 92 }; 93 94 struct UpdateRecordFlagStruct { 95 std::string tableName; 96 bool isRecordConflict; 97 bool isInconsistency; 98 }; 99 100 struct DeviceSyncSaveDataInfo { 101 bool isDefeated = false; // data is defeated by local 102 bool isExist = false; 103 int64_t rowId = -1; 104 LogInfo localLogInfo; 105 }; 106 107 class DataTransformer { 108 public: 109 static int TransformTableData(const TableDataWithLog &tableDataWithLog, 110 const std::vector<FieldInfo> &fieldInfoList, std::vector<DataItem> &dataItems); 111 static int TransformDataItem(const std::vector<DataItem> &dataItems, const std::vector<FieldInfo> &remoteFieldInfo, 112 OptTableDataWithLog &tableDataWithLog); 113 114 static int SerializeDataItem(const RowDataWithLog &data, const std::vector<FieldInfo> &fieldInfo, 115 DataItem &dataItem); 116 static int DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data, 117 const std::vector<FieldInfo> &remoteFieldInfo); 118 119 static uint32_t CalDataValueLength(const DataValue &dataValue); 120 static int DeserializeDataValue(DataValue &dataValue, Parcel &parcel); 121 static int SerializeDataValue(const DataValue &dataValue, Parcel &parcel); 122 123 private: 124 static int SerializeValue(Value &value, const RowData &rowData, const std::vector<FieldInfo> &fieldInfoList); 125 static int DeSerializeValue(const Value &value, OptRowData &optionalData, 126 const std::vector<FieldInfo> &remoteFieldInfo); 127 }; 128 } 129 130 #endif 131 #endif // DATA_TRANSFORMER_H 132