• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstddef>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "perfetto/ext/base/flat_hash_map.h"
26 #include "perfetto/ext/base/string_view.h"
27 
28 namespace perfetto ::trace_processor::sql_modules {
29 
30 using NameToPackage =
31     base::FlatHashMap<std::string,
32                       std::vector<std::pair<std::string, std::string>>>;
33 
34 // Map from include key to sql file. Include key is the string used in INCLUDE
35 // function.
36 struct RegisteredPackage {
37   struct ModuleFile {
38     std::string sql;
39     bool included;
40   };
41   base::FlatHashMap<std::string, ModuleFile> modules;
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 
GetIncludeKey(const std::string & path)53 inline std::string GetIncludeKey(const 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 
GetPackageName(const std::string & str)59 inline std::string GetPackageName(const 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 perfetto::trace_processor::sql_modules
68 #endif  // SRC_TRACE_PROCESSOR_UTIL_SQL_MODULES_H_
69