Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
slot_store.cpp
Go to the documentation of this file.
2#include <slick/logger.hpp>
3#include <algorithm>
4#include <stdexcept>
5
6namespace slick::trader::detail {
7
8namespace {
9
10// Every field at its natural alignment with no padding between them: the layout is the
11// file format.
12struct FileHeader {
13 char magic[8];
14 uint32_t version;
15 uint32_t slot_size;
16 char identity[SlotFile::IDENTITY_SIZE];
17 uint8_t state[SlotFile::STATE_SIZE];
18};
19static_assert(sizeof(FileHeader) == SlotFile::HEADER_SIZE);
20static_assert(offsetof(FileHeader, state) == SlotFile::STATE_OFFSET);
21
22} // namespace
23
24SlotFile::SlotFile(const std::string& path, const SlotStoreFormat& format,
25 const std::array<char, IDENTITY_SIZE>& identity, std::string_view identity_text, size_t slot_size,
26 size_t max_slots, const void* fresh_state, size_t state_size)
27 : path_(path)
28 , name_(format.name)
29 , file_(path, HEADER_SIZE + std::max<size_t>(max_slots, 1) * slot_size)
30 , slot_size_(slot_size)
31{
32 auto* header = reinterpret_cast<FileHeader*>(file_.data());
33 // A file nothing has been written to is all zeros, which a fresh state need not be.
34 const bool blank = std::all_of(file_.data(), file_.data() + HEADER_SIZE, [](uint8_t b) { return b == 0; });
35 if (blank) {
36 created_ = true;
37 FileHeader fresh{};
38 fresh.version = format.version;
39 fresh.slot_size = static_cast<uint32_t>(slot_size);
40 std::memcpy(fresh.identity, identity.data(), IDENTITY_SIZE);
41 std::memcpy(fresh.state, fresh_state, state_size);
42 std::memcpy(fresh.magic, format.magic, sizeof(fresh.magic));
43 std::memcpy(header, &fresh, HEADER_SIZE);
44 } else if (std::memcmp(header->magic, format.magic, sizeof(header->magic)) != 0) {
45 throw std::runtime_error(std::string(format.name) + ": " + path + " is not a session store");
46 } else if (header->version != format.version || header->slot_size != slot_size) {
47 throw std::runtime_error(std::string(format.name) + ": " + path +
48 " was written with a different layout (version " +
49 std::to_string(header->version) + ")");
50 } else if (std::memcmp(header->identity, identity.data(), IDENTITY_SIZE) != 0) {
51 throw std::runtime_error(std::string(format.name) + ": " + path + " belongs to another session, not " +
52 std::string(identity_text));
53 }
54 capacity_ = static_cast<uint32_t>((file_.size() - HEADER_SIZE) / slot_size_);
55 LOG_INFO("{}: {} has room for {} record(s) of {}", name_, path_, capacity_, identity_text);
56}
57
58void report_slot_store_full(const char* name, const std::string& path, size_t capacity) {
59 LOG_ERROR("{}: {} is full ({} records) - further orders are NOT kept across a restart; raise "
60 "max_persisted_orders",
61 name, path, capacity);
62}
63
64} // namespace slick::trader::detail
static constexpr size_t STATE_SIZE
SlotFile(const std::string &path, const SlotStoreFormat &format, const std::array< char, IDENTITY_SIZE > &identity, std::string_view identity_text, size_t slot_size, size_t max_slots, const void *fresh_state, size_t state_size)
const char * name() const noexcept
static constexpr size_t HEADER_SIZE
static constexpr size_t IDENTITY_SIZE
static constexpr size_t STATE_OFFSET
const std::string & path() const noexcept
void report_slot_store_full(const char *name, const std::string &path, size_t capacity)