• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       FilterColumn(c, *columns_[c.col_idx], &rm);
46     }
47     return rm;
48   }
49 
50   // Enables QueryExecutor::Filter on Table columns.
51   static RowMap FilterLegacy(const Table*, const std::vector<Constraint>&);
52 
53   // Enables QueryExecutor::Sort on Table columns.
54   static void SortLegacy(const Table*,
55                          const std::vector<Order>&,
56                          std::vector<uint32_t>&);
57 
58   // Used only in unittests. Exposes private function.
59   static void BoundedColumnFilterForTesting(const Constraint&,
60                                             const column::DataLayerChain&,
61                                             RowMap*);
62 
63   // Used only in unittests. Exposes private function.
64   static void IndexedColumnFilterForTesting(const Constraint&,
65                                             const column::DataLayerChain&,
66                                             RowMap*);
67 
68  private:
69   // Updates RowMap with result of filtering single column using the Constraint.
70   static void FilterColumn(const Constraint&,
71                            const column::DataLayerChain&,
72                            RowMap*);
73 
74   // Filters the column using Range algorithm - tries to find the smallest Range
75   // to filter the storage with.
76   static void LinearSearch(const Constraint&,
77                            const column::DataLayerChain&,
78                            RowMap*);
79 
80   // Filters the column using Index algorithm - finds the indices to filter the
81   // storage with.
82   static void IndexSearch(const Constraint&,
83                           const column::DataLayerChain&,
84                           RowMap*);
85 
86   std::vector<column::DataLayerChain*> columns_;
87 
88   // Number of rows in the outmost overlay.
89   uint32_t row_count_ = 0;
90 };
91 
92 }  // namespace perfetto::trace_processor
93 
94 #endif  // SRC_TRACE_PROCESSOR_DB_QUERY_EXECUTOR_H_
95