1 /*
2 * Copyright (c) 2025 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 #include "insight_intent_rdb_storage_mgr.h"
17 #include "nlohmann/json.hpp"
18
19 namespace OHOS {
20 namespace AbilityRuntime {
InsightRdbStorageMgr()21 InsightRdbStorageMgr::InsightRdbStorageMgr()
22 {
23 }
24
~InsightRdbStorageMgr()25 InsightRdbStorageMgr::~InsightRdbStorageMgr()
26 {
27 TAG_LOGD(AAFwkTag::INTENT, "InsightRdbStorageMgr is deleted");
28 }
29
LoadInsightIntentInfos(const int32_t userId,std::vector<ExtractInsightIntentInfo> & totalInfos)30 int32_t InsightRdbStorageMgr::LoadInsightIntentInfos(const int32_t userId,
31 std::vector<ExtractInsightIntentInfo> &totalInfos)
32 {
33 TAG_LOGD(AAFwkTag::INTENT, "InsightRdbStorageMgr load all intent total infos");
34 std::unordered_map<std::string, std::string> value;
35 std::string key = std::to_string(userId).append("/");
36 bool result = DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->QueryDataBeginWithKey(key, value);
37 if (!result) {
38 TAG_LOGE(AAFwkTag::INTENT, "get entries error");
39 return ERR_INVALID_VALUE;
40 }
41 Transform(value, totalInfos);
42 return ERR_OK;
43 }
44
LoadInsightIntentInfoByName(const std::string & bundleName,const int32_t userId,std::vector<ExtractInsightIntentInfo> & totalInfos)45 int32_t InsightRdbStorageMgr::LoadInsightIntentInfoByName(const std::string &bundleName, const int32_t userId,
46 std::vector<ExtractInsightIntentInfo> &totalInfos)
47 {
48 TAG_LOGD(AAFwkTag::INTENT, "load intent total infos by bundleName, %{public}s", bundleName.c_str());
49 std::unordered_map<std::string, std::string> value;
50 std::string key = std::to_string(userId).append("/").append(bundleName).append("/");
51 bool result = DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->QueryDataBeginWithKey(key, value);
52 if (!result) {
53 TAG_LOGE(AAFwkTag::INTENT, "get entries error");
54 return ERR_INVALID_VALUE;
55 }
56 Transform(value, totalInfos);
57 return ERR_OK;
58 }
59
LoadInsightIntentInfo(const std::string & bundleName,const std::string & moduleName,const std::string & intentName,const int32_t userId,ExtractInsightIntentInfo & totalInfo)60 int32_t InsightRdbStorageMgr::LoadInsightIntentInfo(const std::string &bundleName, const std::string &moduleName,
61 const std::string &intentName, const int32_t userId, ExtractInsightIntentInfo &totalInfo)
62 {
63 TAG_LOGD(AAFwkTag::INTENT, "InsightRdbStorageMgr load intent total info");
64 std::string value;
65 std::string key = std::to_string(userId).append("/").append(bundleName).append("/")
66 .append(moduleName).append("/").append(intentName);
67 bool result = DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->QueryData(key, value);
68 if (!result) {
69 TAG_LOGE(AAFwkTag::INTENT, "get entries error");
70 return ERR_INVALID_VALUE;
71 }
72 ExtractInsightIntentProfileInfoVec profileInfos;
73 if (!ExtractInsightIntentProfile::TransformTo(value, profileInfos)) {
74 TAG_LOGE(AAFwkTag::INTENT, "error key: %{private}s", key.c_str());
75 DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->DeleteData(key);
76 }
77 for (const auto &profileInfo : profileInfos.insightIntents) {
78 if (!ExtractInsightIntentProfile::ProfileInfoFormat(profileInfo, totalInfo)) {
79 TAG_LOGE(AAFwkTag::INTENT, "ProfileInfoFormat error, key: %{private}s", key.c_str());
80 }
81 }
82 return ERR_OK;
83 }
84
Transform(std::unordered_map<std::string,std::string> value,std::vector<ExtractInsightIntentInfo> & totalInfos)85 void InsightRdbStorageMgr::Transform(std::unordered_map<std::string, std::string> value,
86 std::vector<ExtractInsightIntentInfo> &totalInfos)
87 {
88 for (const auto &item : value) {
89 ExtractInsightIntentProfileInfoVec profileInfos;
90 if (!ExtractInsightIntentProfile::TransformTo(item.second, profileInfos)) {
91 TAG_LOGE(AAFwkTag::INTENT, "error key: %{private}s", item.first.c_str());
92 DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->DeleteData(item.first);
93 }
94 for (const auto &profileInfo : profileInfos.insightIntents) {
95 ExtractInsightIntentInfo totalInfo;
96 if (!ExtractInsightIntentProfile::ProfileInfoFormat(profileInfo, totalInfo)) {
97 TAG_LOGE(AAFwkTag::INTENT, "ProfileInfoFormat error, key: %{private}s", item.first.c_str());
98 }
99 totalInfos.emplace_back(totalInfo);
100 }
101 }
102 }
103
SaveStorageInsightIntentData(const std::string & bundleName,const std::string & moduleName,const int32_t userId,ExtractInsightIntentProfileInfoVec & profileInfos)104 int32_t InsightRdbStorageMgr::SaveStorageInsightIntentData(const std::string &bundleName, const std::string &moduleName,
105 const int32_t userId, ExtractInsightIntentProfileInfoVec &profileInfos)
106 {
107 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
108 for (auto profileInfo : profileInfos.insightIntents) {
109 std::string key = std::to_string(userId).append("/").append(bundleName).append("/")
110 .append(moduleName).append("/").append(profileInfo.intentName);
111 nlohmann::json jsonObject;
112 if (!ExtractInsightIntentProfile::ToJson(profileInfo, jsonObject)) {
113 TAG_LOGE(AAFwkTag::INTENT, "Transform error, key: %{private}s", key.c_str());
114 return ERR_INVALID_VALUE;
115 }
116 bool result = DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->InsertData(key, jsonObject.dump());
117 if (!result) {
118 TAG_LOGE(AAFwkTag::INTENT, "InsertData error, key: %{private}s", key.c_str());
119 }
120 }
121 return ERR_OK;
122 }
123
DeleteStorageInsightIntentByUserId(const int32_t userId)124 int32_t InsightRdbStorageMgr::DeleteStorageInsightIntentByUserId(const int32_t userId)
125 {
126 bool result;
127 {
128 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
129 std::string key = std::to_string(userId).append("/");
130 result = DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->DeleteDataBeginWithKey(key);
131 }
132 if (!result) {
133 TAG_LOGE(AAFwkTag::INTENT, "delete key by Id error");
134 return ERR_INVALID_VALUE;
135 }
136 return ERR_OK;
137 }
138
DeleteStorageInsightIntentData(const std::string & bundleName,const std::string & moduleName,const int32_t userId)139 int32_t InsightRdbStorageMgr::DeleteStorageInsightIntentData(const std::string &bundleName,
140 const std::string &moduleName, const int32_t userId)
141 {
142 bool result;
143 {
144 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
145 std::string key = std::to_string(userId).append("/")
146 .append(bundleName).append("/").append(moduleName).append("/");
147 result = DelayedSingleton<InsightIntentRdbDataMgr>::GetInstance()->DeleteDataBeginWithKey(key);
148 }
149 if (!result) {
150 TAG_LOGE(AAFwkTag::INTENT, "delete key error");
151 return ERR_INVALID_VALUE;
152 }
153 return ERR_OK;
154 }
155 } // namespace AbilityRuntime
156 } // namespace OHOS
157