Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multiindex in retained_map to use both hash map as well as ordered index #698

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/mqtt/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ using string_view = boost::string_view;
template<class CharT, class Traits = std::char_traits<CharT> >
using basic_string_view = boost::basic_string_view<CharT, Traits>;

#if BOOST_VERSION < 106900
} // namespace MQTT_NS

#if BOOST_VERSION < 106900
namespace boost {
template <class charT, class traits>
std::size_t hash_value(basic_string_view<charT, traits> s) {
return boost::hash_range(s.begin(), s.end());
return hash_range(s.begin(), s.end());
}
}
#endif // BOOST_VERSION < 106900

} // namespace MQTT_NS
#endif // BOOST_VERSION < 106900

#else // BOOST_VERSION >= 106100

Expand All @@ -68,6 +70,7 @@ using basic_string_view = boost::basic_string_ref<CharT, Traits>;

#endif // BOOST_VERSION >= 106100


#endif // !defined(MQTT_NO_BOOST_STRING_VIEW)

#endif // !defined(MQTT_STD_STRING_VIEW)
Expand Down
38 changes: 37 additions & 1 deletion test/retained_topic_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "retained_topic_map.hpp"

#include <boost/format.hpp>

BOOST_AUTO_TEST_SUITE(test_retained_map)

BOOST_AUTO_TEST_CASE(general) {
Expand All @@ -18,6 +20,7 @@ BOOST_AUTO_TEST_CASE(general) {
BOOST_TEST(map.size() == 1);
BOOST_TEST(map.internal_size() == 4);


BOOST_TEST(map.insert_or_update("a/b", "123") == 1);
BOOST_TEST(map.size() == 2);
BOOST_TEST(map.internal_size() == 4);
Expand All @@ -40,7 +43,7 @@ BOOST_AUTO_TEST_CASE(general) {
"example/test/A", "example/test/B", "example/A/test", "example/B/test"
};

for (auto const& i: values) {
for (auto const& i : values) {
map.insert_or_update(i, i);
}
BOOST_TEST(map.size() == 4);
Expand Down Expand Up @@ -81,6 +84,14 @@ BOOST_AUTO_TEST_CASE(general) {

BOOST_TEST(matches.size() == 4);

matches = { };
map.find("#", [&matches](std::string const &a) {
matches.push_back(a);
});

BOOST_TEST(matches.size() == 4);


std::vector<std::string> diff;
std::sort(matches.begin(), matches.end());
std::sort(values.begin(), values.end());
Expand Down Expand Up @@ -195,5 +206,30 @@ BOOST_AUTO_TEST_CASE(erase_upper_first) {
}
}

BOOST_AUTO_TEST_CASE(large_number_of_topics) {
retained_topic_map<size_t> map;

constexpr size_t num_topics = 10000;
for (size_t i = 0; i < num_topics; ++i) {
map.insert_or_update((boost::format("topic/%d") % i).str(), i);
}

BOOST_TEST(map.size() == num_topics);
for (size_t i = 0; i < num_topics; ++i) {
map.find(
(boost::format("topic/%d") % i).str(),
[&](size_t value) {
BOOST_TEST(value == i);
}
);
}

for (size_t i = 0; i < num_topics; ++i) {
map.erase((boost::format("topic/%d") % i).str());
}

BOOST_TEST(map.size() == 0);
BOOST_TEST(map.internal_size() == 1);
}

BOOST_AUTO_TEST_SUITE_END()
Loading