Merged in simplify_md_sdk (pull request #102)
Simplify md sdk Approved-by: Pedro Pinto <pedro.pinto+atlassian@bluetradesys.com>
This commit is contained in:
commit
97deb63deb
22 files changed
+285
-289
No files matched your search
Vendored
+1
-2
@@ -209,8 +209,7 @@
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd",
|
||||
// "args": ["test","Co2RelayTests"],
|
||||
"args": ["md", "run", "10"],
|
||||
"args": ["test","Co2Tests"],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/",
|
||||
"environment": [],
|
||||
|
||||
@@ -116,6 +116,7 @@ string dump_security(const FixedSecurity& security) {
|
||||
auto& leg = psec->legs[x];
|
||||
if (x > 0) res << ",";
|
||||
res << "{ 'key': " << leg.key
|
||||
<< ", 'exkey': " << leg.exchange_key
|
||||
<< ", 'quantity': " << leg.leg_quantity
|
||||
<< ", 'leg_price': " << leg.leg_price
|
||||
<< ", 'delta': " << leg.leg_delta
|
||||
|
||||
@@ -61,18 +61,6 @@ void MDProducerHandle::set_key(security_key key) {
|
||||
}
|
||||
}
|
||||
|
||||
void MDProducerHandle::set_legs(const std::vector<security_key>& legs) {
|
||||
bts_assert(cdata().security.type == api::SECURITYTYPE_SPREAD);
|
||||
FixedSpreadSecurityL* fss = (FixedSpreadSecurityL*)&cdata().security;
|
||||
bts_assert(fss->leg_count() == legs.size());
|
||||
for (size_t x = 0; x < legs.size(); ++x) {
|
||||
security_key key = legs[x];
|
||||
fss->legs[x].key = key;
|
||||
// attempt to validate that this FixedSpreadLeg is valid
|
||||
bts_assert_d(fss->legs[x].leg_quantity > 0);
|
||||
}
|
||||
}
|
||||
|
||||
void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask, uint16_t seq) {
|
||||
if (!has_depth()) {
|
||||
auto oldtime = hdata().time();
|
||||
|
||||
@@ -32,7 +32,6 @@ public:
|
||||
bts_assert_d(key == cdata().security.key);
|
||||
return key;
|
||||
}
|
||||
void set_legs(const std::vector<security_key>& legs);
|
||||
|
||||
void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0);
|
||||
void update_direct(TimeStamp time, const std::array<PriceSize,COCONUT_MAX_DEPTH>& bids, const std::array<PriceSize,COCONUT_MAX_DEPTH>& asks, uint32_t seq=0) {
|
||||
|
||||
+12
-11
@@ -34,16 +34,17 @@ sqlite3_ptr open_db() {
|
||||
}
|
||||
|
||||
// Create table if it does not exist
|
||||
auto dbptr = utils::open_db(db_path.c_str());
|
||||
auto dbptr = sqlite::open_db(db_path.c_str());
|
||||
bts_assert(dbptr);
|
||||
auto db = dbptr.get();
|
||||
sqlite3_busy_timeout(db, 9999);
|
||||
sqlite3_exec_nofail(db, "PRAGMA journal_mode=WAL");
|
||||
sqlite3_exec_nofail(db, "PRAGMA syncronous=0");
|
||||
sqlite::exec_nofail(db, "PRAGMA journal_mode=WAL");
|
||||
sqlite::exec_nofail(db, "PRAGMA syncronous=0");
|
||||
|
||||
sqlite3_exec_nofail(db,
|
||||
sqlite::exec_nofail(db,
|
||||
"CREATE TABLE IF NOT EXISTS securities (key INTEGER primary key, symbol TEXT NOT NULL, venue INTEGER NOT NULL, type INTEGER NOT NULL, security_blob BLOB NOT NULL, expiration INTEGER NOT NULL);");
|
||||
|
||||
sqlite3_exec_nofail(db,
|
||||
sqlite::exec_nofail(db,
|
||||
"CREATE TABLE IF NOT EXISTS metadata (key INTEGER PRIMARY KEY, data BLOB NOT NULL);");
|
||||
|
||||
return dbptr;
|
||||
@@ -67,7 +68,7 @@ size_t load_securities(
|
||||
|
||||
// Load all existing securities and call the callback
|
||||
auto db = dbptr.get();
|
||||
auto select_stmt = prepare_query(db, "select key,symbol,venue,type,security_blob from securities where expiration > ?");
|
||||
auto select_stmt = sqlite::prepare_query(db, "select key,symbol,venue,type,security_blob from securities where expiration > ?");
|
||||
auto select = select_stmt.get();
|
||||
sqlite3_bind_int(select, 1, sec_expiration_cut_off().to_short());
|
||||
auto result = sqlite3_step(select);
|
||||
@@ -112,8 +113,8 @@ size_t load_securities(
|
||||
|
||||
void store_if_new(const std::vector<Security*>& securities) {
|
||||
auto dbptr = open_db();
|
||||
auto insert = utils::prepare_query(dbptr, "insert or ignore into securities (key, symbol, venue, type, security_blob, expiration) values (?,?,?,?,?,?)");
|
||||
sqlite3_exec_nofail(dbptr, "BEGIN TRANSACTION");
|
||||
auto insert = sqlite::prepare_query(dbptr, "insert or ignore into securities (key, symbol, venue, type, security_blob, expiration) values (?,?,?,?,?,?)");
|
||||
sqlite::exec_nofail(dbptr, "BEGIN TRANSACTION");
|
||||
std::string buffer;
|
||||
for (const api::Security* psecurity : securities) {
|
||||
if (psecurity && psecurity->SerializeToString(&buffer)) {
|
||||
@@ -127,7 +128,7 @@ void store_if_new(const std::vector<Security*>& securities) {
|
||||
sqlite3_reset(insert.get());
|
||||
}
|
||||
}
|
||||
sqlite3_exec_nofail(dbptr, "COMMIT TRANSACTION");
|
||||
sqlite::exec_nofail(dbptr, "COMMIT TRANSACTION");
|
||||
}
|
||||
|
||||
|
||||
@@ -135,7 +136,7 @@ std::vector<api::synthetic::Metadata> load_metadata() {
|
||||
std::vector<api::synthetic::Metadata> values;
|
||||
auto dbptr = open_db();
|
||||
auto db = dbptr.get();
|
||||
auto select_stmt = prepare_query(db, "select key,data from metadata");
|
||||
auto select_stmt = sqlite::prepare_query(db, "select key,data from metadata");
|
||||
auto select = select_stmt.get();
|
||||
auto result = sqlite3_step(select);
|
||||
while(result == SQLITE_ROW) {
|
||||
@@ -156,7 +157,7 @@ std::vector<api::synthetic::Metadata> load_metadata() {
|
||||
}
|
||||
bool store_metadata(const api::synthetic::Metadata& data) {
|
||||
auto dbptr = open_db();
|
||||
auto insert = utils::prepare_query(dbptr, "insert or replace into metadata (key, data) values (?,?)");
|
||||
auto insert = sqlite::prepare_query(dbptr, "insert or replace into metadata (key, data) values (?,?)");
|
||||
|
||||
SQLITE_CHK(sqlite3_bind_int(insert.get(), 1, data.security_key()), dbptr);
|
||||
std::string buffer = data.SerializeAsString();
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/SecurityDB.h"
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <tbb/queuing_mutex.h> // for reservation
|
||||
#include <tbb/concurrent_queue.h>
|
||||
|
||||
#include "lib/config/protos/MarketData.pb.h"
|
||||
#include "lib/config/protos/system.pb.h"
|
||||
@@ -22,53 +22,68 @@ using std::size_t;
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
uint64_t exchange_id(const api::Security& security);
|
||||
|
||||
struct SecurityIDEntry {
|
||||
api::Security security;
|
||||
SecurityIDRecorder::Callback callback;
|
||||
};
|
||||
|
||||
struct SecurityIDRecorderState {
|
||||
SecurityIDRecorderState() :
|
||||
SecurityIDRecorderState(SecurityIDRecorderParams& p) :
|
||||
work(),
|
||||
pending_handle(),
|
||||
thread_id(),
|
||||
db(nullptr, sqlite3_close),
|
||||
timer(),
|
||||
id((size_t)this)
|
||||
lookup_key(p.lookup_key),
|
||||
unresolved_legs()
|
||||
{}
|
||||
|
||||
tbb::concurrent_queue<std::shared_ptr<SecurityIDEntry>> work;
|
||||
tbb::concurrent_queue<MDProducerHandle> pending_handle;
|
||||
std::thread::id thread_id;
|
||||
sqlite3_ptr db;
|
||||
unique_ptr<DispatcherTimer> timer;
|
||||
size_t id;
|
||||
|
||||
KeyLookupOption lookup_key;
|
||||
std::map<ExchangeKey, std::vector<FixedSpreadLeg*>> unresolved_legs;
|
||||
|
||||
// Oh no, I got methods in my state
|
||||
|
||||
void run(utils::Dispatcher& dispatcher) {
|
||||
timer = std::make_unique<utils::DispatcherTimer>(
|
||||
dispatcher, 500,
|
||||
dispatcher, 200,
|
||||
[=]() { do_work(); });
|
||||
timer->start();
|
||||
}
|
||||
void do_work() {
|
||||
thread_id = std::this_thread::get_id();
|
||||
work_spreads();
|
||||
work_handles();
|
||||
work_db();
|
||||
}
|
||||
void work_spreads();
|
||||
void work_handles();
|
||||
void work_db();
|
||||
|
||||
void check_access() { bts_assert(thread_id == std::this_thread::get_id()); }
|
||||
void assign_key(const api::Security& security, const SecurityIDRecorder::Callback& key_assigned);
|
||||
void add_unresolved_spread(MDProducerHandle& handle);
|
||||
security_key search(const std::string& descriptor);
|
||||
security_key insert(const std::string& descriptor, SecurityType);
|
||||
};
|
||||
|
||||
SecurityIDRecorder::SecurityIDRecorder():
|
||||
_self(new SecurityIDRecorderState())
|
||||
SecurityIDRecorder::SecurityIDRecorder(SecurityIDRecorderParams params):
|
||||
_self(new SecurityIDRecorderState(params))
|
||||
{
|
||||
if (params.db) {
|
||||
_self->db = std::move(params.db);
|
||||
}
|
||||
init();
|
||||
}
|
||||
|
||||
SecurityIDRecorder::SecurityIDRecorder(utils::Dispatcher& dispatcher):
|
||||
_self(new SecurityIDRecorderState())
|
||||
{
|
||||
init();
|
||||
_self->run(dispatcher);
|
||||
if (params.dispatcher) {
|
||||
_self->run(*params.dispatcher);
|
||||
}
|
||||
}
|
||||
|
||||
SecurityIDRecorder::~SecurityIDRecorder() { }
|
||||
@@ -81,13 +96,16 @@ void SecurityIDRecorder::init() {
|
||||
boost::filesystem::create_directories(db_dir);
|
||||
}
|
||||
|
||||
_self->db = open_db(db_path.c_str());
|
||||
if (!_self->db) {
|
||||
_self->db = sqlite::open_db(db_path.c_str());
|
||||
bts_assert(_self->db);
|
||||
}
|
||||
|
||||
// Set a long busy timeout
|
||||
sqlite3_busy_timeout(_self->db.get(), 9999);
|
||||
sqlite3_exec_nofail(_self->db, "PRAGMA journal_mode=WAL");
|
||||
sqlite3_exec_nofail(_self->db, "PRAGMA syncronous=0");
|
||||
sqlite3_exec_nofail(_self->db, "CREATE TABLE IF NOT EXISTS security_keys (descriptor STRING NOT NULL PRIMARY KEY, type int);");
|
||||
sqlite::exec_nofail(_self->db, "PRAGMA journal_mode=WAL");
|
||||
sqlite::exec_nofail(_self->db, "PRAGMA syncronous=0");
|
||||
sqlite::exec_nofail(_self->db, "CREATE TABLE IF NOT EXISTS security_keys (descriptor STRING NOT NULL PRIMARY KEY, type int);");
|
||||
}
|
||||
|
||||
void SecurityIDRecorder::ensure_key(MDProducerHandle handle) {
|
||||
@@ -95,6 +113,15 @@ void SecurityIDRecorder::ensure_key(MDProducerHandle handle) {
|
||||
}
|
||||
|
||||
void SecurityIDRecorder::assign_key(const api::Security& security, const SecurityIDRecorder::Callback& callback) {
|
||||
if (security.Contract_case() == Security::ContractCase::kSpread) {
|
||||
for(const auto& leg : security.spread().legs()) {
|
||||
if (leg.security().id() == invalid_security_key) {
|
||||
LOG(md,Error, "Security {} has legs which will never be resolved.", security.has_glbx() ? security.glbx().security_desc() : security.symbol());
|
||||
// No point in trying to add this to the queue
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
_self->assign_key(security, callback);
|
||||
}
|
||||
|
||||
@@ -111,6 +138,46 @@ void SecurityIDRecorder::do_work() {
|
||||
_self->do_work();
|
||||
}
|
||||
|
||||
void SecurityIDRecorderState::add_unresolved_spread(MDProducerHandle& handle) {
|
||||
bts_assert_d(lookup_key);
|
||||
FixedSpreadSecurityXL* pspread = static_cast<FixedSpreadSecurityXL*>(handle.mutable_security());
|
||||
if (pspread != nullptr) {
|
||||
for (size_t x=0; x < pspread->leg_count(); ++x) {
|
||||
auto& leg = pspread->legs[x];
|
||||
bts_assert_d(leg.exchange_key.value() != 0);
|
||||
if (leg.key == invalid_security_key) {
|
||||
// Because leg is a reference to shared memory we know it will not move
|
||||
for (const auto* existing : unresolved_legs[leg.exchange_key]) {
|
||||
if (existing == &leg) goto next;
|
||||
}
|
||||
unresolved_legs[leg.exchange_key].push_back(&leg);
|
||||
}
|
||||
next:;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SecurityIDRecorderState::work_spreads() {
|
||||
check_access();
|
||||
if (!lookup_key) return;
|
||||
KeyLookup& lookup = *lookup_key;
|
||||
int count = 0;
|
||||
for (auto iter = unresolved_legs.begin(); count < 500 && iter != unresolved_legs.end();) {
|
||||
const auto& pair = *iter;
|
||||
++count;
|
||||
SecurityKey key = lookup(pair.first);
|
||||
if (key.value() != invalid_security_key) {
|
||||
for (FixedSpreadLeg* leg : pair.second) {
|
||||
leg->key = key.value();
|
||||
}
|
||||
iter = unresolved_legs.erase(iter);
|
||||
}
|
||||
else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool has_all_leg_keys(const MDProducerHandle& handle) {
|
||||
if (handle.security().type == SecurityType::SECURITYTYPE_SPREAD) {
|
||||
const FixedSpreadSecurityXL* pspread = static_cast<const FixedSpreadSecurityXL*>(&handle.security());
|
||||
@@ -120,11 +187,12 @@ bool has_all_leg_keys(const MDProducerHandle& handle) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// SyntheticFixedSecurity is guarunteed to have all of its legs
|
||||
// SyntheticFixedSecurity is guaranteed to have all of its legs
|
||||
return true;
|
||||
}
|
||||
|
||||
void SecurityIDRecorderState::work_handles() {
|
||||
check_access();
|
||||
Security security;
|
||||
MDProducerHandle handle;
|
||||
vector<MDProducerHandle> retry;
|
||||
@@ -133,6 +201,8 @@ void SecurityIDRecorderState::work_handles() {
|
||||
security.Clear();
|
||||
// if a spread does not have all of its legs we need to wait
|
||||
if (!has_all_leg_keys(handle)) {
|
||||
// add
|
||||
add_unresolved_spread(handle);
|
||||
retry.push_back(handle);
|
||||
}
|
||||
else {
|
||||
@@ -150,6 +220,7 @@ void SecurityIDRecorderState::work_handles() {
|
||||
}
|
||||
|
||||
void SecurityIDRecorderState::work_db() {
|
||||
check_access();
|
||||
// Drain the work queue
|
||||
// Query the database for each security. If it has an ID set it
|
||||
// If it does not have an ID add it to a list
|
||||
@@ -184,7 +255,7 @@ void SecurityIDRecorderState::work_db() {
|
||||
new_descriptors.push_back(descriptor);
|
||||
++count;
|
||||
// Limit the amount of time we spend writing to sqlite during startup to avoid blocking the heartbeat
|
||||
if (count > 500) {
|
||||
if (count > 200) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -196,7 +267,7 @@ void SecurityIDRecorderState::work_db() {
|
||||
|
||||
|
||||
if (!new_entries.empty()) {
|
||||
sqlite3_exec_nofail(db.get(), "BEGIN TRANSACTION");
|
||||
sqlite::exec_nofail(db.get(), "BEGIN TRANSACTION");
|
||||
|
||||
for (size_t x = 0; x < new_entries.size(); ++x) {
|
||||
Security& security = new_entries[x]->security;
|
||||
@@ -205,7 +276,7 @@ void SecurityIDRecorderState::work_db() {
|
||||
security.set_id(key);
|
||||
}
|
||||
|
||||
sqlite3_exec_nofail(db.get(), "COMMIT TRANSACTION");
|
||||
sqlite::exec_nofail(db.get(), "COMMIT TRANSACTION");
|
||||
}
|
||||
|
||||
// record the securities
|
||||
@@ -231,10 +302,10 @@ void SecurityIDRecorderState::work_db() {
|
||||
|
||||
void SecurityIDRecorder::bulk_insert(const std::vector<const api::Security*>& data, const char* prefix) {
|
||||
if (data.size() == 0) return;
|
||||
sqlite3_exec_nofail(_self->db.get(), "BEGIN TRANSACTION");
|
||||
sqlite::exec_nofail(_self->db.get(), "BEGIN TRANSACTION");
|
||||
std::string descriptor;
|
||||
{
|
||||
auto insert_stmt = prepare_query(_self->db, "INSERT INTO security_keys (rowid, descriptor, type) values (?, ?, ?)");
|
||||
auto insert_stmt = sqlite::prepare_query(_self->db, "INSERT INTO security_keys (rowid, descriptor, type) values (?, ?, ?)");
|
||||
for (const api::Security* sec : data) {
|
||||
descriptor = prefix + security_descriptor(*sec);
|
||||
auto type = securitytype_of(*sec);
|
||||
@@ -246,11 +317,11 @@ void SecurityIDRecorder::bulk_insert(const std::vector<const api::Security*>& da
|
||||
sqlite3_reset(insert_stmt.get());
|
||||
}
|
||||
}
|
||||
sqlite3_exec_nofail(_self->db.get(), "COMMIT TRANSACTION");
|
||||
sqlite::exec_nofail(_self->db.get(), "COMMIT TRANSACTION");
|
||||
}
|
||||
|
||||
security_key SecurityIDRecorderState::search(const std::string& descriptor) {
|
||||
auto select_stmt = prepare_query(db, "SELECT rowid from security_keys where descriptor=? LIMIT 1;");
|
||||
auto select_stmt = sqlite::prepare_query(db, "SELECT rowid from security_keys where descriptor=? LIMIT 1;");
|
||||
sqlite3_bind_text(select_stmt.get(), 1, descriptor.c_str(), descriptor.size(), SQLITE_STATIC);
|
||||
int result = sqlite3_step(select_stmt.get());
|
||||
if (result == SQLITE_DONE || result == SQLITE_ROW) {
|
||||
@@ -260,7 +331,7 @@ security_key SecurityIDRecorderState::search(const std::string& descriptor) {
|
||||
}
|
||||
|
||||
security_key SecurityIDRecorderState::insert(const std::string& descriptor, SecurityType type) {
|
||||
auto insert_stmt = prepare_query(db, "INSERT INTO security_keys (descriptor, type) values (?, ?)");
|
||||
auto insert_stmt = sqlite::prepare_query(db, "INSERT INTO security_keys (descriptor, type) values (?, ?)");
|
||||
sqlite3_bind_text(insert_stmt.get(), 1, descriptor.c_str(), descriptor.size(), SQLITE_STATIC);
|
||||
sqlite3_bind_int(insert_stmt.get(), 2, (int)type);
|
||||
int result = sqlite3_step(insert_stmt.get());
|
||||
|
||||
@@ -3,11 +3,14 @@
|
||||
#include "common.h"
|
||||
#include "MDProducerHandle.h"
|
||||
|
||||
#include "lib/utils/Sqlite_utils.h"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
#include <tbb/concurrent_queue.h>
|
||||
#include <optional>
|
||||
|
||||
typedef struct sqlite3 sqlite3;
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
@@ -17,13 +20,24 @@ namespace md {
|
||||
* 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();
|
||||
// Automatically call do_work on this dispatcher
|
||||
SecurityIDRecorder(utils::Dispatcher&);
|
||||
SecurityIDRecorder(SecurityIDRecorderParams);
|
||||
~SecurityIDRecorder();
|
||||
typedef std::function<void(const api::Security&)> Callback;
|
||||
|
||||
@@ -32,7 +46,7 @@ public:
|
||||
// Slow (copies security) - for use by services
|
||||
void assign_key(const api::Security& security, const Callback& key_assigned);
|
||||
|
||||
// Someone must call work periodically to do lookups and flush the reserved ids to disk.
|
||||
// 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);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#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>
|
||||
@@ -26,8 +25,7 @@ class SimpleSecurityIndex
|
||||
{
|
||||
public:
|
||||
SimpleSecurityIndex():
|
||||
_map(),
|
||||
_spread_resolver()
|
||||
_map()
|
||||
{}
|
||||
~SimpleSecurityIndex() {}
|
||||
|
||||
@@ -79,35 +77,11 @@ public:
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
@@ -120,8 +94,7 @@ class ConcurrentSecurityIndex
|
||||
{
|
||||
public:
|
||||
ConcurrentSecurityIndex() :
|
||||
_map(),
|
||||
_spread_resolver()
|
||||
_map()
|
||||
{}
|
||||
~ConcurrentSecurityIndex() {}
|
||||
|
||||
@@ -171,33 +144,10 @@ public:
|
||||
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
|
||||
|
||||
@@ -94,13 +94,14 @@ SettlementRecorder::SettlementRecorder(Platform platform) :
|
||||
}
|
||||
|
||||
// initialize database
|
||||
_self->db = open_db(db_path.c_str());
|
||||
_self->db = sqlite::open_db(db_path.c_str());
|
||||
bts_assert_msg(_self->db, db_path.c_str());
|
||||
sqlite3* db = _self->db.get();
|
||||
sqlite3_busy_timeout(db, 9999);
|
||||
sqlite3_exec_nofail(db, "PRAGMA journal_mode=WAL");
|
||||
sqlite3_exec_nofail(db, "PRAGMA syncronous=0");
|
||||
sqlite::exec_nofail(db, "PRAGMA journal_mode=WAL");
|
||||
sqlite::exec_nofail(db, "PRAGMA syncronous=0");
|
||||
// key=(security_key+date)
|
||||
sqlite3_exec_nofail(db,
|
||||
sqlite::exec_nofail(db,
|
||||
"CREATE TABLE IF NOT EXISTS settlements2 (key INTEGER NOT NULL, date INTEGER NOT NULL, mantissa INTEGER NOT NULL, exponent INTEGER NOT NULL, PRIMARY KEY (key,date));");
|
||||
|
||||
// Delete any records > 10 days old
|
||||
@@ -108,11 +109,11 @@ SettlementRecorder::SettlementRecorder(Platform platform) :
|
||||
auto encoded = datetime::ll_to_date(datetime::ptime_to_ll(cutoff)).to_short();
|
||||
char query[256];
|
||||
sprintf(query, "delete from settlements2 where date < %i", encoded);
|
||||
sqlite3_exec_nofail(db, query);
|
||||
sqlite::exec_nofail(db, query);
|
||||
|
||||
// statements
|
||||
_self->select_stmt = prepare_query(db, "select mantissa,exponent,date from settlements2 where key=? order by date desc limit 2");
|
||||
_self->insert_stmt = prepare_query(db, "insert or replace into settlements2(key, date, mantissa,exponent) values(?,?,?,?)");
|
||||
_self->select_stmt = sqlite::prepare_query(db, "select mantissa,exponent,date from settlements2 where key=? order by date desc limit 2");
|
||||
_self->insert_stmt = sqlite::prepare_query(db, "insert or replace into settlements2(key, date, mantissa,exponent) values(?,?,?,?)");
|
||||
}
|
||||
|
||||
SettlementRecorder::~SettlementRecorder() { }
|
||||
|
||||
@@ -251,6 +251,20 @@ size_t num_legs(const Security& sec) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t exchange_id(const Security& security) {
|
||||
switch(security.ExchangeData_case()) {
|
||||
case Security::ExchangeDataCase::kCboe: return security.cboe().exchange_id();
|
||||
case Security::ExchangeDataCase::kGlbx: return security.glbx().exchange_id();
|
||||
case Security::ExchangeDataCase::kCfe: return security.cfe().exchange_id();
|
||||
case Security::ExchangeDataCase::kIce: bts_assert(false);
|
||||
// Debatable whether this should be an error or not.
|
||||
// If is possible that we might create a spread with no exchange in the future
|
||||
// and if we did this would be a runtime error.
|
||||
case Security::ExchangeDataCase::EXCHANGEDATA_NOT_SET: return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void convert_security(const Security& src, FixedSecurity& dest, bool set_security_key) {
|
||||
dest.key = set_security_key ? src.id() : invalid_security_key;
|
||||
dest.type = securitytype_of_contractcase(src.Contract_case());
|
||||
@@ -272,7 +286,9 @@ void convert_security(const Security& src, FixedSecurity& dest, bool set_securit
|
||||
case Security::ExchangeDataCase::kCfe:
|
||||
dest.cboe_class_key = src.cfe().class_key();
|
||||
break;
|
||||
default:
|
||||
case Security::ExchangeDataCase::kIce:
|
||||
bts_assert_d(false);
|
||||
case Security::ExchangeDataCase::EXCHANGEDATA_NOT_SET:
|
||||
break;
|
||||
}
|
||||
dest.price_break = src.contract_spec().price_break() / src.contract_spec().price_multiplier();
|
||||
@@ -307,6 +323,7 @@ void convert_security(const Security& src, FixedSecurity& dest, bool set_securit
|
||||
for (int x = 0; x < src.spread().legs_size(); ++x) {
|
||||
const auto& src_leg = src.spread().legs(x);
|
||||
spread.legs[x].key = set_security_key ? src_leg.security().id() : invalid_security_key;
|
||||
spread.legs[x].exchange_key = ExchangeKey(exchange_id(src_leg.security()));
|
||||
spread.legs[x].leg_side = src_leg.side();
|
||||
spread.legs[x].leg_quantity = src_leg.quantity();
|
||||
spread.legs[x].leg_price = src_leg.price_multiplier();
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "lib/utils/bts_array_size.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include "lib/utils/ValueWrapper.h"
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
@@ -13,6 +14,9 @@
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
using SecurityKey = utils::ValueWrapper<struct SecurityKeyTag,uint32_t>;
|
||||
using ExchangeKey = utils::ValueWrapper<struct ExchangeKeyTag,uint64_t>;
|
||||
|
||||
GCC_DIAG_OFF("-Weffc++") // No Virtual Destructors by design
|
||||
|
||||
#define STRING_PROPERTY(NAME) const char* NAME() const { return _##NAME; } \
|
||||
@@ -95,6 +99,7 @@ struct BTS_EXPORT FixedSpreadLeg
|
||||
{
|
||||
FixedSpreadLeg();
|
||||
security_key key;
|
||||
ExchangeKey exchange_key;
|
||||
Side leg_side;
|
||||
double leg_quantity;
|
||||
double leg_price;
|
||||
@@ -180,6 +185,8 @@ void convert_security(const Security& src, FixedSecurity& dest, bool set_securit
|
||||
SecurityType securitytype_of_contractcase(Security::ContractCase cc);
|
||||
SecurityType securitytype_of(const Security&);
|
||||
size_t num_legs(const Security&);
|
||||
uint64_t exchange_id(const Security& security);
|
||||
|
||||
|
||||
// Stack allocates a security of the given type
|
||||
template <class C>
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include <vector>
|
||||
#include <tbb/concurrent_queue.h>
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
|
||||
/*
|
||||
* SpreadResolved accepts exchange spread definitions and resolves the legs
|
||||
*/
|
||||
|
||||
template <typename ExchangeKey>
|
||||
class SpreadResolver {
|
||||
struct ResolverEntry {
|
||||
ResolverEntry(ExchangeKey key) : exchange_key(key), legs() {}
|
||||
ResolverEntry() : exchange_key(), legs() {}
|
||||
ExchangeKey exchange_key;
|
||||
std::vector<ExchangeKey> legs;
|
||||
};
|
||||
public:
|
||||
SpreadResolver() : _queue() {}
|
||||
template <class LegContainer>
|
||||
void add(ExchangeKey key, LegContainer legs) {
|
||||
ResolverEntry entry(key);
|
||||
entry.legs.insert(end(entry.legs), begin(legs), end(legs));
|
||||
_queue.push(entry);
|
||||
}
|
||||
|
||||
// callback should accept const ExchangeKey& key, vector<security_key>& legs)
|
||||
// When the callback is called the legs will be completely resolved
|
||||
template <class MapKey, class CallBack>
|
||||
size_t resolve(MapKey keymap, CallBack on_resolved) {
|
||||
// Drain the queue and call keymap to resolve each leg
|
||||
size_t result = 0;
|
||||
std::vector<ResolverEntry> unresolved;
|
||||
std::vector<security_key> legs;
|
||||
while(!_queue.empty()) {
|
||||
legs.clear();
|
||||
ResolverEntry entry;
|
||||
if(_queue.try_pop(entry)) {
|
||||
// Attempt to look up each spread leg
|
||||
for(ExchangeKey key : entry.legs) {
|
||||
security_key result = keymap(key);
|
||||
if (result == invalid_security_key) {
|
||||
unresolved.push_back(entry);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
legs.push_back(result);
|
||||
}
|
||||
}
|
||||
if (legs.size() == entry.legs.size()) {
|
||||
++result;
|
||||
on_resolved(entry.exchange_key, legs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If any legs remain unresolved push the entry back in the queue for later
|
||||
for(auto& entry : unresolved) {
|
||||
_queue.push(entry);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t count() const { return _queue.unsafe_size(); }
|
||||
private:
|
||||
BTS_NO_COPY(SpreadResolver);
|
||||
tbb::concurrent_queue<ResolverEntry> _queue;
|
||||
};
|
||||
|
||||
|
||||
|
||||
}}//namespace
|
||||
|
||||
@@ -25,7 +25,7 @@ typedef SyntheticSecurityServiceBase::MetadataStatefulPublisher MetadataPublishe
|
||||
|
||||
struct SyntheticSecurityService::Pimpl {
|
||||
Pimpl(const MetadataStatefulPublisher& fn) :
|
||||
recorder(utils::Dispatcher::background()),
|
||||
recorder({utils::Dispatcher::background()}),
|
||||
synthetics(), on_security(),
|
||||
meta(), on_metadata(), metadata_publisher(fn)
|
||||
{
|
||||
|
||||
@@ -130,7 +130,7 @@ void replay_recording(istream& in, IRelayWriter& writer, int32_t naggle_ms) {
|
||||
CoconutReplayHeader header;
|
||||
size_t count = 0;
|
||||
#ifndef _WIN32
|
||||
md::SecurityIDRecorder idrec;
|
||||
md::SecurityIDRecorder idrec {{}};
|
||||
#endif
|
||||
if (readbuf(in, &header) && header.magic() == magic && header.version() == 3) {
|
||||
co2::MDReply message;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "lib/metrics/metrics.h"
|
||||
|
||||
#include "lib/config/protos/MarketData.pb.h"
|
||||
#include "lib/utils/Sqlite_utils.h"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@@ -27,7 +28,6 @@
|
||||
#include <thread>
|
||||
#include <iterator>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace bts {
|
||||
@@ -151,6 +151,7 @@ void assign_ids(vector<MDProducerHandle>::iterator begin, vector<MDProducerHandl
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Co2Tests
|
||||
{
|
||||
public:
|
||||
@@ -171,12 +172,20 @@ public:
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
void test_security_id_recorder() {
|
||||
void test_security_id_recorder(const std::string& db_name) {
|
||||
// Verify callbacks happen after work complete
|
||||
SecurityIDRecorder idsvc, idsvc2;
|
||||
auto db1 = sqlite::open_db(db_name.c_str());
|
||||
REQUIRE(db1);
|
||||
auto db2 = sqlite::open_db(db_name.c_str());
|
||||
REQUIRE(db2);
|
||||
|
||||
SecurityIDRecorder idsvc{{db1}}, idsvc2{{db2}};
|
||||
security_key result = invalid_security_key;
|
||||
std::string desc = "FOOX 1";
|
||||
Security s1 = mkoption("FOOX", invalid_security_key, 2018, 1, 20, 1850);
|
||||
Security f1 = mkfuture("FOO", invalid_security_key, 2018, 1, 20);
|
||||
idsvc.assign_key(f1, [](const api::Security& s) {
|
||||
REQUIRE(s.id() != invalid_security_key);
|
||||
});
|
||||
idsvc.assign_key(s1, [&result](const api::Security& s) {
|
||||
result = s.id();
|
||||
});
|
||||
@@ -188,7 +197,66 @@ public:
|
||||
result2 = s.id();
|
||||
});
|
||||
idsvc2.do_work();
|
||||
REQUIRE(result == result2);
|
||||
|
||||
if (db_name == ":memory:") {
|
||||
// This is a meta-test to make sure we don't accidently generate the same key sequence
|
||||
// when writing to different databases.
|
||||
REQUIRE(result != result2);
|
||||
}
|
||||
else {
|
||||
REQUIRE(result == result2);
|
||||
}
|
||||
}
|
||||
|
||||
void test_security_id_recorder_spreads() {
|
||||
// Test that security id recorder can resolve spread legs
|
||||
|
||||
// Set up "shared memory"
|
||||
auto cold_header = std::make_shared<ColdHeader>();
|
||||
auto hot_header = std::make_shared<HotHeader>();
|
||||
|
||||
// Create a spread with 2 legs
|
||||
auto f1 = mkfuture("FO1", invalid_security_key, 2018, 1, 17);
|
||||
f1.mutable_glbx()->set_exchange_id(100);
|
||||
REQUIRE(f1.id() == invalid_security_key);
|
||||
auto f1handle = std::make_unique<Co2TestInstrument> (f1, cold_header, hot_header);
|
||||
|
||||
auto f2 = mkfuture("FO2", invalid_security_key, 2018, 1, 20);
|
||||
f2.mutable_glbx()->set_exchange_id(200);
|
||||
auto f2handle = std::make_unique<Co2TestInstrument> (f2, cold_header, hot_header);
|
||||
|
||||
auto s1 = mkspread("FOO", invalid_security_key, { f1, f2 });
|
||||
auto s1handle = std::make_unique<Co2TestInstrument> (s1, cold_header, hot_header);
|
||||
|
||||
// Add spread to SecurityIDRecorder and work it
|
||||
auto db = sqlite::open_db(":memory:");
|
||||
REQUIRE(db);
|
||||
|
||||
SecurityIDRecorder idsvc { {db, [&](ExchangeKey k) -> SecurityKey {
|
||||
for (const auto fut : { f1handle->producer(), f2handle->producer() }) {
|
||||
if (k.value() == fut.security().numeric_id) return SecurityKey(fut.key());
|
||||
}
|
||||
return SecurityKey(invalid_security_key);
|
||||
} } };
|
||||
|
||||
auto workwork = [&]() { idsvc.do_work(); idsvc.do_work(); };
|
||||
idsvc.ensure_key(s1handle->producer());
|
||||
workwork();
|
||||
|
||||
// Expect no key
|
||||
REQUIRE(s1handle->consumer().key() == invalid_security_key);
|
||||
|
||||
// Add leg spreads
|
||||
idsvc.ensure_key(f1handle->producer());
|
||||
idsvc.ensure_key(f2handle->producer());
|
||||
workwork();
|
||||
|
||||
// Expect legs to have keys
|
||||
REQUIRE(f1handle->consumer().key() != invalid_security_key);
|
||||
REQUIRE(f2handle->consumer().key() != invalid_security_key);
|
||||
|
||||
// Expect spread to have keys
|
||||
REQUIRE(s1handle->consumer().key() != invalid_security_key);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -518,50 +586,6 @@ public:
|
||||
REQUIRE(bbo_count > 0);
|
||||
}
|
||||
|
||||
void test_spread_resolver() {
|
||||
SpreadResolver<string> resolver;
|
||||
vector<string> legs;
|
||||
legs.push_back("1");
|
||||
legs.push_back("2");
|
||||
legs.push_back("3");
|
||||
legs.push_back("4");
|
||||
resolver.add("1234", legs);
|
||||
legs.clear();
|
||||
legs.push_back("4");
|
||||
legs.push_back("5");
|
||||
resolver.add("45", legs);
|
||||
auto key_map = [](string& key) -> security_key {
|
||||
if (key == "1") return 1;
|
||||
if (key == "2") return 2;
|
||||
if (key == "3") return 3;
|
||||
if (key == "4") return 4;
|
||||
if (key == "5") return invalid_security_key;
|
||||
REQUIRE(false);
|
||||
return 0;
|
||||
};
|
||||
auto key_map2 = [](string& key) -> security_key {
|
||||
if (key == "4") return 4;
|
||||
if (key == "5") return 5;
|
||||
REQUIRE(false);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// 2 unresolved spreads
|
||||
REQUIRE(resolver.count() == 2);
|
||||
resolver.resolve(key_map, [&](const string& key, const vector<security_key>& legs) {
|
||||
REQUIRE(key == "1234");
|
||||
REQUIRE(legs.size() == 4);
|
||||
});
|
||||
// 1 spread remains
|
||||
REQUIRE(resolver.count() == 1);
|
||||
resolver.resolve(key_map2, [&](const string& key, const vector<security_key>& legs) {
|
||||
REQUIRE(key == "45");
|
||||
REQUIRE(legs.size() == 2);
|
||||
});
|
||||
REQUIRE(resolver.count() == 0);
|
||||
}
|
||||
|
||||
|
||||
void test_no_double_security_callback() {
|
||||
MDProducer producer(TEST_PLATFORM);
|
||||
vector<MDProducerHandle> producers = create_test_securities(producer);
|
||||
@@ -1002,7 +1026,11 @@ TEST_CASE("CO2Tests","[md]") {
|
||||
|
||||
#ifndef WIN32
|
||||
SECTION("test_security_id_recorder") {
|
||||
co2_tests->test_security_id_recorder();
|
||||
co2_tests->test_security_id_recorder(":memory:");
|
||||
co2_tests->test_security_id_recorder("file::memory:?cache=shared");
|
||||
}
|
||||
SECTION("test_security_id_recorder_spreads") {
|
||||
co2_tests->test_security_id_recorder_spreads();
|
||||
}
|
||||
#endif
|
||||
SECTION("test_price_size") {
|
||||
@@ -1026,9 +1054,6 @@ TEST_CASE("CO2Tests","[md]") {
|
||||
SECTION("test_consumer_defer_handle2") {
|
||||
co2_tests->test_consumer_defer_handle2();
|
||||
}
|
||||
SECTION("test_spread_resolver") {
|
||||
co2_tests->test_spread_resolver();
|
||||
}
|
||||
SECTION("test_no_double_security_callback") {
|
||||
co2_tests->test_no_double_security_callback();
|
||||
}
|
||||
|
||||
@@ -57,42 +57,43 @@ void SqliteStore::init_db() {
|
||||
LOG(persist, Warn, "{} database does not exist yet. Creating it at {}",
|
||||
me->db, path.string());
|
||||
|
||||
me->conn = utils::open_db(path.c_str());
|
||||
me->conn = sqlite::open_db(path.c_str());
|
||||
bts_assert_msg(me->conn, path.c_str());
|
||||
|
||||
const char *create_schema_sql =
|
||||
"create table store ( ns text, key text, value text)";
|
||||
utils::sqlite3_exec_nofail(me->conn, create_schema_sql);
|
||||
sqlite::exec_nofail(me->conn, create_schema_sql);
|
||||
|
||||
const char *key_index_sql =
|
||||
"create unique index key_index on store(ns,key)";
|
||||
utils::sqlite3_exec_nofail(me->conn, key_index_sql);
|
||||
sqlite::exec_nofail(me->conn, key_index_sql);
|
||||
|
||||
const char *ns_index_sql = "create index ns_index on store(ns)";
|
||||
utils::sqlite3_exec_nofail(me->conn, ns_index_sql);
|
||||
sqlite::exec_nofail(me->conn, ns_index_sql);
|
||||
} else {
|
||||
me->conn = utils::open_db(path.c_str());
|
||||
me->conn = sqlite::open_db(path.c_str());
|
||||
}
|
||||
|
||||
// Asynchronous for speed
|
||||
utils::sqlite3_exec_nofail(me->conn, "PRAGMA synchronous = 0;");
|
||||
utils::sqlite3_exec_nofail(me->conn, "PRAGMA journal_mode=WAL");
|
||||
sqlite::exec_nofail(me->conn, "PRAGMA synchronous = 0;");
|
||||
sqlite::exec_nofail(me->conn, "PRAGMA journal_mode=WAL");
|
||||
|
||||
// prepare insert statement
|
||||
const char *insert_sql = "insert into store (ns, key, value) values(?,?,?)";
|
||||
me->insert = utils::prepare_query(me->conn, insert_sql);
|
||||
me->insert = sqlite::prepare_query(me->conn, insert_sql);
|
||||
|
||||
// prepare update statement
|
||||
const char *update_sql =
|
||||
"update store set value = ? where ns = ? and key = ?";
|
||||
me->update = utils::prepare_query(me->conn, update_sql);
|
||||
me->update = sqlite::prepare_query(me->conn, update_sql);
|
||||
|
||||
// prepare remove statement
|
||||
const char *remove_all_sql = "delete from store where ns = ?";
|
||||
me->remove_all = utils::prepare_query(me->conn, remove_all_sql);
|
||||
me->remove_all = sqlite::prepare_query(me->conn, remove_all_sql);
|
||||
|
||||
// prepare remove statement
|
||||
const char *remove_sql = "delete from store where ns = ? and key = ?";
|
||||
me->remove = utils::prepare_query(me->conn, remove_sql);
|
||||
me->remove = sqlite::prepare_query(me->conn, remove_sql);
|
||||
}
|
||||
|
||||
SqliteStore::~SqliteStore() {}
|
||||
@@ -100,7 +101,7 @@ SqliteStore::~SqliteStore() {}
|
||||
SqliteStore::Data SqliteStore::retrieve_all() {
|
||||
SqliteStore::Data result;
|
||||
sqlite3_stmt_ptr query =
|
||||
utils::prepare_query(me->conn, "select ns,key,value from store");
|
||||
sqlite::prepare_query(me->conn, "select ns,key,value from store");
|
||||
|
||||
int sqlite_result;
|
||||
do {
|
||||
@@ -125,7 +126,7 @@ std::pair<bool, std::string> SqliteStore::retrieve_one(std::string const &ns,
|
||||
std::string const &k) {
|
||||
auto query_str = fmt::format(
|
||||
"select ns,key,value from store where ns='{}' and key='{}'", ns, k);
|
||||
sqlite3_stmt_ptr query = utils::prepare_query(me->conn, query_str.c_str());
|
||||
sqlite3_stmt_ptr query = sqlite::prepare_query(me->conn, query_str.c_str());
|
||||
|
||||
std::pair<bool, std::string> result{false, ""};
|
||||
|
||||
|
||||
@@ -3,22 +3,27 @@
|
||||
#include "bts_assert.h"
|
||||
#include <cstring>
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
namespace sqlite {
|
||||
|
||||
void sqlite3_exec_nofail(sqlite3* db, const char* sql) {
|
||||
void exec_nofail(sqlite3* db, const char* sql) {
|
||||
char* error_message = nullptr;
|
||||
int result = sqlite3_exec(db, sql, nullptr, nullptr, &error_message);
|
||||
bts_assert_msg(result == SQLITE_OK, error_message);
|
||||
}
|
||||
|
||||
void sqlite3_exec_nofail(sqlite3_ptr & db, const char * sql) {
|
||||
return sqlite3_exec_nofail(db.get(), sql);
|
||||
void exec_nofail(sqlite3_ptr & db, const char * sql) {
|
||||
return exec_nofail(db.get(), sql);
|
||||
}
|
||||
|
||||
sqlite3_ptr open_db(const char* path) {
|
||||
sqlite3* temp;
|
||||
bts_assert(sqlite3_open(path, &temp) == SQLITE_OK);
|
||||
return sqlite3_ptr(temp, sqlite3_close);
|
||||
if (sqlite3_open(path, &temp) == SQLITE_OK) {
|
||||
return sqlite3_ptr(temp, sqlite3_close);
|
||||
}
|
||||
else {
|
||||
LOG(Gen, Error, "Failed to open database {}", std::string(path));
|
||||
return null_db();
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_stmt_ptr prepare_query(sqlite3 * db, const char * query) {
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
#pragma once
|
||||
#ifndef WIN32
|
||||
#include <sqlite/sqlite3.h>
|
||||
#include <memory>
|
||||
#include "lib/utils/bts_export.h"
|
||||
#include <sqlite/sqlite3.h>
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
typedef struct sqlite3 sqlite3;
|
||||
typedef struct sqlite3_stmt sqlite3_stmt;
|
||||
typedef std::unique_ptr<sqlite3, int(*)(sqlite3*)> sqlite3_ptr;
|
||||
typedef std::unique_ptr<sqlite3_stmt, int(*)(sqlite3_stmt*)> sqlite3_stmt_ptr;
|
||||
|
||||
@@ -18,13 +15,15 @@ typedef std::unique_ptr<sqlite3_stmt, int(*)(sqlite3_stmt*)> sqlite3_stmt_ptr;
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
|
||||
void sqlite3_exec_nofail(sqlite3* db, const char* sql);
|
||||
void sqlite3_exec_nofail(sqlite3_ptr& db, const char* sql);
|
||||
namespace bts {
|
||||
namespace sqlite {
|
||||
|
||||
void exec_nofail(sqlite3* db, const char* sql);
|
||||
void exec_nofail(sqlite3_ptr& db, const char* sql);
|
||||
|
||||
sqlite3_ptr open_db(const char* path);
|
||||
inline sqlite3_ptr null_db() { return sqlite3_ptr(nullptr, sqlite3_close); }
|
||||
sqlite3_stmt_ptr prepare_query(sqlite3* db, const char* query);
|
||||
sqlite3_stmt_ptr prepare_query(sqlite3_ptr& db, const char* query);
|
||||
}}//namespace
|
||||
|
||||
@@ -43,7 +43,7 @@ void run_cx_server(UserSymbol* psyms, size_t symcount)
|
||||
|
||||
CXMain::CXMain() :
|
||||
_producer(co2::PLATFORM_CELOXICA),
|
||||
_security_recorder(),
|
||||
_security_recorder({}),
|
||||
_settlement_recorder(co2::PLATFORM_CELOXICA),
|
||||
_listeners()
|
||||
{
|
||||
|
||||
@@ -312,8 +312,6 @@ void MDP3Listener::onSpread(onixs::Handler&, const onixs::Security&, const Instr
|
||||
|
||||
FixedSpreadSecurityL fsec;
|
||||
set_security_common(sec, fsec);
|
||||
std::vector<uint32_t> leg_exchange_ids;
|
||||
leg_exchange_ids.reserve(sec.legs().size());
|
||||
|
||||
onixs::Decimal min_tick = tick_rule(sec);
|
||||
|
||||
@@ -341,10 +339,10 @@ void MDP3Listener::onSpread(onixs::Handler&, const onixs::Security&, const Instr
|
||||
}
|
||||
fsec.legs[x].leg_quantity = leg.legRatioQty();
|
||||
fsec.legs[x].leg_side = leg.legSide() == onixs::LegSide::BuySide ? api::SIDE_BUY : api::SIDE_SELL;
|
||||
leg_exchange_ids.push_back(leg.legSecurityId());
|
||||
fsec.legs[x].exchange_key = ExchangeKey(leg.legSecurityId());
|
||||
fsec.legs[x].key = invalid_security_key;
|
||||
}
|
||||
add_security(fsec);
|
||||
_index.add_spread_legs(fsec.numeric_id, leg_exchange_ids);
|
||||
}
|
||||
|
||||
template <size_t MAX_DEPTH,class TBook>
|
||||
|
||||
@@ -26,7 +26,9 @@ namespace bts {
|
||||
namespace md {
|
||||
|
||||
MDP3Main::MDP3Main() :
|
||||
_producer(PLATFORM_MDP3ONIXS, { utils::Dispatcher::current() }),
|
||||
_index(),
|
||||
_recorder ({ utils::Dispatcher::current(), [=](ExchangeKey k) { return SecurityKey(_index.get_key(k.value())); } }),
|
||||
_producer(PLATFORM_MDP3ONIXS, _recorder ),
|
||||
// _settlement_recorder(PLATFORM_MDP3ONIXS),
|
||||
_engines(),
|
||||
_handlers(),
|
||||
@@ -152,10 +154,6 @@ void MDP3Main::run(std::vector<UserSymbol>& symbols) {
|
||||
// Timers
|
||||
|
||||
auto& disp = utils::Dispatcher::current();
|
||||
utils::DispatcherTimer spread_resolve_timer(disp, 1000, [&]() {
|
||||
_index.resolve_spreads();
|
||||
});
|
||||
spread_resolve_timer.start();
|
||||
|
||||
// utils::DispatcherTimer settlement_timer(disp, 100, [&]() {
|
||||
// _settlement_recorder.do_work();
|
||||
|
||||
@@ -38,6 +38,7 @@ private:
|
||||
BTS_NO_COPY(MDP3Main);
|
||||
|
||||
CMESecurityIndex _index;
|
||||
SecurityIDRecorder _recorder;
|
||||
MDProducer _producer;
|
||||
// co2::SettlementRecorder _settlement_recorder;
|
||||
std::map<std::string, std::unique_ptr<onixs::FeedEngine>> _engines;
|
||||
|
||||
Reference in new issue
Block a user