![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
The order journal on disk: one JSON object per line, appended and flushed to disk (fsync) before append returns. More...
#include <algotradelib/src/runner/file_journal.hpp>
Public Member Functions | |
| FileJournal (std::string path, std::string scope) | |
| Opens a journal file for appending, creating it if absent. | |
| bool | open () const noexcept |
| Tests whether the journal opened. | |
| const std::string & | repaired () const noexcept |
| Returns what opening had to repair: a torn last line is terminated and a salvaged entry written beside it. | |
| const std::string & | path () const noexcept |
| Returns the file's path. | |
| const std::string & | scope () const noexcept |
| Returns the journal's broker and account. | |
| const std::string & | rejection () const noexcept |
| Returns why the journal refused to open. | |
| bool | append (const JournalEntry &entry) |
| Appends one entry and flushes it to disk. | |
| const JournalReplay & | replay () const |
| Reads the journal back. | |
The order journal on disk: one JSON object per line, appended and flushed to disk (fsync) before append returns.
A write cut short loses only the last line, which shows as missing its newline. Read it with tail. The file starts with a header naming its broker and account; opening a file that belongs to another account is refused.
On POSIX, opening takes an exclusive flock on the file, so a second journal on the same file (in any process) is refused and a program built on it starts nothing. The lock is on the open file, not the path: deleting or renaming the file while it is open lets another journal create and lock a new file at the same path, so do not rotate an active journal. It ends when the process exits, unless a forked child still holds the descriptor (it is closed on exec, not on fork). Keep the journal on a local disk (flock and fsync over NFS are unreliable). Windows takes no lock. A flushed line survives a killed process everywhere; on macOS fsync does not guarantee it survives a power loss. Two journals at different paths for one account are not detected.
replay() keeps what it read, so the parsed journal stays in memory while the journal is open. On POSIX a later call reads only the lines appended since, when the file's device, inode, size and modification and status-change times show that only this journal wrote to it; a change by another program between two calls makes it read the whole file. A change by another program that leaves all of them as they were (within the file system's timestamp resolution), or that happens during one of this journal's own writes (an in-place write, or a truncation), is read at the next whole read, which a newly opened journal always does.
Lines are stamped with the clock the journal's writer is given, and a line whose instant lies outside 1970-01-02 to 2200-01-01 reads as damage (see journal_entry_from_line). So a paper market's clock may run over history from 1970-01-02 on; earlier history is not supported.
Definition at line 1599 of file file_journal.hpp.
|
inline |
Opens a journal file for appending, creating it if absent.
Never throws for an open failure: check open() and rejection(). A torn last line left by a killed predecessor is repaired first (see repaired()).
| path | The file. |
| scope | The broker and account, such as "kis:live:12345678-01"; must not be empty. |
Definition at line 1610 of file file_journal.hpp.
|
inline |
Appends one entry and flushes it to disk.
The journal's own scope replaces the entry's.
| entry | The entry. |
Definition at line 1714 of file file_journal.hpp.
|
inlinenoexcept |
Tests whether the journal opened.
Definition at line 1664 of file file_journal.hpp.
Referenced by append(), FileJournal(), and replay().
|
inlinenoexcept |
Returns the file's path.
Definition at line 1681 of file file_journal.hpp.
Referenced by FileJournal().
|
inlinenoexcept |
Returns why the journal refused to open.
The file holds another account's records, has no header, or cannot be read; another writer holds its lock (POSIX); the lock could not be taken; the file could not be opened; a torn last line could not be repaired; or the scope is empty.
Definition at line 1702 of file file_journal.hpp.
|
inlinenoexcept |
Returns what opening had to repair: a torn last line is terminated and a salvaged entry written beside it.
Definition at line 1673 of file file_journal.hpp.
|
inline |
Reads the journal back.
The result is kept by the journal: it stays valid until the next call, or until the journal is moved or destroyed, and append does not change it. So do not call replay() while still using an earlier result, and do not call it from two threads at once. On POSIX only the lines appended since the last call are read and parsed, when the file shows that only this journal wrote to it; otherwise, and always on Windows, the whole file is.
Definition at line 1750 of file file_journal.hpp.
|
inlinenoexcept |
Returns the journal's broker and account.
Definition at line 1689 of file file_journal.hpp.
Referenced by FileJournal().