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