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 "doc_query.h"
16
17 #include <unordered_set>
18
19 #include "decoded/decoded_event.h"
20 #include "logger.h"
21 #include "sys_event_query.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace EventStore {
26 DEFINE_LOG_TAG("HiView-DocQuery");
27 using EventRaw::DecodedEvent;
28
And(const Cond & cond)29 void DocQuery::And(const Cond& cond)
30 {
31 if (cond.col_ == EventCol::DOMAIN || cond.col_ == EventCol::NAME) {
32 HIVIEW_LOGI("invalid condition, cond.col=%{public}s", cond.col_.c_str());
33 return;
34 }
35 if (IsInnerCond(cond)) {
36 innerConds_.push_back(cond);
37 return;
38 }
39 extraConds_.push_back(cond);
40 }
41
IsInnerCond(const Cond & cond) const42 bool DocQuery::IsInnerCond(const Cond& cond) const
43 {
44 const std::unordered_set<std::string> innerFields = {
45 EventCol::SEQ, EventCol::TS, EventCol::TZ,
46 EventCol::PID, EventCol::TID, EventCol::UID,
47 };
48 return innerFields.find(cond.col_) != innerFields.end();
49 }
50
IsContainCond(const Cond & cond,const FieldValue & value) const51 bool DocQuery::IsContainCond(const Cond& cond, const FieldValue& value) const
52 {
53 switch (cond.op_) {
54 case EQ:
55 return value == cond.fieldValue_;
56 case NE:
57 return value != cond.fieldValue_;
58 case GT:
59 return value > cond.fieldValue_;
60 case GE:
61 return value >= cond.fieldValue_;
62 case LT:
63 return value < cond.fieldValue_;
64 case LE:
65 return value <= cond.fieldValue_;
66 case SW:
67 return value.IsStartWith(cond.fieldValue_);
68 case NSW:
69 return value.IsNotStartWith(cond.fieldValue_);
70 default:
71 return false;
72 }
73 }
74
IsContainInnerCond(const DocEventHeader & eventHeader,const Cond & cond) const75 bool DocQuery::IsContainInnerCond(const DocEventHeader& eventHeader, const Cond& cond) const
76 {
77 FieldValue value;
78 if (cond.col_ == EventCol::SEQ) {
79 value = eventHeader.seq;
80 } else if (cond.col_ == EventCol::TS) {
81 value = static_cast<int64_t>(eventHeader.timestamp);
82 } else if (cond.col_ == EventCol::TZ) {
83 value = static_cast<int64_t>(eventHeader.timeZone);
84 } else if (cond.col_ == EventCol::UID) {
85 value = static_cast<int64_t>(eventHeader.uid);
86 } else if (cond.col_ == EventCol::PID) {
87 value = static_cast<int64_t>(eventHeader.pid);
88 } else if (cond.col_ == EventCol::TID) {
89 value = static_cast<int64_t>(eventHeader.tid);
90 } else {
91 return false;
92 }
93 return IsContainCond(cond, value);
94 }
95
IsContainInnerConds(uint8_t * content) const96 bool DocQuery::IsContainInnerConds(uint8_t* content) const
97 {
98 if (innerConds_.empty()) {
99 return true;
100 }
101 auto eventHeader = *(reinterpret_cast<DocEventHeader*>(content + BLOCK_SIZE));
102 return std::all_of(innerConds_.begin(), innerConds_.end(), [this, &eventHeader] (auto& cond) {
103 return IsContainInnerCond(eventHeader, cond);
104 });
105 }
106
IsContainExtraConds(EventRaw::DecodedEvent & decodedEvent) const107 bool DocQuery::IsContainExtraConds(EventRaw::DecodedEvent& decodedEvent) const
108 {
109 return std::all_of(extraConds_.begin(), extraConds_.end(), [this, &decodedEvent] (auto& cond) {
110 const auto& extraParams = decodedEvent.GetAllCustomizedValues();
111 for (auto& param : extraParams) {
112 if (cond.col_ != param->GetKey()) {
113 continue;
114 }
115 FieldValue paramValue;
116 if (int64_t intValue = 0; param->AsInt64(intValue)) {
117 paramValue = intValue;
118 } else if (uint64_t uintValue = 0; param->AsUint64(uintValue)) {
119 paramValue = uintValue;
120 } else if (double dValue = 0; param->AsDouble(dValue)) {
121 paramValue = dValue;
122 } else if (std::string sValue; param->AsString(sValue)) {
123 paramValue = sValue;
124 } else {
125 return false;
126 }
127 return IsContainCond(cond, paramValue);
128 }
129 return false;
130 });
131 }
132
ToString() const133 std::string DocQuery::ToString() const
134 {
135 std::string output;
136 const std::string connStr = " and ";
137 for (auto& cond : innerConds_) {
138 output.append(cond.ToString());
139 if (&cond != &innerConds_.back()) {
140 output.append(connStr);
141 }
142 }
143 for (auto& cond : extraConds_) {
144 output.append(connStr).append(cond.ToString());
145 if (&cond != &extraConds_.back()) {
146 output.append(connStr);
147 }
148 }
149 return output;
150 }
151 }; // DocQuery
152 } // HiviewDFX
153 } // OHOS
154