79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
#pragma once
|
|
#ifndef WIN32
|
|
#include "MDProducerHandle.h"
|
|
#include "common.h"
|
|
|
|
#include "lib/utils/Sqlite_utils.h"
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
typedef struct sqlite3 sqlite3;
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
/*
|
|
* Manages security to unique 32 bit id mapping across multiple processes
|
|
* Provides a callback when ids are safely persisted.
|
|
*/
|
|
|
|
typedef const std::function<SecurityKey(ExchangeKey)> KeyLookup;
|
|
typedef std::optional<KeyLookup> KeyLookupOption;
|
|
struct SecurityIDRecorderParams {
|
|
SecurityIDRecorderParams()
|
|
: dispatcher(), db(sqlite::null_db()), lookup_key() {}
|
|
SecurityIDRecorderParams(utils::Dispatcher &d)
|
|
: dispatcher(&d), db(sqlite::null_db()), lookup_key() {}
|
|
SecurityIDRecorderParams(utils::Dispatcher &d, KeyLookup &get_key)
|
|
: dispatcher(&d), db(sqlite::null_db()), lookup_key(get_key) {}
|
|
SecurityIDRecorderParams(sqlite3_ptr &db)
|
|
: dispatcher(), db(std::move(db)), lookup_key() {}
|
|
SecurityIDRecorderParams(sqlite3_ptr &db, KeyLookup &get_key)
|
|
: dispatcher(), db(std::move(db)), lookup_key(get_key) {}
|
|
utils::Dispatcher *dispatcher;
|
|
sqlite3_ptr db;
|
|
KeyLookupOption lookup_key;
|
|
};
|
|
struct SecurityIDRecorderState;
|
|
|
|
class SecurityIDRecorder {
|
|
public:
|
|
SecurityIDRecorder(SecurityIDRecorderParams);
|
|
~SecurityIDRecorder();
|
|
typedef std::function<void(const api::Security &)> Callback;
|
|
|
|
// Fast - for use by market data producers
|
|
void ensure_key(MDProducerHandle handle);
|
|
// Slow (copies security) - for use by services
|
|
void assign_key(const api::Security &security, const Callback &key_assigned);
|
|
|
|
// If a dispatcher is not provided you must call work periodically to do
|
|
// lookups and flush the reserved ids to disk.
|
|
void do_work();
|
|
|
|
void
|
|
bulk_insert(const std::vector<const api::Security *> &, const char *prefix);
|
|
|
|
private:
|
|
std::shared_ptr<SecurityIDRecorderState> _self;
|
|
|
|
void init();
|
|
};
|
|
|
|
} // namespace md
|
|
} // namespace bts
|
|
#else
|
|
#include "MDProducerHandle.h"
|
|
namespace bts {
|
|
namespace md {
|
|
class SecurityIDRecorder {
|
|
public:
|
|
void ensure_key(MDProducerHandle){};
|
|
};
|
|
} // namespace md
|
|
} // namespace bts
|
|
#endif
|