@@ -142,21 +142,21 @@ class RecordBatchWriter {
142142
143143 Status AssemblePayload () {
144144 // Perform depth-first traversal of the row-batch
145- for (int i = 0 ; i < columns_->size (); ++i) {
145+ for (size_t i = 0 ; i < columns_->size (); ++i) {
146146 const Array* arr = (*columns_)[i].get ();
147147 RETURN_NOT_OK (VisitArray (arr, &field_nodes_, &buffers_, max_recursion_depth_));
148148 }
149149 return Status::OK ();
150150 }
151151
152152 Status Write (io::OutputStream* dst, int64_t * data_header_offset) {
153- // Write out all the buffers contiguously and compute the total size of the
154- // memory payload
155- int64_t offset = 0 ;
156-
157153 // Get the starting position
158- int64_t position;
159- RETURN_NOT_OK (dst->Tell (&position));
154+ int64_t start_position;
155+ RETURN_NOT_OK (dst->Tell (&start_position));
156+
157+ // Keep track of the current position so we can determine the size of the
158+ // message body
159+ int64_t position = start_position;
160160
161161 for (size_t i = 0 ; i < buffers_.size (); ++i) {
162162 const Buffer* buffer = buffers_[i].get ();
@@ -178,11 +178,11 @@ class RecordBatchWriter {
178178 // are using from any OS-level shared memory. The thought is that systems
179179 // may (in the future) associate integer page id's with physical memory
180180 // pages (according to whatever is the desired shared memory mechanism)
181- buffer_meta_.push_back (flatbuf::Buffer (0 , position + offset , size));
181+ buffer_meta_.push_back (flatbuf::Buffer (0 , position, size));
182182
183183 if (size > 0 ) {
184184 RETURN_NOT_OK (dst->Write (buffer->data (), size));
185- offset += size;
185+ position += size;
186186 }
187187 }
188188
@@ -194,13 +194,24 @@ class RecordBatchWriter {
194194 // determine the data header size then request a buffer such that you can
195195 // construct the flatbuffer data accessor object (see arrow::ipc::Message)
196196 std::shared_ptr<Buffer> data_header;
197- RETURN_NOT_OK (
198- WriteDataHeader ( num_rows_, offset , field_nodes_, buffer_meta_, &data_header));
197+ RETURN_NOT_OK (WriteDataHeader (
198+ num_rows_, position - start_position , field_nodes_, buffer_meta_, &data_header));
199199
200200 // Write the data header at the end
201201 RETURN_NOT_OK (dst->Write (data_header->data (), data_header->size ()));
202+ *data_header_offset = position;
202203
203- *data_header_offset = position + offset;
204+ return Align (dst, &position);
205+ }
206+
207+ Status Align (io::OutputStream* dst, int64_t * position) {
208+ // Write all buffers here on word boundaries
209+ // TODO(wesm): Is there benefit to 64-byte padding in IPC?
210+ int64_t remainder = PaddedLength (*position) - *position;
211+ if (remainder > 0 ) {
212+ RETURN_NOT_OK (dst->Write (kPaddingBytes , remainder));
213+ *position += remainder;
214+ }
204215 return Status::OK ();
205216 }
206217
0 commit comments