1 /* 2 * Copyright (c) 2024 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 16 #ifndef OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H 17 #define OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H 18 #include <cstdint> 19 #include <functional> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <utility> 24 25 #include "knowledge_types.h" 26 #include "rdb_common.h" 27 #include "rdb_store_config.h" 28 #include "rdb_types.h" 29 #include "statement.h" 30 31 namespace DistributedDB { 32 class StoreObserver; 33 } 34 namespace OHOS::NativeRdb { 35 class RdbStoreConfig; 36 class Statement; 37 class Connection { 38 public: 39 using Info = DistributedRdb::RdbDebugInfo; 40 using SConn = std::shared_ptr<Connection>; 41 using Stmt = std::shared_ptr<Statement>; 42 using Notifier = std::function<void(const DistributedRdb::RdbChangedData &rdbChangedData)>; 43 using Creator = std::pair<int32_t, SConn> (*)(const RdbStoreConfig &config, bool isWriter); 44 using Repairer = int32_t (*)(const RdbStoreConfig &config); 45 using Deleter = int32_t (*)(const RdbStoreConfig &config); 46 using Collector = std::map<std::string, Info> (*)(const RdbStoreConfig &config); 47 using ReplicaChecker = int32_t (*)(const RdbStoreConfig &config); 48 static std::pair<int32_t, SConn> Create(const RdbStoreConfig &config, bool isWriter); 49 static int32_t Repair(const RdbStoreConfig &config); 50 static int32_t Delete(const RdbStoreConfig &config); 51 static std::map<std::string, Info> Collect(const RdbStoreConfig &config); 52 static int32_t CheckReplicaIntegrity(const RdbStoreConfig &config); 53 static int32_t RegisterCreator(int32_t dbType, Creator creator); 54 static int32_t RegisterRepairer(int32_t dbType, Repairer repairer); 55 static int32_t RegisterDeleter(int32_t dbType, Deleter deleter); 56 static int32_t RegisterCollector(int32_t dbType, Collector collector); 57 static int32_t RegisterReplicaChecker(int32_t dbType, ReplicaChecker replicaChecker); 58 59 int32_t SetId(int32_t id); 60 int32_t GetId() const; 61 void SetIsRecyclable(bool recyclable); 62 bool IsRecyclable() const; 63 virtual ~Connection() = default; 64 virtual int32_t VerifyAndRegisterHook(const RdbStoreConfig &config) = 0; 65 virtual std::pair<int32_t, Stmt> CreateStatement(const std::string &sql, SConn conn) = 0; 66 virtual std::pair<int32_t, Stmt> CreateReplicaStatement(const std::string &sql, SConn conn) = 0; 67 virtual int CheckReplicaForRestore() = 0; 68 virtual int32_t Rekey(const RdbStoreConfig::CryptoParam &cryptoParam) = 0; 69 virtual int32_t GetDBType() const = 0; 70 virtual bool IsWriter() const = 0; 71 virtual int32_t ResetKey(const RdbStoreConfig &config) = 0; 72 virtual int32_t TryCheckPoint(bool timeout) = 0; 73 virtual int32_t LimitWalSize() = 0; 74 virtual int32_t ConfigLocale(const std::string &localeStr) = 0; 75 virtual int32_t CleanDirtyData(const std::string &table, uint64_t cursor) = 0; 76 virtual int32_t SubscribeTableChanges(const Notifier ¬ifier) = 0; 77 virtual int32_t GetMaxVariable() const = 0; 78 virtual int32_t GetJournalMode() = 0; 79 virtual int32_t ClearCache(bool isForceClear = false) = 0; 80 virtual int32_t Subscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) = 0; 81 virtual int32_t Unsubscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) = 0; 82 virtual int32_t Backup(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, bool isAsync, 83 std::shared_ptr<SlaveStatus> slaveStatus, bool verifyDb = true) = 0; 84 virtual int32_t Restore( 85 const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, 86 std::shared_ptr<SlaveStatus> slaveStatus) = 0; 87 virtual ExchangeStrategy GenerateExchangeStrategy(std::shared_ptr<SlaveStatus> status, bool isRelpay = true) = 0; 88 virtual int SetKnowledgeSchema(const DistributedRdb::RdbKnowledgeSchema &schema) = 0; 89 virtual int CleanDirtyLog(const std::string &table, uint64_t cursor) = 0; 90 virtual int RegisterAlgo(const std::string &clstAlgoName, ClusterAlgoFunc func) = 0; 91 92 private: 93 int32_t id_ = 0; 94 bool isRecyclable_ = true; 95 }; 96 } // namespace OHOS::NativeRdb 97 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H 98