xref: /aosp_15_r20/external/perfetto/src/trace_processor/db/query_executor.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TRACE_PROCESSOR_DB_QUERY_EXECUTOR_H_
17 #define SRC_TRACE_PROCESSOR_DB_QUERY_EXECUTOR_H_
18 
19 #include <cstdint>
20 #include <utility>
21 #include <vector>
22 
23 #include "src/trace_processor/containers/row_map.h"
24 #include "src/trace_processor/db/column.h"
25 #include "src/trace_processor/db/column/data_layer.h"
26 #include "src/trace_processor/db/column/types.h"
27 
28 namespace perfetto::trace_processor {
29 
30 // Responsible for executing filtering/sorting operations on a single Table.
31 // TODO(b/283763282): Introduce sorting.
32 class QueryExecutor {
33  public:
34   static constexpr uint32_t kMaxOverlayCount = 8;
35 
36   // |row_count| is the size of the last overlay.
QueryExecutor(const std::vector<column::DataLayerChain * > & columns,uint32_t row_count)37   QueryExecutor(const std::vector<column::DataLayerChain*>& columns,
38                 uint32_t row_count)
39       : columns_(columns), row_count_(row_count) {}
40 
41   // Apply all the constraints on the data and return the filtered RowMap.
Filter(const std::vector<Constraint> & cs)42   RowMap Filter(const std::vector<Constraint>& cs) {
43     RowMap rm(0, row_count_);
44     for (const auto& c : cs) {
45       ApplyConstraint(c, *columns_[c.col_idx], &rm);
46     }
47     return rm;
48   }
49 
50   // Enables QueryExecutor::Sort on Table columns.
51   static void SortLegacy(const Table*,
52                          const std::vector<Order>&,
53                          std::vector<uint32_t>&);
54 
55   // Used only in unittests. Exposes private function.
56   static void BoundedColumnFilterForTesting(const Constraint&,
57                                             const column::DataLayerChain&,
58                                             RowMap*);
59 
60   // Used only in unittests. Exposes private function.
61   static void IndexedColumnFilterForTesting(const Constraint&,
62                                             const column::DataLayerChain&,
63                                             RowMap*);
64 
65   // Updates RowMap with result of filtering single column using the Constraint.
66   static void ApplyConstraint(const Constraint&,
67                               const column::DataLayerChain&,
68                               RowMap*);
69 
70  private:
71   // Filters the column using Range algorithm - tries to find the smallest Range
72   // to filter the storage with.
73   static void LinearSearch(const Constraint&,
74                            const column::DataLayerChain&,
75                            RowMap*);
76 
77   // Filters the column using Index algorithm - finds the indices to filter the
78   // storage with.
79   static void IndexSearch(const Constraint&,
80                           const column::DataLayerChain&,
81                           RowMap*);
82 
83   std::vector<column::DataLayerChain*> columns_;
84 
85   // Number of rows in the outmost overlay.
86   uint32_t row_count_ = 0;
87 };
88 
89 }  // namespace perfetto::trace_processor
90 
91 #endif  // SRC_TRACE_PROCESSOR_DB_QUERY_EXECUTOR_H_
92