|
3 | 3 | `BinaryLogWriter` writes market data to binary log segments. It handles segment rotation, compression, indexing, and CRC checksums. |
4 | 4 |
|
5 | 5 | ```cpp |
| 6 | +// Callback for custom segment naming on rotation |
| 7 | +using RotationCallback = std::filesystem::path (*)( |
| 8 | + void* user_data, |
| 9 | + const std::filesystem::path& output_dir, |
| 10 | + uint32_t segment_number); |
| 11 | + |
6 | 12 | struct WriterConfig { |
7 | 13 | std::filesystem::path output_dir; |
8 | | - std::string output_filename; // Optional: override generated name |
| 14 | + std::string output_filename; // Optional: first segment name |
9 | 15 | uint64_t max_segment_bytes{256ull << 20}; // 256 MB default |
10 | 16 | uint64_t buffer_size{64ull << 10}; // 64 KB buffer |
11 | 17 | uint8_t exchange_id{0}; |
12 | 18 | bool sync_on_rotate{true}; |
13 | 19 | bool create_index{true}; |
14 | 20 | uint16_t index_interval{1000}; // Events per index entry |
15 | 21 | CompressionType compression{CompressionType::None}; |
| 22 | + RotationCallback rotation_callback{nullptr}; // Custom naming on rotation |
| 23 | + void* rotation_user_data{nullptr}; // User data for callback |
16 | 24 | }; |
17 | 25 |
|
18 | 26 | class BinaryLogWriter { |
@@ -44,14 +52,16 @@ public: |
44 | 52 | | Field | Default | Description | |
45 | 53 | |-------|---------|-------------| |
46 | 54 | | `output_dir` | - | Directory for segment files. | |
47 | | -| `output_filename` | - | Optional fixed filename (disables rotation). | |
| 55 | +| `output_filename` | - | Optional filename for first segment. | |
48 | 56 | | `max_segment_bytes` | 256 MB | Maximum segment size before rotation. | |
49 | 57 | | `buffer_size` | 64 KB | Internal write buffer size. | |
50 | 58 | | `exchange_id` | 0 | Exchange identifier in segment header. | |
51 | 59 | | `sync_on_rotate` | true | Sync to disk on segment rotation. | |
52 | 60 | | `create_index` | true | Build seek index in segments. | |
53 | 61 | | `index_interval` | 1000 | Events between index entries. | |
54 | 62 | | `compression` | None | Compression type (`None` or `LZ4`). | |
| 63 | +| `rotation_callback` | nullptr | Custom callback for segment naming on rotation. | |
| 64 | +| `rotation_user_data` | nullptr | User data passed to rotation callback. | |
55 | 65 |
|
56 | 66 | ## Methods |
57 | 67 |
|
@@ -107,6 +117,47 @@ writer.flush(); |
107 | 117 | writer.close(); |
108 | 118 | ``` |
109 | 119 |
|
| 120 | +## Custom Rotation Callback |
| 121 | +
|
| 122 | +By default, rotated segments use timestamp-based names. To customize naming: |
| 123 | +
|
| 124 | +```cpp |
| 125 | +// Context for the callback |
| 126 | +struct MyContext { |
| 127 | + std::string prefix; |
| 128 | + int sequence{1}; |
| 129 | +}; |
| 130 | +
|
| 131 | +// Callback function (must be a plain function, not a lambda with captures) |
| 132 | +std::filesystem::path my_rotation_cb( |
| 133 | + void* user_data, |
| 134 | + const std::filesystem::path& output_dir, |
| 135 | + uint32_t segment_number) |
| 136 | +{ |
| 137 | + auto* ctx = static_cast<MyContext*>(user_data); |
| 138 | + std::ostringstream fname; |
| 139 | + fname << ctx->prefix << "_" << std::setfill('0') << std::setw(3) |
| 140 | + << (ctx->sequence + segment_number - 1) << ".floxlog"; |
| 141 | + return output_dir / fname.str(); |
| 142 | +} |
| 143 | +
|
| 144 | +// Usage |
| 145 | +MyContext ctx{"2025-01-15", 1}; |
| 146 | +
|
| 147 | +replay::WriterConfig config{ |
| 148 | + .output_dir = "/data/market", |
| 149 | + .output_filename = "2025-01-15_001.floxlog", // First segment |
| 150 | + .max_segment_bytes = 256ull << 20, |
| 151 | + .rotation_callback = my_rotation_cb, |
| 152 | + .rotation_user_data = &ctx, // Must outlive writer! |
| 153 | +}; |
| 154 | +
|
| 155 | +replay::BinaryLogWriter writer(config); |
| 156 | +// When segment rotates, callback generates "2025-01-15_002.floxlog", etc. |
| 157 | +``` |
| 158 | + |
| 159 | +**Important:** The `rotation_user_data` pointer must remain valid for the lifetime of the writer. |
| 160 | + |
110 | 161 | ## Notes |
111 | 162 |
|
112 | 163 | * Thread-safe via internal mutex. |
|
0 commit comments