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 STORAGE_ENGINE_MANAGER_H 17 #define STORAGE_ENGINE_MANAGER_H 18 19 #include <string> 20 #include <map> 21 #include <mutex> 22 #include <set> 23 #include <condition_variable> 24 25 #include "storage_engine.h" 26 27 namespace DistributedDB { 28 class StorageEngineManager final { 29 public: 30 static StorageEngine *GetStorageEngine(const KvDBProperties &property, int &errCode); 31 32 static int ReleaseStorageEngine(StorageEngine *storageEngine); 33 34 static int ForceReleaseStorageEngine(const std::string &identifier); 35 36 static int ExecuteMigration(StorageEngine *storageEngine); 37 38 DISABLE_COPY_ASSIGN_MOVE(StorageEngineManager); 39 40 private: 41 StorageEngineManager(); 42 ~StorageEngineManager(); 43 44 // Get a StorageEngineManager instance, Singleton mode 45 static StorageEngineManager *GetInstance(); 46 47 int RegisterLockStatusListener(); 48 49 void LockStatusNotifier(bool isAccessControlled); 50 51 StorageEngine *CreateStorageEngine(const KvDBProperties &property, int &errCode); 52 53 StorageEngine *FindStorageEngine(const std::string &identifier); 54 55 void InsertStorageEngine(const std::string &identifier, StorageEngine *&storageEngine); 56 57 void EraseStorageEngine(const std::string &identifier); 58 59 void ReleaseResources(const std::string &identifier); 60 61 int ReleaseEngine(StorageEngine *releaseEngine); 62 63 void EnterGetEngineProcess(const std::string &identifier); 64 65 void ExitGetEngineProcess(const std::string &identifier); 66 67 static std::mutex instanceLock_; 68 static std::atomic<StorageEngineManager *> instance_; 69 static volatile bool isRegLockStatusListener_; 70 71 static std::mutex storageEnginesLock_; 72 std::map<std::string, StorageEngine *> storageEngines_; 73 74 std::mutex getEngineMutex_; 75 std::condition_variable getEngineCondition_; 76 std::set<std::string> getEngineSet_; 77 78 NotificationChain::Listener *lockStatusListener_; 79 }; 80 } // namespace DistributedDB 81 82 #endif // STORAGE_ENGINE_MANAGER_H 83