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