1 /*
2 * Copyright (C) 2022 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_UTIL_SQL_MODULES_H_
18 #define SRC_TRACE_PROCESSOR_UTIL_SQL_MODULES_H_
19
20 #include <string>
21
22 #include "perfetto/ext/base/flat_hash_map.h"
23 #include "perfetto/ext/base/string_splitter.h"
24 #include "perfetto/ext/base/string_view.h"
25 #include "perfetto/trace_processor/basic_types.h"
26
27 namespace perfetto {
28 namespace trace_processor {
29 namespace sql_modules {
30
31 using NameToModule =
32 base::FlatHashMap<std::string,
33 std::vector<std::pair<std::string, std::string>>>;
34
35 // Map from include key to sql file. Include key is the string used in INCLUDE
36 // function.
37 struct RegisteredModule {
38 struct ModuleFile {
39 std::string sql;
40 bool included;
41 };
42 base::FlatHashMap<std::string, ModuleFile> include_key_to_file;
43 };
44
ReplaceSlashWithDot(std::string str)45 inline std::string ReplaceSlashWithDot(std::string str) {
46 size_t found = str.find('/');
47 while (found != std::string::npos) {
48 str.replace(found, 1, ".");
49 found = str.find('/');
50 }
51 return str;
52 }
53
GetIncludeKey(std::string path)54 inline std::string GetIncludeKey(std::string path) {
55 base::StringView path_view(path);
56 auto path_no_extension = path_view.substr(0, path_view.rfind('.'));
57 return ReplaceSlashWithDot(path_no_extension.ToStdString());
58 }
59
GetModuleName(std::string str)60 inline std::string GetModuleName(std::string str) {
61 size_t found = str.find('.');
62 if (found == std::string::npos) {
63 return str;
64 }
65 return str.substr(0, found);
66 }
67
68 } // namespace sql_modules
69 } // namespace trace_processor
70 } // namespace perfetto
71 #endif // SRC_TRACE_PROCESSOR_UTIL_SQL_MODULES_H_
72