155 lines
4.3 KiB
C++
155 lines
4.3 KiB
C++
#pragma once
|
|
|
|
#include "MDProducerHandle.h"
|
|
#include "common.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/md/Shm/MDStructure.h"
|
|
#include "lib/utils/bts_no_copy.h"
|
|
#include <memory>
|
|
#include <tbb/concurrent_hash_map.h>
|
|
#include <tbb/concurrent_queue.h>
|
|
#include <tbb/queuing_rw_mutex.h>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
/*
|
|
* Maps the exchange ID to the MDProducerHandle and associated data.
|
|
* Allows fast mapping of exchange id => SecurityData template type
|
|
*/
|
|
|
|
// unused
|
|
template <typename ExchangeKey, class SecurityData>
|
|
class SimpleSecurityIndex {
|
|
public:
|
|
SimpleSecurityIndex() : _map() {}
|
|
~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));
|
|
}
|
|
|
|
private:
|
|
BTS_NO_COPY(SimpleSecurityIndex);
|
|
typedef std::unordered_map<ExchangeKey, std::unique_ptr<SecurityData>>
|
|
MapType;
|
|
MapType _map;
|
|
mutable tbb::queuing_rw_mutex _mutex;
|
|
};
|
|
|
|
//
|
|
// SecurityIndex using tbb concurrent_hash_map
|
|
//
|
|
|
|
template <typename ExchangeKey, class SecurityData>
|
|
class ConcurrentSecurityIndex {
|
|
public:
|
|
ConcurrentSecurityIndex() : _map() {}
|
|
~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;
|
|
}
|
|
|
|
size_t n_secs() const { return _map.size(); }
|
|
|
|
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;
|
|
}
|
|
LOG(md, Error, "Found duplicate entry for platform id '{}' - ", key);
|
|
bts_assert(false);
|
|
return nullptr;
|
|
}
|
|
|
|
SecurityData *add_security(ExchangeKey key, MDProducerHandle &handle) {
|
|
std::unique_ptr<SecurityData> data(new SecurityData(key, handle));
|
|
return add_security(key, std::move(data));
|
|
}
|
|
|
|
private:
|
|
BTS_NO_COPY(ConcurrentSecurityIndex);
|
|
typedef tbb::concurrent_hash_map<ExchangeKey, SecurityData *> MapType;
|
|
MapType _map;
|
|
};
|
|
|
|
} // namespace md
|
|
} // namespace bts
|