/proc/self/cwd/base/attribute_matcher.h
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 | | #ifndef THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_MATCHER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_MATCHER_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | #include <memory> |
20 | | #include <set> |
21 | | #include <string> |
22 | | #include <utility> |
23 | | #include <vector> |
24 | | |
25 | | #include "absl/base/attributes.h" |
26 | | #include "absl/base/optimization.h" |
27 | | #include "absl/functional/function_ref.h" |
28 | | #include "absl/log/absl_check.h" |
29 | | #include "absl/status/status.h" |
30 | | #include "absl/strings/string_view.h" |
31 | | #include "absl/types/span.h" |
32 | | #include "base/attribute.h" |
33 | | |
34 | | namespace cel { |
35 | | |
36 | | class AttributeMatcher; |
37 | | class AttributeMatch; |
38 | | |
39 | | namespace common_internal { |
40 | | |
41 | | // Node in the tree used by AttributeMatcher, represents either a variable or a |
42 | | // qualifier. Nodes with no parent are variables and all others are qualifiers. |
43 | | class AttributeMatcherNode { |
44 | | public: |
45 | | struct Less { |
46 | | using is_transparent = void; |
47 | | |
48 | | bool operator()(const AttributeMatcherNode& lhs, |
49 | 0 | const AttributeMatcherNode& rhs) const { |
50 | 0 | return lhs.key() < rhs.key(); |
51 | 0 | } |
52 | | |
53 | | bool operator()(const AttributeMatcherNode& lhs, |
54 | 0 | const AttributeQualifierPattern& rhs) const { |
55 | 0 | return lhs.key() < rhs; |
56 | 0 | } |
57 | | |
58 | | bool operator()(const AttributeQualifierPattern& lhs, |
59 | 0 | const AttributeMatcherNode& rhs) const { |
60 | 0 | return lhs < rhs.key(); |
61 | 0 | } |
62 | | |
63 | | bool operator()(const AttributeMatcherNode& lhs, |
64 | 0 | const AttributeQualifierView& rhs) const { |
65 | 0 | return lhs.key() < rhs; |
66 | 0 | } |
67 | | |
68 | | bool operator()(const AttributeQualifierView& lhs, |
69 | 0 | const AttributeMatcherNode& rhs) const { |
70 | 0 | return lhs < rhs.key(); |
71 | 0 | } |
72 | | |
73 | | bool operator()(const AttributeMatcherNode& lhs, |
74 | 0 | const AttributeQualifier& rhs) const { |
75 | 0 | return lhs.key() < rhs; |
76 | 0 | } |
77 | | |
78 | | bool operator()(const AttributeQualifier& lhs, |
79 | 0 | const AttributeMatcherNode& rhs) const { |
80 | 0 | return lhs < rhs.key(); |
81 | 0 | } |
82 | | |
83 | | bool operator()(absl::string_view lhs, |
84 | 0 | const AttributeQualifierPattern& rhs) const { |
85 | | // Fast path for top-level variable matching, should only be called in |
86 | | // that context. |
87 | 0 | ABSL_DCHECK(rhs.IsString()); |
88 | 0 | return lhs < rhs.GetString(); |
89 | 0 | } |
90 | | |
91 | | bool operator()(const AttributeQualifierPattern& lhs, |
92 | 0 | absl::string_view rhs) const { |
93 | | // Fast path for top-level variable matching, should only be called in |
94 | | // that context. |
95 | 0 | ABSL_DCHECK(lhs.IsString()); |
96 | 0 | return lhs.GetString() < rhs; |
97 | 0 | } |
98 | | |
99 | | bool operator()(absl::string_view lhs, |
100 | 0 | const AttributeMatcherNode& rhs) const { |
101 | 0 | return (*this)(lhs, rhs.key()); |
102 | 0 | } |
103 | | |
104 | | bool operator()(const AttributeMatcherNode& lhs, |
105 | 0 | absl::string_view rhs) const { |
106 | 0 | return (*this)(lhs.key(), rhs); |
107 | 0 | } |
108 | | }; |
109 | | |
110 | | AttributeMatcherNode(AttributeMatcherNode* parent, |
111 | | const AttributeQualifierPattern& key) |
112 | 0 | : parent_(parent), |
113 | 0 | key_(key), |
114 | 0 | depth_(parent != nullptr ? parent->depth_ + 1 : 0) {} |
115 | | |
116 | | [[nodiscard]] |
117 | 0 | bool IsPartial() const { |
118 | 0 | return children_ != nullptr || wildcard_ != nullptr; |
119 | 0 | } |
120 | | |
121 | | // True if this is a partial match and the only child of this node is a |
122 | | // wildcard and the wildcard has no children. |
123 | | [[nodiscard]] |
124 | 0 | bool IsPartialWildcard() const { |
125 | 0 | return children_ == nullptr && wildcard_ != nullptr && wildcard_->IsFull(); |
126 | 0 | } |
127 | | |
128 | | [[nodiscard]] |
129 | 0 | bool IsFull() const { |
130 | 0 | return !IsPartial(); |
131 | 0 | } |
132 | | |
133 | | [[nodiscard]] |
134 | 0 | const AttributeQualifierPattern& key() const { |
135 | 0 | return key_; |
136 | 0 | } |
137 | | |
138 | | [[nodiscard]] |
139 | 0 | AttributeMatcherNode* parent() const { |
140 | 0 | return parent_; |
141 | 0 | } |
142 | | |
143 | | [[nodiscard]] |
144 | 0 | size_t depth() const { |
145 | 0 | return depth_; |
146 | 0 | } |
147 | | |
148 | | [[nodiscard]] |
149 | | const AttributeMatcherNode* MatchQualifier( |
150 | 0 | const AttributeQualifierView& qualifier) const { |
151 | 0 | if (children_ != nullptr) { |
152 | 0 | auto it = children_->find(qualifier); |
153 | 0 | if (it != children_->end()) { |
154 | 0 | return &*it; |
155 | 0 | } |
156 | 0 | } else if (wildcard_ != nullptr) { |
157 | 0 | return wildcard_.get(); |
158 | 0 | } |
159 | 0 | return nullptr; |
160 | 0 | } |
161 | | |
162 | | [[nodiscard]] |
163 | | const AttributeMatcherNode* MatchQualifier( |
164 | 0 | const AttributeQualifier& qualifier) const { |
165 | 0 | if (children_ != nullptr) { |
166 | 0 | auto it = children_->find(qualifier); |
167 | 0 | if (it != children_->end()) { |
168 | 0 | return &*it; |
169 | 0 | } |
170 | 0 | } else if (wildcard_ != nullptr) { |
171 | 0 | return wildcard_.get(); |
172 | 0 | } |
173 | 0 | return nullptr; |
174 | 0 | } |
175 | | |
176 | | [[nodiscard]] |
177 | | std::pair<AttributeMatcherNode*, bool> Add(const AttributeQualifier& pattern, |
178 | | bool prev_emplaced); |
179 | | [[nodiscard]] |
180 | | std::pair<AttributeMatcherNode*, bool> Add( |
181 | | const AttributeQualifierPattern& pattern, bool prev_emplaced); |
182 | | |
183 | | [[nodiscard]] |
184 | | bool Remove(); |
185 | | |
186 | | [[nodiscard]] |
187 | | size_t RemoveChildren(); |
188 | | |
189 | | private: |
190 | | friend class cel::AttributeMatcher; |
191 | | |
192 | | [[nodiscard]] |
193 | | bool ChildRemoved(); |
194 | | |
195 | | [[nodiscard]] |
196 | | size_t RemoveChildrenInternal(); |
197 | | |
198 | | AttributeMatcherNode* const parent_; |
199 | | const AttributeQualifierPattern key_; |
200 | | const size_t depth_; |
201 | | // Pointer stability is required, so use std::set. |
202 | | std::unique_ptr<std::set<AttributeMatcherNode, Less>> children_; |
203 | | std::unique_ptr<AttributeMatcherNode> wildcard_; |
204 | | }; |
205 | | |
206 | | AttributeMatch MakeAttributeMatch( |
207 | | const common_internal::AttributeMatcherNode* node); |
208 | | |
209 | | [[nodiscard]] |
210 | | const AttributeMatcherNode* GetAttributeMatcherNode(AttributeMatch match); |
211 | | |
212 | | } // namespace common_internal |
213 | | |
214 | | class [[nodiscard]] AttributeMatch { |
215 | | public: |
216 | | using Type = AttributePattern::MatchType; |
217 | | |
218 | 215M | AttributeMatch() = default; |
219 | | AttributeMatch(const AttributeMatch&) = default; |
220 | | AttributeMatch& operator=(const AttributeMatch&) = default; |
221 | | |
222 | | [[nodiscard]] |
223 | 0 | Type GetType() const { |
224 | 0 | return node_ == nullptr ? Type::NONE |
225 | 0 | : node_->IsPartial() ? Type::PARTIAL |
226 | 0 | : Type::FULL; |
227 | 0 | } |
228 | | |
229 | | [[nodiscard]] |
230 | 0 | bool IsNone() const { |
231 | 0 | return node_ == nullptr; |
232 | 0 | } |
233 | | |
234 | | [[nodiscard]] |
235 | 0 | bool IsPartial() const { |
236 | 0 | return node_ != nullptr && node_->IsPartial(); |
237 | 0 | } |
238 | | |
239 | | // True if this is a partial match and the only possible next match is a |
240 | | // wildcard and there is no next match after the wildcard. |
241 | | [[nodiscard]] |
242 | 0 | bool IsPartialWildcard() const { |
243 | 0 | return node_ != nullptr && node_->IsPartialWildcard(); |
244 | 0 | } |
245 | | |
246 | | [[nodiscard]] |
247 | 0 | bool IsFull() const { |
248 | 0 | return node_ != nullptr && node_->IsFull(); |
249 | 0 | } |
250 | | |
251 | | // Attempts to match the next qualifier. This should not be called on full |
252 | | // matches, as those do not have children by definition. Instead the full |
253 | | // match should be handled. |
254 | 0 | AttributeMatch MatchQualifier(const AttributeQualifierView& qualifier) const { |
255 | 0 | ABSL_DCHECK(!IsFull()); |
256 | 0 | ABSL_DCHECK(qualifier); |
257 | 0 | if (IsNone()) { |
258 | 0 | return AttributeMatch(); |
259 | 0 | } |
260 | 0 | return AttributeMatch(node_->MatchQualifier(qualifier)); |
261 | 0 | } |
262 | | |
263 | | private: |
264 | | friend class AttributeMatcher; |
265 | | friend AttributeMatch common_internal::MakeAttributeMatch( |
266 | | const common_internal::AttributeMatcherNode* node); |
267 | | friend const common_internal::AttributeMatcherNode* |
268 | | common_internal::GetAttributeMatcherNode(AttributeMatch match); |
269 | | |
270 | | AttributeMatch NotNoneMatchQualifier( |
271 | 0 | const AttributeQualifierView& qualifier) const { |
272 | 0 | ABSL_DCHECK(!IsNone()); |
273 | 0 | ABSL_DCHECK(qualifier); |
274 | 0 | return AttributeMatch(node_->MatchQualifier(qualifier)); |
275 | 0 | } |
276 | | |
277 | | explicit AttributeMatch(const common_internal::AttributeMatcherNode* node) |
278 | 0 | : node_(node) {} |
279 | | |
280 | | const common_internal::AttributeMatcherNode* node_ = nullptr; |
281 | | }; |
282 | | |
283 | | namespace common_internal { |
284 | | |
285 | | inline AttributeMatch MakeAttributeMatch( |
286 | 0 | const common_internal::AttributeMatcherNode* node) { |
287 | 0 | return AttributeMatch(node); |
288 | 0 | } |
289 | | |
290 | | [[nodiscard]] |
291 | | inline const AttributeMatcherNode* GetAttributeMatcherNode( |
292 | 0 | AttributeMatch match) { |
293 | 0 | return match.node_; |
294 | 0 | } |
295 | | |
296 | | } // namespace common_internal |
297 | | |
298 | | // AttributeMatcher allows for efficiently checking for unknown or missing |
299 | | // attributes at runtime in a scalable manner. AttributeMatcher is |
300 | | // thread-compatible however it must not be modified during any evaluation where |
301 | | // it is being used. |
302 | | class AttributeMatcher { |
303 | | private: |
304 | | using Node = common_internal::AttributeMatcherNode; |
305 | | |
306 | | public: |
307 | 56.9k | AttributeMatcher() = default; |
308 | | AttributeMatcher(const AttributeMatcher&) = delete; |
309 | | AttributeMatcher(AttributeMatcher&&) = default; |
310 | | AttributeMatcher& operator=(const AttributeMatcher&) = delete; |
311 | | AttributeMatcher& operator=(AttributeMatcher&&) = default; |
312 | | |
313 | | // Inserts an attribute to the matcher. Returns an error if the attribute |
314 | | // or attribute pattern being added overlaps with a previously added |
315 | | // attribute. |
316 | | absl::Status InsertAttribute(const AttributePattern& pattern); |
317 | | absl::Status InsertAttribute(const Attribute& attribute); |
318 | | |
319 | | // Inserts or updates the attribute in the matcher. If the attribute being |
320 | | // added is a superset of others, the subsets will be removed and replaced |
321 | | // with this attribute. If the attribute being added is a subset of others, |
322 | | // no action is taken. Otherwise the attribute is inserted. |
323 | | absl::Status UpsertAttribute(const AttributePattern& pattern); |
324 | | absl::Status UpsertAttribute(const Attribute& attribute); |
325 | | |
326 | | // Removes a previously added unknown attribute from the matcher. |
327 | | absl::Status RemoveAttribute(const AttributePattern& pattern); |
328 | | absl::Status RemoveAttribute(const Attribute& attribute); |
329 | | |
330 | | // Resets the matcher such that all observable behavior is equivalent to a |
331 | | // default constructed matcher. |
332 | 0 | void ClearAttributes() { |
333 | 0 | children_.clear(); |
334 | 0 | attributes_ = 0; |
335 | 0 | } |
336 | | |
337 | | AttributeMatch MatchVariable(absl::string_view variable) const |
338 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
339 | 0 | if (!variable.empty()) { |
340 | 0 | auto it = children_.find(variable); |
341 | 0 | if (it != children_.end()) { |
342 | 0 | return common_internal::MakeAttributeMatch(&*it); |
343 | 0 | } |
344 | 0 | } |
345 | 0 | return AttributeMatch(); |
346 | 0 | } |
347 | | |
348 | | AttributeMatch MatchAttribute( |
349 | | absl::string_view variable, |
350 | 0 | absl::Span<const AttributeQualifierView> qualifiers) const { |
351 | 0 | AttributeMatch match = MatchVariable(variable); |
352 | 0 | for (const auto& qualifier : qualifiers) { |
353 | 0 | if (ABSL_PREDICT_FALSE(!qualifier)) { |
354 | 0 | return AttributeMatch(); |
355 | 0 | } |
356 | 0 | if (match.IsNone()) { |
357 | 0 | break; |
358 | 0 | } |
359 | 0 | match = match.NotNoneMatchQualifier(qualifier); |
360 | 0 | } |
361 | 0 | return match; |
362 | 0 | } |
363 | | |
364 | | AttributeMatch MatchAttribute( |
365 | | absl::string_view variable, |
366 | 0 | absl::Span<const AttributeQualifier> qualifiers) const { |
367 | 0 | AttributeMatch match = MatchVariable(variable); |
368 | 0 | for (const auto& qualifier : qualifiers) { |
369 | 0 | if (ABSL_PREDICT_FALSE(!qualifier)) { |
370 | 0 | return AttributeMatch(); |
371 | 0 | } |
372 | 0 | if (match.IsNone()) { |
373 | 0 | break; |
374 | 0 | } |
375 | 0 | match = match.NotNoneMatchQualifier(qualifier); |
376 | 0 | } |
377 | 0 | return match; |
378 | 0 | } |
379 | | |
380 | 0 | AttributeMatch MatchAttribute(const Attribute& attribute) const { |
381 | 0 | return MatchAttribute(attribute.variable_name(), |
382 | 0 | attribute.qualifier_path()); |
383 | 0 | } |
384 | | |
385 | | // Returns an array of attribute patterns corresponding to the |
386 | | // previously added attribute patterns. The order of the attribute |
387 | | // patterns in the array is not guaranteed. |
388 | | [[nodiscard]] |
389 | | std::vector<AttributePattern> GetAttributes() const; |
390 | | |
391 | | // Tests whether any attribute patterns have been added to this |
392 | | // matcher. |
393 | | [[nodiscard]] |
394 | 0 | bool HasAttributes() const { |
395 | 0 | return !children_.empty(); |
396 | 0 | } |
397 | | |
398 | 0 | friend void swap(AttributeMatcher& lhs, AttributeMatcher& rhs) { |
399 | 0 | using std::swap; |
400 | 0 | swap(lhs.children_, rhs.children_); |
401 | 0 | swap(lhs.attributes_, rhs.attributes_); |
402 | 0 | } |
403 | | |
404 | | private: |
405 | | struct AttributeCollector; |
406 | | |
407 | | [[nodiscard]] |
408 | | std::pair<Node*, bool> AddVariable(absl::string_view variable); |
409 | | |
410 | | template <typename Q> |
411 | | absl::Status InsertAttribute(absl::string_view variable, |
412 | | absl::Span<const Q> qualifiers, |
413 | | absl::FunctionRef<std::string()> to_string); |
414 | | |
415 | | template <typename Q> |
416 | | absl::Status UpsertAttribute(absl::string_view variable, |
417 | | absl::Span<const Q> qualifiers, |
418 | | absl::FunctionRef<std::string()> to_string); |
419 | | |
420 | | template <typename Q> |
421 | | absl::Status RemoveAttribute(absl::string_view variable, |
422 | | absl::Span<const Q> qualifiers, |
423 | | absl::FunctionRef<std::string()> to_string); |
424 | | |
425 | | // Pointer stability is required. |
426 | | std::set<Node, Node::Less> children_; |
427 | | size_t attributes_ = 0; |
428 | | }; |
429 | | |
430 | | [[nodiscard]] |
431 | | const AttributeMatcher& EmptyAttributeMatcher(); |
432 | | |
433 | | } // namespace cel |
434 | | |
435 | | #endif // THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_MATCHER_H_ |