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 VIRTUAL_RELATIONAL_VER_SYNC_DB_INTERFACE_H
16 #define VIRTUAL_RELATIONAL_VER_SYNC_DB_INTERFACE_H
17 #ifdef RELATIONAL_STORE
18 
19 #include "data_transformer.h"
20 #include "relational_db_sync_interface.h"
21 #include "sqlite_single_ver_continue_token.h"
22 #include "relational_schema_object.h"
23 
24 namespace DistributedDB {
25 struct ObjectData {
26 public:
27     void PutDataValue(const std::string &fieldName, const DataValue &value) const;
28     int GetDataValue(const std::string &fieldName, DataValue &value) const;
29 private:
30     mutable std::map<std::string, DataValue> fieldData;
31 };
32 
33 struct VirtualRowData {
34     LogInfo logInfo;
35     ObjectData objectData;
36 };
37 
38 class VirtualRelationalVerSyncDBInterface : public RelationalDBSyncInterface {
39 public:
40     VirtualRelationalVerSyncDBInterface();
41     ~VirtualRelationalVerSyncDBInterface() override = default;
42 
43     int PutSyncDataWithQuery(const QueryObject &query, const std::vector<SingleVerKvEntry *> &entries,
44         const std::string &deviceName) override;
45 
46     int PutLocalData(const std::vector<VirtualRowData> &dataList, const std::string &tableName);
47 
48     RelationalSchemaObject GetSchemaInfo() const override;
49     void SetSchemaInfo(const RelationalSchemaObject &schema);
50 
51     int GetDatabaseCreateTimestamp(Timestamp &outTime) const override;
52 
53     std::vector<QuerySyncObject> GetTablesQuery() override;
54 
55     int LocalDataChanged(int notifyEvent, std::vector<QuerySyncObject> &queryObj) override;
56 
57     int GetSyncData(QueryObject &query, const SyncTimeRange &timeRange,
58         const DataSizeSpecInfo &dataSizeInfo, ContinueToken &continueStmtToken,
59         std::vector<SingleVerKvEntry *> &entries) const override;
60 
61     int GetInterfaceType() const override;
62 
63     void IncRefCount() override;
64 
65     void DecRefCount() override;
66 
67     std::vector<uint8_t> GetIdentifier() const override;
68 
69     void GetMaxTimestamp(Timestamp &stamp) const override;
70 
71     // Get the max timestamp of one table.
72     int GetMaxTimestamp(const std::string &tableName, Timestamp &timestamp) const override;
73 
74     int GetMetaData(const Key &key, Value &value) const override;
75 
76     int PutMetaData(const Key &key, const Value &value, bool isInTransaction) override;
77 
78     int DeleteMetaData(const std::vector<Key> &keys) override;
79 
80     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override;
81 
82     int GetAllMetaKeys(std::vector<Key> &keys) const override;
83 
84     const RelationalDBProperties &GetDbProperties() const override;
85 
86     void SetLocalFieldInfo(const std::vector<FieldInfo> &localFieldInfo);
87 
88     int GetAllSyncData(const std::string &tableName, std::vector<VirtualRowData> &data);
89 
90     int GetVirtualSyncData(const std::string &tableName, const std::string &hashKey, VirtualRowData &data);
91 
InterceptData(std::vector<SingleVerKvEntry * > & entries,const std::string & sourceID,const std::string & targetID,bool isPush)92     int InterceptData(std::vector<SingleVerKvEntry *> &entries,
93         const std::string &sourceID, const std::string &targetID, bool isPush) const override
94     {
95         return E_OK;
96     }
97 
CheckAndInitQueryCondition(QueryObject & query)98     int CheckAndInitQueryCondition(QueryObject &query) const override
99     {
100         return E_OK;
101     }
102 
103     int CreateDistributedDeviceTable(const std::string &device, const RelationalSyncStrategy &syncStrategy) override;
104 
105     int RegisterSchemaChangedCallback(const std::function<void()> &onSchemaChanged) override;
106 
107     void EraseSyncData(const std::string &tableName);
108 
109     void SetTableInfo(const TableInfo &tableInfo);
110 
111     int ExecuteQuery(const PreparedStmt &prepStmt, size_t packetSize, RelationalRowDataSet &data,
112         ContinueToken &token) const override;
113 
114     int SaveRemoteDeviceSchema(const std::string &deviceId, const std::string &remoteSchema, uint8_t type) override;
115 
116     int GetRemoteDeviceSchema(const std::string &deviceId, RelationalSchemaObject &schemaObj) override;
117 
118     int GetSchemaFromDB(RelationalSchemaObject &schema) override;
119 
120     void SetPermitCreateDistributedTable(bool permitCreateDistributedTable);
121 
122     int GetSecurityOption(SecurityOption &option) const override;
123 
124     void ReleaseRemoteQueryContinueToken(ContinueToken &token) const override;
125 private:
126     mutable std::map<std::vector<uint8_t>, std::vector<uint8_t>> metadata_;
127     std::map<std::string, std::map<std::string, VirtualRowData>, CaseInsensitiveComparator> syncData_;
128     mutable std::map<std::string, std::map<std::string, VirtualRowData>, CaseInsensitiveComparator> localData_;
129     std::string schema_;
130     RelationalSchemaObject schemaObj_;
131     std::vector<FieldInfo> localFieldInfo_;
132     KvDBProperties properties_;
133     RelationalDBProperties rdbProperties_;
134     SecurityOption secOption_;
135     bool permitCreateDistributedTable_ = true;
136     uint64_t dbCreateTime_;
137 };
138 }
139 #endif
140 #endif // VIRTUAL_RELATIONAL_VER_SYNC_DB_INTERFACE_H