• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_SQLITE_QUERY_CONSTRAINTS_H_
18 #define SRC_TRACE_PROCESSOR_SQLITE_QUERY_CONSTRAINTS_H_
19 
20 #include <sqlite3.h>
21 
22 #include <vector>
23 
24 #include "perfetto/ext/base/scoped_file.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
29 // This class stores the constraints (including the order-by information) for
30 // a query on a sqlite3 virtual table and handles their de/serialization into
31 // strings.
32 // This is because the constraint columns and the order-by clauses are passed
33 // to the xBestIndex method but the constraint values are available only in the
34 // xFilter method. Unfortunately sqlite vtable API don't give any hint about
35 // the validity of the constraints (i.e. constraints passed to xBestIndex can
36 // be used by future xFilter calls in the far future). The only mechanism
37 // offered by sqlite is the idxStr string which is returned by the vtable
38 // in the xBestIndex call and passed to each corresponding xFilter call.
39 class QueryConstraints {
40  public:
41   struct Constraint {
42     // Column this constraint refers to.
43     int column;
44 
45     // SQLite op for the constraint.
46     int op;
47 
48     // The original index of this constraint in the aConstraint array.
49     // Used internally by SqliteTable for xBestIndex - this should not be
50     // read or modified by subclasses of SqliteTable.
51     int a_constraint_idx;
52   };
53 
54   using OrderBy = sqlite3_index_info::sqlite3_index_orderby;
55 
56   static int FreeSqliteString(char* resource);
57 
58   using SqliteString = base::ScopedResource<char*, FreeSqliteString, nullptr>;
59 
60   QueryConstraints();
61   ~QueryConstraints();
62   QueryConstraints(QueryConstraints&&) noexcept;
63   QueryConstraints& operator=(QueryConstraints&&);
64 
65   // Two QueryConstraints with the same constraint and orderby vectors
66   // are equal.
67   bool operator==(const QueryConstraints& other) const;
68 
69   void AddConstraint(int column, unsigned char op, int aconstraint_idx);
70 
71   void AddOrderBy(int column, unsigned char desc);
72 
ClearOrderBy()73   void ClearOrderBy() { order_by_.clear(); }
74 
75   // Converts the constraints and order by information to a string for
76   // use by sqlite.
77   SqliteString ToNewSqlite3String() const;
78 
79   // Deserializes the string into QueryConstraints. String given is in the form
80   // C{# of constraints},col1,op1,col2,op2...,O{# of order by},col1,desc1...
81   // For example C1,0,3,O2,1,0,4,1
82   static QueryConstraints FromString(const char* idxStr);
83 
order_by()84   const std::vector<OrderBy>& order_by() const { return order_by_; }
85 
constraints()86   const std::vector<Constraint>& constraints() const { return constraints_; }
87 
mutable_order_by()88   std::vector<OrderBy>* mutable_order_by() { return &order_by_; }
89 
mutable_constraints()90   std::vector<Constraint>* mutable_constraints() { return &constraints_; }
91 
92  private:
93   QueryConstraints(const QueryConstraints&) = delete;
94   QueryConstraints& operator=(const QueryConstraints&) = delete;
95 
96   std::vector<OrderBy> order_by_;
97   std::vector<Constraint> constraints_;
98 };
99 
100 }  // namespace trace_processor
101 }  // namespace perfetto
102 
103 #endif  // SRC_TRACE_PROCESSOR_SQLITE_QUERY_CONSTRAINTS_H_
104