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/instants_table.h"
18 
19 #include <string>
20 
21 #include "src/trace_processor/storage_columns.h"
22 
23 namespace perfetto {
24 namespace trace_processor {
25 
InstantsTable(sqlite3 *,const TraceStorage * storage)26 InstantsTable::InstantsTable(sqlite3*, const TraceStorage* storage)
27     : storage_(storage) {}
28 
RegisterTable(sqlite3 * db,const TraceStorage * storage)29 void InstantsTable::RegisterTable(sqlite3* db, const TraceStorage* storage) {
30   Table::Register<InstantsTable>(db, storage, "instants");
31 }
32 
CreateStorageSchema()33 StorageSchema InstantsTable::CreateStorageSchema() {
34   const auto& instants = storage_->instants();
35   return StorageSchema::Builder()
36       .AddGenericNumericColumn("id", RowIdAccessor(TableId::kInstants))
37       .AddOrderedNumericColumn("ts", &instants.timestamps())
38       .AddStringColumn("name", &instants.name_ids(), &storage_->string_pool())
39       .AddNumericColumn("value", &instants.values())
40       .AddNumericColumn("ref", &instants.refs())
41       .AddStringColumn("ref_type", &instants.types(), &GetRefTypeStringMap())
42       .AddNumericColumn("arg_set_id", &instants.arg_set_ids())
43       .Build({"name", "ts", "ref"});
44 }
45 
RowCount()46 uint32_t InstantsTable::RowCount() {
47   return static_cast<uint32_t>(storage_->instants().instant_count());
48 }
49 
BestIndex(const QueryConstraints & qc,BestIndexInfo * info)50 int InstantsTable::BestIndex(const QueryConstraints& qc, BestIndexInfo* info) {
51   info->estimated_cost =
52       static_cast<uint32_t>(storage_->instants().instant_count());
53 
54   // Only the string columns are handled by SQLite
55   info->order_by_consumed = true;
56   size_t name_index = schema().ColumnIndexFromName("name");
57   size_t ref_type_index = schema().ColumnIndexFromName("ref_type");
58   for (size_t i = 0; i < qc.constraints().size(); i++) {
59     info->omit[i] =
60         qc.constraints()[i].iColumn != static_cast<int>(name_index) &&
61         qc.constraints()[i].iColumn != static_cast<int>(ref_type_index);
62   }
63 
64   return SQLITE_OK;
65 }
66 }  // namespace trace_processor
67 }  // namespace perfetto
68