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", "UNSIGNED BIG INT"));
27 tableColumn_.push_back(TableBase::ColumnInfo("data", "STRING"));
28 tablePriKey_.push_back("id");
29 }
30
~DataDictTable()31 DataDictTable::~DataDictTable() {}
32
CreateCursor()33 void DataDictTable::CreateCursor()
34 {
35 cursor_ = std::make_unique<Cursor>(dataCache_);
36 }
37
Cursor(const TraceDataCache * dataCache)38 DataDictTable::Cursor::Cursor(const TraceDataCache* dataCache)
39 : TableBase::Cursor(dataCache, 0, static_cast<uint32_t>(dataCache->DataDictSize()))
40 {
41 }
42
~Cursor()43 DataDictTable::Cursor::~Cursor() {}
44
Column(int col) const45 int DataDictTable::Cursor::Column(int col) const
46 {
47 DataIndex index = static_cast<DataIndex>(CurrentRow());
48 switch (col) {
49 case ID:
50 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(CurrentRow()));
51 break;
52 case STR:
53 sqlite3_result_text(context_, dataCache_->GetDataFromDict(index).c_str(), STR_DEFAULT_LEN, nullptr);
54 break;
55 default:
56 TS_LOGF("Unknown column %d", col);
57 break;
58 }
59 return SQLITE_OK;
60 }
61 } // namespace TraceStreamer
62 } // namespace SysTuning
63