1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  *      http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 #ifndef SRC_TRACE_PROCESSOR_STORAGE_TABLE_H_
16 #define SRC_TRACE_PROCESSOR_STORAGE_TABLE_H_
17 
18 #include <set>
19 
20 #include "src/trace_processor/row_iterators.h"
21 #include "src/trace_processor/storage_columns.h"
22 #include "src/trace_processor/storage_schema.h"
23 #include "src/trace_processor/table.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 // Base class for all table implementations which are backed by some data
29 // storage.
30 class StorageTable : public Table {
31  public:
32   // A cursor which abstracts common patterns found in storage backed tables. It
33   // takes a strategy to iterate through rows and a column reporter for each
34   // column to implement the Cursor interface.
35   class Cursor final : public Table::Cursor {
36    public:
37     Cursor(StorageTable* table);
38 
39     // Implementation of Table::Cursor.
40     int Filter(const QueryConstraints& qc, sqlite3_value** argv) override;
41     int Next() override;
42     int Eof() override;
43     int Column(sqlite3_context*, int N) override;
44 
45    private:
46     std::unique_ptr<RowIterator> iterator_;
47     std::vector<std::unique_ptr<StorageColumn>>* columns_ = nullptr;
48     StorageTable* table_ = nullptr;
49   };
50 
51   StorageTable();
52   virtual ~StorageTable() override;
53 
54   // Table implementation.
55   base::Optional<Table::Schema> Init(int, const char* const*) override final;
56   std::unique_ptr<Table::Cursor> CreateCursor() override;
57 
58   // Required methods for subclasses to implement.
59   virtual StorageSchema CreateStorageSchema() = 0;
60   virtual uint32_t RowCount() = 0;
61 
62  protected:
schema()63   const StorageSchema& schema() const { return schema_; }
64 
65   bool HasEqConstraint(const QueryConstraints&, const std::string& col_name);
66 
67  private:
68   // Creates a row iterator which is optimized for a generic storage schema
69   // (i.e. it does not make assumptions about values of columns).
70   std::unique_ptr<RowIterator> CreateBestRowIterator(const QueryConstraints& qc,
71                                                      sqlite3_value** argv);
72 
73   FilteredRowIndex CreateRangeIterator(
74       const std::vector<QueryConstraints::Constraint>& cs,
75       sqlite3_value** argv);
76 
77   std::pair<bool, bool> IsOrdered(
78       const std::vector<QueryConstraints::OrderBy>& obs);
79 
80   std::vector<QueryConstraints::OrderBy> RemoveRedundantOrderBy(
81       const std::vector<QueryConstraints::Constraint>& cs,
82       const std::vector<QueryConstraints::OrderBy>& obs);
83 
84   std::vector<uint32_t> CreateSortedIndexVector(
85       FilteredRowIndex index,
86       const std::vector<QueryConstraints::OrderBy>& obs);
87 
88   StorageSchema schema_;
89 };
90 
91 }  // namespace trace_processor
92 }  // namespace perfetto
93 
94 #endif  // SRC_TRACE_PROCESSOR_STORAGE_TABLE_H_
95