84 auto* header =
reinterpret_cast<FileHeader*
>(file_.data());
86 if (std::memcmp(header, &blank,
sizeof(
FileHeader)) == 0) {
89 std::memcpy(fresh.
magic, MAGIC,
sizeof(MAGIC));
93 std::memcpy(header, &fresh,
sizeof(
FileHeader));
94 }
else if (std::memcmp(header->magic, MAGIC,
sizeof(MAGIC)) != 0) {
95 throw std::runtime_error(
"PositionStore: " + path +
" is not a position store");
96 }
else if (header->version != VERSION || header->slot_size !=
sizeof(
Slot) || header->max_lots !=
MAX_LOTS) {
97 throw std::runtime_error(
"PositionStore: " + path +
" was written with a different layout (version " +
98 std::to_string(header->version) +
")");
102 next_free_ = capacity_;
103 std::copy_n(header->journaled,
MAX_BROKERS, journaled_);
104 for (std::size_t i = 0; i < capacity_; ++i) {
105 const Slot& slot = slot_at(i);
106 if (slot.
state == SLOT_RECORD_0 || slot.
state == SLOT_RECORD_1) {
110 const Record& record = slot.
records[slot.
state - SLOT_RECORD_0];
112 journaled_[record.journal_broker] = std::max(journaled_[record.journal_broker], record.journal_seq);
114 }
else if (slot.
state != SLOT_EMPTY) {
116 }
else if (next_free_ == capacity_) {
121 std::copy_n(journaled_,
MAX_BROKERS, header->journaled);
122 LOG_INFO(
"PositionStore: {} holds {} position(s), room for {}", path_, size_, capacity_);
147 for (std::size_t i = 0; i < capacity_; ++i) {
148 const Slot& slot = slot_at(i);
149 if (slot.
state != SLOT_RECORD_0 && slot.
state != SLOT_RECORD_1) {
153 const Record record = slot.
records[slot.
state - SLOT_RECORD_0];
155 position.
quantity = record.quantity;
162 const std::size_t lot_count = std::min<std::size_t>(record.lot_count,
MAX_LOTS);
163 visit(
static_cast<uint32_t
>(i), load_key(slot.
strategy_id), position, {record.lots, lot_count});
168 std::span<const PositionLot> lots,
BrokerId broker, uint64_t journal_seq) {
170 while (next_free_ < capacity_ && slot_at(next_free_).state != SLOT_EMPTY) {
173 if (next_free_ >= capacity_) {
176 LOG_ERROR(
"PositionStore: {} is full ({} positions) - {} {} and any further new position are NOT "
177 "kept across a restart; raise position_store.capacity",
178 path_, capacity_, strategy_id, position.
instrument);
182 Slot& fresh = slot_at(next_free_);
184 LOG_ERROR(
"PositionStore: {} {} cannot be kept across a restart - a name is longer than {} characters",
188 slot_index =
static_cast<uint32_t
>(next_free_++);
191 if (slot_index >= capacity_) {
195 Slot& slot = slot_at(slot_index);
197 const uint8_t next_state = slot.
state == SLOT_RECORD_0 ? SLOT_RECORD_1 : SLOT_RECORD_0;
199 record.quantity = position.
quantity;
206 if (journal_seq != 0) {
207 record.journal_broker =
static_cast<uint8_t
>(broker);
208 record.journal_seq = journal_seq;
209 }
else if (slot.
state == SLOT_RECORD_0 || slot.
state == SLOT_RECORD_1) {
210 const Record& current = slot.
records[slot.
state - SLOT_RECORD_0];
211 record.journal_broker = current.journal_broker;
212 record.journal_seq = current.journal_seq;
215 const std::size_t kept = std::min(lots.size(),
MAX_LOTS);
216 std::copy_n(lots.begin(), kept, record.lots);
220 double quantity = 0.0;
222 for (std::size_t i =
MAX_LOTS - 1; i < lots.size(); ++i) {
232 LOG_WARN(
"PositionStore: {} {} has {} open lots; the newest {} are kept as one at their average price",
236 record.lot_count =
static_cast<uint32_t
>(kept);
238 std::memcpy(&slot.
records[next_state - SLOT_RECORD_0], &record,
sizeof(Record));
239 std::atomic_signal_fence(std::memory_order_seq_cst);
240 slot.
state = next_state;
uint32_t save(uint32_t slot, std::string_view strategy_id, const Position &position, std::span< const PositionLot > lots, BrokerId broker=BrokerId::UNKNOWN, uint64_t journal_seq=0)