1 /*
2 * Copyright (c) 2023-2025 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 #include "app_event_mapping_dao.h"
16
17 #include <algorithm>
18 #include <cinttypes>
19
20 #include "app_event_cache_common.h"
21 #include "app_event_store.h"
22 #include "hilog/log.h"
23 #include "rdb_helper.h"
24 #include "sql_util.h"
25
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN 0xD002D07
28
29 #undef LOG_TAG
30 #define LOG_TAG "EventMappingDao"
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 namespace AppEventMappingDao {
35 using namespace AppEventCacheCommon;
36 using namespace AppEventCacheCommon::AppEventMapping;
Create(NativeRdb::RdbStore & dbStore)37 int Create(NativeRdb::RdbStore& dbStore)
38 {
39 /**
40 * table: event_observer_mapping
41 *
42 * |-------|-----------|--------------|
43 * | seq | event_seq | observer_seq |
44 * |-------|-----------|--------------|
45 * | INT64 | INT64 | INT64 |
46 * |-------|-----------|--------------|
47 */
48 const std::vector<std::pair<std::string, std::string>> fields = {
49 {FIELD_EVENT_SEQ, SqlUtil::SQL_INT_TYPE},
50 {FIELD_OBSERVER_SEQ, SqlUtil::SQL_INT_TYPE},
51 };
52 std::string sql = SqlUtil::CreateTable(TABLE, fields);
53 return dbStore.ExecuteSql(sql);
54 }
55
Insert(std::shared_ptr<NativeRdb::RdbStore> dbStore,const std::vector<EventObserverInfo> & eventObservers)56 int Insert(std::shared_ptr<NativeRdb::RdbStore> dbStore, const std::vector<EventObserverInfo>& eventObservers)
57 {
58 std::vector<NativeRdb::ValuesBucket> buckets;
59 for (const auto& eventObserver : eventObservers) {
60 NativeRdb::ValuesBucket bucket;
61 bucket.PutLong(FIELD_EVENT_SEQ, eventObserver.eventSeq);
62 bucket.PutLong(FIELD_OBSERVER_SEQ, eventObserver.observerSeq);
63 buckets.emplace_back(bucket);
64 }
65 int64_t insertRows = 0;
66 return dbStore->BatchInsert(insertRows, TABLE, buckets);
67 }
68
Delete(std::shared_ptr<NativeRdb::RdbStore> dbStore,int64_t observerSeq,const std::vector<int64_t> & eventSeqs)69 int Delete(std::shared_ptr<NativeRdb::RdbStore> dbStore, int64_t observerSeq, const std::vector<int64_t>& eventSeqs)
70 {
71 NativeRdb::AbsRdbPredicates predicates(TABLE);
72 if (observerSeq > 0) {
73 predicates.EqualTo(FIELD_OBSERVER_SEQ, observerSeq);
74 }
75 if (!eventSeqs.empty()) {
76 std::vector<std::string> eventSeqStrs(eventSeqs.size());
77 std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
78 return std::to_string(eventSeq);
79 });
80 predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
81 }
82
83 int deleteRows = 0;
84 int ret = dbStore->Delete(deleteRows, predicates);
85 HILOG_INFO(LOG_CORE, "delete %{public}d records, observerSeq=%{public}" PRId64 ", ret=%{public}d",
86 deleteRows, observerSeq, ret);
87 return ret;
88 }
89
QueryExistEvent(std::shared_ptr<NativeRdb::RdbStore> dbStore,const std::vector<int64_t> & eventSeqs,std::unordered_set<int64_t> & existEventSeqs)90 int QueryExistEvent(std::shared_ptr<NativeRdb::RdbStore> dbStore, const std::vector<int64_t>& eventSeqs,
91 std::unordered_set<int64_t>& existEventSeqs)
92 {
93 if (eventSeqs.empty()) {
94 return NativeRdb::E_OK;
95 }
96 NativeRdb::AbsRdbPredicates predicates(TABLE);
97 std::vector<std::string> eventSeqStrs(eventSeqs.size());
98 std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
99 return std::to_string(eventSeq);
100 });
101 predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
102
103 auto resultSet = dbStore->Query(predicates, {FIELD_EVENT_SEQ});
104 if (resultSet == nullptr) {
105 HILOG_ERROR(LOG_CORE, "failed to query table, event size=%{public}zu", eventSeqs.size());
106 return NativeRdb::E_ERROR;
107 }
108 int ret = resultSet->GoToNextRow();
109 while (ret == NativeRdb::E_OK) {
110 int64_t existEventSeq = 0;
111 if (resultSet->GetLong(0, existEventSeq) != NativeRdb::E_OK) {
112 HILOG_ERROR(LOG_CORE, "failed to get event seq value from resultSet");
113 resultSet->Close();
114 return NativeRdb::E_ERROR;
115 }
116 existEventSeqs.insert(existEventSeq);
117 ret = resultSet->GoToNextRow();
118 }
119 resultSet->Close();
120 return ret == NativeRdb::E_SQLITE_CORRUPT ? ret : NativeRdb::E_OK;
121 }
122 } // namespace AppEventMappingDao
123 } // namespace HiviewDFX
124 } // namespace OHOS
125