Ocean
Loading...
Searching...
No Matches
OutputDataSerializer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_IO_SERIALIZATION_OUTPUT_DATA_SERIALIZER_H
9#define META_OCEAN_IO_SERIALIZATION_OUTPUT_DATA_SERIALIZER_H
10
13
14namespace Ocean
15{
16
17namespace IO
18{
19
20namespace Serialization
21{
22
23/**
24 * This class implements an output data serializer.
25 * The output data serializer serializes data samples to a stream (e.g., file or network) for recording purposes.<br>
26 * Before adding samples, channels must be created using addChannel() which assigns a unique channel id for each distinct sample type, name, and content type combination.<br>
27 * Samples are added via addSample() and are written to the stream asynchronously by a background thread, allowing the caller to continue without blocking.<br>
28 * When stopping, all queued samples are written before the serializer terminates, ensuring no data is lost.
29 * @ingroup ioserialization
30 */
31class OCEAN_IO_SERIALIZATION_EXPORT OutputDataSerializer : public DataSerializer
32{
33 protected:
34
35 /**
36 * This class implements an abstract stream for output data serializers.
37 */
38 class Stream
39 {
40 public:
41
42 /**
43 * Destructs the stream.
44 */
45 virtual ~Stream() = default;
46
47 /**
48 * Returns the output bitstream.
49 * @return The output bitstream
50 */
52
53 /**
54 * Returns whether this stream is valid.
55 * @return True, if so
56 */
57 virtual bool isValid() const = 0;
58 };
59
60 /// Definition of a unique pointer holding a stream.
61 using UniqueStream = std::unique_ptr<Stream>;
62
63 /// Definition of a FIFO queue holding sample pairs.
64 using SampleQueue = std::queue<SamplePair>;
65
66 public:
67
68 /**
69 * Adds a new channel to the serializer.
70 * @param sampleType The sample type, must be non-empty
71 * @param name The name of the channel, must be non-empty
72 * @param contentType The content type, must be non-empty
73 * @return The channel id, invalidChannelId() if the channel could not be added (e.g., if a channel with the same configuration already exists)
74 */
75 [[nodiscard]] DataSerializer::ChannelId addChannel(const std::string& sampleType, const std::string& name, const std::string& contentType);
76
77 /**
78 * Adds a new sample to the serializer.
79 * The sample will be added to the internal queue and will be written to the stream asynchronously.
80 * @param channelId The channel id
81 * @param sample The sample to add, will be moved
82 * @return True, if succeeded
83 */
84 bool addSample(const DataSerializer::ChannelId channelId, UniqueDataSample&& sample);
85
86 /**
87 * Starts the serializer.
88 * @return True, if succeeded
89 * @see DataSerializer::start().
90 */
91 bool start() override;
92
93 /**
94 * Stops the serializer.
95 * This function sets a stopping flag and returns immediately; it does not wait for the serializer to actually stop.
96 * The background thread will finish writing all pending samples in the queue before checking the stopping flag and terminating.
97 * This ensures that all queued samples are written to the output stream before the serializer stops.
98 * @return True, if the stop request was accepted; False, if the serializer was not started
99 * @see DataSerializer::stop(), stopAndWait(), hasStopped().
100 */
101 bool stop() override;
102
103 /**
104 * Returns whether the serializer has been started.
105 * @return True, if so
106 * @see DataSerializer::isStarted().
107 */
108 [[nodiscard]] bool isStarted() const override;
109
110 /**
111 * Returns whether the serializer has stopped and all remaining samples have been processed.
112 * @return True, if so
113 * @see DataSerializer::hasFinished().
114 */
115 [[nodiscard]] bool hasFinished() const override;
116
117 protected:
118
119 /**
120 * Creates the output stream.
121 * @return The output stream, nullptr if the stream could not be created
122 */
123 virtual UniqueStream createStream() const = 0;
124
125 /**
126 * Writes the header to the output bitstream.
127 * @param outputBitstream The output bitstream to which the header will be written
128 * @return True, if succeeded
129 */
130 virtual bool writeHeader(OutputBitstream& outputBitstream);
131
132 /**
133 * The thread run function.
134 * @see Thread::threadRun().
135 */
136 void threadRun() override;
137
138 protected:
139
140 /// The output stream.
142
143 /// The next channel id to be assigned.
144 ChannelId nextChannelId_ = ChannelId(0);
145
146 /// The map mapping channel configurations to channel ids.
148
149 /// The queue holding samples which are pending to be written.
151};
152
153/**
154 * This class implements a file-based output data serializer.
155 * This specialization of OutputDataSerializer writes serialized data to a binary file on disk.<br>
156 * Use setFilename() to configure the output file path before calling start().
157 * @ingroup ioserialization
158 */
159class OCEAN_IO_SERIALIZATION_EXPORT FileOutputDataSerializer : public OutputDataSerializer
160{
161 protected:
162
163 /**
164 * This class implements a file stream for file output data serializers.
165 */
166 class FileStream : public Stream
167 {
168 public:
169
170 /**
171 * Creates a new file stream with given filename.
172 * @param filename The filename of the file to write, must be valid
173 */
174 inline explicit FileStream(const std::string& filename);
175
176 /**
177 * Destructs the file stream.
178 */
179 inline ~FileStream() override;
180
181 /**
182 * Returns the output bitstream.
183 * @return The output bitstream
184 * @see Stream::outputBitstream().
185 */
186 inline OutputBitstream& outputBitstream() override;
187
188 /**
189 * Returns whether this stream is valid.
190 * @return True, if so
191 * @see Stream::isValid().
192 */
193 inline bool isValid() const override;
194
195 protected:
196
197 /// The file stream.
198 std::ofstream stream_;
199
200 /// The output bitstream.
202 };
203
204 public:
205
206 /**
207 * Sets the filename of the file to write.
208 * @param filename The filename of the file to write, must be non-empty
209 * @return True, if succeeded
210 */
211 virtual bool setFilename(const std::string& filename);
212
213 protected:
214
215 /**
216 * Creates the output stream.
217 * @return The output stream, nullptr if the stream could not be created
218 * @see OutputDataSerializer::createStream().
219 */
220 UniqueStream createStream() const override;
221
222 protected:
223
224 /// The filename of the file to write.
225 std::string filename_;
226};
227
228inline FileOutputDataSerializer::FileStream::FileStream(const std::string& filename) :
229 stream_(filename.c_str(), std::ios::binary),
230 outputBitstream_(stream_)
231
232{
233 // nothing to do here
234}
235
240
242{
243 return outputBitstream_;
244}
245
247{
248 return stream_.is_open() && !stream_.fail();
249}
250
251}
252
253}
254
255}
256
257#endif // META_OCEAN_IO_SERIALIZATION_OUTPUT_DATA_SERIALIZER_H
This class implements an output bitstream.
Definition Bitstream.h:215
This class implements the base class for data serializers.
Definition DataSerializer.h:38
std::unordered_map< ChannelConfiguration, ChannelId, ChannelConfiguration::Hash > ChannelConfigurationMap
Definition of a map mapping channel configurations to channel ids.
Definition DataSerializer.h:214
uint32_t ChannelId
Definition of a channel id.
Definition DataSerializer.h:42
This class implements a file stream for file output data serializers.
Definition OutputDataSerializer.h:167
OutputBitstream outputBitstream_
The output bitstream.
Definition OutputDataSerializer.h:201
bool isValid() const override
Returns whether this stream is valid.
Definition OutputDataSerializer.h:246
OutputBitstream & outputBitstream() override
Returns the output bitstream.
Definition OutputDataSerializer.h:241
~FileStream() override
Destructs the file stream.
Definition OutputDataSerializer.h:236
std::ofstream stream_
The file stream.
Definition OutputDataSerializer.h:198
FileStream(const std::string &filename)
Creates a new file stream with given filename.
Definition OutputDataSerializer.h:228
This class implements a file-based output data serializer.
Definition OutputDataSerializer.h:160
std::string filename_
The filename of the file to write.
Definition OutputDataSerializer.h:225
UniqueStream createStream() const override
Creates the output stream.
virtual bool setFilename(const std::string &filename)
Sets the filename of the file to write.
This class implements an abstract stream for output data serializers.
Definition OutputDataSerializer.h:39
virtual ~Stream()=default
Destructs the stream.
virtual OutputBitstream & outputBitstream()=0
Returns the output bitstream.
virtual bool isValid() const =0
Returns whether this stream is valid.
This class implements an output data serializer.
Definition OutputDataSerializer.h:32
void threadRun() override
The thread run function.
UniqueStream stream_
The output stream.
Definition OutputDataSerializer.h:141
bool addSample(const DataSerializer::ChannelId channelId, UniqueDataSample &&sample)
Adds a new sample to the serializer.
DataSerializer::ChannelId addChannel(const std::string &sampleType, const std::string &name, const std::string &contentType)
Adds a new channel to the serializer.
std::queue< SamplePair > SampleQueue
Definition of a FIFO queue holding sample pairs.
Definition OutputDataSerializer.h:64
SampleQueue sampleQueue_
The queue holding samples which are pending to be written.
Definition OutputDataSerializer.h:150
ChannelConfigurationMap channelConfigurationMap_
The map mapping channel configurations to channel ids.
Definition OutputDataSerializer.h:147
bool start() override
Starts the serializer.
bool hasFinished() const override
Returns whether the serializer has stopped and all remaining samples have been processed.
bool stop() override
Stops the serializer.
virtual UniqueStream createStream() const =0
Creates the output stream.
virtual bool writeHeader(OutputBitstream &outputBitstream)
Writes the header to the output bitstream.
bool isStarted() const override
Returns whether the serializer has been started.
std::unique_ptr< Stream > UniqueStream
Definition of a unique pointer holding a stream.
Definition OutputDataSerializer.h:61
std::unique_ptr< DataSample > UniqueDataSample
Definition of a unique pointer holding a DataSample.
Definition DataSample.h:39
The namespace covering the entire Ocean framework.
Definition Accessor.h:15