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 "LifeCyclePolicy"
16
17 #include "lifecycle_policy.h"
18
19 #include "log_print.h"
20 #include "preprocess/preprocess_utils.h"
21
22 namespace OHOS {
23 namespace UDMF {
24 using namespace std::chrono;
25
OnGot(const UnifiedKey & key)26 Status LifeCyclePolicy::OnGot(const UnifiedKey &key)
27 {
28 auto store = StoreCache::GetInstance().GetStore(key.intention);
29 if (store == nullptr) {
30 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
31 return E_DB_ERROR;
32 }
33 if (store->Delete(key.key) != E_OK) {
34 ZLOGE("Remove data failed, intention: %{public}s.", key.intention.c_str());
35 return E_DB_ERROR;
36 }
37 return E_OK;
38 }
39
OnStart(const std::string & intention)40 Status LifeCyclePolicy::OnStart(const std::string &intention)
41 {
42 auto store = StoreCache::GetInstance().GetStore(intention);
43 if (store == nullptr) {
44 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
45 return E_DB_ERROR;
46 }
47 if (store->Clear() != E_OK) {
48 ZLOGE("Remove data failed, intention: %{public}s.", intention.c_str());
49 return E_DB_ERROR;
50 }
51 return E_OK;
52 }
53
OnTimeout(const std::string & intention)54 Status LifeCyclePolicy::OnTimeout(const std::string &intention)
55 {
56 auto store = StoreCache::GetInstance().GetStore(intention);
57 if (store == nullptr) {
58 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
59 return E_DB_ERROR;
60 }
61 std::vector<std::string> timeoutKeys;
62 auto status = GetTimeoutKeys(store, INTERVAL, timeoutKeys);
63 if (status != E_OK) {
64 ZLOGE("Get timeout keys failed.");
65 return E_DB_ERROR;
66 }
67 if (store->DeleteBatch(timeoutKeys) != E_OK) {
68 ZLOGE("Remove data failed, intention: %{public}s.", intention.c_str());
69 return E_DB_ERROR;
70 }
71 return E_OK;
72 }
73
GetTimeoutKeys(const std::shared_ptr<Store> & store,Duration interval,std::vector<std::string> & timeoutKeys)74 Status LifeCyclePolicy::GetTimeoutKeys(
75 const std::shared_ptr<Store> &store, Duration interval, std::vector<std::string> &timeoutKeys)
76 {
77 std::vector<UnifiedData> datas;
78 auto status = store->GetBatchData(DATA_PREFIX, datas);
79 if (status != E_OK) {
80 ZLOGE("Get data failed.");
81 return E_DB_ERROR;
82 }
83 if (datas.empty()) {
84 ZLOGD("entries is empty.");
85 return E_OK;
86 }
87 auto curTime = PreProcessUtils::GetTimestamp();
88 for (const auto &data : datas) {
89 if (curTime > data.GetRuntime()->createTime + duration_cast<milliseconds>(interval).count()
90 || curTime < data.GetRuntime()->createTime) {
91 timeoutKeys.push_back(data.GetRuntime()->key.key);
92 }
93 }
94 return E_OK;
95 }
96 } // namespace UDMF
97 } // namespace OHOS