1 /* 2 * Copyright (c) 2021-2022 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 <memory> 25 #include <string> 26 #include <vector> 27 #include <map> 28 29 #include "sys_event.h" 30 31 namespace OHOS { 32 namespace HiviewDFX { 33 class DataQuery; 34 namespace EventStore { 35 enum DbQueryStatus { SUCCEED = 0, CONCURRENT, OVER_TIME, OVER_LIMIT, TOO_FREQENTLY }; 36 using DbQueryTag = struct { 37 bool isInnerQuery; 38 bool needFrequenceCheck; 39 }; 40 using DbQueryCallback = std::function<void(DbQueryStatus)>; 41 using QueryProcessInfo = std::pair<pid_t, std::string>; // first: pid of process, second: process name 42 constexpr pid_t INNER_PROCESS_ID = -1; 43 enum Op { NONE = 0, EQ = 1, NE, LT, LE, GT, GE, SW, NSW }; 44 45 class EventCol { 46 public: 47 static std::string DOMAIN; 48 static std::string NAME; 49 static std::string TYPE; 50 static std::string TS; 51 static std::string TZ; 52 static std::string PID; 53 static std::string TID; 54 static std::string UID; 55 static std::string TRACE_ID; 56 static std::string TRACE_FLAG; 57 static std::string SPAN_ID; 58 static std::string PARENT_SPAN_ID; 59 static std::string INFO; 60 }; 61 62 class FieldValue { 63 public: 64 enum ValueType { NONE = 0, INTEGER = 1, FLOAT = 2, DOUBLE = 3, STRING = 4 }; FieldValue(int value)65 FieldValue(int value): valueType_(INTEGER), iValue_(value), fValue_(0), dValue_(0) {} FieldValue(int64_t value)66 FieldValue(int64_t value): valueType_(INTEGER), iValue_(value), fValue_(0), dValue_(0) {} FieldValue(float value)67 FieldValue(float value): valueType_(FLOAT), iValue_(0), fValue_(value), dValue_(0) {} FieldValue(double value)68 FieldValue(double value): valueType_(DOUBLE), iValue_(0), fValue_(0), dValue_(value) {} FieldValue(const std::string & value)69 FieldValue(const std::string &value): valueType_(STRING), iValue_(0), fValue_(0), dValue_(0), sValue_(value) {} ~FieldValue()70 ~FieldValue() {}; 71 72 bool IsInteger(); 73 bool IsFloat(); 74 bool IsDouble(); 75 bool IsString(); 76 int64_t GetInteger(); 77 float GetFloat(); 78 double GetDouble(); 79 const std::string GetString(); 80 private: 81 ValueType valueType_; 82 int64_t iValue_; 83 float fValue_; 84 double dValue_; 85 std::string sValue_; 86 }; 87 88 class DllExport Cond { 89 public: Cond()90 Cond(): op_(NONE), fieldValue_(0) {}; 91 Cond(const std::string &col, Op op, int8_t value); 92 Cond(const std::string &col, Op op, int16_t value); 93 Cond(const std::string &col, Op op, int32_t value); 94 Cond(const std::string &col, Op op, int64_t value); 95 Cond(const std::string &col, Op op, float value); 96 Cond(const std::string &col, Op op, double value); 97 Cond(const std::string &col, Op op, const std::string &value); 98 Cond(const std::string &col, Op op, const std::vector<int8_t> &ints); 99 Cond(const std::string &col, Op op, const std::vector<int16_t> &ints); 100 Cond(const std::string &col, Op op, const std::vector<int32_t> &ints); 101 Cond(const std::string &col, Op op, const std::vector<int64_t> &longs); 102 Cond(const std::string &col, Op op, const std::vector<float> &floats); 103 Cond(const std::string &col, Op op, const std::vector<double> &doubles); 104 Cond(const std::string &col, Op op, const std::vector<std::string> &strings); 105 ~Cond(); 106 Cond &And(const std::string &col, Op op, const int8_t value); 107 Cond &And(const std::string &col, Op op, const int16_t value); 108 Cond &And(const std::string &col, Op op, const int32_t value); 109 Cond &And(const std::string &col, Op op, const int64_t value); 110 Cond &And(const std::string &col, Op op, const float value); 111 Cond &And(const std::string &col, Op op, const double value); 112 Cond &And(const std::string &col, Op op, const std::string &value); 113 Cond &And(const Cond &cond); 114 Cond &Or(const std::string &col, Op op, const int8_t value); 115 Cond &Or(const std::string &col, Op op, const int16_t value); 116 Cond &Or(const std::string &col, Op op, const int32_t value); 117 Cond &Or(const std::string &col, Op op, const int64_t value); 118 Cond &Or(const std::string &col, Op op, const float value); 119 Cond &Or(const std::string &col, Op op, const double value); 120 Cond &Or(const std::string &col, Op op, const std::string &value); 121 Cond &Or(const Cond &cond); 122 private: 123 friend class SysEventQuery; 124 125 private: 126 static bool IsSimpleCond(Cond &cond); 127 static void Traval(DataQuery &dataQuery, Cond &cond); 128 static void GetCond(DataQuery &dataQuery, Cond &cond); 129 static void GetCondEqualValue(DataQuery &dataQuery, Cond &cond); 130 static void GetCondNotEqualValue(DataQuery &dataQuery, Cond &cond); 131 static void GetCondLessThanValue(DataQuery &dataQuery, Cond &cond); 132 static void GetCondLessEqualValue(DataQuery &dataQuery, Cond &cond); 133 static void GetCondGreatThanValue(DataQuery &dataQuery, Cond &cond); 134 static void GetCondGreatEqualValue(DataQuery &dataQuery, Cond &cond); 135 static void GetCondStartWithValue(DataQuery &dataQuery, Cond &cond); 136 static void GetCondNoStartWithValue(DataQuery &dataQuery, Cond &cond); 137 138 private: 139 std::string col_; 140 Op op_; 141 FieldValue fieldValue_; 142 std::vector<Cond> andConds_; 143 std::vector<Cond> orConds_; 144 }; // Cond 145 146 class DllExport ResultSet { 147 public: 148 ResultSet(); 149 ResultSet(ResultSet &&result); 150 ResultSet& operator = (ResultSet &&result); 151 ~ResultSet(); 152 153 public: 154 using RecordIter = std::vector<SysEvent>::iterator; 155 int GetErrCode() const; 156 bool HasNext() const; 157 RecordIter Next(); 158 159 private: 160 friend class SysEventQuery; 161 friend class SysEventQueryWrapper; 162 void Set(int code, bool has); 163 std::vector<SysEvent> eventRecords_; 164 RecordIter iter_; 165 int code_; 166 bool has_; 167 }; // ResultSet 168 169 using SysEventCallBack = std::function<int(SysEvent &sysEvent)>; 170 class DllExport SysEventQuery { 171 public: 172 SysEventQuery &Select(); 173 SysEventQuery &Select(const std::vector<std::string> &eventCols); 174 SysEventQuery &Where(const std::string &col, Op op, const int8_t value); 175 SysEventQuery &Where(const std::string &col, Op op, const int16_t value); 176 SysEventQuery &Where(const std::string &col, Op op, const int32_t value); 177 SysEventQuery &Where(const std::string &col, Op op, const int64_t value); 178 SysEventQuery &Where(const std::string &col, Op op, const float value); 179 SysEventQuery &Where(const std::string &col, Op op, const double value); 180 SysEventQuery &Where(const std::string &col, Op op, const std::string &value); 181 SysEventQuery &Where(const Cond &cond); 182 SysEventQuery &And(const std::string &col, Op op, const int8_t value); 183 SysEventQuery &And(const std::string &col, Op op, const int16_t value); 184 SysEventQuery &And(const std::string &col, Op op, const int32_t value); 185 SysEventQuery &And(const std::string &col, Op op, const int64_t value); 186 SysEventQuery &And(const std::string &col, Op op, const float value); 187 SysEventQuery &And(const std::string &col, Op op, const double value); 188 SysEventQuery &And(const std::string &col, Op op, const std::string &value); 189 SysEventQuery &And(const Cond &cond); 190 SysEventQuery &And(const std::vector<Cond> &conds); 191 SysEventQuery &Or(const std::string &col, Op op, const int8_t value); 192 SysEventQuery &Or(const std::string &col, Op op, const int16_t value); 193 SysEventQuery &Or(const std::string &col, Op op, const int32_t value); 194 SysEventQuery &Or(const std::string &col, Op op, const int64_t value); 195 SysEventQuery &Or(const std::string &col, Op op, const float value); 196 SysEventQuery &Or(const std::string &col, Op op, const double value); 197 SysEventQuery &Or(const std::string &col, Op op, const std::string &value); 198 SysEventQuery &Or(const Cond &cond); 199 SysEventQuery &Or(const std::string &col, Op op, const std::vector<int8_t> &ints); 200 SysEventQuery &Or(const std::string &col, Op op, const std::vector<int16_t> &ints); 201 SysEventQuery &Or(const std::string &col, Op op, const std::vector<int32_t> &ints); 202 SysEventQuery &Or(const std::string &col, Op op, const std::vector<int64_t> &longs); 203 SysEventQuery &Or(const std::string &col, Op op, const std::vector<float> &floats); 204 SysEventQuery &Or(const std::string &col, Op op, const std::vector<double> &floats); 205 SysEventQuery &Or(const std::string &col, Op op, const std::vector<std::string> &strings); 206 SysEventQuery &Or(const std::vector<Cond> &conds); 207 SysEventQuery &Order(const std::string &col, bool isAsc = true); 208 int ExecuteWithCallback(SysEventCallBack callback, int limit = 100); 209 210 public: 211 virtual ResultSet Execute(int limit = 100, DbQueryTag tag = { true, true }, 212 QueryProcessInfo callerInfo = std::make_pair(INNER_PROCESS_ID, ""), 213 DbQueryCallback queryCallback = nullptr); 214 virtual ~SysEventQuery(); 215 216 protected: 217 friend class SysEventDao; 218 SysEventQuery(const std::string &dbFile); 219 220 protected: 221 std::string GetDbFile(); 222 void GetDataQuery(DataQuery &dataQuery); 223 void BuildDataQuery(DataQuery &dataQuery, int limit); 224 225 private: 226 ResultSet ExecuteSQL(int limit); 227 228 private: 229 std::string dbFile_; 230 std::vector<std::string> eventCols_; 231 std::vector<std::pair<std::string, bool>> orderCols_; 232 Cond cond_; 233 }; // SysEventQuery 234 } // EventStore 235 } // namespace HiviewDFX 236 } // namespace OHOS 237 #endif // HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_H