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