• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef SYS_EVENT_REPEAT_DB_H
17 #define SYS_EVENT_REPEAT_DB_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "rdb_store.h"
23 #include "singleton.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 struct SysEventHashRecord {
SysEventHashRecordSysEventHashRecord28     SysEventHashRecord(const std::string& domain, const std::string& name)
29         : domain(domain), name(name) {}
30     std::string domain;
31     std::string name;
32     std::string eventHash;
33     int64_t happentime = 0;
34 };
35 
36 class SysEventRepeatDb : public OHOS::DelayedRefSingleton<SysEventRepeatDb> {
37 public:
38     SysEventRepeatDb();
39     ~SysEventRepeatDb() = default;
40     bool Insert(const SysEventHashRecord &sysEventHashRecord);
41     int64_t QueryHappentime(SysEventHashRecord &sysEventHashRecord);
42     bool Update(const SysEventHashRecord &sysEventHashRecord);
43     void CheckAndClearDb(const int64_t happentime);
44     void Clear(const int64_t happentime);
45     void Release();
46 
47 private:
48     bool CheckDbStoreValid();
49     void InitDbStore();
50     void ClearHistory(const int64_t happentime);
51     void RefreshDbCount();
52     void CheckAndRepairDbFile(const int32_t errCode);
53 
54 private:
55     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
56     int64_t dbCount_ = 0;
57     std::mutex dbMutex_;
58 };
59 } // HiviewDFX
60 } // OHOS
61 #endif // SYS_EVENT_REPEAT_DB_H