@@ -50,77 +50,12 @@ namespace sparrow_ipc
5050
5151 // II - Serialize the RecordBatch message
5252 // After the Schema, we send the RecordBatch containing the actual data
53- {
54- // Create a new builder for the RecordBatch message's metadata
55- flatbuffers::FlatBufferBuilder batch_builder;
56-
57- // arrow_arr.buffers[0] is the validity bitmap
58- // arrow_arr.buffers[1] is the data buffer
59- const uint8_t * validity_bitmap = reinterpret_cast <const uint8_t *>(arrow_arr.buffers [0 ]);
60- const uint8_t * data_buffer = reinterpret_cast <const uint8_t *>(arrow_arr.buffers [1 ]);
61-
62- // Calculate the size of the validity and data buffers
63- int64_t validity_size = (arrow_arr.length + 7 ) / 8 ;
64- int64_t data_size = arrow_arr.length * sizeof (T);
65- int64_t body_len = validity_size + data_size; // The total size of the message body
66-
67- // Create Flatbuffer descriptions for the data buffers
68- org::apache::arrow::flatbuf::Buffer validity_buffer_struct (0 , validity_size);
69- org::apache::arrow::flatbuf::Buffer data_buffer_struct (validity_size, data_size);
70- // Create the FieldNode, which describes the layout of the array data
71- org::apache::arrow::flatbuf::FieldNode field_node_struct (arrow_arr.length , arrow_arr.null_count );
72-
73- // A RecordBatch contains a vector of nodes and a vector of buffers
74- auto fb_nodes_vector = batch_builder.CreateVectorOfStructs (&field_node_struct, 1 );
75- std::vector<org::apache::arrow::flatbuf::Buffer> buffers_vec = {validity_buffer_struct, data_buffer_struct};
76- auto fb_buffers_vector = batch_builder.CreateVectorOfStructs (buffers_vec);
77-
78- // Build the RecordBatch metadata object
79- auto record_batch_offset = org::apache::arrow::flatbuf::CreateRecordBatch (batch_builder, arrow_arr.length , fb_nodes_vector, fb_buffers_vector);
80-
81- // Wrap the RecordBatch in a top-level Message
82- auto batch_message_offset = org::apache::arrow::flatbuf::CreateMessage (
83- batch_builder,
84- org::apache::arrow::flatbuf::MetadataVersion::V5,
85- org::apache::arrow::flatbuf::MessageHeader::RecordBatch,
86- record_batch_offset.Union (),
87- body_len
88- );
89- batch_builder.Finish (batch_message_offset);
90-
91- // III - Append the RecordBatch message to the final buffer
92- uint32_t batch_meta_len = batch_builder.GetSize (); // Get the size of the batch metadata
93- int64_t aligned_batch_meta_len = utils::align_to_8 (batch_meta_len); // Calculate the padded length
94-
95- size_t current_size = final_buffer.size (); // Get the current size (which is the end of the Schema message)
96- // Resize the buffer to append the new message
97- final_buffer.resize (current_size + sizeof (uint32_t ) + aligned_batch_meta_len + body_len);
98- uint8_t * dst = final_buffer.data () + current_size; // Get a pointer to where the new message will start
99-
100- // Write the 4-byte metadata length for the RecordBatch message
101- *(reinterpret_cast <uint32_t *>(dst)) = batch_meta_len;
102- dst += sizeof (uint32_t );
103- // Copy the RecordBatch metadata into the buffer
104- memcpy (dst, batch_builder.GetBufferPointer (), batch_meta_len);
105- // Add padding to align the body to an 8-byte boundary
106- memset (dst + batch_meta_len, 0 , aligned_batch_meta_len - batch_meta_len);
107- dst += aligned_batch_meta_len;
108- // Copy the actual data buffers (the message body) into the buffer
109- if (validity_bitmap)
110- {
111- memcpy (dst, validity_bitmap, validity_size);
112- }
113- else
114- {
115- // If validity_bitmap is null, it means there are no nulls
116- memset (dst, 0xFF , validity_size);
117- }
118- dst += validity_size;
119- if (data_buffer)
120- {
121- memcpy (dst, data_buffer, data_size);
122- }
123- }
53+
54+ // Calculate the size of the validity and data buffers
55+ int64_t validity_size = (arrow_arr.length + 7 ) / 8 ;
56+ int64_t data_size = arrow_arr.length * sizeof (T);
57+ std::vector<int64_t > buffers_sizes = {validity_size, data_size};
58+ details::serialize_record_batch_message (arrow_arr, buffers_sizes, final_buffer);
12459
12560 // Return the final buffer containing the complete IPC stream
12661 return final_buffer;
0 commit comments