diff --git a/src/lib/collections/UnorderedSetMap.h b/src/lib/collections/UnorderedSetMap.h new file mode 100644 index 000000000..6f0d3e77b --- /dev/null +++ b/src/lib/collections/UnorderedSetMap.h @@ -0,0 +1,92 @@ +#pragma once +#include +#include + +namespace bts::collections { + +template < + class Key, + class Value, + class KeyHash = std::hash, + class ValueHash = std::hash> +class UnorderedSetMap { + public: + using Values = std::unordered_set; + using Data = std::unordered_map; + template + using AllSame = std::enable_if_t...>>; + + std::size_t size() const; + std::size_t size(Key key) const; + + template > + std::tuple add( + Key key, ValuesToAdd... values); + + template < + typename... ValuesToRemove, + typename = AllSame> + std::tuple remove( + Key key, ValuesToRemove... values); + + private: + Data data_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +std::size_t UnorderedSetMap::size() const { + return data_.size(); +} + +template +std::size_t UnorderedSetMap::size( + Key key) const { + auto it = data_.find(key); + return it == data_.end() ? 0 : it->second.size(); +} + +template +template +std::tuple< + typename UnorderedSetMap::Data::iterator, + bool, + std::size_t> +UnorderedSetMap::add( + Key key, ValuesToAdd... values) { + auto [it, is_new] = data_.try_emplace(key); + auto &val_set = it->second; + auto sz_before = val_set.size(); + [](...) {}((val_set.insert(values))...); + std::size_t n_added = val_set.size() - sz_before; + return std::make_tuple(it, is_new, n_added); +} + +template +template +std::tuple< + typename UnorderedSetMap::Data::iterator, + bool, + std::size_t> +UnorderedSetMap::remove( + Key key, ValuesToRemove... values) { + auto it = data_.find(key); + if (it == data_.end()) { + return std::make_tuple(it, false, std::size_t(0)); + } + auto &val_set = it->second; + auto sz_before = val_set.size(); + [](...) {}((val_set.erase(values))...); + std::size_t n_removed = val_set.size() - sz_before; + bool remove_key = val_set.empty(); + if (remove_key) { + data_.erase(it); + it = data_.end(); + } + return std::make_tuple(it, remove_key, n_removed); +} + +} // namespace bts::collections diff --git a/src/lib/md/MarketDataGateway.cpp b/src/lib/md/MarketDataGateway.cpp index 7f4d771c4..96947494a 100644 --- a/src/lib/md/MarketDataGateway.cpp +++ b/src/lib/md/MarketDataGateway.cpp @@ -2,6 +2,7 @@ #include "MarketDataRegistration.h" #include "lib/api/md/MarketDataSubscriptionParams.pb.h" #include "lib/api/md/binance/BinanceMarketDataSubscriptionParams.pb.h" +#include "lib/collections/UnorderedSetMap.h" #include "lib/core/ProtoPrinter.h" #include "lib/domain/SecurityID.h" #include "lib/domain/SecurityResolver.h" @@ -61,7 +62,8 @@ struct MarketDataGateway::PImpl { binance::BinanceDepthStreamSiteKey, binance::BinanceDepthStreamSite> binance_sites; - std::unordered_map sec_sub_ref_counts; + collections::UnorderedSetMap + sec_listeners; std::unordered_map sec_data_map; std::unordered_map registered_listeners; @@ -83,14 +85,14 @@ struct MarketDataGateway::PImpl { binance::BinanceDepthStreamSite &get_binance_site( const binance::BinanceDepthStreamSiteKey &k) { return binance_sites - .try_emplace(k, k, &sec_sub_ref_counts, ®istered_listeners) + .try_emplace(k, k, &sec_listeners, ®istered_listeners) .first->second; } std::pair try_emplace_binance_site( const binance::BinanceDepthStreamSiteKey &k) { auto p = binance_sites.try_emplace( - k, k, &sec_sub_ref_counts, ®istered_listeners); + k, k, &sec_listeners, ®istered_listeners); return {&p.first->second, p.second}; } diff --git a/src/lib/md/binance/BinanceDepthStreamSite.cpp b/src/lib/md/binance/BinanceDepthStreamSite.cpp index dce405c25..33151470c 100644 --- a/src/lib/md/binance/BinanceDepthStreamSite.cpp +++ b/src/lib/md/binance/BinanceDepthStreamSite.cpp @@ -1,4 +1,5 @@ #include "BinanceDepthStreamSite.h" +#include "lib/collections/UnorderedSetMap.h" #include "lib/api/md/binance/BinanceMarketDataSubscriptionParams.pb.h" #include "lib/domain/SecurityID.h" #include "lib/domain/SecurityResolver.h" @@ -20,20 +21,20 @@ namespace bts::md::binance { BinanceDepthStreamSite::BinanceDepthStreamSite( BinanceDepthStreamSiteKey site_key, - std::unordered_map *ref_counts_ptr, + collections::UnorderedSetMap *sec_listeners_ptr, std::unordered_map *listeners_ptr) - : site_key_(std::move(site_key)), ref_counts_ptr_(ref_counts_ptr), + : site_key_(std::move(site_key)), sec_listeners_ptr_(sec_listeners_ptr), listeners_ptr_(listeners_ptr) {} -std::unordered_map & -BinanceDepthStreamSite::ref_counts_() { - return *ref_counts_ptr_; +collections::UnorderedSetMap & +BinanceDepthStreamSite::sec_listeners_() { + return *sec_listeners_ptr_; } -const std::unordered_map & -BinanceDepthStreamSite::ref_counts_() const { - return *ref_counts_ptr_; +const collections::UnorderedSetMap & +BinanceDepthStreamSite::sec_listeners_() const { + return *sec_listeners_ptr_; } std::unordered_map & diff --git a/src/lib/md/binance/BinanceDepthStreamSite.h b/src/lib/md/binance/BinanceDepthStreamSite.h index 79afd576a..008163d28 100644 --- a/src/lib/md/binance/BinanceDepthStreamSite.h +++ b/src/lib/md/binance/BinanceDepthStreamSite.h @@ -2,6 +2,7 @@ #include "BinanceDepthStreamSiteKey.h" #include "lib/api/md/MarketDataSubscriptionParams.pb.h" #include "lib/api/md/binance/BinanceMarketDataSubscriptionParams.pb.h" +#include "lib/collections/UnorderedSetMap.h" #include "lib/domain/SecurityID.h" #include "lib/domain/SecurityResolver.h" #include "lib/domain/Venue.h" @@ -24,16 +25,16 @@ namespace bts::md::binance { * @brief Facilitates the flow of Binance market data updates (from a particular * site) to subscribed listeners (of type IMarketDataListener) * - * @details Maintains a reference count of subscriptions per symbol, so for a - * given symbol, one internal subscription serves all listeners subscribed - * to that symbol + * @details For a given symbol, one internal subscription serves all listeners + * subscribed to that symbol */ class BinanceDepthStreamSite { public: BinanceDepthStreamSite() = default; BinanceDepthStreamSite( BinanceDepthStreamSiteKey site_key, - std::unordered_map *ref_counts_ptr, + collections::UnorderedSetMap + *sec_listeners_ptr, std::unordered_map *listeners_ptr); @@ -67,8 +68,8 @@ class BinanceDepthStreamSite { private: BinanceDepthStreamSiteKey site_key_; - std::unordered_map *ref_counts_ptr_ = - nullptr; + collections::UnorderedSetMap + *sec_listeners_ptr_ = nullptr; std::unordered_map *listeners_ptr_ = nullptr; std::optional binance_ = std::nullopt; @@ -76,9 +77,10 @@ class BinanceDepthStreamSite { // (disposed and recreated) without unbinding the existing callback. BinanceDiffDepthSubscription::OnDepthChanged on_depth_changed_proxy_; - std::unordered_map &ref_counts_(); - const std::unordered_map & - ref_counts_() const; + collections::UnorderedSetMap & + sec_listeners_(); + const collections::UnorderedSetMap + &sec_listeners_() const; std::unordered_map & listeners_(); @@ -109,7 +111,8 @@ void BinanceDepthStreamSite::subscribe( "Cannot subscribe. Listener already subscribed to sec. sid: " "{}; sym: {}", sym, sid); - if (ref_counts_().try_emplace(sid, 0).first->second++ == 0) { + auto [it, is_new_sym, _] = sec_listeners_().add(sid, listener_id); + if (is_new_sym) { ensure_connect_(); // first subscription for this symbol binance_->add_symbol(sym); @@ -134,14 +137,8 @@ void BinanceDepthStreamSite::unsubscribe( "Cannot unsubscribe. Listener not subscribed to sec. sid: {}; " "sym: {}", sid, sym); - auto rc_it = ref_counts_().find(sid); - bts_assert_msg( - rc_it != ref_counts_().end(), - "Inter-listener security subscription ref count out of sync " - "with listener's subscribed securities."); - if (--rc_it->second == 0) { - // last subscription for this symbol - ref_counts_().erase(rc_it); + auto [it, removed_sym, _] = sec_listeners_().remove(sid, listener_id); + if (removed_sym) { binance_->remove_symbol(sym); } }