Skip to content
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
17 changes: 8 additions & 9 deletions test/subscription_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "test_main.hpp"
#include "combi_test.hpp"
#include "checker.hpp"
Expand Down Expand Up @@ -199,7 +198,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
idx.modify(
it,
[&](sub_con_online& e) {
e.h = h; // update handle
e.h = h.first; // update handle
}
);
BOOST_TEST(!elem.h.empty());
Expand All @@ -217,7 +216,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
idx.modify(
it,
[&](sub_con_online& e) {
e.h = h; // update handle
e.h = h.first; // update handle
}
);
BOOST_TEST(!elem.h.empty());
Expand Down Expand Up @@ -256,7 +255,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
auto& idx = scos.get<tag_con_topic_filter>();
auto it = idx.find(std::make_tuple(con1, "a/b/c"_mb));
BOOST_TEST((it != idx.end()));
m.remove(it->h, *it);
m.erase(it->h, *it);
idx.erase(it);
m.find(
"a/b/c",
Expand Down Expand Up @@ -293,7 +292,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
auto const& elem = *it;
// call remove for each handle - Value pairs
// it is a little inefficient but still enough efficient
m.remove(elem.h, elem);
m.erase(elem.h, elem);
++it;
}
idx.erase(r.first, r.second);
Expand Down Expand Up @@ -359,7 +358,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
idx.modify(
it,
[&](sub_con_online& e) {
e.h = h; // update handle
e.h = h.first; // update handle
}
);
BOOST_TEST(!elem.h.empty());
Expand All @@ -377,7 +376,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
idx.modify(
it,
[&](sub_con_online& e) {
e.h = h; // update handle
e.h = h.first; // update handle
}
);
BOOST_TEST(!elem.h.empty());
Expand Down Expand Up @@ -436,7 +435,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
auto& idx = scos.get<tag_con_topic_filter>();
auto it = idx.find(std::make_tuple(con1, "a/+/c"_mb));
BOOST_TEST((it != idx.end()));
m.remove(it->h, *it);
m.erase(it->h, *it);
idx.erase(it);
m.find(
"a/b/c",
Expand Down Expand Up @@ -472,7 +471,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
auto const& elem = *it;
// call remove for each handle - Value pairs
// it is a little inefficient but still enough efficient
m.remove(elem.h, elem);
m.erase(elem.h, elem);
++it;
}
idx.erase(r.first, r.second);
Expand Down
85 changes: 29 additions & 56 deletions test/subscription_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ class single_subscription_map

// Insert a value at the specified subscription path
handle insert(MQTT_NS::string_view subscription, Value value) {
if (!this->find_subscription(subscription).empty()) {
throw std::runtime_error(std::string("Subscription already exists in map: ").append(subscription.data(), subscription.size()));
}
auto existing_subscription = this->find_subscription(subscription);
if (!existing_subscription.empty())
throw std::runtime_error("Subscription already exists in map");

auto new_subscription_path = this->create_subscription(subscription);
new_subscription_path.back()->second.value = std::move(value);
Expand All @@ -337,7 +337,7 @@ class single_subscription_map
}

// Remove a value at the specified subscription path
void remove(MQTT_NS::string_view subscription) {
void erase(MQTT_NS::string_view subscription) {
auto path = this->find_subscription(subscription);
if (path.empty()) {
return;
Expand All @@ -347,7 +347,7 @@ class single_subscription_map
}

// Remove a value using a handle
void remove(handle h) {
void erase(handle h) {
auto path = this->handle_to_iterators(h);
if (!path.empty()) {
this->remove_subscription(path);
Expand All @@ -371,29 +371,22 @@ class multiple_subscription_map
// Handle of an entry
using handle = typename subscription_map_base< Value >::handle;

// Insert or update a value at the specified subscription path
handle insert_or_update(MQTT_NS::string_view subscription, Value value) {
// Insert a value at the specified subscription path
std::pair<handle, bool> insert(MQTT_NS::string_view subscription, Value value) {
auto path = this->find_subscription(subscription);
if (!path.empty()) {
auto &subscription_set = path.back()->second.value;
subscription_set.erase(value);
subscription_set.insert(std::move(value));
return this->path_to_handle(path);
}
else {
if (path.empty()) {
auto new_subscription_path = this->create_subscription(subscription);
new_subscription_path.back()->second.value.insert(std::move(value));
return this->path_to_handle(new_subscription_path);
return std::make_pair(this->path_to_handle(new_subscription_path), true);
}
}

// Insert a value at the specified subscription path
handle insert(MQTT_NS::string_view subscription, Value const& value) {
return insert_or_update(subscription, value);
auto &subscription_set = path.back()->second.value;
bool insert_result = subscription_set.insert(std::move(value)).second;
return std::make_pair(this->path_to_handle(path), insert_result);
}

// Update an existing entry using a handle
void update(handle h, Value value) {
// Insert a value with a handle to the subscription
std::pair<handle, bool> insert(handle h, Value value) {
if (h.empty()) {
throw std::runtime_error("Invalid handle was specified");
}
Expand All @@ -405,20 +398,13 @@ class multiple_subscription_map
}

auto& subscription_set = h_iter->second.value;
auto subscription_iter = subscription_set.find(value);
if (subscription_iter != subscription_set.end()) {
auto hint = std::next(subscription_iter);
subscription_set.erase(subscription_iter);
subscription_set.insert(hint, std::move(value));
}
else{
subscription_set.insert(std::move(value));
}
bool insert_result = subscription_set.insert(std::move(value)).second;
return std::make_pair(h, insert_result);
}

// Remove a value at the specified subscription path
// returns the value of the removed element (if found)
boost::optional<Value> remove(MQTT_NS::string_view subscription, Value const& value) {
size_t erase(MQTT_NS::string_view subscription, Value const& value) {
// Find the subscription in the map
auto path = this->find_subscription(subscription);
if (path.empty()) {
Expand All @@ -427,25 +413,18 @@ class multiple_subscription_map

// Remove the specified value
auto& subscription_set = path.back()->second.value;
auto subscription_iter = subscription_set.find(value);
if (subscription_iter != subscription_set.end()) {
auto result = boost::make_optional(*subscription_iter);
subscription_set.erase(subscription_iter);

// Remove the subscription when all entries are removed
if (subscription_set.empty()) {
this->remove_subscription(path);
}
auto result = subscription_set.erase(value);

return result;
}
// If all values removed, remove the subscription
if(subscription_set.empty())
this->remove_subscription(path);

return boost::optional<Value>();
return result;
}

// Remove a value at the specified handle
// returns the value of the removed element (if found)
boost::optional<Value> remove(handle h, Value const& value) {
size_t erase(handle h, Value const& value) {
if (h.empty()) {
throw std::runtime_error("Invalid handle was specified");
}
Expand All @@ -456,21 +435,15 @@ class multiple_subscription_map
throw std::runtime_error("Invalid handle was specified");
}

// Remove the specified value
auto& subscription_set = h_iter->second.value;
auto subscription_iter = subscription_set.find(value);
if (subscription_iter != subscription_set.end()) {
auto result = boost::make_optional(*subscription_iter);
subscription_set.erase(subscription_iter);

// Remove the subscription when all entries are removed
if (subscription_set.empty()) {
this->remove_subscription(this->handle_to_iterators(h));
}
auto result = subscription_set.erase(value);

return result;
}
// If all values removed, remove the subscription
if(subscription_set.empty())
this->remove_subscription(this->handle_to_iterators(h));

return boost::optional<Value>();
return result;
}

// Find all subscriptions that match the specified topic
Expand Down