1 /* 2 * Copyright (c) 2021-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 16 #ifndef HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_H 17 #define HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_H 18 19 #ifndef DllExport 20 #define DllExport 21 #endif // DllExport 22 23 #include <functional> 24 #include <queue> 25 #include <memory> 26 #include <string> 27 #include <variant> 28 #include <vector> 29 30 #include "base_def.h" 31 #include "doc_query.h" 32 #include "sys_event.h" 33 34 namespace OHOS { 35 namespace HiviewDFX { 36 namespace EventStore { 37 enum DbQueryStatus { SUCCEED = 0, CONCURRENT, OVER_TIME, OVER_LIMIT, TOO_FREQENTLY }; 38 using DbQueryTag = struct { 39 bool isInnerQuery; 40 bool needFrequenceCheck; 41 }; 42 using DbQueryCallback = std::function<void(DbQueryStatus)>; 43 using QueryProcessInfo = std::pair<pid_t, std::string>; // first: pid of process, second: process name 44 45 using CompareFunc = bool(*)(const Entry&, const Entry&); 46 using EntryQueue = std::priority_queue<Entry, std::vector<Entry>, CompareFunc>; 47 48 constexpr pid_t INNER_PROCESS_ID = -1; 49 50 enum Op { NONE = 0, EQ = 1, NE, LT, LE, GT, GE, SW, NSW }; 51 52 class SysEventDao; 53 class SysEventDatabase; 54 55 class FieldNumber final { 56 public: 57 enum ValueType { DOUBLE = 0, UINT = 1, INT = 2 }; 58 59 template<typename T> FieldNumber(T val)60 FieldNumber(T val) 61 { 62 if constexpr (std::is_same_v<std::decay_t<T>, uint8_t> || 63 std::is_same_v<std::decay_t<T>, uint16_t> || 64 std::is_same_v<std::decay_t<T>, uint32_t> || 65 std::is_same_v<std::decay_t<T>, uint64_t>) { 66 val_ = static_cast<uint64_t>(val); 67 return; 68 } 69 if constexpr (std::is_same_v<std::decay_t<T>, int8_t> || 70 std::is_same_v<std::decay_t<T>, int16_t> || 71 std::is_same_v<std::decay_t<T>, int32_t> || 72 std::is_same_v<std::decay_t<T>, int64_t>) { 73 val_ = static_cast<int64_t>(val); 74 return; 75 } 76 if constexpr (std::is_same_v<std::decay_t<T>, float> || 77 std::is_same_v<std::decay_t<T>, double>) { 78 val_ = static_cast<double>(val); 79 return; 80 } 81 val_ = static_cast<int64_t>(0); // default 82 } 83 84 bool operator==(const FieldNumber& fieldNum) const; 85 bool operator!=(const FieldNumber& fieldNum) const; 86 bool operator<(const FieldNumber& fieldNum) const; 87 bool operator<=(const FieldNumber& fieldNum) const; 88 bool operator>(const FieldNumber& fieldNum) const; 89 bool operator>=(const FieldNumber& fieldNum) const; 90 91 template<typename T, 92 std::enable_if_t<std::is_same_v<std::decay_t<T>, double> || 93 std::is_same_v<std::decay_t<T>, uint64_t> || 94 std::is_same_v<std::decay_t<T>, int64_t>>* = nullptr> decltype(auto)95 decltype(auto) GetNumber() const 96 { 97 if (val_.index() == DOUBLE) { 98 return static_cast<std::decay_t<T>>(std::get<DOUBLE>(val_)); 99 } 100 if (val_.index() == UINT) { 101 return static_cast<std::decay_t<T>>(std::get<UINT>(val_)); 102 } 103 return static_cast<std::decay_t<T>>(std::get<INT>(val_)); 104 } 105 106 std::string FormatAsString() const; 107 ValueType Index() const; 108 109 private: 110 std::variant<double, uint64_t, int64_t> val_; 111 }; 112 113 class FieldValue { 114 public: 115 enum ValueType { STRING = 0, NUMBER = 1 }; 116 FieldValue()117 FieldValue(): FieldValue(0) {} 118 119 template<typename T> FieldValue(T val)120 FieldValue(T val) 121 { 122 if constexpr (std::is_same_v<std::decay_t<T>, uint8_t> || 123 std::is_same_v<std::decay_t<T>, uint16_t> || 124 std::is_same_v<std::decay_t<T>, uint32_t> || 125 std::is_same_v<std::decay_t<T>, uint64_t> || 126 std::is_same_v<std::decay_t<T>, int8_t> || 127 std::is_same_v<std::decay_t<T>, int16_t> || 128 std::is_same_v<std::decay_t<T>, int32_t> || 129 std::is_same_v<std::decay_t<T>, int64_t> || 130 std::is_same_v<std::decay_t<T>, float> || 131 std::is_same_v<std::decay_t<T>, double>) { 132 val_ = val; 133 return; 134 } 135 if constexpr (std::is_same_v<std::decay_t<T>, std::string> || 136 std::is_same_v<std::decay_t<T>, const char*>) { 137 val_ = std::string(val); 138 return; 139 } 140 val_ = 0; 141 } 142 ~FieldValue()143 ~FieldValue() {} 144 145 bool operator==(const FieldValue& fieldValue) const; 146 bool operator!=(const FieldValue& fieldValue) const; 147 bool operator<(const FieldValue& fieldValue) const; 148 bool operator<=(const FieldValue& fieldValue) const; 149 bool operator>(const FieldValue& fieldValue) const; 150 bool operator>=(const FieldValue& fieldValue) const; 151 bool IsStartWith(const FieldValue& fieldValue) const; 152 bool IsNotStartWith(const FieldValue& fieldValue) const; 153 154 bool IsNumber() const; 155 bool IsString() const; 156 FieldNumber GetFieldNumber() const; 157 std::string GetString() const; 158 std::string FormatAsString() const; 159 ValueType Index() const; 160 161 private: 162 std::variant<std::string, FieldNumber> val_; 163 }; 164 165 class DllExport Cond { 166 public: Cond()167 Cond(): op_(NONE), fieldValue_(0) {} ~Cond()168 ~Cond() {} 169 170 template <typename T> Cond(const std::string & col,Op op,const T & value)171 Cond(const std::string &col, Op op, const T &value): col_(col), op_(op), fieldValue_(value) {} 172 173 template <typename T> And(const std::string & col,Op op,const T & value)174 Cond &And(const std::string &col, Op op, const T &value) 175 { 176 andConds_.emplace_back(Cond(col, op, value)); 177 return *this; 178 } 179 Cond &And(const Cond &cond); 180 181 std::string ToString() const; 182 183 private: 184 friend class DocQuery; 185 friend class SysEventQuery; 186 static bool IsSimpleCond(const Cond &cond); 187 static void Traval(DocQuery &docQuery, const Cond &cond); 188 189 private: 190 std::string col_; 191 Op op_; 192 FieldValue fieldValue_; 193 std::vector<Cond> andConds_; 194 }; // Cond 195 196 class DllExport ResultSet { 197 public: 198 ResultSet(); 199 ResultSet(ResultSet &&result); 200 ResultSet& operator = (ResultSet &&result); 201 ~ResultSet(); 202 203 public: 204 using RecordIter = std::vector<SysEvent>::iterator; 205 int GetErrCode() const; 206 bool HasNext() const; 207 RecordIter Next(); 208 209 private: 210 friend class SysEventQuery; 211 friend class SysEventQueryWrapper; 212 void Set(int code, bool has); 213 std::vector<SysEvent> eventRecords_; 214 RecordIter iter_; 215 int code_; 216 bool has_; 217 }; // ResultSet 218 219 /* Query parameters for filtering file names */ 220 struct SysEventQueryArg { 221 std::string domain; 222 std::vector<std::string> names; 223 uint32_t type; 224 int64_t toSeq; 225 SysEventQueryArgSysEventQueryArg226 SysEventQueryArg() : SysEventQueryArg("", {}, 0, INVALID_VALUE_INT) {} SysEventQueryArgSysEventQueryArg227 SysEventQueryArg(const std::string& domain, const std::vector<std::string>& names, 228 uint32_t type, int64_t toSeq) 229 { 230 this->domain = domain; 231 this->names.assign(names.begin(), names.end()); 232 this->type = type; 233 this->toSeq = toSeq; 234 } ~SysEventQueryArgSysEventQueryArg235 ~SysEventQueryArg() {} 236 }; 237 238 class DllExport SysEventQuery { 239 public: 240 SysEventQuery(const std::string& domain, const std::vector<std::string>& names); ~SysEventQuery()241 virtual ~SysEventQuery() {} 242 243 SysEventQuery &Select(const std::vector<std::string> &eventCols); 244 245 template <typename T> Where(const std::string & col,Op op,const T & value)246 SysEventQuery &Where(const std::string &col, Op op, const T &value) 247 { 248 cond_.And(col, op, value); 249 return *this; 250 } 251 SysEventQuery &Where(const Cond &cond); 252 253 template <typename T> And(const std::string & col,Op op,const T & value)254 SysEventQuery &And(const std::string &col, Op op, const T &value) 255 { 256 cond_.And(col, op, value); 257 return *this; 258 } 259 SysEventQuery &And(const Cond &cond); 260 261 SysEventQuery &Order(const std::string &col, bool isAsc = true); 262 263 virtual ResultSet Execute(int limit = 100, DbQueryTag tag = { true, true }, 264 QueryProcessInfo callerInfo = std::make_pair(INNER_PROCESS_ID, ""), 265 DbQueryCallback queryCallback = nullptr); 266 267 std::string ToString() const; 268 269 friend class SysEventDao; 270 friend class SysEventDatabase; 271 272 protected: 273 SysEventQuery(); 274 SysEventQuery(const std::string& domain, const std::vector<std::string>& names, uint32_t type, int64_t toSeq); 275 276 private: 277 void BuildDocQuery(DocQuery &docQuery) const; 278 CompareFunc CreateCompareFunc() const; 279 280 int limit_; 281 std::pair<std::string, bool> orderCol_; 282 Cond cond_; 283 SysEventQueryArg queryArg_; 284 }; // SysEventQuery 285 } // EventStore 286 } // namespace HiviewDFX 287 } // namespace OHOS 288 #endif // HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_H 289