• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_db_clean.h"
17 
18 #include "file_util.h"
19 #include "logger.h"
20 #include "sys_event_dao.h"
21 #include "time_util.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("HiView-SysEventDbClean");
26 #ifdef CUSTOM_MAX_FILE_SIZE_MB
27 const int MAX_FILE_SIZE = 1024 * 1024 * CUSTOM_MAX_FILE_SIZE_MB;
28 #else
29 const int MAX_FILE_SIZE = 1024 * 1024 * 100; // 100M
30 #endif // CUSTOM_MAX_FILE_SIZE
31 using EventStore::SysEventDao;
32 using EventStore::SysEventQuery;
33 using EventStore::EventCol;
34 using EventStore::Op;
35 using EventStore::ResultSet;
36 
CleanDbStore()37 bool SysEventDbClean::CleanDbStore()
38 {
39     std::string dbFile = SysEventDao::GetDataFile();
40     if (FileUtil::GetFileSize(dbFile) < MAX_FILE_SIZE) {
41         HIVIEW_LOGD("does not need to clean db file");
42         return true;
43     }
44     int count = 0;
45     int64_t zeroClock = TimeUtil::Get0ClockStampMs();
46     while (true) {
47         count++;
48         zeroClock = zeroClock - TimeUtil::SECONDS_PER_DAY * TimeUtil::SEC_TO_MILLISEC;
49         SysEventQuery sysEventQuery = SysEventDao::BuildQuery();
50         ResultSet result = sysEventQuery.Where(EventCol::TS, Op::LT, zeroClock).Execute(1);
51         if (!result.HasNext()) {
52             break;
53         }
54         if (count > 7) { // 7 days of week
55             break;
56         }
57     }
58     zeroClock = zeroClock + TimeUtil::SECONDS_PER_DAY * TimeUtil::SEC_TO_MILLISEC;
59     SysEventQuery sysEventQuery = SysEventDao::BuildQuery();
60     sysEventQuery.Where(EventCol::TS, Op::LT, zeroClock);
61     int retCode = SysEventDao::Delete(sysEventQuery);
62     if (retCode != 0) {
63         HIVIEW_LOGI("clean the db file error at timestamp %{public}" PRId64 "", zeroClock);
64         return false;
65     }
66     HIVIEW_LOGI("clean the db file at timestamp %{public}" PRId64 " successful", zeroClock);
67     return true;
68 }
69 } // namespace HiviewDFX
70 } // namespace OHOS