1 /*
2 * Copyright (c) 2023 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 "EvenMappingDao"
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 using namespace AppEventCacheCommon;
35 using namespace AppEventCacheCommon::AppEventMapping;
AppEventMappingDao(std::shared_ptr<NativeRdb::RdbStore> dbStore)36 AppEventMappingDao::AppEventMappingDao(std::shared_ptr<NativeRdb::RdbStore> dbStore) : dbStore_(dbStore)
37 {
38 if (Create() != DB_SUCC) {
39 HILOG_ERROR(LOG_CORE, "failed to create table=%{public}s", TABLE.c_str());
40 }
41 }
42
Create()43 int AppEventMappingDao::Create()
44 {
45 /**
46 * table: event_observer_mapping
47 *
48 * |-------|-----------|--------------|
49 * | seq | event_seq | observer_seq |
50 * |-------|-----------|--------------|
51 * | INT64 | INT64 | INT64 |
52 * |-------|-----------|--------------|
53 */
54 const std::vector<std::pair<std::string, std::string>> fields = {
55 {FIELD_EVENT_SEQ, SqlUtil::SQL_INT_TYPE},
56 {FIELD_OBSERVER_SEQ, SqlUtil::SQL_INT_TYPE},
57 };
58 std::string sql = SqlUtil::CreateTable(TABLE, fields);
59 if (dbStore_->ExecuteSql(sql) != NativeRdb::E_OK) {
60 return DB_FAILED;
61 }
62 return DB_SUCC;
63 }
64
Insert(int64_t eventSeq,int64_t observerSeq)65 int64_t AppEventMappingDao::Insert(int64_t eventSeq, int64_t observerSeq)
66 {
67 NativeRdb::ValuesBucket bucket;
68 bucket.PutLong(FIELD_EVENT_SEQ, eventSeq);
69 bucket.PutLong(FIELD_OBSERVER_SEQ, observerSeq);
70 int64_t seq = 0;
71 if (dbStore_->Insert(seq, TABLE, bucket) != NativeRdb::E_OK) {
72 return DB_FAILED;
73 }
74 return seq;
75 }
76
Delete(int64_t observerSeq,const std::vector<int64_t> & eventSeqs)77 int AppEventMappingDao::Delete(int64_t observerSeq, const std::vector<int64_t>& eventSeqs)
78 {
79 NativeRdb::AbsRdbPredicates predicates(TABLE);
80 if (observerSeq > 0) {
81 predicates.EqualTo(FIELD_OBSERVER_SEQ, observerSeq);
82 }
83 if (!eventSeqs.empty()) {
84 std::vector<std::string> eventSeqStrs(eventSeqs.size());
85 std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
86 return std::to_string(eventSeq);
87 });
88 predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
89 }
90
91 int deleteRows = 0;
92 if (dbStore_->Delete(deleteRows, predicates) != NativeRdb::E_OK) {
93 return DB_FAILED;
94 }
95 HILOG_INFO(LOG_CORE, "delete %{public}d records, observerSeq=%{public}" PRId64, deleteRows, observerSeq);
96 return deleteRows;
97 }
98
QueryExistEvent(const std::vector<int64_t> & eventSeqs,std::unordered_set<int64_t> & existEventSeqs)99 int AppEventMappingDao::QueryExistEvent(const std::vector<int64_t>& eventSeqs,
100 std::unordered_set<int64_t>& existEventSeqs)
101 {
102 if (eventSeqs.empty()) {
103 return DB_SUCC;
104 }
105 NativeRdb::AbsRdbPredicates predicates(TABLE);
106 std::vector<std::string> eventSeqStrs(eventSeqs.size());
107 std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
108 return std::to_string(eventSeq);
109 });
110 predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
111
112 auto resultSet = dbStore_->Query(predicates, {FIELD_EVENT_SEQ});
113 if (resultSet == nullptr) {
114 HILOG_ERROR(LOG_CORE, "failed to query table, event size=%{public}zu", eventSeqs.size());
115 return DB_FAILED;
116 }
117 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
118 int64_t existEventSeq = 0;
119 if (resultSet->GetLong(0, existEventSeq) != NativeRdb::E_OK) {
120 HILOG_ERROR(LOG_CORE, "failed to get event seq value from resultSet");
121 resultSet->Close();
122 return DB_FAILED;
123 }
124 existEventSeqs.insert(existEventSeq);
125 }
126 resultSet->Close();
127 return DB_SUCC;
128 }
129 } // namespace HiviewDFX
130 } // namespace OHOS
131