Files
spark/src/lib/md/SecurityIndex.h
T

205 lines
5.7 KiB
C++

#pragma once
#include "lib/utils/bts_no_copy.h"
#include "lib/logging/logging.h"
#include "MDProducerHandle.h"
#include "common.h"
#include "SpreadResolver.h"
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_queue.h>
#include <tbb/queuing_rw_mutex.h>
#include <vector>
#include <unordered_map>
#include <memory>
namespace bts {
namespace md {
/*
* Maps the exchange ID to the MDProducerHandle and associated data.
* Allows fast mapping of exchange id => SecurityData template type
*/
template <typename ExchangeKey, class SecurityData>
class SimpleSecurityIndex
{
public:
SimpleSecurityIndex():
_map(),
_spread_resolver()
{}
~SimpleSecurityIndex() {}
bool has_security(ExchangeKey key) const {
tbb::queuing_rw_mutex::scoped_lock lock(_mutex, false);
return _map.find(key) != _map.end();
}
SecurityData* get_security(ExchangeKey key) const {
tbb::queuing_rw_mutex::scoped_lock lock(_mutex, false);
auto result = _map.find(key);
if (result == _map.end()) {
return nullptr;
}
return result->second.get();
}
security_key get_key(ExchangeKey key) const {
auto security = get_security(key);
if (security != nullptr) {
return security->handle.key();
}
return invalid_security_key;
}
template <class CB>
void walk_security_list(CB cb) const {
tbb::queuing_rw_mutex::scoped_lock lock(_mutex, false);
for (const auto& pair : _map) {
cb(*pair.second);
}
}
// Transfer ownership of the data to the index
SecurityData* add_security(ExchangeKey key, std::unique_ptr<SecurityData> entry) {
tbb::queuing_rw_mutex::scoped_lock lock(_mutex, true);
// result is pair<iterator, bool>
auto result = _map.insert(std::make_pair(key, std::unique_ptr<SecurityData>()));
if (!result.second) {
LOG(md,Error, "Found duplicate entry for platform id '{} - " ,key);
bts_assert(result.second);
}
result.first->second = std::move(entry);
return result.first->second.get();
}
SecurityData* add_security(ExchangeKey key, MDProducerHandle& handle) {
std::unique_ptr<SecurityData> data(new SecurityData(key, handle));
return add_security(key, std::move(data));
}
//
// Spread resolve logic
void add_spread_legs(ExchangeKey key, const std::vector<ExchangeKey>& legs) {
_spread_resolver.add(key, legs);
}
// Resolves spreads and returns the number spreads resolved
size_t resolve_spreads() {
return _spread_resolver.resolve(
[&](ExchangeKey key) { return get_key(key); },
[&](ExchangeKey& key, std::vector<security_key>& legs) { on_spread_resolved(key, legs); });
}
protected:
void on_spread_resolved(ExchangeKey& key, std::vector<security_key>& legs) {
auto* entry = get_security(key);
bts_assert_d(entry);
if (entry) {
entry->handle.set_legs(legs);
}
}
private:
BTS_NO_COPY(SimpleSecurityIndex);
typedef std::unordered_map<ExchangeKey, std::unique_ptr<SecurityData>> MapType;
MapType _map;
mutable tbb::queuing_rw_mutex _mutex;
SpreadResolver<ExchangeKey> _spread_resolver;
};
//
// SecurityIndex using tbb concurrent_hash_map
//
template <typename ExchangeKey, class SecurityData>
class ConcurrentSecurityIndex
{
public:
ConcurrentSecurityIndex() :
_map(),
_spread_resolver()
{}
~ConcurrentSecurityIndex() {}
SecurityData* get_security(ExchangeKey key) const {
typename MapType::const_accessor accessor;
if (_map.find(accessor, key)) {
return accessor->second;
}
return nullptr;
}
bool has_security(ExchangeKey key) const {
return get_security(key) != nullptr;
}
security_key get_key(ExchangeKey key) const {
const auto* result = get_security(key);
if (result != nullptr) {
return result->handle.key();
}
return invalid_security_key;
}
template <class CB>
void walk_security_list(CB cb) const {
for (auto iter = _map.begin(); iter != _map.end(); ++iter) {
cb(*iter->second);
}
}
// Transfer ownership of the data to the index
SecurityData* add_security(ExchangeKey key, std::unique_ptr<SecurityData> entry) {
typename MapType::accessor accessor;
if (_map.insert(accessor, key)) {
// There is no delete security and concurrent_has_map does not yet support std::move
accessor->second = entry.release();
return accessor->second;
}
else {
LOG(md,Error, "Found duplicate entry for platform id '{}' - ",key);
bts_assert(false);
}
}
SecurityData* add_security(ExchangeKey key, MDProducerHandle& handle) {
std::unique_ptr<SecurityData> data(new SecurityData(key, handle));
return add_security(key, std::move(data));
}
//
// Spread resolve logic
void add_spread_legs(ExchangeKey key, const std::vector<ExchangeKey>& legs) {
_spread_resolver.add(key, legs);
}
// Resolves spreads and returns the number spreads resolved
size_t resolve_spreads() {
return _spread_resolver.resolve(
[&](ExchangeKey key) { return get_key(key); },
[&](ExchangeKey& key, std::vector<security_key>& legs) { on_spread_resolved(key, legs); });
}
protected:
void on_spread_resolved(ExchangeKey& key, std::vector<security_key>& legs) {
auto* entry = get_security(key);
bts_assert_d(entry);
if (entry) {
entry->handle.set_legs(legs);
}
}
private:
BTS_NO_COPY(ConcurrentSecurityIndex);
typedef tbb::concurrent_hash_map<ExchangeKey, SecurityData*> MapType;
MapType _map;
SpreadResolver<ExchangeKey> _spread_resolver;
};
}}//namespace