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 HIVIEW_BASE_EVENT_EXPORT_EXPORT_DB_STORAGE_H
17 #define HIVIEW_BASE_EVENT_EXPORT_EXPORT_DB_STORAGE_H
18 
19 #include <string>
20 
21 #include "rdb_helper.h"
22 #include "rdb_store.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 constexpr int64_t INVALID_SEQ_VAL = -1;
27 struct ExportDetailRecord {
28     // name of the module which export sysevents
29     std::string moduleName;
30 
31     // update when status of export switch changed and value will be
32     // set to be INVALID_SEQ_VAL when export switch is closed
33     int64_t exportEnabledSeq = INVALID_SEQ_VAL;
34 
35     // update when export task is finished
36     int64_t exportedMaxSeq = INVALID_SEQ_VAL;
37 };
38 
39 class ExportDbOpenCallback : public NativeRdb::RdbOpenCallback {
40 public:
41     int OnCreate(NativeRdb::RdbStore& rdbStore) override;
42     int OnUpgrade(NativeRdb::RdbStore& rdbStore, int oldVersion, int newVersion) override;
43 }; // class ExportDbOpenCallback
44 
45 class ExportDbStorage {
46 public:
47     ExportDbStorage(const std::string& dbStoreDir);
48     ~ExportDbStorage() = default;
49 
50 public:
51     void InsertExportDetailRecord(ExportDetailRecord& record);
52     void UpdateExportEnabledSeq(ExportDetailRecord& record);
53     void UpdateExportedMaxSeq(ExportDetailRecord& record);
54     void QueryExportDetailRecord(const std::string& moduleName, ExportDetailRecord& record);
55 
56 private:
57     void InitDbStore(const std::string& dbStoreDir);
58     void UpdateExportDetailRecordSeq(ExportDetailRecord& record, const std::string& seqName, int64_t seqValue);
59 
60 private:
61     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
62 };
63 } // namespace HiviewDFX
64 } // namespace OHOS
65 
66 #endif // HIVIEW_BASE_EVENT_EXPORT_EXPORT_DB_STORAGE_H