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 <sqlite3.h> 21 #include <string> 22 #include <vector> 23 24 #include "filter_constraints.h" 25 #include "index_map.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, 46 [](const TraceDataCache* cache) { 47 return std::unique_ptr<TableBase>(std::make_unique<T>(cache)); 48 }); 49 dataCache->AppendNewTable(tableName); 50 } 51 std::string CreateTableSql() const; 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 int Next(); 63 64 virtual int 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 int RowId(sqlite3_int64* id); 70 virtual int Filter(const FilterConstraints& fc, sqlite3_value** argv) = 0; 71 virtual int Column(int n) const = 0; 72 virtual void FilterId(unsigned char op, sqlite3_value* argv); 73 virtual void FilterEnd(); 74 public: 75 sqlite3_context* context_; 76 TableBase* table_ = nullptr; 77 78 protected: 79 const TraceDataCache* dataCache_; 80 std::unique_ptr<IndexMap> indexMap_; 81 uint32_t rowCount_; 82 }; 83 84 struct ColumnInfo { ColumnInfoColumnInfo85 ColumnInfo(const std::string& name, const std::string& type) : name_(name), type_(type) {} 86 std::string name_; 87 std::string type_; 88 }; 89 90 protected: TableBase(const TraceDataCache * dataCache)91 explicit TableBase(const TraceDataCache* dataCache) : dataCache_(dataCache), cursor_(nullptr) {} 92 93 struct EstimatedIndexInfo { 94 int64_t estimatedRows = 0; 95 double estimatedCost = 0.0; 96 bool isOrdered = false; 97 }; 98 99 static void TableRegister(sqlite3& db, TraceDataCache* cache, const std::string& tableName, TabTemplate tmplate); Update(int argc,sqlite3_value ** argv,sqlite3_int64 * pRowid)100 virtual int Update(int argc, sqlite3_value** argv, sqlite3_int64* pRowid) 101 { 102 return SQLITE_READONLY; 103 } 104 int BestIndex(sqlite3_index_info* idxInfo); 105 // needs to correspond to Cursor::Filter() 106 virtual void EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei) = 0; 107 virtual std::unique_ptr<Cursor> CreateCursor() = 0; 108 int Open(sqlite3_vtab_cursor** ppCursor); Init(int,const char * const *)109 virtual void Init(int, const char* const*) 110 { 111 return; 112 }; 113 114 public: 115 std::string name_; 116 117 protected: 118 std::vector<ColumnInfo> tableColumn_ = {}; 119 std::vector<std::string> tablePriKey_ = {}; 120 const TraceDataCache* dataCache_; 121 TraceDataCache* wdataCache_ = nullptr; 122 std::unique_ptr<Cursor> cursor_; 123 124 private: 125 uint16_t bestIndexNum_ = 0; 126 int cacheIdxNum_ = 0; 127 FilterConstraints cacheConstraint_; 128 }; 129 } // namespace TraceStreamer 130 } // namespace SysTuning 131 132 #endif // TABLE_H 133