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 int GetBatchMetaData(const std::vector<Key> &keys, std::vector<Entry> &entries) const override; 54 55 int PutBatchMetaData(std::vector<Entry> &entries) override; 56 57 std::vector<QuerySyncObject> GetTablesQuery() override; 58 59 int LocalDataChanged(int notifyEvent, std::vector<QuerySyncObject> &queryObj) override; 60 61 int GetSyncData(QueryObject &query, const SyncTimeRange &timeRange, 62 const DataSizeSpecInfo &dataSizeInfo, ContinueToken &continueStmtToken, 63 std::vector<SingleVerKvEntry *> &entries) const override; 64 65 int GetInterfaceType() const override; 66 67 void IncRefCount() override; 68 69 void DecRefCount() override; 70 71 std::vector<uint8_t> GetIdentifier() const override; 72 73 void GetMaxTimestamp(Timestamp &stamp) const override; 74 75 // Get the max timestamp of one table. 76 int GetMaxTimestamp(const std::string &tableName, Timestamp ×tamp) const override; 77 78 int GetMetaData(const Key &key, Value &value) const override; 79 80 int PutMetaData(const Key &key, const Value &value) override; 81 82 int DeleteMetaData(const std::vector<Key> &keys) override; 83 84 int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override; 85 86 int GetAllMetaKeys(std::vector<Key> &keys) const override; 87 88 const RelationalDBProperties &GetDbProperties() const override; 89 90 void SetLocalFieldInfo(const std::vector<FieldInfo> &localFieldInfo); 91 92 int GetAllSyncData(const std::string &tableName, std::vector<VirtualRowData> &data); 93 94 int GetVirtualSyncData(const std::string &tableName, const std::string &hashKey, VirtualRowData &data); 95 InterceptData(std::vector<SingleVerKvEntry * > & entries,const std::string & sourceID,const std::string & targetID)96 int InterceptData(std::vector<SingleVerKvEntry *> &entries, 97 const std::string &sourceID, const std::string &targetID) const override 98 { 99 return E_OK; 100 } 101 CheckAndInitQueryCondition(QueryObject & query)102 int CheckAndInitQueryCondition(QueryObject &query) const override 103 { 104 return E_OK; 105 } 106 107 int CreateDistributedDeviceTable(const std::string &device, const RelationalSyncStrategy &syncStrategy) override; 108 109 int RegisterSchemaChangedCallback(const std::function<void()> &onSchemaChanged) override; 110 111 void EraseSyncData(const std::string &tableName); 112 113 void SetTableInfo(const TableInfo &tableInfo); 114 115 int ExecuteQuery(const PreparedStmt &prepStmt, size_t packetSize, RelationalRowDataSet &data, 116 ContinueToken &token) const override; 117 118 const RelationalDBProperties &GetRelationalDbProperties() const 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>> syncData_; 128 mutable std::map<std::string, std::map<std::string, VirtualRowData>> 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