Home | History | Annotate | Download | only in trace_processor
      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_QUERY_CONSTRAINTS_H_
     18 #define SRC_TRACE_PROCESSOR_QUERY_CONSTRAINTS_H_
     19 
     20 #include <sqlite3.h>
     21 
     22 #include <vector>
     23 
     24 #include "perfetto/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   using Constraint = sqlite3_index_info::sqlite3_index_constraint;
     42   using OrderBy = sqlite3_index_info::sqlite3_index_orderby;
     43 
     44   static int FreeSqliteString(char* resource);
     45 
     46   using SqliteString = base::ScopedResource<char*, FreeSqliteString, nullptr>;
     47 
     48   QueryConstraints();
     49   ~QueryConstraints();
     50   QueryConstraints(QueryConstraints&&) noexcept;
     51   QueryConstraints& operator=(QueryConstraints&&);
     52 
     53   // Two QueryConstraints with the same constraint and orderby vectors
     54   // are equal.
     55   bool operator==(const QueryConstraints& other) const;
     56 
     57   void AddConstraint(int column, unsigned char op);
     58 
     59   void AddOrderBy(int column, unsigned char desc);
     60 
     61   void ClearOrderBy() { order_by_.clear(); }
     62 
     63   // Converts the constraints and order by information to a string for
     64   // use by sqlite.
     65   SqliteString ToNewSqlite3String() const;
     66 
     67   // Deserializes the string into QueryConstraints. String given is in the form
     68   // C{# of constraints},col1,op1,col2,op2...,O{# of order by},col1,desc1...
     69   // For example C1,0,3,O2,1,0,4,1
     70   static QueryConstraints FromString(const char* idxStr);
     71 
     72   const std::vector<OrderBy>& order_by() const { return order_by_; }
     73 
     74   const std::vector<Constraint>& constraints() const { return constraints_; }
     75 
     76  private:
     77   QueryConstraints(const QueryConstraints&) = delete;
     78   QueryConstraints& operator=(const QueryConstraints&) = delete;
     79 
     80   std::vector<OrderBy> order_by_;
     81   std::vector<Constraint> constraints_;
     82 };
     83 
     84 }  // namespace trace_processor
     85 }  // namespace perfetto
     86 
     87 #endif  // SRC_TRACE_PROCESSOR_QUERY_CONSTRAINTS_H_
     88