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