1 /* 2 * Copyright (c) 2023 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_STORE_MANAGER_H 17 #define NATIVE_RDB_STORE_MANAGER_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <utility> 24 25 #include "lru_bucket.h" 26 #include "rdb_open_callback.h" 27 #include "rdb_store_config.h" 28 29 namespace OHOS { 30 namespace NativeRdb { 31 class RdbStoreImpl; 32 class RdbStoreManager { 33 public: 34 static RdbStoreManager &GetInstance(); 35 RdbStoreManager(); 36 virtual ~RdbStoreManager(); 37 std::shared_ptr<RdbStore> GetRdbStore( 38 const RdbStoreConfig &config, int &errCode, int version, RdbOpenCallback &openCallback); 39 void Clear(); 40 bool Remove(const std::string &path, bool shouldClose); 41 bool Delete(const RdbStoreConfig &config, bool shouldClose); 42 int SetSecurityLabel(const RdbStoreConfig &config); 43 44 private: 45 using Param = DistributedRdb::RdbSyncerParam; 46 using Info = DistributedRdb::RdbDebugInfo; 47 using DebugInfos = std::map<std::string, RdbStoreManager::Info>; 48 using DfxInfo = DistributedRdb::RdbDfxInfo; 49 int ProcessOpenCallback(RdbStore &rdbStore, int version, RdbOpenCallback &openCallback); 50 bool IsConfigInvalidChanged(const std::string &path, RdbStoreConfig &config); 51 int32_t GetParamFromService(DistributedRdb::RdbSyncerParam ¶m); 52 static bool IsPermitted(const DistributedRdb::RdbSyncerParam ¶m); 53 static int32_t CheckConfig(const RdbStoreConfig &config); 54 static Param GetSyncParam(const RdbStoreConfig &config); 55 static int32_t Collector(const RdbStoreConfig &config, DebugInfos &debugInfos, DfxInfo &dfxInfo); 56 std::shared_ptr<RdbStoreImpl> GetStoreFromCache(const std::string &path); 57 std::pair<int32_t, std::shared_ptr<RdbStoreImpl>> OpenStore(const RdbStoreConfig &config, const std::string &path); 58 59 static constexpr uint32_t BUCKET_MAX_SIZE = 4; 60 static const bool regCollector_; 61 std::mutex mutex_; 62 std::map<std::string, std::weak_ptr<RdbStoreImpl>> storeCache_; 63 LRUBucket<std::string, Param> configCache_; 64 }; 65 } // namespace NativeRdb 66 } // namespace OHOS 67 #endif 68