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 #include "src/trace_processor/string_table.h"
18 
19 #include <sqlite3.h>
20 #include <string.h>
21 
22 #include <algorithm>
23 #include <bitset>
24 #include <numeric>
25 
26 #include "src/trace_processor/sqlite_utils.h"
27 #include "src/trace_processor/trace_storage.h"
28 
29 namespace perfetto {
30 namespace trace_processor {
31 
StringTable(sqlite3 *,const TraceStorage * storage)32 StringTable::StringTable(sqlite3*, const TraceStorage* storage)
33     : storage_(storage) {}
34 
RegisterTable(sqlite3 * db,const TraceStorage * storage)35 void StringTable::RegisterTable(sqlite3* db, const TraceStorage* storage) {
36   Table::Register<StringTable>(db, storage, "strings");
37 }
38 
Init(int,const char * const *)39 base::Optional<Table::Schema> StringTable::Init(int, const char* const*) {
40   return Schema(
41       {
42           Table::Column(Column::kStringId, "id", ColumnType::kUint),
43           Table::Column(Column::kString, "str", ColumnType::kString),
44       },
45       {Column::kStringId});
46 }
47 
CreateCursor()48 std::unique_ptr<Table::Cursor> StringTable::CreateCursor() {
49   return std::unique_ptr<Table::Cursor>(new Cursor(this));
50 }
51 
BestIndex(const QueryConstraints &,BestIndexInfo * info)52 int StringTable::BestIndex(const QueryConstraints&, BestIndexInfo* info) {
53   info->order_by_consumed = false;  // Delegate sorting to SQLite.
54   info->estimated_cost = static_cast<uint32_t>(storage_->string_count());
55   return SQLITE_OK;
56 }
57 
Cursor(StringTable * table)58 StringTable::Cursor::Cursor(StringTable* table)
59     : Table::Cursor(table), storage_(table->storage_), table_(table) {}
60 
61 StringTable::Cursor::~Cursor() = default;
62 
Filter(const QueryConstraints &,sqlite3_value **)63 int StringTable::Cursor::Filter(const QueryConstraints&, sqlite3_value**) {
64   *this = Cursor(table_);
65   num_rows_ = storage_->string_count();
66   return SQLITE_OK;
67 }
68 
Next()69 int StringTable::Cursor::Next() {
70   row_++;
71   return SQLITE_OK;
72 }
73 
Eof()74 int StringTable::Cursor::Eof() {
75   return row_ >= num_rows_;
76 }
77 
Column(sqlite3_context * context,int col)78 int StringTable::Cursor::Column(sqlite3_context* context, int col) {
79   StringId string_id = static_cast<StringId>(row_);
80   switch (col) {
81     case Column::kStringId:
82       sqlite3_result_int64(context, static_cast<sqlite3_int64>(row_));
83       break;
84     case Column::kString:
85       sqlite3_result_text(context, storage_->GetString(string_id).c_str(), -1,
86                           sqlite_utils::kSqliteStatic);
87       break;
88   }
89   return SQLITE_OK;
90 }
91 
92 }  // namespace trace_processor
93 }  // namespace perfetto
94