• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_table.h"
17 
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t { ID = 0, TYPE, NAME, SOURCE_ARG_SET_ID };
FilterTable(const TraceDataCache * dataCache)21 FilterTable::FilterTable(const TraceDataCache *dataCache) : TableBase(dataCache)
22 {
23     tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24     tableColumn_.push_back(TableBase::ColumnInfo("type", "TEXT"));
25     tableColumn_.push_back(TableBase::ColumnInfo("name", "TEXT"));
26     tableColumn_.push_back(TableBase::ColumnInfo("source_arg_set_id", "INTEGER"));
27     tablePriKey_.push_back("id");
28 }
29 
~FilterTable()30 FilterTable::~FilterTable() {}
31 
FilterByConstraint(FilterConstraints & filterfc,double & filterfilterCost,size_t filterrowCount,uint32_t filtercurrenti)32 void FilterTable::FilterByConstraint(FilterConstraints &filterfc,
33                                      double &filterfilterCost,
34                                      size_t filterrowCount,
35                                      uint32_t filtercurrenti)
36 {
37     // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
38     // for loop
39     const auto &filterc = filterfc.GetConstraints()[filtercurrenti];
40     switch (static_cast<Index>(filterc.col)) {
41         case Index::ID: {
42             if (CanFilterId(filterc.op, filterrowCount)) {
43                 filterfc.UpdateConstraint(filtercurrenti, true);
44                 filterfilterCost += 1; // id can position by 1 step
45             } else {
46                 filterfilterCost += filterrowCount; // scan all rows
47             }
48             break;
49         }
50         default:                                // other column
51             filterfilterCost += filterrowCount; // scan all rows
52             break;
53     }
54 }
55 
CreateCursor()56 std::unique_ptr<TableBase::Cursor> FilterTable::CreateCursor()
57 {
58     return std::make_unique<Cursor>(dataCache_, this);
59 }
60 
Cursor(const TraceDataCache * dataCache,TableBase * table)61 FilterTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
62     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstFilterData().Size())),
63       filterObj_(dataCache->GetConstFilterData())
64 {
65 }
66 
~Cursor()67 FilterTable::Cursor::~Cursor() {}
68 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)69 int32_t FilterTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
70 {
71     // reset indexMap_
72     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
73 
74     if (rowCount_ <= 0) {
75         return SQLITE_OK;
76     }
77 
78     auto &filterCs = fc.GetConstraints();
79     for (size_t i = 0; i < filterCs.size(); i++) {
80         const auto &c = filterCs[i];
81         switch (static_cast<Index>(c.col)) {
82             case Index::ID:
83                 FilterId(c.op, argv[i]);
84                 break;
85             default:
86                 break;
87         }
88     }
89 
90     auto filterTabOrderbys = fc.GetOrderBys();
91     for (auto i = filterTabOrderbys.size(); i > 0;) {
92         i--;
93         switch (static_cast<Index>(filterTabOrderbys[i].iColumn)) {
94             case Index::ID:
95                 indexMap_->SortBy(filterTabOrderbys[i].desc);
96                 break;
97             default:
98                 break;
99         }
100     }
101 
102     return SQLITE_OK;
103 }
104 
Column(int32_t col) const105 int32_t FilterTable::Cursor::Column(int32_t col) const
106 {
107     switch (static_cast<Index>(col)) {
108         case Index::ID:
109             sqlite3_result_int64(context_, filterObj_.IdsData()[CurrentRow()]); // IdsData() will be optimized
110             break;
111         case Index::TYPE:
112             sqlite3_result_text(context_, filterObj_.TypeData()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
113             break;
114         case Index::NAME:
115             sqlite3_result_text(context_, filterObj_.NameData()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
116             break;
117         case Index::SOURCE_ARG_SET_ID:
118             sqlite3_result_int64(context_, static_cast<int64_t>(filterObj_.SourceArgSetIdData()[CurrentRow()]));
119             break;
120         default:
121             TS_LOGF("Unregistered column : %d", col);
122             break;
123     }
124     return SQLITE_OK;
125 }
GetOrbyes(FilterConstraints & filterfc,EstimatedIndexInfo & filterei)126 void FilterTable::GetOrbyes(FilterConstraints &filterfc, EstimatedIndexInfo &filterei)
127 {
128     auto filterorderbys = filterfc.GetOrderBys();
129     for (auto i = 0; i < filterorderbys.size(); i++) {
130         switch (static_cast<Index>(filterorderbys[i].iColumn)) {
131             case Index::ID:
132                 break;
133             default: // other columns can be sorted by SQLite
134                 filterei.isOrdered = false;
135                 break;
136         }
137     }
138 }
139 } // namespace TraceStreamer
140 } // namespace SysTuning
141