• 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 "data_type_table.h"
17 #include "trace_data_cache.h"
18 
19 namespace SysTuning {
20 namespace TraceStreamer {
21 namespace {
22 enum Index { ID = 0, TYPEID, DESC };
23 }
DataTypeTable(const TraceDataCache * dataCache)24 DataTypeTable::DataTypeTable(const TraceDataCache* dataCache) : TableBase(dataCache)
25 {
26     tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
27     tableColumn_.push_back(TableBase::ColumnInfo("typeId", "INTEGER"));
28     tableColumn_.push_back(TableBase::ColumnInfo("desc", "TEXT"));
29     tablePriKey_.push_back("id");
30 }
31 
~DataTypeTable()32 DataTypeTable::~DataTypeTable() {}
33 
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)34 void DataTypeTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
35 {
36     constexpr double filterBaseCost = 1000.0; // set-up and tear-down
37     constexpr double indexCost = 2.0;
38     ei.estimatedCost = filterBaseCost;
39 
40     auto rowCount = dataCache_->GetConstDataTypeData().Size();
41     if (rowCount == 0 || rowCount == 1) {
42         ei.estimatedRows = rowCount;
43         ei.estimatedCost += indexCost * rowCount;
44         return;
45     }
46 
47     double filterCost = 0.0;
48     auto constraints = fc.GetConstraints();
49     if (constraints.empty()) { // scan all rows
50         filterCost = rowCount;
51     } else {
52         FilterByConstraint(fc, filterCost, rowCount);
53     }
54     ei.estimatedCost += filterCost;
55     ei.estimatedRows = rowCount;
56     ei.estimatedCost += rowCount * indexCost;
57 
58     ei.isOrdered = true;
59     auto orderbys = fc.GetOrderBys();
60     for (auto i = 0; i < orderbys.size(); i++) {
61         switch (orderbys[i].iColumn) {
62             case ID:
63                 break;
64             default: // other columns can be sorted by SQLite
65                 ei.isOrdered = false;
66                 break;
67         }
68     }
69 }
70 
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)71 void DataTypeTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
72 {
73     auto fcConstraints = fc.GetConstraints();
74     for (int i = 0; i < static_cast<int>(fcConstraints.size()); i++) {
75         if (rowCount <= 1) {
76             // only one row or nothing, needn't filter by constraint
77             filterCost += rowCount;
78             break;
79         }
80         const auto& c = fcConstraints[i];
81         switch (c.col) {
82             case ID: {
83                 if (CanFilterId(c.op, rowCount)) {
84                     fc.UpdateConstraint(i, true);
85                     filterCost += 1; // id can position by 1 step
86                 } else {
87                     filterCost += rowCount; // scan all rows
88                 }
89                 break;
90             }
91             default:                    // other column
92                 filterCost += rowCount; // scan all rows
93                 break;
94         }
95     }
96 }
97 
CanFilterId(const char op,size_t & rowCount)98 bool DataTypeTable::CanFilterId(const char op, size_t& rowCount)
99 {
100     switch (op) {
101         case SQLITE_INDEX_CONSTRAINT_EQ:
102             rowCount = 1;
103             break;
104         case SQLITE_INDEX_CONSTRAINT_GT:
105         case SQLITE_INDEX_CONSTRAINT_GE:
106         case SQLITE_INDEX_CONSTRAINT_LE:
107         case SQLITE_INDEX_CONSTRAINT_LT:
108             // assume filter out a half of rows
109             rowCount = (rowCount >> 1);
110             break;
111         default:
112             return false;
113     }
114     return true;
115 }
116 
CreateCursor()117 std::unique_ptr<TableBase::Cursor> DataTypeTable::CreateCursor()
118 {
119     return std::make_unique<Cursor>(dataCache_, this);
120 }
121 
Cursor(const TraceDataCache * dataCache,TableBase * table)122 DataTypeTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
123     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstDataTypeData().Size())),
124       dataTypeObj_(dataCache->GetConstDataTypeData())
125 {
126 }
127 
~Cursor()128 DataTypeTable::Cursor::~Cursor() {}
129 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)130 int DataTypeTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
131 {
132     // reset indexMap_
133     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
134 
135     if (rowCount_ <= 0) {
136         return SQLITE_OK;
137     }
138 
139     auto& cs = fc.GetConstraints();
140     for (size_t i = 0; i < cs.size(); i++) {
141         const auto& c = cs[i];
142         switch (c.col) {
143             case ID:
144                 FilterId(c.op, argv[i]);
145                 break;
146             default:
147                 break;
148         }
149     }
150 
151     auto orderbys = fc.GetOrderBys();
152     for (auto i = orderbys.size(); i > 0;) {
153         i--;
154         switch (orderbys[i].iColumn) {
155             case ID:
156                 indexMap_->SortBy(orderbys[i].desc);
157                 break;
158             default:
159                 break;
160         }
161     }
162 
163     return SQLITE_OK;
164 }
165 
Column(int col) const166 int DataTypeTable::Cursor::Column(int col) const
167 {
168     switch (col) {
169         case ID:
170             sqlite3_result_int64(context_, static_cast<sqlite3_int64>(CurrentRow()));
171             break;
172         case TYPEID:
173             sqlite3_result_int64(context_, static_cast<int64_t>(dataTypeObj_.DataTypes()[CurrentRow()]));
174             break;
175         case DESC:
176             sqlite3_result_text(context_, dataCache_->GetDataFromDict(dataTypeObj_.DataDesc()[CurrentRow()]).c_str(),
177                 STR_DEFAULT_LEN, nullptr);
178             break;
179         default:
180             TS_LOGF("Unknown column %d", col);
181             break;
182     }
183     return SQLITE_OK;
184 }
185 } // namespace TraceStreamer
186 } // namespace SysTuning
187