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 };
61 
62 struct RowDataWithLog {
63     LogInfo logInfo;
64     RowData rowData;
65 };
66 
67 struct OptRowDataWithLog {
68     LogInfo logInfo;
69     OptRowData optionalData;
70 };
71 
72 struct TableDataWithLog {
73     std::string tableName;
74     std::vector<RowDataWithLog> dataList;
75 };
76 
77 struct OptTableDataWithLog {
78     std::string tableName;
79     std::vector<OptRowDataWithLog> dataList;
80 };
81 
82 // use for cloud sync
83 struct DataInfoWithLog {
84     LogInfo logInfo;
85     VBucket primaryKeys;
86 };
87 
88 class DataTransformer {
89 public:
90     static int TransformTableData(const TableDataWithLog &tableDataWithLog,
91         const std::vector<FieldInfo> &fieldInfoList, std::vector<DataItem> &dataItems);
92     static int TransformDataItem(const std::vector<DataItem> &dataItems, const std::vector<FieldInfo> &remoteFieldInfo,
93         OptTableDataWithLog &tableDataWithLog);
94 
95     static int SerializeDataItem(const RowDataWithLog &data, const std::vector<FieldInfo> &fieldInfo,
96         DataItem &dataItem);
97     static int DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data,
98         const std::vector<FieldInfo> &remoteFieldInfo);
99 
100     static uint32_t CalDataValueLength(const DataValue &dataValue);
101     static int DeserializeDataValue(DataValue &dataValue, Parcel &parcel);
102     static int SerializeDataValue(const DataValue &dataValue, Parcel &parcel);
103 
104 private:
105     static int SerializeValue(Value &value, const RowData &rowData, const std::vector<FieldInfo> &fieldInfoList);
106     static int DeSerializeValue(const Value &value, OptRowData &optionalData,
107         const std::vector<FieldInfo> &remoteFieldInfo);
108 };
109 }
110 
111 #endif
112 #endif // DATA_TRANSFORMER_H
113