1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 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 17 #ifndef SRC_TRACE_PROCESSOR_DB_RUNTIME_TABLE_H_ 18 #define SRC_TRACE_PROCESSOR_DB_RUNTIME_TABLE_H_ 19 20 #include <cstdint> 21 #include <memory> 22 #include <optional> 23 #include <string> 24 #include <variant> 25 #include <vector> 26 27 #include "perfetto/base/status.h" 28 #include "perfetto/ext/base/status_or.h" 29 #include "perfetto/trace_processor/ref_counted.h" 30 #include "src/trace_processor/containers/string_pool.h" 31 #include "src/trace_processor/db/column.h" 32 #include "src/trace_processor/db/column/overlay_layer.h" 33 #include "src/trace_processor/db/column/storage_layer.h" 34 #include "src/trace_processor/db/column_storage.h" 35 #include "src/trace_processor/db/column_storage_overlay.h" 36 #include "src/trace_processor/db/table.h" 37 38 namespace perfetto::trace_processor { 39 40 // Represents a table of data with named, strongly typed columns. Only used 41 // where the schema of the table is decided at runtime. 42 class RuntimeTable : public Table { 43 public: 44 using NullIntStorage = ColumnStorage<std::optional<int64_t>>; 45 using IntStorage = ColumnStorage<int64_t>; 46 using StringStorage = ColumnStorage<StringPool::Id>; 47 using NullDoubleStorage = ColumnStorage<std::optional<double>>; 48 using DoubleStorage = ColumnStorage<double>; 49 using VariantStorage = std::variant<uint32_t, 50 IntStorage, 51 NullIntStorage, 52 StringStorage, 53 DoubleStorage, 54 NullDoubleStorage>; 55 enum BuilderColumnType { 56 kNull, 57 kInt, 58 kNullInt, 59 kString, 60 kDouble, 61 kNullDouble 62 }; 63 64 class Builder { 65 public: 66 Builder(StringPool* pool, const std::vector<std::string>& col_names); 67 Builder(StringPool* pool, 68 const std::vector<std::string>& col_names, 69 const std::vector<BuilderColumnType>& col_types); 70 71 base::Status AddNull(uint32_t idx); 72 base::Status AddInteger(uint32_t idx, int64_t res); 73 base::Status AddFloat(uint32_t idx, double res); 74 base::Status AddText(uint32_t idx, const char* ptr); 75 base::Status AddIntegers(uint32_t idx, int64_t res, uint32_t count); 76 base::Status AddFloats(uint32_t idx, double res, uint32_t count); 77 base::Status AddTexts(uint32_t idx, const char* ptr, uint32_t count); 78 base::Status AddNulls(uint32_t idx, uint32_t count); 79 AddNonNullIntegerUnchecked(uint32_t idx,int64_t res)80 void AddNonNullIntegerUnchecked(uint32_t idx, int64_t res) { 81 std::get<IntStorage>(*storage_[idx]).Append(res); 82 } 83 void AddNonNullIntegersUnchecked(uint32_t idx, const std::vector<int64_t>&); 84 void AddNullIntegersUnchecked(uint32_t idx, const std::vector<int64_t>&); 85 void AddNonNullDoublesUnchecked(uint32_t idx, const std::vector<double>&); 86 void AddNullDoublesUnchecked(uint32_t idx, const std::vector<double>&); 87 88 base::StatusOr<std::unique_ptr<RuntimeTable>> Build(uint32_t rows) &&; 89 90 private: 91 StringPool* string_pool_ = nullptr; 92 std::vector<std::string> col_names_; 93 std::vector<std::unique_ptr<VariantStorage>> storage_; 94 }; 95 96 explicit RuntimeTable( 97 StringPool*, 98 uint32_t row_count, 99 std::vector<ColumnLegacy>, 100 std::vector<ColumnStorageOverlay>, 101 std::vector<RefPtr<column::StorageLayer>> storage_layers, 102 std::vector<RefPtr<column::OverlayLayer>> null_layers, 103 std::vector<RefPtr<column::OverlayLayer>> overlay_layers); 104 ~RuntimeTable() override; 105 106 RuntimeTable(RuntimeTable&&) = default; 107 RuntimeTable& operator=(RuntimeTable&&) = default; 108 schema()109 const Table::Schema& schema() const { return schema_; } 110 111 private: 112 std::vector<std::string> col_names_; 113 std::vector<std::unique_ptr<VariantStorage>> storage_; 114 Table::Schema schema_; 115 }; 116 117 } // namespace perfetto::trace_processor 118 119 #endif // SRC_TRACE_PROCESSOR_DB_RUNTIME_TABLE_H_ 120