|
1 | 1 | #pragma once |
| 2 | +#include <date/date.h> |
2 | 3 |
|
3 | 4 | #include <cstdint> |
| 5 | +#include <map> |
| 6 | +#include <memory> |
4 | 7 | #include <string> |
5 | 8 | #include <unordered_map> |
| 9 | +#include <utility> |
6 | 10 |
|
7 | 11 | #include "databento/compat.hpp" |
8 | 12 | #include "databento/record.hpp" |
9 | 13 |
|
10 | 14 | namespace databento { |
11 | | -// A point-in-time symbol map. Useful for working with live symbology or |
12 | | -// a historical request over a single day or other situations where the |
13 | | -// symbol mappings are known not to change. |
| 15 | +// Forward declare |
| 16 | +struct Metadata; |
| 17 | + |
| 18 | +/// A timeseries symbol map. Useful for working with historical |
| 19 | +/// data. |
| 20 | +class TsSymbolMap { |
| 21 | + public: |
| 22 | + // UTC date and instrument ID to text symbol. |
| 23 | + using Store = std::map<std::pair<date::year_month_day, std::uint32_t>, |
| 24 | + std::shared_ptr<const std::string>>; |
| 25 | + |
| 26 | + TsSymbolMap() = default; |
| 27 | + explicit TsSymbolMap(const Metadata& metadata); |
| 28 | + |
| 29 | + bool IsEmpty() const { return map_.empty(); } |
| 30 | + std::size_t Size() const { return map_.size(); } |
| 31 | + const Store& Map() const { return map_; } |
| 32 | + Store& Map() { return map_; } |
| 33 | + Store::const_iterator Find(date::year_month_day date, |
| 34 | + std::uint32_t instrument_id) const { |
| 35 | + return map_.find(std::make_pair(date, instrument_id)); |
| 36 | + } |
| 37 | + template <typename R> |
| 38 | + Store::const_iterator Find(const R& rec) const { |
| 39 | + static_assert( |
| 40 | + has_header_v<R>, |
| 41 | + "must be a DBN record struct with an `hd` RecordHeader field"); |
| 42 | + date::year_month_day index_date{ |
| 43 | + date::sys_days{date::floor<date::days>(rec.IndexTs())}}; |
| 44 | + return map_.find(std::make_pair(index_date, rec.hd.instrument_id)); |
| 45 | + } |
| 46 | + const std::string& At(date::year_month_day date, |
| 47 | + std::uint32_t instrument_id) const { |
| 48 | + return *map_.at(std::make_pair(date, instrument_id)); |
| 49 | + } |
| 50 | + template <typename R> |
| 51 | + const std::string& At(const R& rec) const { |
| 52 | + static_assert( |
| 53 | + has_header_v<R>, |
| 54 | + "must be a DBN record struct with an `hd` RecordHeader field"); |
| 55 | + date::year_month_day index_date{ |
| 56 | + date::sys_days{date::floor<date::days>(rec.IndexTs())}}; |
| 57 | + return *map_.at(std::make_pair(index_date, rec.hd.instrument_id)); |
| 58 | + } |
| 59 | + void Insert(std::uint32_t instrument_id, date::year_month_day start_date, |
| 60 | + date::year_month_day end_date, |
| 61 | + const std::shared_ptr<const std::string>& symbol); |
| 62 | + |
| 63 | + private: |
| 64 | + Store map_; |
| 65 | +}; |
| 66 | + |
| 67 | +// A point-in-time symbol map. Useful for working with live |
| 68 | +// symbology or a historical request over a single day or other |
| 69 | +// situations where the symbol mappings are known not to change. |
14 | 70 | class PitSymbolMap { |
15 | 71 | public: |
| 72 | + // Instrument ID to text symbol |
16 | 73 | using Store = std::unordered_map<std::uint32_t, std::string>; |
17 | 74 |
|
18 | 75 | PitSymbolMap() = default; |
| 76 | + PitSymbolMap(const Metadata& metadata, date::year_month_day date); |
19 | 77 |
|
20 | 78 | bool IsEmpty() const { return map_.empty(); } |
21 | 79 | std::size_t Size() const { return map_.size(); } |
22 | 80 | const Store& Map() const { return map_; } |
23 | 81 | Store& Map() { return map_; } |
| 82 | + Store::const_iterator Find(const Record& rec) const { |
| 83 | + return map_.find(rec.Header().instrument_id); |
| 84 | + } |
24 | 85 | Store::const_iterator Find(std::uint32_t instrument_id) const { |
25 | 86 | return map_.find(instrument_id); |
26 | 87 | } |
27 | | - std::string& operator[](std::uint32_t instrument_id) { |
| 88 | + template <typename R> |
| 89 | + const std::string& At(const R& rec) const { |
| 90 | + static_assert( |
| 91 | + has_header_v<R>, |
| 92 | + "must be a DBN record struct with an `hd` RecordHeader field"); |
| 93 | + return map_.at(rec.hd.instrument_id); |
| 94 | + } |
| 95 | + const std::string& At(const Record& rec) const { |
| 96 | + return map_.at(rec.Header().instrument_id); |
| 97 | + } |
| 98 | + const std::string& operator[](std::uint32_t instrument_id) { |
28 | 99 | return map_[instrument_id]; |
29 | 100 | } |
30 | 101 | void OnRecord(const Record& rec); |
|
0 commit comments