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 #ifndef RELATIONAL_STORE_INSTANCE_H 16 #define RELATIONAL_STORE_INSTANCE_H 17 #ifdef RELATIONAL_STORE 18 19 #include <string> 20 #include <mutex> 21 22 #include "irelational_store.h" 23 #include "relationaldb_properties.h" 24 25 namespace DistributedDB { 26 class RelationalStoreInstance final { 27 public: 28 RelationalStoreInstance(); 29 ~RelationalStoreInstance() = default; 30 31 static RelationalStoreConnection *GetDatabaseConnection(const RelationalDBProperties &properties, int &errCode, 32 bool isNeedIfOpened = true); 33 static RelationalStoreInstance *GetInstance(); 34 35 static int ReleaseDataBaseConnection(RelationalStoreConnection *connection); 36 37 // public for test mock 38 static IRelationalStore *GetDataBase(const RelationalDBProperties &properties, int &errCode, 39 bool isNeedIfOpened = true); 40 41 void Dump(int fd); 42 private: 43 44 IRelationalStore *OpenDatabase(const RelationalDBProperties &properties, int &errCode); 45 46 void RemoveKvDBFromCache(const RelationalDBProperties &properties); 47 void SaveRelationalDBToCache(IRelationalStore *store, const RelationalDBProperties &properties); 48 49 void EnterDBOpenCloseProcess(const std::string &identifier); 50 void ExitDBOpenCloseProcess(const std::string &identifier); 51 52 static RelationalStoreInstance *instance_; 53 static std::mutex instanceLock_; 54 55 std::string appId_; 56 std::string userId_; 57 58 std::mutex relationalDBOpenMutex_; 59 std::condition_variable relationalDBOpenCondition_; 60 std::set<std::string> relationalDBOpenSet_; 61 }; 62 } // namespace DistributedDB 63 64 #endif 65 #endif // RELATIONAL_STORE_INSTANCE_H