forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Compression - 3] Add compressor interface
[Compression - 3] Add compressor interface
- Loading branch information
Showing
6 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
rosbag2_compression/include/rosbag2_compression/base_compressor_interface.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
rosbag2_compression/test/rosbag2_compression/fake_compressor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
rosbag2_compression/test/rosbag2_compression/fake_compressor.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
rosbag2_compression/test/rosbag2_compression/test_compressor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |