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 "slice_table.h"
17 #include <cmath>
18
19 namespace SysTuning {
20 namespace TraceStreamer {
21 namespace {
22 enum Index { TS = 0, ENDTS = 1, ST = 2, ET = 3, VALUE = 4, SLICE_ID = 5 };
23 }
SliceTable(const TraceDataCache * dataCache)24 SliceTable::SliceTable(const TraceDataCache* dataCache) : TableBase(dataCache)
25 {
26 tableColumn_.push_back(TableBase::ColumnInfo("start_ts", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("end_ts", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("start", "REAL"));
29 tableColumn_.push_back(TableBase::ColumnInfo("end", "REAL"));
30 tableColumn_.push_back(TableBase::ColumnInfo("value", "DOUBLE"));
31 tableColumn_.push_back(TableBase::ColumnInfo("slice_id", "INTEGER"));
32 tablePriKey_.push_back("start_ts");
33 tablePriKey_.push_back("slice_id");
34 }
35
~SliceTable()36 SliceTable::~SliceTable() {}
37
CreateCursor()38 std::unique_ptr<TableBase::Cursor> SliceTable::CreateCursor()
39 {
40 return std::make_unique<Cursor>(dataCache_, this);
41 }
42
Cursor(const TraceDataCache * dataCache,TableBase * table)43 SliceTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
44 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstSliceData().Size())),
45 sliceDataObj_(dataCache->GetConstSliceData())
46 {
47 }
48
~Cursor()49 SliceTable::Cursor::~Cursor() {}
50
Column(int32_t column) const51 int32_t SliceTable::Cursor::Column(int32_t column) const
52 {
53 switch (column) {
54 case TS: {
55 sqlite3_result_int64(context_, static_cast<int64_t>(sliceDataObj_.TimeStamp()[CurrentRow()]));
56 break;
57 }
58 case ENDTS: {
59 sqlite3_result_int64(context_, static_cast<int64_t>(sliceDataObj_.EndTs()[CurrentRow()]));
60 break;
61 }
62 case ST: {
63 sqlite3_result_text(context_, sliceDataObj_.StartTime()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
64 break;
65 }
66 case ET: {
67 sqlite3_result_text(context_, sliceDataObj_.EndTime()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
68 break;
69 }
70 case VALUE: {
71 sqlite3_result_double(context_, static_cast<double>(sliceDataObj_.Value()[CurrentRow()]));
72 break;
73 }
74 case SLICE_ID: {
75 sqlite3_result_int64(context_, static_cast<int64_t>(sliceDataObj_.SliceId()[CurrentRow()]));
76 break;
77 }
78 default:
79 TS_LOGF("Unregistered column : %d", column);
80 break;
81 }
82 return SQLITE_OK;
83 }
84 } // namespace TraceStreamer
85 } // namespace SysTuning
86