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 "data_type_table.h"
17 #include "trace_data_cache.h"
18
19 namespace SysTuning {
20 namespace TraceStreamer {
21 enum class Index : int32_t { ID = 0, TYPEID, DESC };
DataTypeTable(const TraceDataCache * dataCache)22 DataTypeTable::DataTypeTable(const TraceDataCache *dataCache) : TableBase(dataCache)
23 {
24 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
25 tableColumn_.push_back(TableBase::ColumnInfo("typeId", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("desc", "TEXT"));
27 tablePriKey_.push_back("id");
28 }
29
~DataTypeTable()30 DataTypeTable::~DataTypeTable() {}
31
CreateCursor()32 std::unique_ptr<TableBase::Cursor> DataTypeTable::CreateCursor()
33 {
34 return std::make_unique<Cursor>(dataCache_, this);
35 }
36
Cursor(const TraceDataCache * dataCache,TableBase * table)37 DataTypeTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
38 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstDataTypeData().Size())),
39 dataTypeObj_(dataCache->GetConstDataTypeData())
40 {
41 }
42
~Cursor()43 DataTypeTable::Cursor::~Cursor() {}
44
Filter(const FilterConstraints & fc,sqlite3_value ** argv)45 int32_t DataTypeTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
46 {
47 // reset indexMap_
48 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
49
50 if (rowCount_ <= 0) {
51 return SQLITE_OK;
52 }
53
54 auto &dataTypeTabCs = fc.GetConstraints();
55 for (size_t i = 0; i < dataTypeTabCs.size(); i++) {
56 const auto &c = dataTypeTabCs[i];
57 switch (static_cast<Index>(c.col)) {
58 case Index::ID:
59 FilterId(c.op, argv[i]);
60 break;
61 default:
62 break;
63 }
64 }
65
66 auto dataTypeTabOrderbys = fc.GetOrderBys();
67 for (auto i = dataTypeTabOrderbys.size(); i > 0;) {
68 i--;
69 switch (static_cast<Index>(dataTypeTabOrderbys[i].iColumn)) {
70 case Index::ID:
71 indexMap_->SortBy(dataTypeTabOrderbys[i].desc);
72 break;
73 default:
74 break;
75 }
76 }
77
78 return SQLITE_OK;
79 }
80
Column(int32_t col) const81 int32_t DataTypeTable::Cursor::Column(int32_t col) const
82 {
83 switch (static_cast<Index>(col)) {
84 case Index::ID:
85 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(CurrentRow()));
86 break;
87 case Index::TYPEID:
88 sqlite3_result_int64(context_, static_cast<int64_t>(dataTypeObj_.DataTypes()[CurrentRow()]));
89 break;
90 case Index::DESC:
91 sqlite3_result_text(context_, dataCache_->GetDataFromDict(dataTypeObj_.DataDesc()[CurrentRow()]).c_str(),
92 STR_DEFAULT_LEN, nullptr);
93 break;
94 default:
95 TS_LOGF("Unknown column %d", col);
96 break;
97 }
98 return SQLITE_OK;
99 }
100 } // namespace TraceStreamer
101 } // namespace SysTuning
102