#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" #include #include #include #include namespace bts { namespace md { struct SecurityCacheImpl { utils::LockBox> cache; utils::LockBox> ex_cache; tbb::concurrent_queue update_queue; void enqueue_update_db(const SecurityPtr &); void flush(); }; void SecurityCacheImpl::enqueue_update_db(const SecurityPtr &security) { LOG(md, Diag, "SecurityCacheImpl::enqueue_update_db security: {}", bts::securities::descriptor(*security)); update_queue.push(security); } void SecurityCacheImpl::flush() { #ifndef _WIN32 if (!update_queue.empty()) { std::vector work; SecurityPtr temp; while (update_queue.try_pop(temp)) { work.push_back(temp); } if (!work.empty()) { if (db::insert_or_replace(work) == Result::error) { // If we fail to insert then retry later for (auto &s : work) update_queue.push(std::move(s)); } } } #endif } ////////////////// SecurityCache::SecurityCache() : _me(std::make_shared()) {} SecurityCache::~SecurityCache() {} const Security *SecurityCache::try_get_security(SecurityKey key) const { return _me->cache.lock([key](btree::map &t) { auto iter = t.find(key); if (iter != t.end()) { return iter->second.get(); } return (const api::Security *)nullptr; }); } const Security *SecurityCache::try_get_security(ExchangeSecurityKey key) const { return _me->ex_cache.lock( [key](btree::map &t) { auto iter = t.find(key); if (iter != t.end()) { return iter->second.get(); } return (const api::Security *)nullptr; }); } const Security *SecurityCache::get_security(SecurityKey key) { const Security *result = try_get_security(key); if (result == nullptr) { // outside lock auto security = db::find_security(key); if (security) { LOG(md, Info, "Loaded security from database: {} {}", security->symbol(), security->id()); return add_db_security(security); } return nullptr; } 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); _me->cache.lock([&result](auto &cache) { for (auto const &p : cache) { result.push_back(p.second.get()); } }); return result; } const Security *SecurityCache::add_db_security(const SecurityPtr &security) { bts_assert(security); LOG(md, Info, "SecurityCache::add_db_security security: {}", bts::securities::descriptor(*security)); if (security->has_glbx()) { _me->ex_cache.lock([&security](auto &cache) { ExchangeSecurityKey key(security->id()); cache.insert(std::make_pair(key, security)); }); } return _me->cache.lock([&security](auto &cache) { SecurityKey key(security->id()); auto result = cache.insert(std::make_pair(key, security)); // If result was inserted result.first->second is equal to security // If the value was already present it must be a recently inserted 'live' // security and we should use it return result.first->second.get(); }); } bool SecurityCache::add_live_security(const SecurityPtr &security) { bts_assert(security); LOG(md, Diag, "SecurityCache::add_live_security security: {}", bts::securities::descriptor(*security)); if (security->has_glbx()) { _me->ex_cache.lock([&security](auto &cache) { ExchangeSecurityKey key(security->id()); cache.insert(std::make_pair(key, security)); }); } return _me->cache.lock([&](auto &cache) { SecurityKey key(security->id()); // Insert but don't overwrite existing auto result = cache.insert(std::make_pair(SecurityKey(security->id()), security)); _me->enqueue_update_db(security); return result.second; }); } void SecurityCache::flush_to_disk() { _me->flush(); } } // namespace md } // namespace bts