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 OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H
18 #include <list>
19 #include <memory>
20 #include <shared_mutex>
21 #include <set>
22 #include "concurrent_map.h"
23 #include "error/general_error.h"
24 #include "executor_pool.h"
25 #include "metadata/store_meta_data.h"
26 #include "store/general_store.h"
27 #include "store/general_value.h"
28 #include "store/general_watcher.h"
29 #include "visibility.h"
30 namespace OHOS::DistributedData {
31 class SchemaMeta;
32 class AutoCache {
33 public:
34     using Error = GeneralError;
35     using Store = std::shared_ptr<GeneralStore>;
36     using Stores = std::list<std::shared_ptr<GeneralStore>>;
37     using Watcher = GeneralWatcher;
38     using Watchers = std::set<std::shared_ptr<GeneralWatcher>>;
39     using Time = std::chrono::steady_clock::time_point;
40     using Executor = ExecutorPool;
41     using TaskId = ExecutorPool::TaskId;
42     using Creator = std::function<GeneralStore *(const StoreMetaData &)>;
43     API_EXPORT static AutoCache &GetInstance();
44 
45     API_EXPORT int32_t RegCreator(int32_t type, Creator creator);
46 
47     API_EXPORT void Bind(std::shared_ptr<Executor> executor);
48 
49     API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers);
50 
51     API_EXPORT Stores GetStoresIfPresent(uint32_t tokenId, const std::string &storeName = "");
52 
53     API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId = "");
54 
55     API_EXPORT void CloseExcept(const std::set<int32_t> &users);
56 
57     API_EXPORT void SetObserver(uint32_t tokenId, const std::string &storeId, const Watchers &watchers);
58 
59     API_EXPORT void Enable(uint32_t tokenId, const std::string &storeId = "");
60 
61     API_EXPORT void Disable(uint32_t tokenId, const std::string &storeId);
62 
63 private:
64     AutoCache();
65     ~AutoCache();
66     void GarbageCollect(bool isForce);
67     void StartTimer();
68     struct Delegate : public GeneralWatcher {
69         Delegate(GeneralStore *delegate, const Watchers &watchers, int32_t user, const StoreMetaData &meta);
70         Delegate(const Delegate& delegate);
71         ~Delegate();
72         operator Store();
73         bool operator<(const Time &time) const;
74         bool Close();
75         int32_t GetUser() const;
76         int32_t GetArea() const;
77         const std::string& GetDataDir() const;
78         void SetObservers(const Watchers &watchers);
79         int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override;
80         int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) override;
81         void PostDataChange(const StoreMetaData &meta, const std::vector<std::string> &tables);
82 
83     private:
84         mutable Time time_;
85         GeneralStore *store_ = nullptr;
86         Watchers watchers_;
87         int32_t user_;
88         const StoreMetaData meta_;
89         std::shared_mutex mutex_;
90     };
91 
92     static constexpr int64_t INTERVAL = 1;
93     static constexpr int32_t MAX_CREATOR_NUM = 30;
94 
95     std::shared_ptr<Executor> executor_;
96     TaskId taskId_ = Executor::INVALID_TASK_ID;
97     ConcurrentMap<uint32_t, std::map<std::string, Delegate>> stores_;
98     ConcurrentMap<uint32_t, std::set<std::string>> disables_;
99     Creator creators_[MAX_CREATOR_NUM];
100     std::set<std::string> disableStores_;
101 };
102 } // namespace OHOS::DistributedData
103 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H