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 16 #ifndef GENERIC_KVDB_H 17 #define GENERIC_KVDB_H 18 19 #include <cstdint> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 #include "store_types.h" 25 #include "version.h" 26 #include "ikvdb.h" 27 #include "generic_kvdb_connection.h" 28 #include "performance_analysis.h" 29 #include "kvdb_conflict_entry.h" 30 #include "db_types.h" 31 32 namespace DistributedDB { 33 class KvDBCommitNotifyFilterAbleData; 34 35 struct ImportFileInfo { 36 std::string backupDir; // the directory of the current database backup 37 std::string unpackedDir; // the directory of the unpacked import file 38 std::string currentDir; // the directory of the current database 39 std::string curValidFile; // the file imply that the current directory is valid 40 std::string backValidFile; // the file imply that the backup directory is valid 41 }; 42 43 enum class RegisterFuncType { 44 OBSERVER_SINGLE_VERSION_NS_PUT_EVENT = 0, 45 OBSERVER_SINGLE_VERSION_NS_SYNC_EVENT, 46 OBSERVER_SINGLE_VERSION_NS_LOCAL_EVENT, 47 OBSERVER_SINGLE_VERSION_NS_CONFLICT_EVENT, 48 OBSERVER_MULTI_VERSION_NS_COMMIT_EVENT, 49 CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ONLY, 50 CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ORIG, 51 CONFLICT_SINGLE_VERSION_NS_NATIVE_ALL, 52 REGISTER_FUNC_TYPE_MAX 53 }; 54 55 class GenericKvDB : public IKvDB { 56 public: 57 GenericKvDB(); 58 ~GenericKvDB() override; 59 60 DISABLE_COPY_ASSIGN_MOVE(GenericKvDB); 61 62 // Get properties of this database. 63 const KvDBProperties &GetMyProperties() const override; 64 65 // Create a db connection. 66 IKvDBConnection *GetDBConnection(int &errCode) final; 67 68 // Called when all connections of this database closed. 69 void OnClose(const std::function<void(void)> ¬ifier) final; 70 71 // Publish event when a commit action happened. 72 virtual void CommitNotify(int notifyEvent, KvDBCommitNotifyFilterAbleData *data); 73 74 // Invoked automatically when connection count is zero 75 virtual void Close() = 0; 76 77 virtual int TryToDisableConnection(OperatePerm perm); 78 79 virtual void ReEnableConnection(OperatePerm perm); 80 81 virtual int Rekey(const CipherPassword &passwd) = 0; 82 83 // Empty passwords represent non-encrypted files. 84 // Export existing database files to a specified database file in the specified directory. 85 virtual int Export(const std::string &filePath, const CipherPassword &passwd) = 0; 86 87 // Import the existing database files to the specified database file in the specified directory. 88 virtual int Import(const std::string &filePath, const CipherPassword &passwd, 89 bool isNeedIntegrityCheck = false) = 0; 90 91 // Release a db connection. 92 void ReleaseDBConnection(GenericKvDBConnection *connection); 93 94 // Register an event listener. 95 NotificationChain::Listener *RegisterEventListener(EventType type, 96 const NotificationChain::Listener::OnEvent &onEvent, 97 const NotificationChain::Listener::OnFinalize &onFinalize, int &errCode); 98 99 // Get event notify counter. 100 uint64_t GetEventNotifyCounter() const; 101 WakeUpSyncer()102 void WakeUpSyncer() override {}; 103 EnableAutonomicUpgrade()104 void EnableAutonomicUpgrade() override {}; 105 106 void SetCorruptHandler(const DatabaseCorruptHandler &handler) override; 107 108 int RegisterFunction(RegisterFuncType type); 109 110 int UnregisterFunction(RegisterFuncType type); 111 112 uint32_t GetRegisterFunctionCount(RegisterFuncType type) const; 113 114 virtual int TransObserverTypeToRegisterFunctionType(int observerType, RegisterFuncType &type) const; 115 116 virtual int TransConflictTypeToRegisterFunctionType(int conflictType, RegisterFuncType &type) const; 117 118 virtual uint32_t GetMaxValueSize() const; 119 120 virtual int CheckDataStatus(const Key &key, const Value &value, bool isDeleted) const; 121 122 virtual bool CheckWritePermission() const; 123 124 virtual bool IsDataMigrating() const; 125 126 virtual void SetConnectionFlag(bool isExisted) const; 127 128 int CheckIntegrity() const override; 129 130 std::string GetStorePath() const override; 131 132 void Dump(int fd) override; 133 134 virtual void ResetSyncStatus(); 135 136 void MarkRebuild() override; 137 138 virtual int PreClose(); 139 protected: 140 // Create a connection object, no DB ref increased. 141 virtual GenericKvDBConnection *NewConnection(int &errCode) = 0; 142 143 // Delete a connection object. 144 virtual void DelConnection(GenericKvDBConnection *connection); 145 146 // Called when a new connection created. 147 void IncreaseConnectionCounter(); 148 149 // Called when a connection released. 150 void DecreaseConnectionCounter(); 151 152 // Register a new notification event type. 153 int RegisterNotificationEventType(int eventType); 154 155 // Unregister a notification event type. 156 void UnRegisterNotificationEventType(int eventType); 157 158 // Access 'properties_' for derived class. 159 const KvDBProperties &MyProp() const; 160 KvDBProperties &MyProp(); 161 162 #ifndef OMIT_MULTI_VER 163 static int GetWorkDir(const KvDBProperties &kvDBProp, std::string &workDir); 164 #endif 165 166 void CorruptNotify() const; 167 168 std::string GetStoreIdOnlyIdentifier(const KvDBProperties &properties) const; 169 170 void GetStoreDirectory(const KvDBProperties &properties, int dbType, 171 std::string &storeDir, std::string &storeOnlyDir) const; 172 173 PerformanceAnalysis *performance_; 174 DatabaseCorruptHandler corruptHandler_; 175 DeviceID devId_; 176 177 private: 178 // Do commit notify in task pool. 179 void CommitNotifyAsync(int notifyEvent, KvDBCommitNotifyFilterAbleData *data); 180 181 void CorruptNotifyAsync() const; 182 183 // Get the ID of this kvdb. 184 std::string GetStoreId() const; 185 186 DECLARE_OBJECT_TAG(GenericKvDB); 187 188 // Databasse event notify counter. 189 std::atomic<uint64_t> eventNotifyCounter_; 190 191 // Fields for tracking the connection count and invoking callbacks. 192 std::atomic<int> connectionCount_; 193 std::vector<std::function<void(void)>> closeNotifiers_; 194 NotificationChain *notificationChain_; 195 KvDBProperties properties_; 196 std::mutex connectMutex_; 197 mutable std::mutex corruptMutex_; 198 OperatePerm operatePerm_; 199 mutable std::mutex regFuncCountMutex_; 200 std::vector<uint32_t> registerFunctionCount_; 201 std::atomic<bool> isRebuild_; 202 }; 203 } // namespace DistributedDB 204 205 #endif // GENERIC_KVDB_H 206