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
26 namespace perfetto {
27 namespace trace_processor {
28 namespace sql_modules {
29
30 using NameToModule =
31 base::FlatHashMap<std::string,
32 std::vector<std::pair<std::string, std::string>>>;
33
34 // Map from import key to sql file. Import key is the string used in IMPORT
35 // function.
36 struct RegisteredModule {
37 struct ModuleFile {
38 std::string sql;
39 bool imported;
40 };
41 base::FlatHashMap<std::string, ModuleFile> import_key_to_file;
42 };
43
ReplaceSlashWithDot(std::string str)44 inline std::string ReplaceSlashWithDot(std::string str) {
45 size_t found = str.find('/');
46 while (found != std::string::npos) {
47 str.replace(found, 1, ".");
48 found = str.find('/');
49 }
50 return str;
51 }
52
GetImportKey(std::string path)53 inline std::string GetImportKey(std::string path) {
54 base::StringView path_view(path);
55 auto path_no_extension = path_view.substr(0, path_view.rfind('.'));
56 return ReplaceSlashWithDot(path_no_extension.ToStdString());
57 }
58
GetModuleName(std::string str)59 inline std::string GetModuleName(std::string str) {
60 size_t found = str.find('.');
61 if (found == std::string::npos) {
62 return str;
63 }
64 return str.substr(0, found);
65 }
66
67 } // namespace sql_modules
68 } // namespace trace_processor
69 } // namespace perfetto
70 #endif // SRC_TRACE_PROCESSOR_UTIL_SQL_MODULES_H_
71