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