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