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