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 static RelationalStoreInstance *GetInstance(); 33 34 static int ReleaseDataBaseConnection(RelationalStoreConnection *connection); 35 36 int CheckDatabaseFileStatus(const std::string &id); 37 38 // public for test mock 39 static IRelationalStore *GetDataBase(const RelationalDBProperties &properties, int &errCode); 40 private: 41 42 IRelationalStore *OpenDatabase(const RelationalDBProperties &properties, int &errCode); 43 44 void RemoveKvDBFromCache(const RelationalDBProperties &properties); 45 void SaveRelationalDBToCache(IRelationalStore *store, const RelationalDBProperties &properties); 46 47 void EnterDBOpenCloseProcess(const std::string &identifier); 48 void ExitDBOpenCloseProcess(const std::string &identifier); 49 50 static RelationalStoreInstance *instance_; 51 static std::mutex instanceLock_; 52 53 std::string appId_; 54 std::string userId_; 55 56 std::mutex relationalDBOpenMutex_; 57 std::condition_variable relationalDBOpenCondition_; 58 std::set<std::string> relationalDBOpenSet_; 59 }; 60 } // namespace DistributedDB 61 62 #endif 63 #endif // RELATIONAL_STORE_INSTANCE_H