• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void Init();
41     bool Destroy();
42     bool Remove(const std::string &path, bool shouldClose);
43     bool Delete(const RdbStoreConfig &config, bool shouldClose);
44 
45 private:
46     using Param = DistributedRdb::RdbSyncerParam;
47     using Info = DistributedRdb::RdbDebugInfo;
48     using DebugInfos = std::map<std::string, RdbStoreManager::Info>;
49     using DfxInfo = DistributedRdb::RdbDfxInfo;
50     bool IsConfigInvalidChanged(const std::string &path, RdbStoreConfig &config);
51     int32_t GetParamFromService(DistributedRdb::RdbSyncerParam &param);
52     bool IsPermitted(const DistributedRdb::RdbSyncerParam &param, const std::string &path);
53     int32_t CheckConfig(const RdbStoreConfig &config, const std::string &path);
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         const RdbStoreConfig &config, int &errCode);
58 
59     static constexpr uint32_t BUCKET_MAX_SIZE = 4;
60     static constexpr uint32_t PROMISEINFO_CACHE_SIZE = 32;
61     static const bool regCollector_;
62     std::mutex mutex_;
63     std::map<std::string, std::weak_ptr<RdbStoreImpl>> storeCache_;
64     LRUBucket<std::string, Param> configCache_;
65     LRUBucket<std::string, bool> promiseInfoCache_;
66 };
67 } // namespace NativeRdb
68 } // namespace OHOS
69 #endif
70