Flutter Engine
 
Loading...
Searching...
No Matches
Catalog Class Reference

#include <catalog.h>

Classes

struct  Entry
 VisibleForTesting. More...
 
class  Match
 

Public Member Functions

absl::StatusOr< std::vector< Match > > FindMatch (std::string_view query) const
 Tries to identify a match for the query across the Catalog.
 

Static Public Member Functions

static absl::StatusOr< CatalogOpen (std::string_view data_dir)
 
static absl::StatusOr< CatalogMake (const std::vector< Entry > &entries)
 Make a Catalog for testing.
 
static absl::StatusOr< EntryParseEntry (std::istream &is)
 VisibleForTesting.
 

Detailed Description

A storage of licenses that can be matched against. The in memory representation of the data/headers and data/licenses directories. This represents a 2 tiered search, first the sector is used to determine what matcher should be used, then a match is performend on that. This approach was chosen to minimize the size of the RE2::Set.

Definition at line 21 of file catalog.h.

Member Function Documentation

◆ FindMatch()

absl::StatusOr< std::vector< Catalog::Match > > Catalog::FindMatch ( std::string_view  query) const

Tries to identify a match for the query across the Catalog.

Parameters
queryThe text that will be matched against.
Returns
absl::StatusCode::kNotFound when a match can't be found. absl::StatusCode::kInvalidArgument if more than one match comes up from the selector.

Definition at line 183 of file catalog.cc.

184 {
185 std::vector<int> selector_results;
186 if (!selector_.Match(query, &selector_results)) {
187 return absl::NotFoundError("Selector didn't match.");
188 }
189
190 std::vector<Catalog::Match> results;
191 std::vector<int> missed_results;
192 missed_results.reserve(selector_results.size());
193 std::vector<int> hit_results;
194 hit_results.reserve(selector_results.size());
195 for (int selector_result : selector_results) {
196 RE2* matcher = matchers_[selector_result].get();
197 std::optional<Match> match =
198 FindMatchForSelectedMatcher(query, matcher, names_[selector_result]);
199 if (match.has_value()) {
200 results.emplace_back(std::move(match.value()));
201 hit_results.push_back(selector_result);
202 } else {
203 missed_results.push_back(selector_result);
204 }
205 }
206 if (selector_results.size() != results.size()) {
207 std::stringstream missed;
208 for (size_t i = 0; i < missed_results.size(); ++i) {
209 if (i != 0) {
210 missed << ", ";
211 }
212 missed << names_[missed_results[i]];
213 }
214 std::stringstream hit;
215 hit << " Hit matcher(s): (";
216 for (size_t i = 0; i < hit_results.size(); ++i) {
217 if (i != 0) {
218 hit << ", ";
219 }
220 hit << names_[hit_results[i]];
221 }
222 hit << ")";
223 return absl::NotFoundError(
224 absl::StrCat("Selected matcher(s) (", missed.str(), ") didn't match.",
225 hit_results.empty() ? "" : hit.str()));
226 } else {
227 for (size_t i = 0; i < results.size(); ++i) {
228 for (size_t j = i + 1; j < results.size(); ++j) {
229 if (Overlaps(results[i].GetMatchedText(),
230 results[j].GetMatchedText())) {
231 return absl::InvalidArgumentError(absl::StrCat(
232 "Selected matchers overlap (", results[i].GetMatcher(), ", ",
233 results[j].GetMatcher(), ").\n", results[i].GetMatchedText(),
234 "\n############\n", results[j].GetMatchedText()));
235 }
236 }
237 }
238
239 return results;
240 }
241}

References i.

◆ Make()

absl::StatusOr< Catalog > Catalog::Make ( const std::vector< Entry > &  entries)
static

Make a Catalog for testing.

Definition at line 151 of file catalog.cc.

151 {
152 RE2::Set selector(RE2::Options(), RE2::Anchor::UNANCHORED);
153 std::vector<std::unique_ptr<RE2>> matchers;
154 std::vector<std::string> names;
155
156 for (const Entry& entry : entries) {
157 std::string err;
158 names.push_back(std::string(entry.name));
159 int idx = selector.Add(entry.unique, &err);
160 if (idx < 0) {
161 return absl::InvalidArgumentError(
162 absl::StrCat("Unable to add set entry: ", entry.unique, " ", err));
163 }
164 matchers.push_back(std::make_unique<RE2>(entry.matcher));
165 }
166
167 bool did_compile = selector.Compile();
168 if (!did_compile) {
169 return absl::OutOfRangeError("RE2::Set ran out of memory.");
170 }
171 return Catalog(std::move(selector), std::move(matchers), std::move(names));
172}

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ Open()

absl::StatusOr< Catalog > Catalog::Open ( std::string_view  data_dir)
static

Definition at line 98 of file catalog.cc.

98 {
99 fs::path data_dir_path(data_dir);
100 if (!fs::exists(data_dir_path)) {
101 return absl::InvalidArgumentError(
102 absl::StrCat("Data directory doesn't exist ", data_dir));
103 }
104 fs::path licenses_path = data_dir_path / "licenses";
105 if (!fs::exists(licenses_path)) {
106 return absl::InvalidArgumentError(absl::StrCat(
107 "Licenses directory doesn't exist ", licenses_path.string()));
108 }
109
110 RE2::Set selector(RE2::Options(), RE2::Anchor::UNANCHORED);
111 std::vector<std::unique_ptr<RE2>> matchers;
112 std::vector<std::string> names;
113
114 for (const fs::path& file : fs::directory_iterator(licenses_path)) {
115 std::ifstream infile(file.string());
116 if (!infile.good()) {
117 return absl::InvalidArgumentError("Unable to open file " + file.string());
118 }
119
120 absl::StatusOr<Entry> entry = ParseEntry(infile);
121 if (!entry.ok()) {
122 return absl::InvalidArgumentError(
123 absl::StrCat("Unable to parse data entry at ", file.string(), " : ",
124 entry.status()));
125 }
126
127 std::string err;
128 selector.Add(entry->unique, &err);
129 if (!err.empty()) {
130 return absl::InvalidArgumentError(absl::StrCat(
131 "Unable to add unique key from ", file.string(), " : ", err));
132 }
133 names.emplace_back(std::move(entry->name));
134
135 auto matcher_re2 = std::make_unique<RE2>(entry->matcher);
136 if (!matcher_re2) {
137 return absl::InvalidArgumentError("Unable to make matcher.");
138 }
139
140 matchers.emplace_back(std::move(matcher_re2));
141 }
142
143 bool did_compile = selector.Compile();
144 if (!did_compile) {
145 return absl::UnknownError("Unable to compile selector.");
146 }
147
148 return Catalog(std::move(selector), std::move(matchers), std::move(names));
149}
static absl::StatusOr< Entry > ParseEntry(std::istream &is)
VisibleForTesting.
Definition catalog.cc:243

References ParseEntry().

Referenced by Data::Open().

◆ ParseEntry()

absl::StatusOr< Catalog::Entry > Catalog::ParseEntry ( std::istream &  is)
static

VisibleForTesting.

Definition at line 243 of file catalog.cc.

243 {
244 if (!is.good()) {
245 return absl::InvalidArgumentError("Bad stream.");
246 }
247 std::string name;
248 std::getline(is, name);
249 if (is.eof()) {
250 return absl::InvalidArgumentError("Bad stream.");
251 }
252 std::string unique;
253 std::getline(is, unique);
254 if (is.eof()) {
255 return absl::InvalidArgumentError("Bad stream.");
256 }
257
258 std::string matcher_text((std::istreambuf_iterator<char>(is)),
259 std::istreambuf_iterator<char>());
260
261 std::string ignore_whitespace_matcher = IgnoreWhitespace(matcher_text);
262
263 VLOG(4) << "matcher:" << name << ":\n" << ignore_whitespace_matcher;
264
265 return Catalog::Entry{.name = std::move(name),
266 .unique = std::move(unique),
267 .matcher = std::move(ignore_whitespace_matcher)};
268}
const char * name
Definition fuchsia.cc:49
VisibleForTesting.
Definition catalog.h:24
std::string name
Definition catalog.h:25

References name, and Catalog::Entry::name.

Referenced by Open(), and TEST().


The documentation for this class was generated from the following files: