1+ #include " sparrow_ipc/serialize.hpp"
2+
3+ #include < cstdint>
14#include < optional>
25
3- #include " sparrow_ipc/serialize.hpp"
46#include " sparrow_ipc/flatbuffer_utils.hpp"
57#include " sparrow_ipc/utils.hpp"
68
79namespace sparrow_ipc
810{
9- void common_serialize (
10- const flatbuffers::FlatBufferBuilder& builder,
11- any_output_stream& stream
12- )
11+ void common_serialize (const flatbuffers::FlatBufferBuilder& builder, any_output_stream& stream)
1312 {
1413 stream.write (continuation);
1514 const flatbuffers::uoffset_t size = builder.GetSize ();
16- const std::span<const uint8_t > size_span (reinterpret_cast <const uint8_t *>(&size), sizeof (uint32_t ));
15+ const int32_t size_with_padding = utils::align_to_8 (static_cast <int32_t >(size));
16+ const std::span<const uint8_t > size_span (
17+ reinterpret_cast <const uint8_t *>(&size_with_padding),
18+ sizeof (int32_t )
19+ );
1720 stream.write (size_span);
1821 stream.write (std::span (builder.GetBufferPointer (), size));
1922 stream.add_padding ();
@@ -24,8 +27,12 @@ namespace sparrow_ipc
2427 common_serialize (get_schema_message_builder (record_batch), stream);
2528 }
2629
27- serialized_record_batch_info serialize_record_batch (const sparrow::record_batch& record_batch, any_output_stream& stream,
28- std::optional<CompressionType> compression,
30+ serialized_record_batch_info serialize_record_batch (
31+ const sparrow::record_batch& record_batch,
32+ any_output_stream& stream,
33+
34+ std::optional<CompressionType> compression
35+ ,
2936 std::optional<std::reference_wrapper<CompressionCache>> cache)
3037 {
3138 // Build and serialize metadata
@@ -49,16 +56,16 @@ namespace sparrow_ipc
4956
5057 // Write metadata
5158 common_serialize (builder, stream);
52-
59+
5360 // Track position before body to calculate body length
5461 const size_t body_start = stream.size ();
55-
62+
5663 // Write body
5764 generate_body (record_batch, stream, compression, cache);
58-
59- // Calculate body length (should already be 8-aligned since generate_body pads each buffer)
60- const int64_t body_length = static_cast < int64_t >(stream. size () - body_start );
61-
62- return {metadata_length, body_length};
65+
66+ const auto body_length = static_cast < int64_t >(stream. size () - body_start);
67+ const flatbuffers:: uoffset_t flatbuffer_size = builder. GetSize ( );
68+ const auto metadata_length = static_cast < int32_t >( utils::align_to_8 (flatbuffer_size));
69+ return {. metadata_length = metadata_length, . body_length = body_length};
6370 }
6471}
0 commit comments