1 /* 2 * Copyright (c) 2022 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_SQLITE_CONNECTION_H 17 #define NATIVE_RDB_SQLITE_CONNECTION_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <list> 22 #include <memory> 23 #include <mutex> 24 #include <vector> 25 26 #include "connection.h" 27 #include "rdb_store_config.h" 28 #include "sqlite3sym.h" 29 #include "sqlite_statement.h" 30 #include "value_object.h" 31 32 typedef struct ClientChangedData ClientChangedData; 33 namespace OHOS { 34 namespace NativeRdb { 35 /** 36 * @brief Use DataChangeCallback replace std::function<void(ClientChangedData &clientChangedData)>. 37 */ 38 using DataChangeCallback = std::function<void(ClientChangedData &clientChangedData)>; 39 40 class SqliteConnection : public Connection { 41 public: 42 static std::pair<int32_t, std::shared_ptr<Connection>> Create(const RdbStoreConfig &config, bool isWrite); 43 static int32_t Delete(const RdbStoreConfig &config); 44 static int32_t Delete(const std::string &path); 45 static int32_t Repair(const RdbStoreConfig &config); 46 static int32_t Restore(const RdbStoreConfig &config, const std::string &srcPath, const std::string &destPath); 47 static std::map<std::string, Info> Collect(const RdbStoreConfig &config); 48 SqliteConnection(const RdbStoreConfig &config, bool isWriteConnection); 49 ~SqliteConnection(); 50 int32_t VerifyAndRegisterHook(const RdbStoreConfig &config) override; 51 int TryCheckPoint(bool timeout) override; 52 int LimitWalSize() override; 53 int ConfigLocale(const std::string &localeStr) override; 54 int CleanDirtyData(const std::string &table, uint64_t cursor) override; 55 int ReSetKey(const RdbStoreConfig &config) override; 56 int32_t GetJournalMode() override; 57 std::pair<int32_t, Stmt> CreateStatement(const std::string &sql, SConn conn) override; 58 bool IsWriter() const override; 59 int SubscribeTableChanges(const Notifier ¬ifier) override; 60 int GetMaxVariable() const override; 61 int32_t GetDBType() const override; 62 int32_t ClearCache() override; 63 int32_t Subscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) override; 64 int32_t Unsubscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) override; 65 int32_t Backup(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, bool isAsync, 66 SlaveStatus &slaveStatus) override; 67 int32_t Restore(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, 68 SlaveStatus &slaveStatus) override; 69 ExchangeStrategy GenerateExchangeStrategy(const SlaveStatus &status) override; 70 71 protected: 72 std::pair<int32_t, ValueObject> ExecuteForValue( 73 const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()); 74 int ExecuteSql(const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()); 75 76 private: 77 struct Suffix { 78 const char *suffix_ = nullptr; 79 const char *debug_ = nullptr; 80 }; 81 82 enum SlaveOpenPolicy : int32_t { 83 FORCE_OPEN = 0, 84 OPEN_IF_DB_VALID // DB exists and there are no -slaveFailure and -syncInterrupt files 85 }; 86 87 int InnerOpen(const RdbStoreConfig &config); 88 int Configure(const RdbStoreConfig &config, std::string &dbPath); 89 int SetPageSize(const RdbStoreConfig &config); 90 int SetEncrypt(const RdbStoreConfig &config); 91 int SetEncryptKey(const std::vector<uint8_t> &key, const RdbStoreConfig &config); 92 int SetServiceKey(const RdbStoreConfig &config, int32_t errCode); 93 int SetEncryptAgo(const RdbStoreConfig &config); 94 int SetJournalMode(const RdbStoreConfig &config); 95 int SetAutoCheckpoint(const RdbStoreConfig &config); 96 int SetWalFile(const RdbStoreConfig &config); 97 int SetWalSyncMode(const std::string &syncMode); 98 int SetTokenizer(const RdbStoreConfig &config); 99 void LimitPermission(const RdbStoreConfig &config, const std::string &dbPath) const; 100 101 int SetPersistWal(const RdbStoreConfig &config); 102 int SetBusyTimeout(int timeout); 103 104 int RegDefaultFunctions(sqlite3 *dbHandle); 105 int SetCustomFunctions(const RdbStoreConfig &config); 106 int SetCustomScalarFunction(const std::string &functionName, int argc, ScalarFunction *function); 107 int32_t UnsubscribeLocalDetail( 108 const std::string &event, const std::shared_ptr<DistributedRdb::RdbStoreObserver> &observer); 109 int32_t UnsubscribeLocalDetailAll(const std::string &event); 110 int32_t OpenDatabase(const std::string &dbPath, int openFileFlags); 111 int LoadExtension(const RdbStoreConfig &config, sqlite3 *dbHandle); 112 RdbStoreConfig GetSlaveRdbStoreConfig(const RdbStoreConfig &rdbConfig); 113 std::pair<int32_t, std::shared_ptr<SqliteConnection>> CreateSlaveConnection( 114 const RdbStoreConfig &config, SlaveOpenPolicy slaveOpenPolicy); 115 int ExchangeSlaverToMaster(bool isRestore, bool verifyDb, SlaveStatus &status); 116 int ExchangeVerify(bool isRestore); 117 int SqliteNativeBackup(bool isRestore, SlaveStatus &curStatus); 118 int VeritySlaveIntegrity(); 119 bool IsDbVersionBelowSlave(); 120 int RegisterStoreObs(); 121 int RegisterClientObs(); 122 int RegisterHookIfNecessary(); 123 static std::pair<int32_t, std::shared_ptr<SqliteConnection>> InnerCreate( 124 const RdbStoreConfig &config, bool isWrite); 125 static int CopyDb(const RdbStoreConfig &config, const std::string &srcPath, const std::string &destPath); 126 static constexpr SqliteConnection::Suffix FILE_SUFFIXES[] = { { "", "DB" }, { "-shm", "SHM" }, { "-wal", "WAL" }, 127 { "-dwr", "DWR" }, { "-journal", "JOURNAL" }, { "-slaveFailure", nullptr }, { "-syncInterrupt", nullptr }, 128 { ".corruptedflg", nullptr }, { "-compare", nullptr } }; 129 static constexpr int CHECKPOINT_TIME = 500; 130 static constexpr int DEFAULT_BUSY_TIMEOUT_MS = 2000; 131 static constexpr int BACKUP_PAGES_PRE_STEP = 12800; // 1024 * 4 * 12800 == 50m 132 static constexpr int BACKUP_PRE_WAIT_TIME = 10; 133 static constexpr ssize_t SLAVE_WAL_SIZE_LIMIT = 2147483647; // 2147483647 = 2g - 1 134 static constexpr ssize_t SLAVE_INTEGRITY_CHECK_LIMIT = 524288000; // 524288000 == 1024 * 1024 * 500 135 static constexpr uint32_t NO_ITER = 0; 136 static constexpr uint32_t DB_INDEX = 0; 137 static constexpr uint32_t WAL_INDEX = 2; 138 static constexpr uint32_t ITER_V1 = 5000; 139 static const int32_t regCreator_; 140 static const int32_t regRepairer_; 141 static const int32_t regDeleter_; 142 static const int32_t regCollector_; 143 static const int32_t regRestorer_; 144 using EventHandle = int (SqliteConnection::*)(); 145 struct HandleInfo { 146 RegisterType Type; 147 EventHandle handle; 148 }; 149 static constexpr HandleInfo onEventHandlers_[RegisterType::OBSERVER_END] = { 150 { RegisterType::STORE_OBSERVER, &SqliteConnection::RegisterStoreObs }, 151 { RegisterType::CLIENT_OBSERVER, &SqliteConnection::RegisterClientObs }, 152 }; 153 154 std::atomic<uint64_t> backupId_; 155 sqlite3 *dbHandle_; 156 bool isWriter_; 157 bool isReadOnly_; 158 bool isConfigured_ = false; 159 JournalMode mode_ = JournalMode::MODE_WAL; 160 int maxVariableNumber_; 161 std::shared_ptr<SqliteConnection> slaveConnection_; 162 std::map<std::string, ScalarFunctionInfo> customScalarFunctions_; 163 const RdbStoreConfig config_; 164 }; 165 } // namespace NativeRdb 166 } // namespace OHOS 167 #endif