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 #define LOG_TAG "StoreCache"
16 #include "store_cache.h"
17
18 #include "log_print.h"
19 #include "runtime_store.h"
20 #include "account/account_delegate.h"
21
22 namespace OHOS {
23 namespace UDMF {
GetInstance()24 StoreCache &StoreCache::GetInstance()
25 {
26 static StoreCache instance;
27 return instance;
28 }
29
GetStore(std::string intention)30 std::shared_ptr<Store> StoreCache::GetStore(std::string intention)
31 {
32 std::shared_ptr<Store> store;
33 int foregroundUserId = 0;
34 bool ret = DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId);
35 if (!ret) {
36 ZLOGE("QueryForegroundUserId failed.");
37 return nullptr;
38 }
39 std::string key = intention;
40 key.append(std::to_string(foregroundUserId));
41 stores_.Compute(key, [&store, intention](const auto &key, std::shared_ptr<Store> &storePtr) -> bool {
42 if (storePtr != nullptr) {
43 store = storePtr;
44 return true;
45 }
46
47 if (IsValidIntention(intention)) {
48 storePtr = std::make_shared<RuntimeStore>(intention);
49 if (!storePtr->Init()) {
50 ZLOGE("Init runtime store failed.");
51 return false;
52 }
53 store = storePtr;
54 return true;
55 }
56 return false;
57 });
58 std::unique_lock<std::mutex> lock(taskMutex_);
59 if (taskId_ == ExecutorPool::INVALID_TASK_ID && executorPool_ != nullptr) {
60 taskId_ = executorPool_->Schedule(std::chrono::minutes(INTERVAL), [this]() { this->GarbageCollect(); });
61 }
62 return store;
63 }
64
GarbageCollect()65 void StoreCache::GarbageCollect()
66 {
67 auto current = std::chrono::steady_clock::now();
68 stores_.EraseIf([¤t](auto &key, std::shared_ptr<Store> &storePtr) {
69 if (*storePtr < current) {
70 ZLOGI("GarbageCollect, stores:%{public}s time limit, will be close.", key.c_str());
71 return true;
72 }
73 return false;
74 });
75 std::unique_lock<std::mutex> lock(taskMutex_);
76 if (!stores_.Empty() && executorPool_ != nullptr) {
77 ZLOGD("GarbageCollect, stores size:%{public}zu", stores_.Size());
78 taskId_ = executorPool_->Schedule(std::chrono::minutes(INTERVAL), [this]() { this->GarbageCollect(); });
79 } else {
80 taskId_ = ExecutorPool::INVALID_TASK_ID;
81 }
82 }
83
SetThreadPool(std::shared_ptr<ExecutorPool> executors)84 void StoreCache::SetThreadPool(std::shared_ptr<ExecutorPool> executors)
85 {
86 executorPool_ = executors;
87 }
88
CloseStores()89 void StoreCache::CloseStores()
90 {
91 ZLOGI("CloseStores, stores size:%{public}zu", stores_.Size());
92 stores_.Clear();
93 }
94
RemoveStore(const std::string & intention)95 void StoreCache::RemoveStore(const std::string &intention)
96 {
97 ZLOGI("RemoveStore, intention:%{public}s", intention.c_str());
98 int foregroundUserId = 0;
99 if (!DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId)) {
100 ZLOGE("QueryForegroundUserId failed.");
101 return;
102 }
103 std::string key = intention;
104 key.append(std::to_string(foregroundUserId));
105 stores_.Erase(key);
106 }
107
IsValidIntention(const std::string & intention)108 bool StoreCache::IsValidIntention(const std::string &intention)
109 {
110 return UnifiedDataUtils::GetIntentionByString(intention) != UD_INTENTION_BUTT;
111 }
112 } // namespace UDMF
113 } // namespace OHOS
114