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 <string>
20 
21 #include "rdb_store.h"
22 #include "singleton.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 struct SysEventHashRecord {
SysEventHashRecordSysEventHashRecord27     SysEventHashRecord(const std::string& domain, const std::string& name)
28         : domain(domain), name(name) {}
29     std::string domain;
30     std::string name;
31     std::string eventHash;
32     int64_t happentime = 0;
33 };
34 
35 class SysEventRepeatDb : public OHOS::DelayedRefSingleton<SysEventRepeatDb> {
36 public:
37     SysEventRepeatDb();
38     ~SysEventRepeatDb() = default;
39     bool Insert(const SysEventHashRecord &sysEventHashRecord);
40     int64_t QueryHappentime(SysEventHashRecord &sysEventHashRecord);
41     bool Update(const SysEventHashRecord &sysEventHashRecord);
42     void CheckAndClearDb(const int64_t happentime);
43     void Clear(const int64_t happentime);
44 
45 private:
46     void InitDbStore();
47     void CreateTable();
48     void ClearHistory(const int64_t happentime);
49     void RefreshDbCount();
50 
51 private:
52     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
53     int64_t dbCount_ = 0;
54 };
55 } // HiviewDFX
56 } // OHOS
57 #endif // SYS_EVENT_REPEAT_DB_H