145 lines
4.7 KiB
C++
145 lines
4.7 KiB
C++
#ifndef WIN32
|
|
#include "SettlementRecorder.h"
|
|
#include "lib/utils/Sqlite_utils.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/system.pb.h"
|
|
#include <tbb/concurrent_queue.h>
|
|
#include <sqlite/sqlite3.h>
|
|
#include <string>
|
|
#include <boost/filesystem.hpp>
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <boost/date_time/gregorian/gregorian.hpp>
|
|
|
|
namespace bts {
|
|
namespace co2 {
|
|
|
|
using datetime::Date;
|
|
using std::unique_ptr;
|
|
using std::string;
|
|
using namespace utils;
|
|
|
|
enum WorkType {
|
|
SNAPSHOT,
|
|
UPDATE
|
|
};
|
|
|
|
struct Work {
|
|
WorkType type;
|
|
MDProducerHandle handle;
|
|
IntPrice price;
|
|
Date date;
|
|
};
|
|
|
|
struct SettlementRecorderImpl {
|
|
SettlementRecorderImpl() :
|
|
work_queue(),
|
|
db(nullptr, sqlite3_close),
|
|
select_stmt(nullptr, sqlite3_finalize),
|
|
insert_stmt(nullptr, sqlite3_finalize)
|
|
{}
|
|
tbb::concurrent_queue<Work> work_queue;
|
|
sqlite3_ptr db;
|
|
sqlite3_stmt_ptr select_stmt;
|
|
sqlite3_stmt_ptr insert_stmt;
|
|
};
|
|
|
|
bool record_settle(sqlite3_stmt* insert, security_key key, IntPrice price, Date date) {
|
|
sqlite3_bind_int64(insert, 1, key);
|
|
sqlite3_bind_int(insert, 2, date.to_short());
|
|
sqlite3_bind_int64(insert, 3, price.mantissa());
|
|
sqlite3_bind_int(insert, 4, price.exponent());
|
|
auto result = sqlite3_step(insert);
|
|
sqlite3_reset(insert);
|
|
return (result == SQLITE_DONE || result == SQLITE_ROW);
|
|
}
|
|
void publish_settles(sqlite3_stmt* select, MDProducerHandle handle) {
|
|
// Query sqlite for the top 2 settlements
|
|
sqlite3_bind_int(select, 1, handle.key());
|
|
auto result = sqlite3_step(select);
|
|
if (result == SQLITE_ROW) {
|
|
int64_t mantissa = sqlite3_column_int(select, 0);
|
|
int exponent = sqlite3_column_int(select, 1);
|
|
Date date = Date(sqlite3_column_int(select, 2));
|
|
handle.update_settlement(TimeStamp::now(), {mantissa,(int8_t)exponent}, date);
|
|
result = sqlite3_step(select);
|
|
if (result == SQLITE_ROW) {
|
|
int64_t mantissa = sqlite3_column_int(select, 0);
|
|
int exponent = sqlite3_column_int(select, 1);
|
|
date = Date(sqlite3_column_int(select, 2));
|
|
handle.update_previous_settlement(TimeStamp::now(), {mantissa,(int8_t)exponent}, date);
|
|
}
|
|
}
|
|
sqlite3_reset(select);
|
|
}
|
|
|
|
void apply_work(SettlementRecorderImpl& me, Work work) {
|
|
switch (work.type) {
|
|
case UPDATE:
|
|
if (record_settle(me.insert_stmt.get(), work.handle.key(), work.price, work.date)) {
|
|
publish_settles(me.select_stmt.get(), work.handle);
|
|
}
|
|
break;
|
|
case SNAPSHOT:
|
|
publish_settles(me.select_stmt.get(), work.handle);
|
|
}
|
|
}
|
|
|
|
SettlementRecorder::SettlementRecorder(Platform platform) :
|
|
_self(new SettlementRecorderImpl())
|
|
{
|
|
string db_dir = config::get<config::System>().data_path() + "databases/md/";
|
|
string db_path = db_dir + "/settlements_" + Platform_Name(platform) +".sqlite";
|
|
if (!boost::filesystem::exists(db_dir)) {
|
|
boost::filesystem::create_directories(db_dir);
|
|
}
|
|
|
|
// initialize database
|
|
_self->db = open_db(db_path.c_str());
|
|
sqlite3* db = _self->db.get();
|
|
sqlite3_busy_timeout(db, 9999);
|
|
sqlite3_exec_nofail(db, "PRAGMA journal_mode=OFF");
|
|
sqlite3_exec_nofail(db, "PRAGMA syncronous=0");
|
|
// key=(security_key+date)
|
|
sqlite3_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
|
|
auto cutoff = datetime::local_now() - boost::gregorian::days(10);
|
|
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);
|
|
|
|
// 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(?,?,?,?)");
|
|
}
|
|
|
|
SettlementRecorder::~SettlementRecorder() { }
|
|
|
|
void SettlementRecorder::restore_settlement(MDProducerHandle handle) {
|
|
Work work = { SNAPSHOT, handle, INT_NAN, Date() };
|
|
_self->work_queue.push(work);
|
|
}
|
|
|
|
void SettlementRecorder::add_settlement(MDProducerHandle handle, IntPrice price, datetime::Date date) {
|
|
Work work = { UPDATE, handle, price, date };
|
|
_self->work_queue.push(work);
|
|
}
|
|
|
|
void SettlementRecorder::do_work() {
|
|
TimeStamp start = TimeStamp::now();
|
|
Work work;
|
|
while ((TimeStamp::now() - start).milliseconds() < 100 &&
|
|
_self->work_queue.try_pop(work)) {
|
|
if (work.handle.key() == invalid_security_key) {
|
|
_self->work_queue.push(work);
|
|
continue;
|
|
}
|
|
apply_work(*_self, work);
|
|
}
|
|
}
|
|
|
|
}}//namespace
|
|
#endif
|