Pogosim
Loading...
Searching...
No Matches
data_logger.h
Go to the documentation of this file.
1
2#ifndef DATA_LOGGER_H
3#define DATA_LOGGER_H
4
5#include <arrow/api.h>
6#include <arrow/io/file.h>
7#include <arrow/ipc/api.h>
8#include <arrow/util/compression.h>
9#include <optional>
10#include <unordered_set>
11#include <unordered_map>
12#include <variant>
13#include <vector>
14
27public:
33 DataLogger() = default;
34
42 explicit DataLogger(int64_t flush_row_count);
43
50 virtual ~DataLogger();
51
59 bool column_exists(const std::string& column_name);
60
69 bool column_value_already_set(const std::string& column_name);
70
82 void add_metadata(const std::string& key, const std::string& value);
83
87 void set_logged_fields(const std::vector<std::string>& field_names);
88
102 void add_field(const std::string& name, std::shared_ptr<arrow::DataType> type, bool ignore_existing_name = false);
103
116 void open_file(const std::string& filename);
117
128 void set_value(const std::string& column_name, int64_t value);
129
140 void set_value(const std::string& column_name, int32_t value);
141
152 void set_value(const std::string& column_name, int16_t value);
153
164 void set_value(const std::string& column_name, int8_t value);
165
176 void set_value(const std::string& column_name, double value);
177
188 void set_value(const std::string& column_name, float value);
189
200 void set_value(const std::string& column_name, const std::string& value);
201
212 void set_value(const std::string& column_name, bool value);
213
224 void set_value_float16(const std::string& column_name, float value);
225
235 void save_row();
236
245 void flush();
246
247private:
248 // Store Arrow float16 values as their raw 16-bit representation
249 using half_float_t = arrow::NumericBuilder<arrow::HalfFloatType>::value_type;
250
252 std::vector<std::shared_ptr<arrow::Field>> fields_;
254 std::shared_ptr<arrow::Schema> schema_;
256 std::shared_ptr<arrow::io::OutputStream> outfile_;
258 std::shared_ptr<arrow::ipc::RecordBatchWriter> writer_;
260 std::vector<std::shared_ptr<arrow::ArrayBuilder>> builders_;
262 std::unordered_map<std::string, size_t> column_indices_;
264 std::unordered_map<std::string, std::variant<int64_t, int32_t, int16_t, int8_t, float, double, std::string, bool, half_float_t>> row_values_;
266 bool file_opened_ = false;
268 std::unordered_map<std::string, std::string> user_metadata_;
270 std::optional<std::unordered_set<std::string>> logged_fields_;
272 int64_t flush_row_count_ = 1024;
274 int64_t buffered_row_count_ = 0;
275
285 void check_column(const std::string& column_name);
286
290 bool field_is_logged(const std::string& column_name) const;
291
297 void reset_row();
298
306 void initialize_builders();
307
315 void append_current_row_to_builders();
316
317 // Convert float32 -> IEEE-754 half payload (16-bit)
318 static half_float_t float_to_half_bits(float f);
319};
320
321#endif // DATA_LOGGER_H
void flush()
Flushes all currently buffered rows to the Feather file.
Definition data_logger.cpp:291
void set_logged_fields(const std::vector< std::string > &field_names)
Restricts the output schema to the listed field names.
Definition data_logger.cpp:111
bool column_value_already_set(const std::string &column_name)
Checks if the specified column value has been set for the current row.
Definition data_logger.cpp:330
void set_value_float16(const std::string &column_name, float value)
Sets the value for a specified column in the current row.
Definition data_logger.cpp:249
bool column_exists(const std::string &column_name)
Checks if the specified column exists.
Definition data_logger.cpp:326
void open_file(const std::string &filename)
Opens the output file for writing.
Definition data_logger.cpp:136
void save_row()
Saves the current row to the Feather file.
Definition data_logger.cpp:257
void add_metadata(const std::string &key, const std::string &value)
Adds arbitrary string metadata to be embedded in the Feather file.
Definition data_logger.cpp:98
void add_field(const std::string &name, std::shared_ptr< arrow::DataType > type, bool ignore_existing_name=false)
Adds a new field to the schema.
Definition data_logger.cpp:119
virtual ~DataLogger()
Destructor.
Definition data_logger.cpp:81
DataLogger()=default
Default constructor.
void set_value(const std::string &column_name, int64_t value)
Sets the value for a specified column in the current row.
Definition data_logger.cpp:185