LCOV - code coverage report
Current view: top level - source/common/router - metadatamatchcriteria_impl.cc (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 22 32 68.8 %
Date: 2024-01-05 06:35:25 Functions: 2 3 66.7 %

          Line data    Source code
       1             : #include "source/common/router/metadatamatchcriteria_impl.h"
       2             : 
       3             : namespace Envoy {
       4             : namespace Router {
       5             : std::vector<MetadataMatchCriterionConstSharedPtr>
       6             : MetadataMatchCriteriaImpl::extractMetadataMatchCriteria(const MetadataMatchCriteriaImpl* parent,
       7          32 :                                                         const ProtobufWkt::Struct& matches) {
       8          32 :   std::vector<MetadataMatchCriterionConstSharedPtr> v;
       9             : 
      10             :   // Track locations of each name (from the parent) in v to make it
      11             :   // easier to replace them when the same name exists in matches.
      12          32 :   absl::node_hash_map<std::string, std::size_t> existing;
      13             : 
      14          32 :   if (parent) {
      15          16 :     for (const auto& it : parent->metadata_match_criteria_) {
      16             :       // v.size() is the index of the emplaced name.
      17          16 :       existing.emplace(it->name(), v.size());
      18          16 :       v.emplace_back(it);
      19          16 :     }
      20          16 :   }
      21             : 
      22             :   // Add values from matches, replacing name/values copied from parent.
      23          32 :   for (const auto& it : matches.fields()) {
      24          32 :     const auto index_it = existing.find(it.first);
      25          32 :     if (index_it != existing.end()) {
      26           0 :       v[index_it->second] = std::make_shared<MetadataMatchCriterionImpl>(it.first, it.second);
      27          32 :     } else {
      28          32 :       v.emplace_back(std::make_shared<MetadataMatchCriterionImpl>(it.first, it.second));
      29          32 :     }
      30          32 :   }
      31             : 
      32             :   // Sort criteria by name to speed matching in the subset load balancer.
      33             :   // See source/docs/subset_load_balancer.md.
      34          32 :   std::sort(
      35          32 :       v.begin(), v.end(),
      36          32 :       [](const MetadataMatchCriterionConstSharedPtr& a,
      37          32 :          const MetadataMatchCriterionConstSharedPtr& b) -> bool { return a->name() < b->name(); });
      38             : 
      39          32 :   return v;
      40          32 : }
      41             : 
      42             : MetadataMatchCriteriaConstPtr
      43           0 : MetadataMatchCriteriaImpl::filterMatchCriteria(const std::set<std::string>& names) const {
      44             : 
      45           0 :   std::vector<MetadataMatchCriterionConstSharedPtr> v;
      46             : 
      47             :   // iterating over metadata_match_criteria_ ensures correct order without sorting
      48           0 :   for (const auto& it : metadata_match_criteria_) {
      49           0 :     if (names.count(it->name()) == 1) {
      50           0 :       v.emplace_back(it);
      51           0 :     }
      52           0 :   }
      53           0 :   return MetadataMatchCriteriaImplConstPtr(new MetadataMatchCriteriaImpl(v));
      54           0 : };
      55             : 
      56             : } // namespace Router
      57             : } // namespace Envoy

Generated by: LCOV version 1.15