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