forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexact_map_matcher.h
60 lines (50 loc) · 2.04 KB
/
exact_map_matcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include "envoy/matcher/matcher.h"
namespace Envoy {
namespace Matcher {
/**
* Implementation of a `sublinear` match tree that provides O(1) lookup of exact values,
* with one OnMatch per result.
*/
template <class DataType>
class ExactMapMatcher : public MatchTree<DataType>, Logger::Loggable<Logger::Id::matcher> {
public:
ExactMapMatcher(DataInputPtr<DataType>&& data_input,
absl::optional<OnMatch<DataType>> on_no_match)
: data_input_(std::move(data_input)), on_no_match_(std::move(on_no_match)) {}
typename MatchTree<DataType>::MatchResult match(const DataType& data) override {
const auto input = data_input_->get(data);
ENVOY_LOG(debug, "Attempting to match {}", input);
if (input.data_availability_ == DataInputGetResult::DataAvailability::NotAvailable) {
return {MatchState::UnableToMatch, absl::nullopt};
}
if (!input.data_) {
return {MatchState::MatchComplete, on_no_match_};
}
const auto itr = children_.find(*input.data_);
if (itr != children_.end()) {
const auto result = itr->second;
if (result.matcher_) {
return result.matcher_->match(data);
} else {
return {MatchState::MatchComplete, OnMatch<DataType>{result.action_cb_, nullptr}};
}
} else if (input.data_availability_ ==
DataInputGetResult::DataAvailability::MoreDataMightBeAvailable) {
// It's possible that we were attempting a lookup with a partial value, so delay matching
// until we know that we actually failed.
return {MatchState::UnableToMatch, absl::nullopt};
}
return {MatchState::MatchComplete, on_no_match_};
}
void addChild(std::string value, OnMatch<DataType>&& on_match) {
const auto itr_and_exists = children_.emplace(value, std::move(on_match));
ASSERT(itr_and_exists.second);
}
private:
absl::flat_hash_map<std::string, OnMatch<DataType>> children_;
const DataInputPtr<DataType> data_input_;
const absl::optional<OnMatch<DataType>> on_no_match_;
};
} // namespace Matcher
} // namespace Envoy