1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 #include "filter_constraints.h"
17
18 #include "log.h"
19
20 namespace SysTuning {
21 namespace TraceStreamer {
AddConstraint(int32_t idx,int32_t col,unsigned char op,bool isSupport)22 void FilterConstraints::AddConstraint(int32_t idx, int32_t col, unsigned char op, bool isSupport)
23 {
24 Constraint &c = constraints_.emplace_back();
25 c.idxInaConstraint = idx;
26 c.col = col;
27 c.op = op;
28 c.isSupport = isSupport;
29 }
30
UpdateConstraint(int32_t idx,bool isSupport)31 void FilterConstraints::UpdateConstraint(int32_t idx, bool isSupport)
32 {
33 if (idx >= 0 && static_cast<size_t>(idx) < constraints_.size()) {
34 constraints_[idx].isSupport = isSupport;
35 }
36 }
37
AddOrderBy(int32_t col,unsigned char desc)38 void FilterConstraints::AddOrderBy(int32_t col, unsigned char desc)
39 {
40 OrderBy &o = orderBys_.emplace_back();
41 o.iColumn = col;
42 o.desc = desc;
43 }
44
Clear()45 void FilterConstraints::Clear()
46 {
47 constraints_.clear();
48 orderBys_.clear();
49 }
50
ToString(std::string & idxStr) const51 void FilterConstraints::ToString(std::string &idxStr) const
52 {
53 idxStr.clear();
54 idxStr.reserve(idxStrSize_);
55 idxStr = "C" + std::to_string(constraints_.size());
56 for (size_t i = 0; i < constraints_.size(); i++) {
57 idxStr += " " + std::to_string(constraints_[i].col);
58 idxStr += " " + std::to_string(constraints_[i].op);
59 }
60 idxStr += " O" + std::to_string(orderBys_.size());
61 for (size_t i = 0; i < orderBys_.size(); i++) {
62 idxStr += " " + std::to_string(orderBys_[i].iColumn);
63 idxStr += " " + std::to_string(orderBys_[i].desc);
64 }
65 }
66
GetColAndOp(const char ** p,char ** pNext,int32_t & col,unsigned char & op)67 void FilterConstraints::GetColAndOp(const char **p, char **pNext, int32_t &col, unsigned char &op)
68 {
69 *p = *pNext;
70 errno = 0;
71 const uint32_t intergerRadixTypeDec = 10;
72 col = static_cast<int32_t>(strtol(*p, pNext, intergerRadixTypeDec));
73 if (errno != 0) {
74 TS_LOGW("strtol failed!");
75 return;
76 }
77 TS_ASSERT(*p != *pNext);
78 *p = *pNext;
79 errno = 0;
80 op = static_cast<unsigned char>(strtol(*p, pNext, intergerRadixTypeDec));
81 if (errno != 0) {
82 TS_LOGW("strtol failed!");
83 return;
84 }
85 TS_ASSERT(p != pNext);
86 }
87
FromString(const std::string & idxStr)88 void FilterConstraints::FromString(const std::string &idxStr)
89 {
90 const char *p = static_cast<const char *>(idxStr.c_str());
91 char *pNext = nullptr;
92 TS_ASSERT(*p == 'C');
93 errno = 0;
94 int32_t constraintCount = static_cast<int32_t>(strtol(p + 1, &pNext, 10));
95 if (errno != 0) {
96 TS_LOGW("strtol failed!");
97 return;
98 }
99 TS_ASSERT(p != pNext);
100 unsigned char op = 0;
101 int32_t col = 0;
102 for (int32_t i = 0; i < constraintCount; i++) {
103 GetColAndOp(&p, &pNext, col, op);
104 AddConstraint(i, col, op);
105 }
106
107 pNext++; // jump the ' '
108 p = pNext;
109 TS_ASSERT(*p == 'O');
110 errno = 0;
111 int32_t orderbyCount = static_cast<int32_t>(strtol(p + 1, &pNext, 10));
112 if (errno != 0) {
113 TS_LOGW("strtol failed!");
114 return;
115 }
116 TS_ASSERT(p != pNext);
117 for (int32_t i = 0; i < orderbyCount; i++) {
118 GetColAndOp(&p, &pNext, col, op);
119 AddOrderBy(col, op);
120 }
121 }
122 } // namespace TraceStreamer
123 } // namespace SysTuning
124