From 1daf83341d5dde4d3221e4c8cf84af75b7de91c9 Mon Sep 17 00:00:00 2001 From: tomoya Date: Tue, 20 Oct 2020 14:22:05 +0900 Subject: [PATCH] if cache data exists, it needs to flush the data into the storage before shutdown (#541) fix #540 * if cache data exists, it needs to flush the data into the storage before shutdown. * cache_ needs to reset after writing to storage. * add reset_cache() method. Signed-off-by: Tomoya.Fujita --- .../rosbag2_cpp/writers/sequential_writer.hpp | 3 +++ .../rosbag2_cpp/writers/sequential_writer.cpp | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/rosbag2_cpp/include/rosbag2_cpp/writers/sequential_writer.hpp b/rosbag2_cpp/include/rosbag2_cpp/writers/sequential_writer.hpp index 8617e71385..b52d523dcd 100644 --- a/rosbag2_cpp/include/rosbag2_cpp/writers/sequential_writer.hpp +++ b/rosbag2_cpp/include/rosbag2_cpp/writers/sequential_writer.hpp @@ -141,6 +141,9 @@ class ROSBAG2_CPP_PUBLIC SequentialWriter // Record TopicInformation into metadata void finalize_metadata(); + // Flush data into storage, and reset cache + void reset_cache(); + // Helper method used by write to get the message in a format that is ready to be written. // Common use cases include converting the message using the converter or // performing other operations like compression on it diff --git a/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp b/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp index 15d5e9a41e..e460bbebbc 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp @@ -140,6 +140,7 @@ void SequentialWriter::reset() metadata_io_->write_metadata(base_folder_, metadata_); } + reset_cache(); storage_.reset(); // Necessary to ensure that the storage is destroyed before the factory storage_factory_.reset(); } @@ -251,11 +252,7 @@ void SequentialWriter::write(std::shared_ptrserialized_data->buffer_length; if (current_cache_size_ >= max_cache_size_) { - storage_->write(cache_); - // reset cache - cache_.clear(); - cache_.reserve(max_cache_size_); - current_cache_size_ = 0u; + reset_cache(); } } } @@ -312,5 +309,16 @@ void SequentialWriter::finalize_metadata() } } +void SequentialWriter::reset_cache() +{ + // if cache data exists, it must flush the data into the storage + if (!cache_.empty()) { + storage_->write(cache_); + // reset cache + cache_.clear(); + cache_.reserve(max_cache_size_); + current_cache_size_ = 0u; + } +} } // namespace writers } // namespace rosbag2_cpp