Add tests for SecurityIDRecorder += SpreadResolver

This commit is contained in:
Eric Harding committed 2018-11-14 19:58:31 -05:00
1 parent 2fc4c8bd60
commit 4b3cdf9ca4
8 files changed
+127 -35

No files matched your search

+24 -20
View File
@@ -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,6 +22,8 @@ using std::size_t;
namespace bts {
namespace md {
uint64_t exchange_id(const api::Security& security);
struct SecurityIDEntry {
api::Security security;
SecurityIDRecorder::Callback callback;
@@ -58,17 +60,16 @@ struct SecurityIDRecorderState {
security_key insert(const std::string& descriptor, SecurityType);
};
SecurityIDRecorder::SecurityIDRecorder():
SecurityIDRecorder::SecurityIDRecorder(SecurityIDRecorderParams params):
_self(new SecurityIDRecorderState())
{
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 +82,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) {
@@ -196,7 +200,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 +209,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 +235,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 +250,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 +264,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());