diff --git a/src/lib/md/SecurityCache.cpp b/src/lib/md/SecurityCache.cpp index 27a2f7e67..c3904e256 100644 --- a/src/lib/md/SecurityCache.cpp +++ b/src/lib/md/SecurityCache.cpp @@ -1,6 +1,7 @@ #include "SecurityCache.h" #include "SecurityDB.h" #include "lib/domain_model/Security.h" +#include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/LockBox.h" @@ -86,6 +87,57 @@ const Security *SecurityCache::get_security(SecurityKey key) { return result; } +const Security *SecurityCache::get_security( + api::Venue venue, api::SecurityType type, const std::string &name) { + const Security *result = nullptr; + _me->cache.lock([&result, venue, type, name](auto &cache) { + for (auto const &p : cache) { + if ((type == api::SECURITYTYPE_UNSET || + type == securities::security_type(p.second->Contract_case())) && + (venue == api::VENUE_UNSET && venue == p.second->exchange()) && + (name.empty() || name == securities::descriptor(*p.second))) { + result = p.second.get(); + break; + } + } + }); + return result; +} + +std::vector SecurityCache::get_securities( + api::Venue venue, api::SecurityType type, const std::string &name) { + std::vector result; + _me->cache.lock([&result, venue, type, name](auto &cache) { + const bool all_types = type == api::SECURITYTYPE_UNSET; + const bool all_venues = venue == api::VENUE_UNSET; + const bool all_names = name.empty(); + for (auto const &p : cache) { + const auto &s = *p.second; + if ((all_types || + type == securities::security_type(s.Contract_case())) && + (all_venues || venue == s.exchange()) && + (all_names || name == securities::descriptor(s))) { + result.push_back(p.second.get()); + } + } + }); + return result; +} + +std::vector SecurityCache::get_securities( + std::function include) { + std::vector result; + _me->cache.lock([&result, include](auto &cache) { + for (auto const &p : cache) { + auto &s = *p.second; + if (include(s)) { + result.push_back(&s); + } + } + }); + return result; +} + std::vector SecurityCache::get_securities() { std::vector result; result.reserve(100000); diff --git a/src/lib/md/SecurityCache.h b/src/lib/md/SecurityCache.h index 075cd6b83..c64fba6de 100644 --- a/src/lib/md/SecurityCache.h +++ b/src/lib/md/SecurityCache.h @@ -23,6 +23,26 @@ class SecurityCache { // Load security from cache with fallback to database const Security *get_security(SecurityKey key); + /** + * Finds the first security with matching @p venue @p type and @p name. Params that + * are empty/unset are ignored. + */ + const Security *get_security( + api::Venue venue, api::SecurityType type, const std::string &name); + + /** + * Finds all securities with matching @p venue @p type and @p name. Params that + * are empty/unset are ignored. + */ + std::vector get_securities( + api::Venue venue, api::SecurityType type, const std::string &name); + + /** + * Finds all securities where @p include returns true. + */ + std::vector get_securities( + std::function include); + // Load all securities from cache, no fallback to database std::vector get_securities(); diff --git a/src/lib/md/SecurityOracle.cpp b/src/lib/md/SecurityOracle.cpp index f5c9cf4a2..69d4d2247 100644 --- a/src/lib/md/SecurityOracle.cpp +++ b/src/lib/md/SecurityOracle.cpp @@ -21,6 +21,16 @@ std::vector SecurityOracle::get_securities() const { return _cache.get_securities(); } +std::vector SecurityOracle::get_securities( + api::Venue venue, api::SecurityType type, const std::string &name) const { + return _cache.get_securities(venue, type, name); +} + +std::vector SecurityOracle::get_securities( + std::function include) const { + return _cache.get_securities(include); +} + void SecurityOracle::troubleshoot_log_all_securities() const { size_t sec_no = 0; for (const auto s : get_securities()) { diff --git a/src/lib/md/SecurityOracle.h b/src/lib/md/SecurityOracle.h index 8e8a3f664..9309df1b7 100644 --- a/src/lib/md/SecurityOracle.h +++ b/src/lib/md/SecurityOracle.h @@ -20,6 +20,15 @@ class SecurityOracle { ~SecurityOracle(); const api::Security *get_security(SecurityKey key) const; const api::Security *get_security(ExchangeSecurityKey key) const; + + /** + * Finds all securities where @p include returns true. + */ + std::vector get_securities( + std::function include) const; + std::vector get_securities( + api::Venue venue, api::SecurityType type, const std::string &name) const; + std::vector get_securities() const; // this iterates through all securities and simply logs them. Use it // ONLY for troubleshooting