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 <unordered_map>
10#include <vector>
11
20public:
24 DataLogger() = default;
25
32 virtual ~DataLogger();
33
45 void add_metadata(const std::string& key, const std::string& value);
46
58 void add_field(const std::string& name, std::shared_ptr<arrow::DataType> type);
59
71 void open_file(const std::string& filename);
72
83 void set_value(const std::string& column_name, int64_t value);
84
95 void set_value(const std::string& column_name, int32_t value);
96
107 void set_value(const std::string& column_name, int16_t value);
108
119 void set_value(const std::string& column_name, int8_t value);
120
131 void set_value(const std::string& column_name, double value);
132
143 void set_value(const std::string& column_name, const std::string& value);
144
155 void set_value(const std::string& column_name, bool value);
156
157
168 void set_value_float16(const std::string& column_name, float value);
169
170
180 void save_row();
181
182private:
183 // Store Arrow float16 values as their raw 16-bit representation
184 using half_float_t = arrow::NumericBuilder<arrow::HalfFloatType>::value_type;
185
187 std::vector<std::shared_ptr<arrow::Field>> fields_;
189 std::shared_ptr<arrow::Schema> schema_;
191 std::shared_ptr<arrow::io::OutputStream> outfile_;
193 std::shared_ptr<arrow::ipc::RecordBatchWriter> writer_;
195 std::unordered_map<std::string, size_t> column_indices_;
197 std::unordered_map<std::string, std::variant<int64_t, int32_t, int16_t, int8_t, double, std::string, bool, half_float_t>> row_values_;
199 bool file_opened_ = false;
201 std::unordered_map<std::string, std::string> user_metadata_;
202
212 void check_column(const std::string& column_name);
213
219 void reset_row();
220
221 // Convert float32 -> IEEE-754 half payload (16-bit)
222 static half_float_t float_to_half_bits(float f);
223};
224
225
226#endif // DATA_LOGGER_H
227
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:186
void open_file(const std::string &filename)
Opens the output file for writing.
Definition data_logger.cpp:112
void save_row()
Saves the current row to the Feather file.
Definition data_logger.cpp:192
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:86
virtual ~DataLogger()
Destructor.
Definition data_logger.cpp:75
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:157
void add_field(const std::string &name, std::shared_ptr< arrow::DataType > type)
Adds a new field to the schema.
Definition data_logger.cpp:100