Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use converters in writer iterations #66

Merged
merged 3 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use converter options
  • Loading branch information
Karsten1987 committed Nov 29, 2018
commit 3bfa713276e8edfef7fd92ddfcd87544cb2485da
6 changes: 6 additions & 0 deletions rosbag2/include/rosbag2/converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unordered_map>
#include <vector>

#include "rosbag2/converter_options.hpp"
#include "rosbag2/serialization_format_converter_factory.hpp"
#include "rosbag2/serialization_format_converter_factory_interface.hpp"
#include "rosbag2/serialization_format_converter_interface.hpp"
Expand Down Expand Up @@ -55,6 +56,11 @@ class ROSBAG2_PUBLIC Converter
std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory =
std::make_shared<SerializationFormatConverterFactory>());

Converter(
const rosbag2::ConverterOptions & converter_options,
std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory =
std::make_shared<SerializationFormatConverterFactory>());

~Converter();

/**
Expand Down
31 changes: 31 additions & 0 deletions rosbag2/include/rosbag2/converter_options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018, Bosch Software Innovations GmbH.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ROSBAG2__CONVERTER_OPTIONS_HPP_
#define ROSBAG2__CONVERTER_OPTIONS_HPP_

#include <string>

namespace rosbag2
{

struct ConverterOptions
{
std::string input_serialization_format;
std::string output_serialization_format;
};

} // namespace rosbag2

#endif // ROSBAG2__CONVERTER_OPTIONS_HPP_
15 changes: 9 additions & 6 deletions rosbag2/include/rosbag2/sequential_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ class ROSBAG2_PUBLIC SequentialReader
std::make_unique<rosbag2_storage::StorageFactory>(),
std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory =
std::make_shared<SerializationFormatConverterFactory>());

virtual ~SequentialReader();

/**
* Open a rosbag for reading messages sequentially (time-ordered). Throws if file could not be
* opened. This must be called before any other function is used. The rosbag is
* automatically closed on destruction.
*
* If the rmw_serialization_format is not the same as the format of the underlying storage, a
* converter will be used to automatically convert the functions. Throws if the converter
* plugin does not exist
* If the `output_serialization_format` within the `converter_options` is not the same as the
* format of the underlying stored data, a converter will be used to automatically convert the
* data to the specified output format.
* Throws if the converter plugin does not exist.
*
* \param options Options to configure the storage
* \param rmw_serialization_format Messages will be serialized in this format
* \param storage_options Options to configure the storage
* \param converter_options Options for specifying the output data format
*/
virtual void open(const StorageOptions & options, const std::string & rmw_serialization_format);
virtual void open(
const StorageOptions & storage_options, const ConverterOptions & converter_options);

/**
* Ask whether the underlying bagfile contains at least one more message.
Expand Down
12 changes: 5 additions & 7 deletions rosbag2/include/rosbag2/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ class ROSBAG2_PUBLIC Writer
* 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.
*
* \param options Options to configure the storage
* \param input_serialization_format The serialization format of the messages to write
* \param output_serialization_format The serialization format in which the messages must be saved
*/
* \param storage_options Options to configure the storage
* \param converter_options options to define in which format incoming messages are stored
**/
virtual void open(
const StorageOptions & options,
const std::string & input_serialization_format,
const std::string & output_serialization_format);
const StorageOptions & storage_options,
const ConverterOptions & converter_options);

/**
* Create a new topic in the underlying storage. Needs to be called for every topic used within
Expand Down
20 changes: 15 additions & 5 deletions rosbag2/src/rosbag2/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ Converter::Converter(
const std::string & input_format,
const std::string & output_format,
std::shared_ptr<rosbag2::SerializationFormatConverterFactoryInterface> converter_factory)
: converter_factory_(converter_factory)
: Converter({input_format, output_format}, converter_factory)
{}

Converter::Converter(
const rosbag2::ConverterOptions & converter_options,
std::shared_ptr<rosbag2::SerializationFormatConverterFactoryInterface> converter_factory)
: converter_factory_(converter_factory),
input_converter_(converter_factory_->load_converter(
converter_options.input_serialization_format)),
output_converter_(converter_factory_->load_converter(
converter_options.output_serialization_format))
{
input_converter_ = converter_factory_->load_converter(input_format);
output_converter_ = converter_factory_->load_converter(output_format);
if (!input_converter_) {
throw std::runtime_error("Could not find converter for format " + input_format);
throw std::runtime_error(
"Could not find converter for format " + converter_options.input_serialization_format);
}
if (!output_converter_) {
throw std::runtime_error("Could not find converter for format " + output_format);
throw std::runtime_error(
"Could not find converter for format " + converter_options.output_serialization_format);
}
}

Expand Down
9 changes: 5 additions & 4 deletions rosbag2/src/rosbag2/sequential_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ SequentialReader::~SequentialReader()
}

void
SequentialReader::open(const StorageOptions & options, const std::string & rmw_serialization_format)
SequentialReader::open(
const StorageOptions & storage_options, const ConverterOptions & converter_options)
{
storage_ = storage_factory_->open_read_only(options.uri, options.storage_id);
storage_ = storage_factory_->open_read_only(storage_options.uri, storage_options.storage_id);
if (!storage_) {
throw std::runtime_error("No storage could be initialized. Abort");
}
Expand All @@ -57,10 +58,10 @@ SequentialReader::open(const StorageOptions & options, const std::string & rmw_s
}
}

if (rmw_serialization_format != storage_serialization_format) {
if (converter_options.output_serialization_format != storage_serialization_format) {
converter_ = std::make_unique<Converter>(
storage_serialization_format,
rmw_serialization_format,
converter_options.output_serialization_format,
converter_factory_);
auto topics = storage_->get_all_topics_and_types();
for (const auto & topic_with_type : topics) {
Expand Down
18 changes: 8 additions & 10 deletions rosbag2/src/rosbag2/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ Writer::~Writer()
}

void Writer::open(
const StorageOptions & options,
const std::string & input_serialization_format,
const std::string & output_serialization_format)
const StorageOptions & storage_options,
const ConverterOptions & converter_options)
{
if (output_serialization_format != input_serialization_format) {
converter_ = std::make_unique<Converter>(
input_serialization_format,
output_serialization_format,
converter_factory_);
if (converter_options.output_serialization_format !=
converter_options.input_serialization_format)
{
converter_ = std::make_unique<Converter>(converter_options, converter_factory_);
}

storage_ = storage_factory_->open_read_write(options.uri, options.storage_id);
storage_ = storage_factory_->open_read_write(storage_options.uri, storage_options.storage_id);
if (!storage_) {
throw std::runtime_error("No storage could be initialized. Abort");
}
uri_ = options.uri;
uri_ = storage_options.uri;
}

void Writer::create_topic(const TopicMetadata & topic_with_type)
Expand Down
6 changes: 3 additions & 3 deletions rosbag2/test/rosbag2/test_sequential_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ TEST_F(SequentialReaderTest, read_next_uses_converters_to_convert_serialization_
EXPECT_CALL(*converter_factory_, load_converter(output_format))
.WillOnce(Return(ByMove(std::move(format2_converter))));

reader_->open(rosbag2::StorageOptions(), output_format);
reader_->open(rosbag2::StorageOptions(), {"", output_format});
reader_->read_next();
}

Expand All @@ -99,7 +99,7 @@ TEST_F(SequentialReaderTest, open_throws_error_if_converter_plugin_does_not_exis
EXPECT_CALL(*converter_factory_, load_converter(output_format))
.WillOnce(Return(ByMove(nullptr)));

EXPECT_ANY_THROW(reader_->open(rosbag2::StorageOptions(), output_format));
EXPECT_ANY_THROW(reader_->open(rosbag2::StorageOptions(), {"", output_format}));
}

TEST_F(SequentialReaderTest,
Expand All @@ -110,6 +110,6 @@ TEST_F(SequentialReaderTest,

EXPECT_CALL(*converter_factory_, load_converter(storage_serialization_format)).Times(0);

reader_->open(rosbag2::StorageOptions(), storage_serialization_format);
reader_->open(rosbag2::StorageOptions(), {"", storage_serialization_format});
reader_->read_next();
}
8 changes: 4 additions & 4 deletions rosbag2/test/rosbag2/test_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ TEST_F(WriterTest,

auto message = std::make_shared<rosbag2::SerializedBagMessage>();
message->topic_name = "test_topic";
writer_->open(storage_options_, input_format, storage_serialization_format);
writer_->open(storage_options_, {input_format, storage_serialization_format});
writer_->create_topic({"test_topic", "test_msgs/Primitives", ""});
writer_->write(message);
}
Expand All @@ -89,7 +89,7 @@ TEST_F(WriterTest, write_does_not_use_converters_if_input_and_output_format_are_

auto message = std::make_shared<rosbag2::SerializedBagMessage>();
message->topic_name = "test_topic";
writer_->open(storage_options_, storage_serialization_format, storage_serialization_format);
writer_->open(storage_options_, {storage_serialization_format, storage_serialization_format});
writer_->create_topic({"test_topic", "test_msgs/Primitives", ""});
writer_->write(message);
}
Expand All @@ -101,7 +101,7 @@ TEST_F(WriterTest, metadata_io_writes_metadata_file_in_destructor) {

std::string rmw_format = "rmw_format";

writer_->open(storage_options_, rmw_format, rmw_format);
writer_->open(storage_options_, {rmw_format, rmw_format});
writer_.reset();
}

Expand All @@ -118,5 +118,5 @@ TEST_F(WriterTest, open_throws_error_if_converter_plugin_does_not_exist) {
EXPECT_CALL(*converter_factory_, load_converter(output_format))
.WillOnce(Return(ByMove(nullptr)));

EXPECT_ANY_THROW(writer_->open(storage_options_, input_format, output_format));
EXPECT_ANY_THROW(writer_->open(storage_options_, {input_format, output_format}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void Rosbag2Transport::record(
{
try {
writer_->open(
storage_options, rmw_get_serialization_format(), record_options.rmw_serialization_format);
storage_options, {rmw_get_serialization_format(), record_options.rmw_serialization_format});

auto transport_node = setup_node();

Expand All @@ -88,7 +88,7 @@ void Rosbag2Transport::play(
const StorageOptions & storage_options, const PlayOptions & play_options)
{
try {
reader_->open(storage_options, rmw_get_serialization_format());
reader_->open(storage_options, {"", rmw_get_serialization_format()});

auto transport_node = setup_node();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
class MockSequentialReader : public rosbag2::SequentialReader
{
public:
void open(const rosbag2::StorageOptions & options, const std::string & rmw_format) override
void open(
const rosbag2::StorageOptions & storage_options,
const rosbag2::ConverterOptions & converter_options) override
{
(void) options;
(void) rmw_format;
(void) storage_options;
(void) converter_options;
}

bool has_next() override
Expand Down
10 changes: 4 additions & 6 deletions rosbag2_transport/test/rosbag2_transport/mock_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class MockWriter : public rosbag2::Writer
{
public:
void open(
const rosbag2::StorageOptions & options,
const std::string & input_serialization_format,
const std::string & output_serialization_format) override
const rosbag2::StorageOptions & storage_options,
const rosbag2::ConverterOptions & converter_options) override
{
(void) options;
(void) input_serialization_format;
(void) output_serialization_format;
(void) storage_options;
(void) converter_options;
}

void create_topic(const rosbag2::TopicMetadata & topic_with_type) override
Expand Down