• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "EvenMappingDao"
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,int64_t eventSeq,int64_t observerSeq)56 int Insert(std::shared_ptr<NativeRdb::RdbStore> dbStore, int64_t eventSeq, int64_t observerSeq)
57 {
58     NativeRdb::ValuesBucket bucket;
59     bucket.PutLong(FIELD_EVENT_SEQ, eventSeq);
60     bucket.PutLong(FIELD_OBSERVER_SEQ, observerSeq);
61     int64_t seq = 0;
62     return dbStore->Insert(seq, TABLE, bucket);
63 }
64 
Delete(std::shared_ptr<NativeRdb::RdbStore> dbStore,int64_t observerSeq,const std::vector<int64_t> & eventSeqs)65 int Delete(std::shared_ptr<NativeRdb::RdbStore> dbStore, int64_t observerSeq, const std::vector<int64_t>& eventSeqs)
66 {
67     NativeRdb::AbsRdbPredicates predicates(TABLE);
68     if (observerSeq > 0) {
69         predicates.EqualTo(FIELD_OBSERVER_SEQ, observerSeq);
70     }
71     if (!eventSeqs.empty()) {
72         std::vector<std::string> eventSeqStrs(eventSeqs.size());
73         std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
74             return std::to_string(eventSeq);
75         });
76         predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
77     }
78 
79     int deleteRows = 0;
80     int ret = dbStore->Delete(deleteRows, predicates);
81     HILOG_INFO(LOG_CORE, "delete %{public}d records, observerSeq=%{public}" PRId64 ", ret=%{public}d",
82         deleteRows, observerSeq, ret);
83     return ret;
84 }
85 
QueryExistEvent(std::shared_ptr<NativeRdb::RdbStore> dbStore,const std::vector<int64_t> & eventSeqs,std::unordered_set<int64_t> & existEventSeqs)86 int QueryExistEvent(std::shared_ptr<NativeRdb::RdbStore> dbStore, const std::vector<int64_t>& eventSeqs,
87     std::unordered_set<int64_t>& existEventSeqs)
88 {
89     if (eventSeqs.empty()) {
90         return NativeRdb::E_OK;
91     }
92     NativeRdb::AbsRdbPredicates predicates(TABLE);
93     std::vector<std::string> eventSeqStrs(eventSeqs.size());
94     std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
95         return std::to_string(eventSeq);
96     });
97     predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
98 
99     auto resultSet = dbStore->Query(predicates, {FIELD_EVENT_SEQ});
100     if (resultSet == nullptr) {
101         HILOG_ERROR(LOG_CORE, "failed to query table, event size=%{public}zu", eventSeqs.size());
102         return NativeRdb::E_ERROR;
103     }
104     int ret = resultSet->GoToNextRow();
105     while (ret == NativeRdb::E_OK) {
106         int64_t existEventSeq = 0;
107         if (resultSet->GetLong(0, existEventSeq) != NativeRdb::E_OK) {
108             HILOG_ERROR(LOG_CORE, "failed to get event seq value from resultSet");
109             resultSet->Close();
110             return NativeRdb::E_ERROR;
111         }
112         existEventSeqs.insert(existEventSeq);
113         ret = resultSet->GoToNextRow();
114     }
115     resultSet->Close();
116     return ret == NativeRdb::E_SQLITE_CORRUPT ? ret : NativeRdb::E_OK;
117 }
118 } // namespace AppEventMappingDao
119 } // namespace HiviewDFX
120 } // namespace OHOS
121