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/data_layer.h" 33 #include "src/trace_processor/db/column_storage.h" 34 #include "src/trace_processor/db/column_storage_overlay.h" 35 #include "src/trace_processor/db/table.h" 36 37 namespace perfetto::trace_processor { 38 39 // Represents a table of data with named, strongly typed columns. Only used 40 // where the schema of the table is decided at runtime. 41 class RuntimeTable : public Table { 42 public: 43 using NullIntStorage = ColumnStorage<std::optional<int64_t>>; 44 using IntStorage = ColumnStorage<int64_t>; 45 using StringStorage = ColumnStorage<StringPool::Id>; 46 using NullDoubleStorage = ColumnStorage<std::optional<double>>; 47 using DoubleStorage = ColumnStorage<double>; 48 using VariantStorage = std::variant<uint32_t, 49 IntStorage, 50 NullIntStorage, 51 StringStorage, 52 DoubleStorage, 53 NullDoubleStorage>; 54 class Builder { 55 public: 56 Builder(StringPool* pool, std::vector<std::string> col_names); 57 58 base::Status AddNull(uint32_t idx); 59 base::Status AddInteger(uint32_t idx, int64_t res); 60 base::Status AddFloat(uint32_t idx, double res); 61 base::Status AddText(uint32_t idx, const char* ptr); 62 63 base::StatusOr<std::unique_ptr<RuntimeTable>> Build(uint32_t rows) &&; 64 65 private: 66 StringPool* string_pool_ = nullptr; 67 std::vector<std::string> col_names_; 68 std::vector<std::unique_ptr<VariantStorage>> storage_; 69 }; 70 71 explicit RuntimeTable(StringPool*, 72 uint32_t row_count, 73 std::vector<ColumnLegacy>, 74 std::vector<ColumnStorageOverlay>, 75 std::vector<RefPtr<column::DataLayer>> storage_layers, 76 std::vector<RefPtr<column::DataLayer>> null_layers, 77 std::vector<RefPtr<column::DataLayer>> overlay_layers); 78 ~RuntimeTable() override; 79 80 RuntimeTable(RuntimeTable&&) = default; 81 RuntimeTable& operator=(RuntimeTable&&) = default; 82 schema()83 const Table::Schema& schema() const { return schema_; } 84 85 private: 86 std::vector<std::string> col_names_; 87 std::vector<std::unique_ptr<VariantStorage>> storage_; 88 Table::Schema schema_; 89 }; 90 91 } // namespace perfetto::trace_processor 92 93 #endif // SRC_TRACE_PROCESSOR_DB_RUNTIME_TABLE_H_ 94