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 #include "export_db_manager.h"
17 
18 #include <algorithm>
19 #include <limits>
20 
21 #include "hiview_logger.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("HiView-ExportDbManager");
ExportDbManager(const std::string & dbStoreDir)26 ExportDbManager::ExportDbManager(const std::string& dbStoreDir)
27 {
28     HIVIEW_LOGD("db store directory is %{public}s.", dbStoreDir.c_str());
29     storage_ = std::make_shared<ExportDbStorage>(dbStoreDir);
30 }
31 
GetExportEnabledSeq(const std::string & moduleName)32 int64_t ExportDbManager::GetExportEnabledSeq(const std::string& moduleName)
33 {
34     ExportDetailRecord record = GetExportDetailRecord(moduleName);
35     if (record.moduleName.empty()) {
36         HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
37         return INVALID_SEQ_VAL;
38     }
39     HIVIEW_LOGD("export enabled sequence is %{public}" PRId64 "", record.exportEnabledSeq);
40     return record.exportEnabledSeq;
41 }
42 
GetExportBeginSeq(const std::string & moduleName)43 int64_t ExportDbManager::GetExportBeginSeq(const std::string& moduleName)
44 {
45     HIVIEW_LOGD("get beginning sequence of event for module %{public}s to export", moduleName.c_str());
46     ExportDetailRecord record = GetExportDetailRecord(moduleName);
47     if (record.exportEnabledSeq == INVALID_SEQ_VAL) {
48         HIVIEW_LOGI("export switch of %{public}s is off, no need to export event", moduleName.c_str());
49         return INVALID_SEQ_VAL;
50     }
51     return std::max(record.exportEnabledSeq, record.exportedMaxSeq);
52 }
53 
HandleExportSwitchChanged(const std::string & moduleName,int64_t curSeq)54 void ExportDbManager::HandleExportSwitchChanged(const std::string& moduleName, int64_t curSeq)
55 {
56     HIVIEW_LOGI("export switch for %{public}s module is changed, current event sequence is %{public}" PRId64 "",
57         moduleName.c_str(), curSeq);
58     if (IsUnrecordedModule(moduleName)) {
59         HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
60         ExportDetailRecord record = {
61             .moduleName = moduleName,
62             .exportEnabledSeq = curSeq,
63             .exportedMaxSeq = INVALID_SEQ_VAL,
64         };
65         storage_->InsertExportDetailRecord(record);
66         return;
67     }
68     ExportDetailRecord record {
69         .moduleName = moduleName,
70         .exportEnabledSeq = curSeq,
71     };
72     storage_->UpdateExportEnabledSeq(record);
73 }
74 
HandleExportTaskFinished(const std::string & moduleName,int64_t eventSeq)75 void ExportDbManager::HandleExportTaskFinished(const std::string& moduleName, int64_t eventSeq)
76 {
77     HIVIEW_LOGI("export task of %{public}s module is finished, maximum event sequence is %{public}" PRId64 "",
78         moduleName.c_str(), eventSeq);
79     if (IsUnrecordedModule(moduleName)) {
80         HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
81         ExportDetailRecord record = {
82             .moduleName = moduleName,
83             .exportEnabledSeq = INVALID_SEQ_VAL,
84             .exportedMaxSeq = eventSeq,
85         };
86         storage_->InsertExportDetailRecord(record);
87         return;
88     }
89     ExportDetailRecord record {
90         .moduleName = moduleName,
91         .exportedMaxSeq = eventSeq,
92     };
93     storage_->UpdateExportedMaxSeq(record);
94 }
95 
GetExportDetailRecord(const std::string & moduleName)96 ExportDetailRecord ExportDbManager::GetExportDetailRecord(const std::string& moduleName)
97 {
98     ExportDetailRecord record;
99     storage_->QueryExportDetailRecord(moduleName, record);
100     return record;
101 }
102 
IsUnrecordedModule(const std::string & moduleName)103 bool ExportDbManager::IsUnrecordedModule(const std::string& moduleName)
104 {
105     ExportDetailRecord record = GetExportDetailRecord(moduleName);
106     return record.moduleName.empty();
107 }
108 } // HiviewDFX
109 } // OHOS