• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "filter_constraints.h"
17 
18 #include "log.h"
19 
20 namespace SysTuning {
21 namespace TraceStreamer {
AddConstraint(int idx,int col,unsigned char op,bool isSupport)22 void FilterConstraints::AddConstraint(int idx, int 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(int idx,bool isSupport)31 void FilterConstraints::UpdateConstraint(int idx, bool isSupport)
32 {
33     if (idx >= 0 && static_cast<size_t>(idx) < constraints_.size()) {
34         constraints_[idx].isSupport = isSupport;
35     }
36 }
37 
AddOrderBy(int col,unsigned char desc)38 void FilterConstraints::AddOrderBy(int 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 
FromString(const std::string & idxStr)67 void FilterConstraints::FromString(const std::string& idxStr)
68 {
69     const char* p = static_cast<const char*>(idxStr.c_str());
70     char* pNext = nullptr;
71     TS_ASSERT(*p == 'C');
72     errno = 0;
73     int constraintCount = static_cast<int>(strtol(p + 1, &pNext, 10));
74     if (errno != 0) {
75         TS_LOGW("strtol failed!");
76         return;
77     }
78     TS_ASSERT(p != pNext);
79     for (int i = 0; i < constraintCount; i++) {
80         p = pNext;
81         errno = 0;
82         int col = static_cast<int>(strtol(p, &pNext, 10));
83         if (errno != 0) {
84             TS_LOGW("strtol failed!");
85             return;
86         }
87         TS_ASSERT(p != pNext);
88         p = pNext;
89         errno = 0;
90         unsigned char op = static_cast<unsigned char>(strtol(p, &pNext, 10));
91         if (errno != 0) {
92             TS_LOGW("strtol failed!");
93             return;
94         }
95         TS_ASSERT(p != pNext);
96 
97         AddConstraint(i, col, op);
98     }
99 
100     pNext++; // jump the ' '
101     p = pNext;
102     TS_ASSERT(*p == 'O');
103     errno = 0;
104     int orderbyCount = static_cast<int>(strtol(p + 1, &pNext, 10));
105     if (errno != 0) {
106         TS_LOGW("strtol failed!");
107         return;
108     }
109     TS_ASSERT(p != pNext);
110     for (int i = 0; i < orderbyCount; i++) {
111         p = pNext;
112         errno = 0;
113         int col = static_cast<int>(strtol(p, &pNext, 10));
114         if (errno != 0) {
115             TS_LOGW("strtol failed!");
116             return;
117         }
118         TS_ASSERT(p != pNext);
119         p = pNext;
120         errno = 0;
121         unsigned char desc = static_cast<unsigned char>(strtol(p, &pNext, 10));
122         if (errno != 0) {
123             TS_LOGW("strtol failed!");
124             return;
125         }
126         TS_ASSERT(p != pNext);
127 
128         AddOrderBy(col, desc);
129     }
130 }
131 } // namespace TraceStreamer
132 } // namespace SysTuning
133