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 NATIVE_RDB_RD_CONNECTION_H 17 #define NATIVE_RDB_RD_CONNECTION_H 18 19 #include <memory> 20 #include <mutex> 21 #include <vector> 22 23 #include "connection.h" 24 #include "rd_utils.h" 25 #include "rdb_common.h" 26 #include "rdb_store_config.h" 27 #include "value_object.h" 28 29 typedef struct ClientChangedData ClientChangedData; 30 namespace OHOS { 31 namespace NativeRdb { 32 33 class RdConnection : public Connection { 34 public: 35 static std::pair<int32_t, std::shared_ptr<Connection>> Create(const RdbStoreConfig &config, bool isWrite); 36 static int32_t Repair(const RdbStoreConfig &config); 37 static int32_t Delete(const RdbStoreConfig &config); 38 RdConnection(const RdbStoreConfig &config, bool isWriteConnection); 39 ~RdConnection(); 40 int32_t VerifyAndRegisterHook(const RdbStoreConfig &config) override; 41 std::pair<int32_t, Stmt> CreateStatement(const std::string &sql, SConn conn) override; 42 std::pair<int32_t, Stmt> CreateReplicaStatement(const std::string &sql, SConn conn) override; 43 int CheckReplicaForRestore() override; 44 int32_t GetDBType() const override; 45 bool IsWriter() const override; 46 int32_t ResetKey(const RdbStoreConfig &config) override; 47 int32_t Rekey(const RdbStoreConfig::CryptoParam &cryptoParam) override; 48 int32_t TryCheckPoint(bool timeout) override; 49 int32_t LimitWalSize() override; 50 int32_t ConfigLocale(const std::string &localeStr) override; 51 int32_t CleanDirtyData(const std::string &table, uint64_t cursor) override; 52 int32_t SubscribeTableChanges(const Notifier ¬ifier) override; 53 int32_t GetMaxVariable() const override; 54 int32_t GetJournalMode() override; 55 int32_t ClearCache(bool isForceClear = false) override; 56 int32_t Subscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) override; 57 int32_t Unsubscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) override; 58 int32_t Backup(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, bool isAsync, 59 std::shared_ptr<SlaveStatus> slaveStatus, bool verifyDb = true) override; 60 int32_t Restore(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, 61 std::shared_ptr<SlaveStatus> slaveStatus) override; 62 ExchangeStrategy GenerateExchangeStrategy(std::shared_ptr<SlaveStatus> status, bool isRelpay) override; 63 int SetKnowledgeSchema(const DistributedRdb::RdbKnowledgeSchema &schema) override; 64 int CleanDirtyLog(const std::string &table, uint64_t cursor) override; 65 int RegisterAlgo(const std::string &clstAlgoName, ClusterAlgoFunc func) override; 66 67 private: 68 static constexpr int MAX_VARIABLE_NUM = 32766; 69 static constexpr const char *GRD_OPEN_CONFIG_STR = 70 "\"pageSize\":8, \"crcCheckEnable\":0, \"redoFlushByTrx\":1, \"bufferPoolSize\":10240," 71 "\"sharedModeEnable\":1, \"metaInfoBak\":1, \"maxConnNum\":500, \"defaultIsolationLevel\":2"; 72 static constexpr uint32_t NO_ITER = 0; 73 static constexpr uint32_t ITER_V1 = 5000; 74 static constexpr uint32_t ITERS[] = { NO_ITER, ITER_V1 }; 75 static constexpr uint32_t ITERS_COUNT = sizeof(ITERS) / sizeof(ITERS[0]); 76 static const int32_t regCreator_; 77 static const int32_t regRepairer_; 78 static const int32_t regDeleter_; 79 80 static std::string GetConfigStr(const std::vector<uint8_t> &keys, bool isEncrypt); 81 std::string GetConfigStr(const std::vector<uint8_t> &keys); 82 int InnerOpen(const RdbStoreConfig &config); 83 bool isWriter_ = false; 84 GRD_DB *dbHandle_ = nullptr; 85 const RdbStoreConfig config_; 86 }; 87 88 } // namespace NativeRdb 89 } // namespace OHOS 90 #endif // NATIVE_RDB_RD_CONNECTION_H 91