/proc/self/cwd/base/attribute_matcher.cc
Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "base/attribute_matcher.h" |
16 | | |
17 | | #include <cstddef> |
18 | | #include <memory> |
19 | | #include <set> |
20 | | #include <string> |
21 | | #include <tuple> |
22 | | #include <utility> |
23 | | #include <vector> |
24 | | |
25 | | #include "absl/base/no_destructor.h" |
26 | | #include "absl/functional/function_ref.h" |
27 | | #include "absl/log/absl_check.h" |
28 | | #include "absl/status/status.h" |
29 | | #include "absl/strings/str_cat.h" |
30 | | #include "absl/strings/string_view.h" |
31 | | #include "absl/types/source_location.h" |
32 | | #include "absl/types/span.h" |
33 | | #include "base/attribute.h" |
34 | | #include "internal/status_macros.h" |
35 | | |
36 | | namespace cel { |
37 | | |
38 | | namespace common_internal { |
39 | | |
40 | | std::pair<AttributeMatcherNode*, bool> AttributeMatcherNode::Add( |
41 | 0 | const AttributeQualifier& pattern, bool prev_emplaced) { |
42 | | // If this node was not just created and it has no children, it is a full |
43 | | // match and additional children cannot be added. |
44 | 0 | if (wildcard_ == nullptr && children_ == nullptr && !prev_emplaced) { |
45 | 0 | return {nullptr, false}; |
46 | 0 | } |
47 | | // If this node has a wildcard already, no more children can be added. |
48 | 0 | if (wildcard_ != nullptr) { |
49 | 0 | return {nullptr, false}; |
50 | 0 | } |
51 | 0 | if (children_ == nullptr) { |
52 | 0 | children_ = std::make_unique<std::set<AttributeMatcherNode, Less>>(); |
53 | 0 | } |
54 | 0 | auto [child, emplaced] = children_->emplace(this, pattern); |
55 | 0 | return {const_cast<AttributeMatcherNode*>(&*child), emplaced}; |
56 | 0 | } |
57 | | |
58 | | std::pair<AttributeMatcherNode*, bool> AttributeMatcherNode::Add( |
59 | 0 | const AttributeQualifierPattern& pattern, bool prev_emplaced) { |
60 | 0 | if (auto qualifier = pattern.ToQualifier(); qualifier.has_value()) { |
61 | 0 | return Add(*qualifier, prev_emplaced); |
62 | 0 | } |
63 | | // If this node was not just created and it has no children, it is a full |
64 | | // match and additional children cannot be added. |
65 | 0 | if (wildcard_ == nullptr && children_ == nullptr && !prev_emplaced) { |
66 | 0 | return {nullptr, false}; |
67 | 0 | } |
68 | | // If this node has children already, no wildcard can be added. |
69 | 0 | if (children_ != nullptr) { |
70 | 0 | return {nullptr, false}; |
71 | 0 | } |
72 | 0 | if (wildcard_ == nullptr) { |
73 | 0 | wildcard_ = std::make_unique<AttributeMatcherNode>( |
74 | 0 | this, AttributeQualifierPattern::Wildcard()); |
75 | 0 | return {wildcard_.get(), true}; |
76 | 0 | } |
77 | 0 | return {wildcard_.get(), false}; |
78 | 0 | } |
79 | | |
80 | 0 | size_t AttributeMatcherNode::RemoveChildren() { |
81 | 0 | if (children_ == nullptr && wildcard_ == nullptr) { |
82 | 0 | return 0; |
83 | 0 | } |
84 | 0 | return RemoveChildrenInternal(); |
85 | 0 | } |
86 | | |
87 | 0 | size_t AttributeMatcherNode::RemoveChildrenInternal() { |
88 | 0 | if (children_ == nullptr && wildcard_ == nullptr) { |
89 | 0 | return 1; |
90 | 0 | } |
91 | 0 | size_t count = 0; |
92 | 0 | if (children_ != nullptr) { |
93 | 0 | auto children_begin = children_->begin(); |
94 | 0 | const auto children_end = children_->end(); |
95 | 0 | while (children_begin != children_end) { |
96 | 0 | count += const_cast<AttributeMatcherNode*>(&*children_begin) |
97 | 0 | ->RemoveChildrenInternal(); |
98 | 0 | children_begin = children_->erase(children_begin); |
99 | 0 | } |
100 | 0 | children_.reset(); |
101 | 0 | } |
102 | 0 | if (wildcard_ != nullptr) { |
103 | 0 | count += wildcard_->RemoveChildrenInternal(); |
104 | 0 | wildcard_.reset(); |
105 | 0 | } |
106 | 0 | return count; |
107 | 0 | } |
108 | | |
109 | 0 | bool AttributeMatcherNode::Remove() { |
110 | 0 | AttributeMatcherNode* parent = this->parent(); |
111 | 0 | if (parent == nullptr) { |
112 | 0 | return children_ == nullptr && wildcard_ == nullptr; |
113 | 0 | } |
114 | 0 | if (children_ == nullptr && wildcard_ == nullptr) { |
115 | 0 | if (key_.IsWildcard()) { |
116 | 0 | ABSL_DCHECK_EQ(parent->wildcard_.get(), this); |
117 | 0 | parent->wildcard_.reset(); |
118 | 0 | } else { |
119 | 0 | parent->children_->erase(*this); |
120 | 0 | if (parent->children_->empty()) { |
121 | 0 | parent->children_.reset(); |
122 | 0 | } |
123 | 0 | } |
124 | 0 | return parent->ChildRemoved(); |
125 | 0 | } |
126 | 0 | return false; |
127 | 0 | } |
128 | | |
129 | 0 | bool AttributeMatcherNode::ChildRemoved() { return Remove(); } |
130 | | |
131 | | } // namespace common_internal |
132 | | |
133 | | namespace { |
134 | | |
135 | | absl::Status AttributePatternNotFound( |
136 | | absl::string_view pattern, |
137 | 0 | absl::SourceLocation source_location = absl::SourceLocation::current()) { |
138 | 0 | return absl::NotFoundError( |
139 | 0 | absl::StrCat("attribute pattern ", pattern, " not found"), |
140 | 0 | source_location); |
141 | 0 | } |
142 | | |
143 | 0 | bool IsWildcard(const AttributeQualifierPattern& qualifier) { |
144 | 0 | return qualifier.IsWildcard(); |
145 | 0 | } |
146 | | |
147 | 0 | bool IsWildcard(const AttributeQualifier&) { return false; } |
148 | | |
149 | | template <typename Q> |
150 | | absl::Status ValidateAttribute(absl::string_view variable, |
151 | | absl::Span<const Q> qualifiers, |
152 | 0 | absl::FunctionRef<std::string()> to_string) { |
153 | 0 | if (variable.empty()) { |
154 | 0 | return absl::InvalidArgumentError( |
155 | 0 | "attribute pattern variable name is required"); |
156 | 0 | } |
157 | 0 | for (size_t i = 0; i < qualifiers.size(); ++i) { |
158 | 0 | if (!qualifiers[i]) { |
159 | 0 | return absl::InvalidArgumentError("bad attribute qualifier pattern"); |
160 | 0 | } |
161 | 0 | if (IsWildcard(qualifiers[i]) && i != qualifiers.size() - 1) { |
162 | 0 | return absl::InvalidArgumentError( |
163 | 0 | "wildcard qualifier in attribute pattern must be last"); |
164 | 0 | } |
165 | 0 | } |
166 | 0 | return absl::OkStatus(); |
167 | 0 | } Unexecuted instantiation: attribute_matcher.cc:absl::lts_20260526::Status cel::(anonymous namespace)::ValidateAttribute<cel::AttributeQualifierPattern>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifierPattern const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) Unexecuted instantiation: attribute_matcher.cc:absl::lts_20260526::Status cel::(anonymous namespace)::ValidateAttribute<cel::AttributeQualifier>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifier const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) |
168 | | |
169 | | } // namespace |
170 | | |
171 | | template <typename Q> |
172 | | absl::Status AttributeMatcher::InsertAttribute( |
173 | | absl::string_view variable, absl::Span<const Q> qualifiers, |
174 | 0 | absl::FunctionRef<std::string()> to_string) { |
175 | 0 | CEL_RETURN_IF_ERROR(ValidateAttribute(variable, qualifiers, to_string)); |
176 | 0 | auto [node, emplaced] = AddVariable(variable); |
177 | 0 | for (const auto& qualifier : qualifiers) { |
178 | 0 | std::tie(node, emplaced) = node->Add(qualifier, emplaced); |
179 | 0 | if (node == nullptr) { |
180 | 0 | break; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | if (node == nullptr) { |
184 | 0 | return absl::AlreadyExistsError( |
185 | 0 | absl::StrCat("attribute pattern which is a superset of ", to_string(), |
186 | 0 | " already exists")); |
187 | 0 | } |
188 | 0 | if (!emplaced) { |
189 | 0 | return absl::AlreadyExistsError(absl::StrCat( |
190 | 0 | "attribute pattern covering ", to_string(), " already exists")); |
191 | 0 | } |
192 | 0 | ++attributes_; |
193 | 0 | return absl::OkStatus(); |
194 | 0 | } Unexecuted instantiation: absl::lts_20260526::Status cel::AttributeMatcher::InsertAttribute<cel::AttributeQualifierPattern>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifierPattern const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) Unexecuted instantiation: absl::lts_20260526::Status cel::AttributeMatcher::InsertAttribute<cel::AttributeQualifier>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifier const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) |
195 | | |
196 | | absl::Status AttributeMatcher::InsertAttribute( |
197 | 0 | const AttributePattern& pattern) { |
198 | 0 | return InsertAttribute( |
199 | 0 | pattern.variable(), pattern.qualifier_path(), |
200 | 0 | [&pattern]() -> std::string { return pattern.ToString(); }); |
201 | 0 | } |
202 | | |
203 | 0 | absl::Status AttributeMatcher::InsertAttribute(const Attribute& attribute) { |
204 | 0 | return InsertAttribute( |
205 | 0 | attribute.variable_name(), attribute.qualifier_path(), |
206 | 0 | [&attribute]() -> std::string { return attribute.ToString(); }); |
207 | 0 | } |
208 | | |
209 | | template <typename Q> |
210 | | absl::Status AttributeMatcher::UpsertAttribute( |
211 | | absl::string_view variable, absl::Span<const Q> qualifiers, |
212 | 0 | absl::FunctionRef<std::string()> to_string) { |
213 | 0 | CEL_RETURN_IF_ERROR(ValidateAttribute(variable, qualifiers, to_string)); |
214 | 0 | auto [node, emplaced] = AddVariable(variable); |
215 | 0 | for (const auto& qualifier : qualifiers) { |
216 | 0 | std::tie(node, emplaced) = node->Add(qualifier, emplaced); |
217 | 0 | if (node == nullptr) { |
218 | | // subset |
219 | 0 | return absl::OkStatus(); |
220 | 0 | } |
221 | 0 | } |
222 | 0 | if (node->children_ == nullptr && node->wildcard_ == nullptr) { |
223 | | // duplicate. |
224 | 0 | return absl::OkStatus(); |
225 | 0 | } |
226 | 0 | if (!emplaced) { |
227 | | // superset |
228 | 0 | attributes_ -= node->RemoveChildren(); |
229 | 0 | ++attributes_; |
230 | 0 | } |
231 | 0 | return absl::OkStatus(); |
232 | 0 | } Unexecuted instantiation: absl::lts_20260526::Status cel::AttributeMatcher::UpsertAttribute<cel::AttributeQualifierPattern>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifierPattern const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) Unexecuted instantiation: absl::lts_20260526::Status cel::AttributeMatcher::UpsertAttribute<cel::AttributeQualifier>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifier const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) |
233 | | |
234 | | absl::Status AttributeMatcher::UpsertAttribute( |
235 | 0 | const AttributePattern& pattern) { |
236 | 0 | return UpsertAttribute( |
237 | 0 | pattern.variable(), pattern.qualifier_path(), |
238 | 0 | [&pattern]() -> std::string { return pattern.ToString(); }); |
239 | 0 | } |
240 | | |
241 | 0 | absl::Status AttributeMatcher::UpsertAttribute(const Attribute& attribute) { |
242 | 0 | return UpsertAttribute( |
243 | 0 | attribute.variable_name(), attribute.qualifier_path(), |
244 | 0 | [&attribute]() -> std::string { return attribute.ToString(); }); |
245 | 0 | } |
246 | | |
247 | | template <typename Q> |
248 | | absl::Status AttributeMatcher::RemoveAttribute( |
249 | | absl::string_view variable, absl::Span<const Q> qualifiers, |
250 | 0 | absl::FunctionRef<std::string()> to_string) { |
251 | 0 | CEL_RETURN_IF_ERROR(ValidateAttribute(variable, qualifiers, to_string)); |
252 | 0 | auto children_it = children_.find(AttributeQualifierView::OfString(variable)); |
253 | 0 | if (children_it == children_.end()) { |
254 | 0 | return AttributePatternNotFound(to_string()); |
255 | 0 | } |
256 | 0 | Node* variable_node = const_cast<Node*>(&*children_it); |
257 | 0 | Node* node = variable_node; |
258 | 0 | for (const auto& qualifier : qualifiers) { |
259 | 0 | if (IsWildcard(qualifier)) { |
260 | 0 | if (node->wildcard_ == nullptr) { |
261 | 0 | return AttributePatternNotFound(to_string()); |
262 | 0 | } |
263 | 0 | node = node->wildcard_.get(); |
264 | 0 | } else { |
265 | 0 | if (node->children_ == nullptr) { |
266 | 0 | return AttributePatternNotFound(to_string()); |
267 | 0 | } |
268 | 0 | children_it = node->children_->find(qualifier); |
269 | 0 | if (children_it == node->children_->end()) { |
270 | 0 | return AttributePatternNotFound(to_string()); |
271 | 0 | } |
272 | 0 | node = const_cast<Node*>(&*children_it); |
273 | 0 | } |
274 | 0 | } |
275 | 0 | if (node->children_ != nullptr || node->wildcard_ != nullptr) { |
276 | 0 | return AttributePatternNotFound(to_string()); |
277 | 0 | } |
278 | 0 | --attributes_; |
279 | 0 | if (node->Remove()) { |
280 | 0 | children_.erase(*variable_node); |
281 | 0 | } |
282 | 0 | return absl::OkStatus(); |
283 | 0 | } Unexecuted instantiation: absl::lts_20260526::Status cel::AttributeMatcher::RemoveAttribute<cel::AttributeQualifierPattern>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifierPattern const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) Unexecuted instantiation: absl::lts_20260526::Status cel::AttributeMatcher::RemoveAttribute<cel::AttributeQualifier>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::Span<cel::AttributeQualifier const>, absl::lts_20260526::FunctionRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>) |
284 | | |
285 | | absl::Status AttributeMatcher::RemoveAttribute( |
286 | 0 | const AttributePattern& pattern) { |
287 | 0 | return RemoveAttribute( |
288 | 0 | pattern.variable(), pattern.qualifier_path(), |
289 | 0 | [&pattern]() -> std::string { return pattern.ToString(); }); |
290 | 0 | } |
291 | | |
292 | 0 | absl::Status AttributeMatcher::RemoveAttribute(const Attribute& attribute) { |
293 | 0 | return RemoveAttribute( |
294 | 0 | attribute.variable_name(), attribute.qualifier_path(), |
295 | 0 | [&attribute]() -> std::string { return attribute.ToString(); }); |
296 | 0 | } |
297 | | |
298 | | struct AttributeMatcher::AttributeCollector { |
299 | | std::vector<AttributePattern> result; |
300 | | absl::string_view variable; |
301 | | std::vector<AttributeQualifierPattern> qualifiers; |
302 | | |
303 | 0 | explicit AttributeCollector(size_t capacity) { result.reserve(capacity); } |
304 | | |
305 | | void CollectVariable(absl::string_view variable, |
306 | 0 | const common_internal::AttributeMatcherNode* node) { |
307 | 0 | if (node->children_ == nullptr && node->wildcard_ == nullptr) { |
308 | 0 | result.push_back(AttributePattern(std::string(variable), {})); |
309 | 0 | return; |
310 | 0 | } |
311 | 0 | this->variable = variable; |
312 | 0 | qualifiers.clear(); |
313 | 0 | if (node->children_ != nullptr) { |
314 | 0 | for (const auto& child : *node->children_) { |
315 | 0 | CollectQualifier(child.key(), &child); |
316 | 0 | } |
317 | 0 | } |
318 | 0 | if (node->wildcard_ != nullptr) { |
319 | 0 | CollectQualifier(AttributeQualifierPattern::Wildcard(), |
320 | 0 | node->wildcard_.get()); |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | void CollectQualifier(const AttributeQualifierPattern& qualifier, |
325 | 0 | const common_internal::AttributeMatcherNode* node) { |
326 | 0 | qualifiers.emplace_back(qualifier); |
327 | 0 | if (node->children_ == nullptr && node->wildcard_ == nullptr) { |
328 | 0 | result.push_back(AttributePattern(std::string(variable), qualifiers)); |
329 | 0 | } else { |
330 | 0 | if (node->children_ != nullptr) { |
331 | 0 | for (const auto& child : *node->children_) { |
332 | 0 | CollectQualifier(child.key(), &child); |
333 | 0 | } |
334 | 0 | } |
335 | 0 | if (node->wildcard_ != nullptr) { |
336 | 0 | CollectQualifier(AttributeQualifierPattern::Wildcard(), |
337 | 0 | node->wildcard_.get()); |
338 | 0 | } |
339 | 0 | } |
340 | 0 | qualifiers.pop_back(); |
341 | 0 | } |
342 | | }; |
343 | | |
344 | | [[nodiscard]] |
345 | 0 | std::vector<AttributePattern> AttributeMatcher::GetAttributes() const { |
346 | 0 | AttributeCollector collector(attributes_); |
347 | 0 | for (const auto& child : children_) { |
348 | | // Guaranteed to be string. |
349 | 0 | collector.CollectVariable(child.key().GetString(), &child); |
350 | 0 | } |
351 | 0 | return std::move(collector.result); |
352 | 0 | } |
353 | | |
354 | | std::pair<typename AttributeMatcher::Node*, bool> AttributeMatcher::AddVariable( |
355 | 0 | absl::string_view variable) { |
356 | 0 | auto [child, emplaced] = |
357 | 0 | children_.emplace(nullptr, AttributeQualifierPattern::OfString(variable)); |
358 | 0 | return {const_cast<Node*>(&*child), emplaced}; |
359 | 0 | } |
360 | | |
361 | 56.9k | const AttributeMatcher& EmptyAttributeMatcher() { |
362 | 56.9k | static const absl::NoDestructor<AttributeMatcher> empty; |
363 | 56.9k | return *empty; |
364 | 56.9k | } |
365 | | |
366 | | } // namespace cel |