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 "args_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t { ID = 0, KEY, DATATYPE, VALUE, ARGSETID };
ArgsTable(const TraceDataCache * dataCache)21 ArgsTable::ArgsTable(const TraceDataCache *dataCache) : TableBase(dataCache)
22 {
23 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24 tableColumn_.push_back(TableBase::ColumnInfo("key", "INTEGER"));
25 tableColumn_.push_back(TableBase::ColumnInfo("datatype", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("value", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("argset", "INTEGER"));
28 tablePriKey_.push_back("id");
29 }
30
~ArgsTable()31 ArgsTable::~ArgsTable() {}
32
FilterByConstraint(FilterConstraints & argsfc,double & argsfilterCost,size_t argsrowCount,uint32_t argscurrenti)33 void ArgsTable::FilterByConstraint(FilterConstraints &argsfc,
34 double &argsfilterCost,
35 size_t argsrowCount,
36 uint32_t argscurrenti)
37 {
38 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
39 // for loop
40 const auto &argsc = argsfc.GetConstraints()[argscurrenti];
41 switch (static_cast<Index>(argsc.col)) {
42 case Index::ID: {
43 if (CanFilterId(argsc.op, argsrowCount)) {
44 argsfc.UpdateConstraint(argscurrenti, true);
45 argsfilterCost += 1; // id can position by 1 step
46 } else {
47 argsfilterCost += argsrowCount; // scan all rows
48 }
49 break;
50 }
51 default: // other column
52 argsfilterCost += argsrowCount; // scan all rows
53 break;
54 }
55 }
56
CreateCursor()57 std::unique_ptr<TableBase::Cursor> ArgsTable::CreateCursor()
58 {
59 return std::make_unique<Cursor>(dataCache_, this);
60 }
61
Cursor(const TraceDataCache * dataCache,TableBase * table)62 ArgsTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
63 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstArgSetData().Size())),
64 argSet_(dataCache->GetConstArgSetData())
65 {
66 }
67
~Cursor()68 ArgsTable::Cursor::~Cursor() {}
69
Filter(const FilterConstraints & fc,sqlite3_value ** argv)70 int32_t ArgsTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
71 {
72 // reset indexMap_
73 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
74
75 if (rowCount_ <= 0) {
76 return SQLITE_OK;
77 }
78
79 auto &argsTabCs = fc.GetConstraints();
80 for (size_t i = 0; i < argsTabCs.size(); i++) {
81 const auto &c = argsTabCs[i];
82 switch (static_cast<Index>(c.col)) {
83 case Index::ID:
84 FilterId(c.op, argv[i]);
85 break;
86 default:
87 break;
88 }
89 }
90
91 auto argsTabOrderbys = fc.GetOrderBys();
92 for (auto i = argsTabOrderbys.size(); i > 0;) {
93 i--;
94 switch (static_cast<Index>(argsTabOrderbys[i].iColumn)) {
95 case Index::ID:
96 indexMap_->SortBy(argsTabOrderbys[i].desc);
97 break;
98 default:
99 break;
100 }
101 }
102
103 return SQLITE_OK;
104 }
105
Column(int32_t col) const106 int32_t ArgsTable::Cursor::Column(int32_t col) const
107 {
108 switch (static_cast<Index>(col)) {
109 case Index::ID:
110 sqlite3_result_int64(context_, static_cast<int64_t>(argSet_.IdsData()[CurrentRow()]));
111 break;
112 case Index::KEY:
113 sqlite3_result_int64(context_, static_cast<int64_t>(argSet_.NamesData()[CurrentRow()]));
114 break;
115 case Index::DATATYPE:
116 sqlite3_result_int64(context_, static_cast<int64_t>(argSet_.DataTypes()[CurrentRow()]));
117 break;
118 case Index::VALUE:
119 sqlite3_result_int64(context_, static_cast<int64_t>(argSet_.ValuesData()[CurrentRow()]));
120 break;
121 case Index::ARGSETID:
122 sqlite3_result_int64(context_, static_cast<int64_t>(argSet_.ArgsData()[CurrentRow()]));
123 break;
124 default:
125 TS_LOGF("Unregistered column : %d", col);
126 break;
127 }
128 return SQLITE_OK;
129 }
130
GetOrbyes(FilterConstraints & argsfc,EstimatedIndexInfo & argsei)131 void ArgsTable::GetOrbyes(FilterConstraints &argsfc, EstimatedIndexInfo &argsei)
132 {
133 auto argsorderbys = argsfc.GetOrderBys();
134 for (auto i = 0; i < argsorderbys.size(); i++) {
135 switch (static_cast<Index>(argsorderbys[i].iColumn)) {
136 case Index::ID:
137 break;
138 default: // other columns can be sorted by SQLite
139 argsei.isOrdered = false;
140 break;
141 }
142 }
143 }
144 } // namespace TraceStreamer
145 } // namespace SysTuning
146