Skip to content

Commit

Permalink
if cache data exists, it needs to flush the data into the storage bef…
Browse files Browse the repository at this point in the history
…ore shutdown (ros2#541)

fix ros2#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 <Tomoya.Fujita@sony.com>
  • Loading branch information
fujitatomoya committed Oct 20, 2020
1 parent 54cee3c commit 1daf833
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions rosbag2_cpp/include/rosbag2_cpp/writers/sequential_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -251,11 +252,7 @@ void SequentialWriter::write(std::shared_ptr<rosbag2_storage::SerializedBagMessa
cache_.push_back(converted_msg);
current_cache_size_ += converted_msg->serialized_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();
}
}
}
Expand Down Expand Up @@ -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

0 comments on commit 1daf833

Please sign in to comment.