forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support cache key for partial string map value (envoyproxy#50)
* Support cache key for string map. * Update comment
- Loading branch information
Showing
10 changed files
with
209 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright 2017 Istio Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "src/cache_key_set.h" | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
|
||
CacheKeySet::CacheKeySet(const std::vector<std::string>& cache_keys) { | ||
for (const std::string& key : cache_keys) { | ||
size_t pos = key.find_first_of('/'); | ||
if (pos == std::string::npos) { | ||
// Always override with an empty one. | ||
// It handles case where | ||
// "key/sub_key" comes first, then "key" come. | ||
// In this case, "key" wins. | ||
key_map_[key] = SubKeySet(); | ||
} else { | ||
std::string split_key = key.substr(0, pos); | ||
std::string split_sub_key = key.substr(pos + 1); | ||
auto it = key_map_.find(split_key); | ||
if (it == key_map_.end()) { | ||
key_map_[split_key] = SubKeySet(); | ||
key_map_[split_key].sub_keys_.insert(split_sub_key); | ||
} else { | ||
// If map to empty set, it was created by "key" | ||
// without sub_key, keep it as empty set. | ||
if (!it->second.sub_keys_.empty()) { | ||
it->second.sub_keys_.insert(split_sub_key); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} // namespace mixer_client | ||
} // namespace istio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* Copyright 2017 Istio Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef MIXER_CLIENT_CACHE_KEY_SET_H_ | ||
#define MIXER_CLIENT_CACHE_KEY_SET_H_ | ||
|
||
#include <map> | ||
#include <set> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
|
||
// A class to store sub key set | ||
class SubKeySet { | ||
public: | ||
// Check if the sub key is in the set. | ||
bool Found(const std::string& sub_key) const { | ||
if (sub_keys_.empty()) { | ||
return true; | ||
} else { | ||
return sub_keys_.find(sub_key) != sub_keys_.end(); | ||
} | ||
} | ||
|
||
private: | ||
friend class CacheKeySet; | ||
std::set<std::string> sub_keys_; | ||
}; | ||
|
||
// A class to handle cache key set | ||
// A std::set will not work for string map sub keys. | ||
// For a StringMap attruibute, | ||
// If cache key is "attribute.key", all values will be used. | ||
// If cache key is "attribute.key/map.key", only the map key is used. | ||
// If both format exists, the whole map will be used. | ||
class CacheKeySet { | ||
public: | ||
CacheKeySet(const std::vector<std::string>& cache_keys); | ||
|
||
// Return nullptr if the key is not in the set. | ||
const SubKeySet* Find(const std::string& key) const { | ||
const auto it = key_map_.find(key); | ||
return it == key_map_.end() ? nullptr : &it->second; | ||
} | ||
|
||
bool empty() const { return key_map_.empty(); } | ||
|
||
private: | ||
std::map<std::string, SubKeySet> key_map_; | ||
}; | ||
|
||
} // namespace mixer_client | ||
} // namespace istio | ||
|
||
#endif // MIXER_CLIENT_CACHE_KEY_SET_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* Copyright 2017 Istio Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "src/cache_key_set.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
namespace { | ||
|
||
TEST(CacheKeySetTest, TestNonSubkey) { | ||
CacheKeySet s({"key1", "key2"}); | ||
EXPECT_TRUE(s.Find("key1") != nullptr); | ||
EXPECT_TRUE(s.Find("key2") != nullptr); | ||
EXPECT_TRUE(s.Find("xyz") == nullptr); | ||
} | ||
|
||
TEST(CacheKeySetTest, TestSubkeyLater) { | ||
// Whole key comes first, then sub key comes. | ||
CacheKeySet s({"key1", "key2", "key2/sub"}); | ||
EXPECT_TRUE(s.Find("key2")->Found("sub")); | ||
EXPECT_TRUE(s.Find("key2")->Found("xyz")); | ||
} | ||
|
||
TEST(CacheKeySetTest, TestSubkeyFirst) { | ||
// The sub key comes first, then the whole key comes | ||
CacheKeySet s({"key1", "key2/sub", "key2"}); | ||
EXPECT_TRUE(s.Find("key2")->Found("sub")); | ||
EXPECT_TRUE(s.Find("key2")->Found("xyz")); | ||
} | ||
|
||
TEST(CacheKeySetTest, TestSubkey) { | ||
CacheKeySet s({"key1", "key2/sub1", "key2/sub2"}); | ||
EXPECT_TRUE(s.Find("key2")->Found("sub1")); | ||
EXPECT_TRUE(s.Find("key2")->Found("sub2")); | ||
EXPECT_FALSE(s.Find("key2")->Found("xyz")); | ||
} | ||
|
||
} // namespace | ||
} // namespace mixer_client | ||
} // namespace istio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters