1 2 /* 3 * Copyright (c) 2021 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef SAPN_JOIN_H 17 #define SAPN_JOIN_H 18 #include "table_base.h" 19 #include "trace_data_cache.h" 20 namespace SysTuning { 21 namespace TraceStreamer { 22 struct TableParse { 23 std::string name; 24 std::string partitionCol; 25 }; 26 27 struct TableDesc { 28 std::string name; 29 std::string partition; 30 std::vector<TableBase::ColumnInfo> cols; 31 int tsIdx; 32 int durIdx; 33 int partitionIdx; 34 }; 35 36 struct TableColumnInfo { 37 TableDesc* tableDesc; 38 int colIdx; 39 }; 40 41 enum PartitionState { 42 TS_REAL, 43 TS_PARTITION, 44 TS_MISSING, 45 TS_EOF, 46 }; 47 48 class SpanJoin : public TableBase { 49 public: 50 SpanJoin(const TraceDataCache*); ~SpanJoin()51 ~SpanJoin() override{}; 52 void Parse(const std::string& tablePartition, TableParse& tableParse); 53 std::vector<std::string> TableNameSplitToVec(std::string& str, const std::string& pat); 54 void GetTableField(TableParse& tableParse, TableDesc& tableDesc); 55 void GetColumns(const TraceDataCache* dataCache, 56 const std::string& tableName, 57 std::vector<TableBase::ColumnInfo>& columns); 58 void CreateCols(TableDesc& tableDesc, std::vector<ColumnInfo>& cols); 59 bool IsTsOrDurCol(const std::string& name); 60 bool DeduplicationForColumn(const std::string& name, std::vector<ColumnInfo>& cols); EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)61 void EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei) override{}; 62 void Init(int argc, const char* const* argv) override; 63 std::unique_ptr<TableBase::Cursor> CreateCursor() override; 64 65 class CaclSpan { 66 public: 67 CaclSpan(TableBase* tableBase, const TableDesc* tableDesc, sqlite3* db); 68 virtual ~CaclSpan(); 69 static std::string GetMergeColumns(std::vector<std::string>& columns); 70 int InitQuerySql(sqlite3_value** argv); 71 bool IsQueryNext(); 72 bool GetCursorNext(); 73 bool GetNextState(); 74 bool SearchNextslice(); 75 void Next(); 76 std::string GetSqlQuery(); 77 void setResult(sqlite3_context* context, size_t index) const; 78 int64_t GetPatitonForMiss(); 79 80 public: 81 bool isEof_ = false; 82 int64_t ts_ = 0; 83 int64_t endTs_ = 0; 84 PartitionState partitionState_ = PartitionState::TS_MISSING; 85 int partition_ = 0; 86 int missPartitionStart_ = 0; 87 int missPartitionEnd_ = 0; 88 std::string sqlQuery_; 89 sqlite3_stmt* stmt_; 90 const TableDesc* desc_ = nullptr; 91 sqlite3* db_ = nullptr; 92 SpanJoin* table_ = nullptr; 93 }; 94 95 class Cursor : public TableBase::Cursor { 96 public: 97 explicit Cursor(const TraceDataCache* dataCache, SpanJoin* table); 98 int Filter(const FilterConstraints& fc, sqlite3_value** argv) override; 99 int Column(int column) const override; Next()100 int Next() override 101 { 102 queryNext_->Next(); 103 auto status = IsFindSpan(); 104 if (!status) { 105 return SQLITE_ERROR; 106 } 107 return SQLITE_OK; 108 } Eof()109 int Eof() override 110 { 111 return tableFirst_.isEof_ || tableSecond_.isEof_; 112 } 113 114 private: 115 bool IsFindSpan(); 116 bool CaclOverLap(); 117 CaclSpan* FindQueryResult(); 118 CaclSpan tableFirst_; 119 CaclSpan tableSecond_; 120 CaclSpan* queryNext_ = nullptr; 121 SpanJoin* table_; 122 }; 123 124 public: 125 bool isSamepartitioning_ = false; 126 127 private: 128 TableDesc tableFirstDesc_; 129 TableDesc tableSecondDesc_; 130 std::unordered_map<int, TableColumnInfo> mTableColumnInfo_; 131 }; 132 133 } // namespace TraceStreamer 134 } // namespace SysTuning 135 #endif // SAPN_JOIN_H 136