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_view.h"
24
25 namespace perfetto ::trace_processor::sql_modules {
26
27 using NameToPackage =
28 base::FlatHashMap<std::string,
29 std::vector<std::pair<std::string, std::string>>>;
30
31 // Map from include key to sql file. Include key is the string used in INCLUDE
32 // function.
33 struct RegisteredPackage {
34 struct ModuleFile {
35 std::string sql;
36 bool included;
37 };
38 base::FlatHashMap<std::string, ModuleFile> modules;
39 };
40
ReplaceSlashWithDot(std::string str)41 inline std::string ReplaceSlashWithDot(std::string str) {
42 size_t found = str.find('/');
43 while (found != std::string::npos) {
44 str.replace(found, 1, ".");
45 found = str.find('/');
46 }
47 return str;
48 }
49
GetIncludeKey(const std::string & path)50 inline std::string GetIncludeKey(const std::string& path) {
51 base::StringView path_view(path);
52 auto path_no_extension = path_view.substr(0, path_view.rfind('.'));
53 return ReplaceSlashWithDot(path_no_extension.ToStdString());
54 }
55
GetPackageName(const std::string & str)56 inline std::string GetPackageName(const std::string& str) {
57 size_t found = str.find('.');
58 if (found == std::string::npos) {
59 return str;
60 }
61 return str.substr(0, found);
62 }
63
64 } // namespace perfetto::trace_processor::sql_modules
65 #endif // SRC_TRACE_PROCESSOR_UTIL_SQL_MODULES_H_
66