129 lines
4.0 KiB
C++
129 lines
4.0 KiB
C++
#include "SecurityCache.h"
|
|
#include "SecurityDB.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/LockBox.h"
|
|
|
|
#include <unordered_map>
|
|
#include <google/btree/btree_map.h>
|
|
#include <tbb/concurrent_queue.h>
|
|
#include <mutex>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
struct SecurityCacheImpl {
|
|
utils::LockBox<btree::btree_map<SecurityKey, SecurityPtr>> cache;
|
|
utils::LockBox<btree::btree_map<ExchangeSecurityKey, SecurityPtr>> ex_cache;
|
|
tbb::concurrent_queue<SecurityPtr> update_queue;
|
|
void enqueue_update_db(const SecurityPtr&);
|
|
void flush();
|
|
};
|
|
|
|
void SecurityCacheImpl::enqueue_update_db(const SecurityPtr& security) {
|
|
update_queue.push(security);
|
|
}
|
|
|
|
void SecurityCacheImpl::flush() {
|
|
#ifndef _WIN32
|
|
if (!update_queue.empty()) {
|
|
std::vector<SecurityPtr> 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<SecurityCacheImpl>()) {}
|
|
SecurityCache::~SecurityCache() {}
|
|
|
|
const Security* SecurityCache::try_get_security(SecurityKey key) const {
|
|
return _me->cache.lock([key](btree::btree_map<SecurityKey, SecurityPtr>& 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::btree_map<ExchangeSecurityKey,SecurityPtr>& 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;
|
|
}
|
|
|
|
std::vector<api::Security const*> SecurityCache::get_securities() {
|
|
std::vector<api::Security const*> 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);
|
|
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);
|
|
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(); }
|
|
|
|
}}//ns
|