algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::FileCalendarCache Class Reference

A CalendarCache in a private folder: one file per name. POSIX only; not provided on Windows. More...

#include <algotradelib/src/network/calendar_cache.hpp>

Inheritance diagram for algotradelib::network::FileCalendarCache:
Collaboration diagram for algotradelib::network::FileCalendarCache:

Public Member Functions

 FileCalendarCache (std::string folder)
 Constructs a cache; nothing is created or opened until the first call.
Result< std::optional< TradingCalendar > > load (const std::string &name) override
 Reads the name's calendar file.
std::optional< BrokerError > publish (const std::string &name, const TradingCalendar &calendar) override
 Replaces the name's calendar file.

Friends

struct FileCalendarCacheTestAccess

Detailed Description

A CalendarCache in a private folder: one file per name. POSIX only; not provided on Windows.

The folder is created (mode 0700) if it does not exist; its parent must. For name N the cache keeps N.calendar: JSON with the format (1), the country, received_at_ns (nanoseconds since the epoch), unreadable_days and the days as [YYYYMMDD, open] pairs in their order. Session hours (TradingDay::regular_, extended_) are not stored: a calendar read back has none. The folder and the file must be owned by the user running the program and give no permission to group or others; a cache that finds otherwise returns its error and leaves them as they were. A symbolic link at the file's name is refused, and so is one at the folder's own name when the folder is given without a trailing separator. A link earlier in the folder's path, or one reached through a trailing separator, is followed: avoiding both is the caller's part, and the folder and file opened must still be the user's alone. Descriptors are closed on exec. A file over 256 KiB, or one that is not well formed, reads as no calendar. publish refuses a calendar received outside what signed 64-bit nanoseconds since the epoch hold, less a day at each end (about the years 1677 to 2262). A count of unreadable days larger than a signed 64-bit integer holds is stored but reads as no calendar.

Writers take no lock: each writes a temporary file of its own, N.calendar.<process>-<count>.tmp, flushes it and renames it over N.calendar, so a reader sees a whole file and the last writer's calendar stays. A writer stopped before its rename can leave its temporary file; it is never read and may be removed.

Definition at line 96 of file calendar_cache.hpp.

Constructor & Destructor Documentation

◆ FileCalendarCache()

algotradelib::network::FileCalendarCache::FileCalendarCache ( std::string folder)
inlineexplicit

Constructs a cache; nothing is created or opened until the first call.

Parameters
folderThe folder.

Definition at line 102 of file calendar_cache.hpp.

Member Function Documentation

◆ load()

Result< std::optional< TradingCalendar > > algotradelib::network::FileCalendarCache::load ( const std::string & name)
inlineoverridevirtual

Reads the name's calendar file.

Parameters
nameThe name.
Returns
The calendar; nothing if there is no file or it is not well formed; or the cache's error.

Implements algotradelib::network::CalendarCache.

Definition at line 109 of file calendar_cache.hpp.

◆ publish()

std::optional< BrokerError > algotradelib::network::FileCalendarCache::publish ( const std::string & name,
const TradingCalendar & calendar )
inlineoverridevirtual

Replaces the name's calendar file.

Parameters
nameThe name.
calendarThe calendar.
Returns
The cache's error, or nothing; the old file is left as it was on failure.

Implements algotradelib::network::CalendarCache.

Definition at line 144 of file calendar_cache.hpp.

◆ FileCalendarCacheTestAccess

friend struct FileCalendarCacheTestAccess
friend

Definition at line 166 of file calendar_cache.hpp.


The documentation for this class was generated from the following file: