Class SequentialWriter

Inheritance Relationships

Base Type

Class Documentation

class SequentialWriter : public rosbag2_cpp::writer_interfaces::BaseWriterInterface

The Writer allows writing messages to a new bag. For every topic, information about its type needs to be added before writing the first message.

Public Functions

explicit SequentialWriter(std::unique_ptr<rosbag2_storage::StorageFactoryInterface> storage_factory = std::make_unique<rosbag2_storage::StorageFactory>(), std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory = std::make_shared<SerializationFormatConverterFactory>(), std::unique_ptr<rosbag2_storage::MetadataIo> metadata_io = std::make_unique<rosbag2_storage::MetadataIo>())
~SequentialWriter() override
virtual void open(const rosbag2_storage::StorageOptions &storage_options, const ConverterOptions &converter_options) override

Opens a new bagfile and prepare it for writing messages. The bagfile must not exist. This must be called before any other function is used.

Parameters:
  • storage_options – Options to configure the storage

  • converter_options – options to define in which format incoming messages are stored

virtual void close() override
virtual void create_topic(const rosbag2_storage::TopicMetadata &topic_with_type) override

Create a new topic in the underlying storage. Needs to be called for every topic used within a message which is passed to write(…).

Parameters:

topic_with_type – name and type identifier of topic to be created

Throws:

runtime_error – if the Writer is not open.

virtual void create_topic(const rosbag2_storage::TopicMetadata &topic_with_type, const rosbag2_storage::MessageDefinition &message_definition) override

Create a new topic in the underlying storage. Needs to be called for every topic used within a message which is passed to write(…).

Parameters:
  • topic_with_type – name and type identifier of topic to be created

  • message_definition – message definition content for this topic’s type

Throws:

runtime_error – if the Writer is not open.

virtual void remove_topic(const rosbag2_storage::TopicMetadata &topic_with_type) override

Remove a new topic in the underlying storage. If creation of subscription fails remove the topic from the db (more of cleanup)

Parameters:

topic_with_type – name and type identifier of topic to be created

Throws:

runtime_error – if the Writer is not open.

virtual void write(std::shared_ptr<const rosbag2_storage::SerializedBagMessage> message) override

Write a message to a bagfile. The topic needs to have been created before writing is possible.

Parameters:

message – to be written to the bagfile

Throws:

runtime_error – if the Writer is not open.

virtual bool take_snapshot() override

Take a snapshot by triggering a circular buffer flip, writing data to disk. *

Returns:

true if snapshot is successful

virtual void add_event_callbacks(const bag_events::WriterEventCallbacks &callbacks) override

Add callbacks for events that may occur during bag writing.

Parameters:

callbacks – the structure containing the callback to add for each event.

virtual void split_bagfile() override

Closes the current backed storage and opens the next bagfile.

Protected Functions

void switch_to_next_storage()
std::string format_storage_uri(const std::string &base_folder, uint64_t storage_count)
bool should_split_bagfile(const std::chrono::time_point<std::chrono::high_resolution_clock> &current_time) const
virtual void init_metadata()
void finalize_metadata()
virtual std::shared_ptr<const rosbag2_storage::SerializedBagMessage> get_writeable_message(std::shared_ptr<const rosbag2_storage::SerializedBagMessage> message)

Protected Attributes

std::string base_folder_
std::unique_ptr<rosbag2_storage::StorageFactoryInterface> storage_factory_
std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory_
std::shared_ptr<rosbag2_storage::storage_interfaces::ReadWriteInterface> storage_
std::unique_ptr<rosbag2_storage::MetadataIo> metadata_io_
std::unique_ptr<Converter> converter_
bool use_cache_ = {false}
std::shared_ptr<rosbag2_cpp::cache::MessageCacheInterface> message_cache_
std::unique_ptr<rosbag2_cpp::cache::CacheConsumer> cache_consumer_
rosbag2_storage::StorageOptions storage_options_
std::unordered_map<std::string, rosbag2_storage::TopicInformation> topics_names_to_info_
std::mutex topics_info_mutex_
LocalMessageDefinitionSource message_definitions_
std::unordered_map<std::string, rosbag2_storage::MessageDefinition> topic_names_to_message_definitions_
rosbag2_storage::BagMetadata metadata_