-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma | ||
|
||
#include <functional> | ||
|
||
namespace prometheus { | ||
|
||
void hash_combine(std::size_t* seed) { | ||
|
||
} | ||
|
||
//TODO(qwang) should we provide an interface for user to | ||
// provide their defined logic of computing hash value? | ||
template <typename T> | ||
void hash_combine(std::size_t* seed, const T& value) { | ||
*seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (*seed << 6) + (seed >> 2); | ||
} | ||
|
||
template <typename T, typename ... Types> | ||
void hash_combine(std::size_t* seed, const T& value, const Types&... args) { | ||
hash_combine(seed, value); | ||
hash_combine(seed, args...); | ||
} | ||
|
||
template <typename... Types> | ||
std::size_t hash_value(const Types&... args) { | ||
std::size_t seed = 0; | ||
hash_combine(&seed, args...); | ||
return seed; | ||
} | ||
|
||
} // prometheus |
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,15 @@ | ||
#pragma onece | ||
|
||
#include <map> | ||
#include <cstddef> | ||
|
||
namespace prometheus { | ||
|
||
namespace utils { | ||
|
||
///TODO(qwang): doc and test this. | ||
std::size_t hash_labels(const std::map<std::string, std::string>& labels); | ||
|
||
} // utils | ||
|
||
} // prometheus |
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,19 @@ | ||
#include "prometheus/detail/utils.h" | ||
#include "prometheus/detail/hash.h" | ||
|
||
namespace prometheus { | ||
|
||
namespace utils { | ||
|
||
std::size_t hash_labels(const std::map<std::string, std::string>& labels) { | ||
size_t seed = 0; | ||
for (auto& label : labels) { | ||
hash_combine(&seed, label.first, label.second); | ||
} | ||
|
||
return seed; | ||
} | ||
|
||
} // utils | ||
|
||
} // prometheus |
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,43 @@ | ||
#include "prometheus/utils/util.h" | ||
|
||
#include <map> | ||
#include <gmock/gmock.h> | ||
|
||
namespace promethues { | ||
|
||
namespace { | ||
|
||
void expect_not_equal(const std::map<std::string, std::string>& label1, | ||
const std::map<std::string, std::string>& label2) { | ||
EXPECT_NOT_EQ(utils::hash_labels(label1), utils::hash_labels(label2)); | ||
} | ||
|
||
TEST(UtilsTest, hash_labels_1) { | ||
std::map<std::string, std::string> labels; | ||
labels.insert(std::make_pair<std::string, std::string>("key1", "value1")); | ||
labels.insert(std::make_pair<std::string, std::string>("key2", "vaule2")); | ||
auto value1 = utils::hash_labels(labels); | ||
auto value2 = utils::hash_labels(labels); | ||
|
||
EXPECT_EQ(value1, value2); | ||
} | ||
|
||
|
||
TEST(UtilsTest, hash_labels_2) { | ||
std::map<std::string, std::string> labels1; | ||
labels1.insert(std::make_pair<std::string, std::string>("aa", "bb")); | ||
std::map<std::string, std::string> labels2; | ||
labels2.insert(std::make_pair<std::string, std::string>("a", "abb")); | ||
expect_not_equal(labels1, labels2); | ||
|
||
std::map<std::string, std::string> labels3; | ||
labels3.insert(std::make_pair<std::string, std::string>("a", "a")); | ||
std::map<std::string, std::string> labels4; | ||
labels4.insert(std::make_pair<std::string, std::string>("aa", "")); | ||
expect_not_equal(labels3, labels4); | ||
} | ||
|
||
|
||
} | ||
|
||
} //prometheus |