1 /* 2 * Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 #ifndef SRC_TRACE_PROCESSOR_STORAGE_SCHEMA_H_ 16 #define SRC_TRACE_PROCESSOR_STORAGE_SCHEMA_H_ 17 18 #include <algorithm> 19 #include <deque> 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "src/trace_processor/filtered_row_index.h" 26 #include "src/trace_processor/sqlite_utils.h" 27 #include "src/trace_processor/storage_columns.h" 28 #include "src/trace_processor/table.h" 29 #include "src/trace_processor/trace_storage.h" 30 31 namespace perfetto { 32 namespace trace_processor { 33 34 // Defines the schema for a table which is backed by concrete storage (i.e. does 35 // not generate data on the fly). 36 // Used by all tables which are backed by data in TraceStorage. 37 class StorageSchema { 38 public: 39 using Columns = std::vector<std::unique_ptr<StorageColumn>>; 40 41 // Builder class for StorageSchema. 42 class Builder { 43 public: 44 template <class T, class... Args> 45 Builder& AddColumn(std::string column_name, Args&&... args) { 46 columns_.emplace_back(new T(column_name, std::forward<Args>(args)...)); 47 return *this; 48 } 49 50 template <class NumericType> 51 Builder& AddNumericColumn( 52 std::string column_name, 53 const std::deque<NumericType>* vals, 54 const std::deque<std::vector<uint32_t>>* index = nullptr) { 55 NumericDequeAccessor<NumericType> accessor(vals, index, 56 false /* has_ordering */); 57 return AddGenericNumericColumn(column_name, accessor); 58 } 59 60 template <class NumericType> 61 Builder& AddOrderedNumericColumn(std::string column_name, 62 const std::deque<NumericType>* vals) { 63 NumericDequeAccessor<NumericType> accessor(vals, nullptr, 64 true /* has_ordering */); 65 return AddGenericNumericColumn(column_name, accessor); 66 } 67 68 template <class Accessor> 69 Builder& AddGenericNumericColumn(std::string column_name, 70 Accessor accessor) { 71 columns_.emplace_back(new NumericColumn<decltype(accessor)>( 72 column_name, false /* hidden */, accessor)); 73 return *this; 74 } 75 76 template <class Id> 77 Builder& AddStringColumn(std::string column_name, 78 const std::deque<Id>* ids, 79 const std::vector<const char*>* string_map) { 80 StringVectorAccessor<Id> accessor(ids, string_map); 81 columns_.emplace_back( 82 new StringColumn<decltype(accessor)>(column_name, accessor)); 83 return *this; 84 } 85 86 Builder& AddStringColumn(std::string column_name, 87 const std::deque<StringPool::Id>* ids, 88 const StringPool* string_pool) { 89 StringPoolAccessor accessor(ids, string_pool); 90 columns_.emplace_back( 91 new StringColumn<StringPoolAccessor>(column_name, accessor)); 92 return *this; 93 } 94 95 StorageSchema Build(std::vector<std::string> primary_keys) { 96 return StorageSchema(std::move(columns_), std::move(primary_keys)); 97 } 98 99 private: 100 Columns columns_; 101 }; 102 103 StorageSchema(); 104 StorageSchema(Columns columns, std::vector<std::string> primary_keys); 105 106 Table::Schema ToTableSchema(); 107 108 size_t ColumnIndexFromName(const std::string& name) const; 109 110 const StorageColumn& GetColumn(size_t idx) const { return *(columns_[idx]); } 111 112 Columns* mutable_columns() { return &columns_; } 113 114 private: 115 friend class Builder; 116 117 Columns columns_; 118 std::vector<std::string> primary_keys_; 119 }; 120 121 } // namespace trace_processor 122 } // namespace perfetto 123 124 #endif // SRC_TRACE_PROCESSOR_STORAGE_SCHEMA_H_ 125