• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "sys_event_dao.h"
17 
18 #include "file_util.h"
19 #include "hiview_global.h"
20 #include "logger.h"
21 #include "store_mgr_proxy.h"
22 #include "sys_event_query_wrapper.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace EventStore {
27 namespace {
28 constexpr int ERR_INVALID_DB_FILE = -1;
29 constexpr int ERR_FAILED_DB_OPERATION = -2;
30 constexpr int ERR_INVALID_QUERY = -3;
31 }
32 DEFINE_LOG_TAG("HiView-SysEventDao");
BuildQuery(StoreType type)33 std::shared_ptr<SysEventQuery> SysEventDao::BuildQuery(StoreType type)
34 {
35     return std::make_shared<SysEventQueryWrapper>(GetDataFile(type));
36 }
37 
BuildQuery(uint16_t eventType)38 std::shared_ptr<SysEventQuery> SysEventDao::BuildQuery(uint16_t eventType)
39 {
40     return std::make_shared<SysEventQueryWrapper>(GetDataFile(static_cast<StoreType>(eventType)));
41 }
42 
BuildQuery(const std::string & dbFile)43 std::shared_ptr<SysEventQuery> SysEventDao::BuildQuery(const std::string& dbFile)
44 {
45     return std::make_shared<SysEventQueryWrapper>(dbFile);
46 }
47 
Insert(std::shared_ptr<SysEvent> sysEvent)48 int SysEventDao::Insert(std::shared_ptr<SysEvent> sysEvent)
49 {
50     std::string dbFile = GetDataFile(static_cast<StoreType>(sysEvent->what_));
51     if (dbFile.empty()) {
52         HIVIEW_LOGE("failed to get db file by eventType=%{public}d", sysEvent->what_);
53         return ERR_INVALID_DB_FILE;
54     }
55 
56     HIVIEW_LOGD("insert db file %{public}s with %{public}s", dbFile.c_str(), sysEvent->eventName_.c_str());
57     Entry entry;
58     entry.id = 0;
59     entry.value = sysEvent->jsonExtraInfo_;
60     auto docStore = StoreMgrProxy::GetInstance().GetDocStore(dbFile);
61     if (docStore->Put(entry) != 0) {
62         HIVIEW_LOGE("insert error for event %{public}s", sysEvent->eventName_.c_str());
63         return ERR_FAILED_DB_OPERATION;
64     }
65     sysEvent->SetSeq(entry.id);
66     return 0;
67 }
68 
Update(std::shared_ptr<SysEvent> sysEvent,bool isNotifyChange)69 int SysEventDao::Update(std::shared_ptr<SysEvent> sysEvent, bool isNotifyChange)
70 {
71     std::string dbFile = GetDataFile(static_cast<StoreType>(sysEvent->what_));
72     if (dbFile.empty()) {
73         HIVIEW_LOGE("failed to get db file by eventType=%{public}d", sysEvent->what_);
74         return ERR_INVALID_DB_FILE;
75     }
76 
77     HIVIEW_LOGD("update db file %{public}s", dbFile.c_str());
78     Entry entry;
79     entry.id = sysEvent->GetSeq();
80     entry.value = sysEvent->jsonExtraInfo_;
81     auto docStore = StoreMgrProxy::GetInstance().GetDocStore(dbFile);
82     if (docStore->Merge(entry) != 0) {
83         HIVIEW_LOGE("update error for event %{public}s", sysEvent->eventName_.c_str());
84         return ERR_FAILED_DB_OPERATION;
85     }
86 
87     if (isNotifyChange) {
88         HiviewGlobal::GetInstance()->PostUnorderedEvent(sysEvent);
89     }
90     return 0;
91 }
92 
Delete(std::shared_ptr<SysEventQuery> sysEventQuery,int limit)93 int SysEventDao::Delete(std::shared_ptr<SysEventQuery> sysEventQuery, int limit)
94 {
95     if (sysEventQuery == nullptr) {
96         return ERR_INVALID_QUERY;
97     }
98     std::vector<std::string> dbFiles;
99     if (sysEventQuery->GetDbFile().empty()) {
100         GetDataFiles(dbFiles);
101     } else {
102         dbFiles.push_back(sysEventQuery->GetDbFile());
103     }
104 
105     DataQuery dataQuery;
106     sysEventQuery->GetDataQuery(dataQuery);
107     dataQuery.Limit(limit);
108     int delNum = 0;
109     for (auto dbFile : dbFiles) {
110         HIVIEW_LOGD("delete event from db file %{public}s", dbFile.c_str());
111         auto docStore = StoreMgrProxy::GetInstance().GetDocStore(dbFile);
112         if (delNum = docStore->Delete(dataQuery); delNum < 0) {
113             HIVIEW_LOGE("delete event error from db file %{public}s", dbFile.c_str());
114             return ERR_FAILED_DB_OPERATION;
115         }
116     }
117     return delNum;
118 }
119 
GetNum(StoreType type)120 int SysEventDao::GetNum(StoreType type)
121 {
122     std::string dbFile = GetDataFile(type);
123     if (dbFile.empty()) {
124         HIVIEW_LOGE("failed to get db file by eventType=%{public}d", type);
125         return ERR_INVALID_DB_FILE;
126     }
127     int result = 0;
128     auto docStore = StoreMgrProxy::GetInstance().GetDocStore(dbFile);
129     if (result = docStore->GetNum(); result < 0) {
130         HIVIEW_LOGE("failed to get the number of events from db=%{public}s", dbFile.c_str());
131         return ERR_FAILED_DB_OPERATION;
132     }
133     return result;
134 }
135 
BackupDB(StoreType type)136 int SysEventDao::BackupDB(StoreType type)
137 {
138     std::string dbFile = GetDataFile(type);
139     if (dbFile.empty()) {
140         HIVIEW_LOGE("failed to get db file by storeType=%{public}d", type);
141         return ERR_INVALID_DB_FILE;
142     }
143     return StoreMgrProxy::GetInstance().BackupDocStore(dbFile, GetBakFile(type));
144 }
145 
DeleteDB(StoreType type)146 int SysEventDao::DeleteDB(StoreType type)
147 {
148     std::string dbFile = GetDataFile(type);
149     if (dbFile.empty()) {
150         HIVIEW_LOGE("failed to get db file by storeType=%{public}d", type);
151         return ERR_INVALID_DB_FILE;
152     }
153     return StoreMgrProxy::GetInstance().DeleteDocStore(dbFile);
154 }
155 
CloseDB(StoreType type)156 int SysEventDao::CloseDB(StoreType type)
157 {
158     std::string dbFile = GetDataFile(type);
159     if (dbFile.empty()) {
160         HIVIEW_LOGE("failed to get db file by storeType=%{public}d", type);
161         return ERR_INVALID_DB_FILE;
162     }
163     return StoreMgrProxy::GetInstance().CloseDocStore(dbFile);
164 }
165 
GetDataDir()166 std::string SysEventDao::GetDataDir()
167 {
168     std::string workPath = HiviewGlobal::GetInstance()->GetHiViewDirectory(
169         HiviewContext::DirectoryType::WORK_DIRECTORY);
170     if (workPath.back() != '/') {
171         workPath = workPath + "/";
172     }
173     std::string dbFilePath = workPath + "sys_event_db/";
174     if (!FileUtil::FileExists(dbFilePath)) {
175         if (FileUtil::ForceCreateDirectory(dbFilePath, FileUtil::FILE_PERM_770)) {
176             HIVIEW_LOGE("create sys_event_db path successful");
177         } else {
178             dbFilePath = workPath;
179             HIVIEW_LOGE("create sys_event_db path fail, use default");
180         }
181     }
182     return dbFilePath;
183 }
184 
GetDataFiles(std::vector<std::string> & dbFiles)185 void SysEventDao::GetDataFiles(std::vector<std::string>& dbFiles)
186 {
187     dbFiles.push_back(GetDataFile(StoreType::FAULT));
188     dbFiles.push_back(GetDataFile(StoreType::STATISTIC));
189     dbFiles.push_back(GetDataFile(StoreType::SECURITY));
190     dbFiles.push_back(GetDataFile(StoreType::BEHAVIOR));
191 }
192 
GetDataFile(StoreType type)193 std::string SysEventDao::GetDataFile(StoreType type)
194 {
195     switch (type) {
196         case StoreType::FAULT:
197             return GetDataDir() + "fault.db";
198         case StoreType::STATISTIC:
199             return GetDataDir() + "statistic.db";
200         case StoreType::SECURITY:
201             return GetDataDir() + "security.db";
202         case StoreType::BEHAVIOR:
203             return GetDataDir() + "behavior.db";
204         default:
205             break;
206     }
207     return "";
208 }
209 
GetBakFile(StoreType type)210 std::string SysEventDao::GetBakFile(StoreType type)
211 {
212     switch (type) {
213         case StoreType::FAULT:
214             return GetDataDir() + "back_fault.db";
215         case StoreType::STATISTIC:
216             return GetDataDir() + "back_statistic.db";
217         case StoreType::SECURITY:
218             return GetDataDir() + "back_security.db";
219         case StoreType::BEHAVIOR:
220             return GetDataDir() + "back_behavior.db";
221         default:
222             break;
223     }
224     return "";
225 }
226 } // EventStore
227 } // namespace HiviewDFX
228 } // namespace OHOS
229