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