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