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_STRING_TABLE_H_
18 #define SRC_TRACE_PROCESSOR_STRING_TABLE_H_
19 
20 #include <limits>
21 #include <memory>
22 
23 #include "src/trace_processor/table.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 class QueryConstraints;
29 class TraceStorage;
30 
31 // A virtual table that allows to list and query all the strings in the db.
32 class StringTable : public Table {
33  public:
34   enum Column {
35     kStringId = 0,
36     kString = 1,
37   };
38   // Implementation of the SQLite cursor interface.
39   class Cursor : public Table::Cursor {
40    public:
41     Cursor(StringTable*);
42     ~Cursor() override;
43 
44     // Implementation of Table::Cursor.
45     int Filter(const QueryConstraints&, sqlite3_value**) override;
46     int Next() override;
47     int Eof() override;
48     int Column(sqlite3_context*, int N) override;
49 
50    private:
51     Cursor(Cursor&) = delete;
52     Cursor& operator=(const Cursor&) = delete;
53 
54     Cursor(Cursor&&) noexcept = default;
55     Cursor& operator=(Cursor&&) = default;
56 
57     size_t row_ = 0;
58     size_t num_rows_ = 0;
59 
60     const TraceStorage* storage_ = nullptr;
61     StringTable* table_ = nullptr;
62   };
63 
64   StringTable(sqlite3*, const TraceStorage* storage);
65 
66   static void RegisterTable(sqlite3* db, const TraceStorage* storage);
67 
68   // Table implementation.
69   base::Optional<Table::Schema> Init(int, const char* const*) override;
70   std::unique_ptr<Table::Cursor> CreateCursor() override;
71   int BestIndex(const QueryConstraints&, BestIndexInfo*) override;
72 
73  private:
74   const TraceStorage* const storage_;
75 };
76 
77 }  // namespace trace_processor
78 }  // namespace perfetto
79 
80 #endif  // SRC_TRACE_PROCESSOR_STRING_TABLE_H_
81