Skip to content

Commit

Permalink
[Compression - 3] Add compressor interface
Browse files Browse the repository at this point in the history
[Compression - 3] Add compressor interface
  • Loading branch information
Anas Abou Allaban authored Dec 2, 2019
2 parents 8712f9a + dae4265 commit 22a6822
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 0 deletions.
27 changes: 27 additions & 0 deletions rosbag2_compression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,43 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

option(DISABLE_SANITIZERS "Disable the use of gcc sanitizers")
# Only enable sanitizers on x64 architectures
# https://github.com/google/sanitizers/issues/794
if(NOT ${CMAKE_SIZEOF_VOID_P} EQUAL 8)
set(DISABLE_SANITIZERS ON)
endif()

find_package(ament_cmake REQUIRED)
find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rosbag2_storage REQUIRED)

install(
DIRECTORY include/
DESTINATION include)

ament_export_include_directories(include)

if(BUILD_TESTING)
find_package(ament_cmake_gmock REQUIRED)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()

add_library(fake_compressor SHARED
test/rosbag2_compression/fake_compressor.cpp)
target_include_directories(fake_compressor PUBLIC include)
ament_target_dependencies(fake_compressor rosbag2_storage)
set_target_properties(fake_compressor
PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN 1)
target_compile_definitions(fake_compressor PRIVATE ROSBAG2_COMPRESSION_BUILDING_DLL)

ament_add_gmock(test_compressor
test/rosbag2_compression/test_compressor.cpp)
target_link_libraries(test_compressor fake_compressor)
ament_target_dependencies(test_compressor rcpputils)
endif()

ament_package()
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// 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_COMPRESSION__BASE_COMPRESSOR_INTERFACE_HPP_
#define ROSBAG2_COMPRESSION__BASE_COMPRESSOR_INTERFACE_HPP_

#include <string>

#include "rosbag2_storage/serialized_bag_message.hpp"
#include "visibility_control.hpp"

namespace rosbag2_compression
{

/**
* An interface for developers adding a new compression algorithm to rosbag2.
* These functions must be implemented so that a writer can properly compress a file or bag message.
* A corresponding decompressor with an identical decompression format must also be implemented.
*
* Example file compression usage:
*
* MyCompressor my_compressor();
* std::string current_uri = storage.get_relative_path();
* std::string compressed_path_uri = my_compressor.compress_uri(current_uri);
* relative_file_paths.push_back(compressed_path_uri);
*
* Example message compression usage:
*
* MyCompressor my_compressor();
* std::shared_ptr<SerializedBagMessage> bag_message = std::make_shared<SerializedBagMessage>();
* ...fill message
* my_compressor.compress_serialized_bag_message(bag_message.get());
*/
class ROSBAG2_COMPRESSION_PUBLIC BaseCompressorInterface
{
public:
virtual ~BaseCompressorInterface() = default;

/**
* Compress a file on disk.
*
* \param uri Input file to compress with file extension.
* \return The relative path to the compressed file with the compressed extension.
*/
virtual std::string compress_uri(const std::string & uri) = 0;

/**
* Compress the serialized_data of a serialized bag message in place.
*
* \param bag_message A serialized bag message.
*/
virtual void compress_serialized_bag_message(
rosbag2_storage::SerializedBagMessage * bag_message) = 0;

/**
* Get the identifier of the compression algorithm.
* This is appended to the extension of the compressed file.
*/
virtual std::string get_compression_identifier() const = 0;
};

} // namespace rosbag2_compression

#endif // ROSBAG2_COMPRESSION__BASE_COMPRESSOR_INTERFACE_HPP_
3 changes: 3 additions & 0 deletions rosbag2_compression/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rcpputils</depend>
<depend>rcutils</depend>
<depend>rosbag2_storage</depend>

<test_depend>ament_cmake_gmock</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

Expand Down
30 changes: 30 additions & 0 deletions rosbag2_compression/test/rosbag2_compression/fake_compressor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// 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.

#include <string>

#include "fake_compressor.hpp"

std::string FakeCompressor::compress_uri(const std::string & uri)
{
return uri + "." + get_compression_identifier();
}

void FakeCompressor::compress_serialized_bag_message(
rosbag2_storage::SerializedBagMessage *) {}

std::string FakeCompressor::get_compression_identifier() const
{
return "fake_comp";
}
40 changes: 40 additions & 0 deletions rosbag2_compression/test/rosbag2_compression/fake_compressor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// 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_COMPRESSION__FAKE_COMPRESSOR_HPP_
#define ROSBAG2_COMPRESSION__FAKE_COMPRESSOR_HPP_

#include <string>

#include "rosbag2_compression/base_compressor_interface.hpp"
#include "rosbag2_compression/visibility_control.hpp"
#include "rosbag2_storage/serialized_bag_message.hpp"

class ROSBAG2_COMPRESSION_PUBLIC FakeCompressor : public rosbag2_compression::
BaseCompressorInterface
{
public:
FakeCompressor() = default;

std::string compress_uri(const std::string & uri) override;

void compress_serialized_bag_message(
rosbag2_storage::SerializedBagMessage * bag_message) override;

std::string get_compression_identifier() const override;

~FakeCompressor() override = default;
};

#endif // ROSBAG2_COMPRESSION__FAKE_COMPRESSOR_HPP_
44 changes: 44 additions & 0 deletions rosbag2_compression/test/rosbag2_compression/test_compressor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// 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.

#include <gmock/gmock.h>

#include <memory>
#include <string>

#include "rcpputils/filesystem_helper.hpp"

#include "fake_compressor.hpp"

class FakeCompressorTest : public ::testing::Test
{
public:
FakeCompressor compressor;
};

TEST_F(FakeCompressorTest, test_compress_file_method)
{
rcpputils::fs::path test_path("/path/file.txt");
auto expected_compressed_uri = test_path.string() + "." +
compressor.get_compression_identifier();
auto compressed_uri = compressor.compress_uri(test_path.string());
EXPECT_EQ(compressed_uri, expected_compressed_uri);
}

TEST_F(FakeCompressorTest, test_compress_bag_method)
{
auto bag_message = std::make_shared<rosbag2_storage::SerializedBagMessage>();
compressor.compress_serialized_bag_message(bag_message.get());
EXPECT_THAT(bag_message, ::testing::NotNull());
}

0 comments on commit 22a6822

Please sign in to comment.