• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TABLE_H
17 #define TABLE_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "filter_constraints.h"
24 #include "index_map.h"
25 #include "sqlite3.h"
26 #include "trace_data_cache.h"
27 
28 #define UNUSED(expr)             \
29     do {                         \
30         static_cast<void>(expr); \
31     } while (0)
32 namespace SysTuning {
33 namespace TraceStreamer {
34 class TableBase;
35 using TabTemplate = std::unique_ptr<TableBase> (*)(const TraceDataCache* dataCache);
36 class TableBase : public sqlite3_vtab {
37 public:
38     virtual ~TableBase();
39     TableBase(const TableBase&) = delete;
40     TableBase& operator=(const TableBase&) = delete;
41 
42     template <typename T>
TableDeclare(sqlite3 & db,TraceDataCache * dataCache,const std::string & tableName)43     static void TableDeclare(sqlite3& db, TraceDataCache* dataCache, const std::string& tableName)
44     {
45         TableRegister(db, dataCache, tableName, [](const TraceDataCache* cache) {
46             return std::unique_ptr<TableBase>(std::make_unique<T>(cache));
47         });
48         dataCache->AppendNewTable(tableName);
49     }
50     std::string CreateTableSql() const;
51     virtual bool CanFilterId(const char op, size_t& rowCount);
52 
53     class Cursor : public sqlite3_vtab_cursor {
54     public:
55         Cursor(const TraceDataCache* dataCache, TableBase* table, uint32_t rowCount);
56         virtual ~Cursor();
Reset()57         virtual void Reset()
58         {
59             indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
60         }
61 
62         virtual int32_t Next();
63 
64         virtual int32_t Eof();
65 
66         virtual uint32_t CurrentRow() const;
67         virtual void FilterTS(unsigned char op, sqlite3_value* argv, const std::deque<InternalTime>& times);
68 
69         virtual int32_t RowId(sqlite3_int64* id);
70         virtual int32_t Filter(const FilterConstraints& fc, sqlite3_value** argv) = 0;
71         virtual int32_t Column(int32_t n) const = 0;
72         virtual void FilterId(unsigned char op, sqlite3_value* argv);
73         virtual void FilterEnd();
74 
75     public:
76         sqlite3_context* context_;
77         TableBase* table_ = nullptr;
78 
79     protected:
80         const TraceDataCache* dataCache_;
81         std::unique_ptr<IndexMap> indexMap_;
82         uint32_t rowCount_;
83     };
84 
85     struct ColumnInfo {
ColumnInfoColumnInfo86         ColumnInfo(const std::string& name, const std::string& type) : name_(name), type_(type) {}
87         std::string name_;
88         std::string type_;
89     };
90 
91 protected:
TableBase(const TraceDataCache * dataCache)92     explicit TableBase(const TraceDataCache* dataCache) : dataCache_(dataCache), cursor_(nullptr) {}
93 
94     struct EstimatedIndexInfo {
95         int64_t estimatedRows = 0;
96         double estimatedCost = 0.0;
97         bool isOrdered = false;
98     };
99 
100     static void TableRegister(sqlite3& db, TraceDataCache* cache, const std::string& tableName, TabTemplate tmplate);
Update(int32_t argc,sqlite3_value ** argv,sqlite3_int64 * pRowid)101     virtual int32_t Update(int32_t argc, sqlite3_value** argv, sqlite3_int64* pRowid)
102     {
103         return SQLITE_READONLY;
104     }
105     int32_t BestIndex(sqlite3_index_info* idxInfo);
106     // needs to correspond to Cursor::Filter()
107     virtual void EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei) = 0;
108     virtual std::unique_ptr<Cursor> CreateCursor() = 0;
109     int32_t Open(sqlite3_vtab_cursor** ppCursor);
Init(int32_t,const char * const *)110     virtual void Init(int32_t, const char* const*)
111     {
112         return;
113     };
114 
115 public:
116     std::string name_;
117 
118 protected:
119     std::vector<ColumnInfo> tableColumn_ = {};
120     std::vector<std::string> tablePriKey_ = {};
121     const TraceDataCache* dataCache_;
122     TraceDataCache* wdataCache_ = nullptr;
123     std::unique_ptr<Cursor> cursor_;
124 
125 private:
126     uint16_t bestIndexNum_ = 0;
127     int32_t cacheIdxNum_ = 0;
128     FilterConstraints cacheConstraint_;
129 };
130 } // namespace TraceStreamer
131 } // namespace SysTuning
132 
133 #endif // TABLE_H
134