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 ×tamp) const override; 73 74 int GetMetaData(const Key &key, Value &value) const override; 75 76 int GetMetaDataByPrefixKey(const Key &keyPrefix, std::map<Key, Value> &data) const override; 77 78 int PutMetaData(const Key &key, const Value &value, bool isInTransaction) override; 79 80 int DeleteMetaData(const std::vector<Key> &keys) override; 81 82 int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override; 83 84 int GetAllMetaKeys(std::vector<Key> &keys) const override; 85 86 const RelationalDBProperties &GetDbProperties() const override; 87 88 void SetLocalFieldInfo(const std::vector<FieldInfo> &localFieldInfo); 89 90 int GetAllSyncData(const std::string &tableName, std::vector<VirtualRowData> &data); 91 92 int GetVirtualSyncData(const std::string &tableName, const std::string &hashKey, VirtualRowData &data); 93 InterceptData(std::vector<SingleVerKvEntry * > & entries,const std::string & sourceID,const std::string & targetID,bool isPush)94 int InterceptData(std::vector<SingleVerKvEntry *> &entries, 95 const std::string &sourceID, const std::string &targetID, bool isPush) const override 96 { 97 return E_OK; 98 } 99 CheckAndInitQueryCondition(QueryObject & query)100 int CheckAndInitQueryCondition(QueryObject &query) const override 101 { 102 return E_OK; 103 } 104 105 int CreateDistributedDeviceTable(const std::string &device, const RelationalSyncStrategy &syncStrategy) override; 106 107 int RegisterSchemaChangedCallback(const std::function<void()> &onSchemaChanged) override; 108 109 void EraseSyncData(const std::string &tableName); 110 111 void SetTableInfo(const TableInfo &tableInfo); 112 113 int ExecuteQuery(const PreparedStmt &prepStmt, size_t packetSize, RelationalRowDataSet &data, 114 ContinueToken &token) const override; 115 116 int SaveRemoteDeviceSchema(const std::string &deviceId, const std::string &remoteSchema, uint8_t type) override; 117 118 int GetRemoteDeviceSchema(const std::string &deviceId, RelationalSchemaObject &schemaObj) const override; 119 120 int GetSchemaFromDB(RelationalSchemaObject &schema) override; 121 122 void SetPermitCreateDistributedTable(bool permitCreateDistributedTable); 123 124 int GetSecurityOption(SecurityOption &option) const override; 125 126 void ReleaseRemoteQueryContinueToken(ContinueToken &token) const override; 127 128 void SetDistributedSchema(const DistributedSchema &schema); 129 130 void SetGetSyncDataResult(int errCode); 131 private: 132 mutable std::map<std::vector<uint8_t>, std::vector<uint8_t>> metadata_; 133 std::map<std::string, std::map<std::string, VirtualRowData>, CaseInsensitiveComparator> syncData_; 134 mutable std::map<std::string, std::map<std::string, VirtualRowData>, CaseInsensitiveComparator> localData_; 135 std::string schema_; 136 RelationalSchemaObject schemaObj_; 137 std::vector<FieldInfo> localFieldInfo_; 138 KvDBProperties properties_; 139 RelationalDBProperties rdbProperties_; 140 SecurityOption secOption_; 141 bool permitCreateDistributedTable_ = true; 142 uint64_t dbCreateTime_; 143 mutable std::mutex remoteSchemaMutex_; 144 std::map<std::string, std::string> remoteSchema_; 145 int getSyncDataResult_ = E_OK; 146 }; 147 } 148 #endif 149 #endif // VIRTUAL_RELATIONAL_VER_SYNC_DB_INTERFACE_H