From 9c256c8675ebcc2f3b22c88f938a1500696daf5c Mon Sep 17 00:00:00 2001 From: Mikael Persson Date: Thu, 12 Dec 2024 15:40:53 -0500 Subject: [PATCH] Preemptively close chunks before big messages to isolate them --- cpp/mcap/include/mcap/writer.inl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cpp/mcap/include/mcap/writer.inl b/cpp/mcap/include/mcap/writer.inl index dd9b82cb9..ffa348631 100644 --- a/cpp/mcap/include/mcap/writer.inl +++ b/cpp/mcap/include/mcap/writer.inl @@ -547,6 +547,16 @@ Status McapWriter::write(const Message& message) { ++statistics_.channelCount; } + // Before writing a large message (bigger than chunk size), close current chunk. + auto* chunkWriter = getChunkWriter(); + if (chunkWriter != nullptr && /* Chunked? */ + uncompressedSize_ != 0 && /* Current chunk is not empty/new? */ + message.dataSize >= chunkSize_ /* Big message? */ ) { + auto& fileOutput = *output_; + writeChunk(fileOutput, *chunkWriter); + } + + // For the chunk-local message index. const uint64_t messageOffset = uncompressedSize_; // Write the message @@ -565,8 +575,7 @@ Status McapWriter::write(const Message& message) { channelMessageCounts[message.channelId] += 1; } - auto* chunkWriter = getChunkWriter(); - if (chunkWriter) { + if (chunkWriter != nullptr) { if (!options_.noMessageIndex) { // Update the message index auto& messageIndex = currentMessageIndex_[message.channelId];