• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H
18 #include <chrono>
19 #include <kv_store_delegate_manager.h>
20 #include <memory>
21 #include <shared_mutex>
22 
23 #include "concurrent_map.h"
24 #include "executor_pool.h"
25 #include "ikvstore_observer.h"
26 #include "kv_store_nb_delegate.h"
27 #include "metadata/store_meta_data.h"
28 #include "refbase.h"
29 
30 namespace OHOS::DistributedKv {
31 class StoreCache {
32 public:
33     template<class T>
34     struct Less {
35     public:
operatorLess36         bool operator()(const sptr<T> &x, const sptr<T> &y) const
37         {
38             return x.GetRefPtr() < y.GetRefPtr();
39         }
40     };
41     using DBStatus = DistributedDB::DBStatus;
42     using DBStore = DistributedDB::KvStoreNbDelegate;
43     using Store = std::shared_ptr<DBStore>;
44     using DBManager = DistributedDB::KvStoreDelegateManager;
45     using DBObserver = DistributedDB::KvStoreObserver;
46     using DBChangeData = DistributedDB::KvStoreChangedData;
47     using DBEntry = DistributedDB::Entry;
48     using Observers = std::set<sptr<IKvStoreObserver>, Less<IKvStoreObserver>>;
49     using StoreMetaData = OHOS::DistributedData::StoreMetaData;
50     using Time = std::chrono::steady_clock::time_point;
51     using DBOption = DistributedDB::KvStoreNbDelegate::Option;
52     using DBSecurity = DistributedDB::SecurityOption;
53     using DBPassword = DistributedDB::CipherPassword;
54 
55     struct DBStoreDelegate : public DBObserver {
56         DBStoreDelegate(DBStore *delegate, std::shared_ptr<Observers> observers);
57         ~DBStoreDelegate();
58         operator std::shared_ptr<DBStore> ();
59         bool operator<(const Time &time) const;
60         bool Close(DBManager &manager);
61         void OnChange(const DBChangeData &data) override;
62         void SetObservers(std::shared_ptr<Observers> observers);
63 
64     private:
65         std::vector<Entry> Convert(const std::list<DBEntry> &dbEntries);
66         mutable Time time_;
67         DBStore *delegate_ = nullptr;
68         std::shared_ptr<Observers> observers_ = nullptr;
69         std::shared_mutex mutex_;
70     };
71 
72     Store GetStore(const StoreMetaData &data, std::shared_ptr<Observers> observers, DBStatus &status);
73     void CloseStore(uint32_t tokenId, const std::string &storeId);
74     void CloseExcept(const std::set<int32_t> &users);
75     void SetObserver(uint32_t tokenId, const std::string &storeId, std::shared_ptr<Observers> observers);
76     static DBOption GetDBOption(const StoreMetaData &data, const DBPassword &password);
77     static DBSecurity GetDBSecurity(int32_t secLevel);
78     static DBPassword GetDBPassword(const StoreMetaData &data);
79     void SetThreadPool(std::shared_ptr<ExecutorPool> executors);
80 private:
81     void GarbageCollect();
82     static constexpr int64_t INTERVAL = 1;
83     ConcurrentMap<uint32_t, std::map<std::string, DBStoreDelegate>> stores_;
84     std::shared_ptr<ExecutorPool> executors_;
85 };
86 } // namespace OHOS::DistributedKv
87 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H
88