paleolimbot commented on code in PR #570: URL: https://github.com/apache/arrow-nanoarrow/pull/570#discussion_r1697788237
########## src/nanoarrow/ipc/writer.c: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> + +#include "nanoarrow/nanoarrow.h" +#include "nanoarrow/nanoarrow_ipc.h" + +void ArrowIpcOutputStreamMove(struct ArrowIpcOutputStream* src, + struct ArrowIpcOutputStream* dst) { + memcpy(dst, src, sizeof(struct ArrowIpcOutputStream)); + src->release = NULL; +} + +struct ArrowIpcOutputStreamBufferPrivate { + struct ArrowBuffer* output; +}; + +static ArrowErrorCode ArrowIpcOutputStreamBufferWrite(struct ArrowIpcOutputStream* stream, + const void* buf, + int64_t buf_size_bytes, + int64_t* size_written_out, + struct ArrowError* error) { + NANOARROW_UNUSED(error); + struct ArrowIpcOutputStreamBufferPrivate* private_data = + (struct ArrowIpcOutputStreamBufferPrivate*)stream->private_data; + NANOARROW_RETURN_NOT_OK(ArrowBufferAppend(private_data->output, buf, buf_size_bytes)); + *size_written_out = buf_size_bytes; + return NANOARROW_OK; +} + +static void ArrowIpcOutputStreamBufferRelease(struct ArrowIpcOutputStream* stream) { + struct ArrowIpcOutputStreamBufferPrivate* private_data = + (struct ArrowIpcOutputStreamBufferPrivate*)stream->private_data; + ArrowFree(private_data); + stream->release = NULL; +} + +ArrowErrorCode ArrowIpcOutputStreamInitBuffer(struct ArrowIpcOutputStream* stream, + struct ArrowBuffer* output) { + struct ArrowIpcOutputStreamBufferPrivate* private_data = + (struct ArrowIpcOutputStreamBufferPrivate*)ArrowMalloc( + sizeof(struct ArrowIpcOutputStreamBufferPrivate)); + if (private_data == NULL) { + return ENOMEM; + } + + private_data->output = output; + stream->write = &ArrowIpcOutputStreamBufferWrite; + stream->release = &ArrowIpcOutputStreamBufferRelease; + stream->private_data = private_data; + + return NANOARROW_OK; +} + +struct ArrowIpcOutputStreamFilePrivate { + FILE* file_ptr; + int stream_finished; + int close_on_release; +}; + +static void ArrowIpcOutputStreamFileRelease(struct ArrowIpcOutputStream* stream) { + struct ArrowIpcOutputStreamFilePrivate* private_data = + (struct ArrowIpcOutputStreamFilePrivate*)stream->private_data; + + if (private_data->file_ptr != NULL && private_data->close_on_release) { + fclose(private_data->file_ptr); + } + + ArrowFree(private_data); + stream->release = NULL; +} + +static ArrowErrorCode ArrowIpcOutputStreamFileWrite(struct ArrowIpcOutputStream* stream, + const void* buf, + int64_t buf_size_bytes, + int64_t* size_written_out, + struct ArrowError* error) { + struct ArrowIpcOutputStreamFilePrivate* private_data = + (struct ArrowIpcOutputStreamFilePrivate*)stream->private_data; + + if (private_data->stream_finished) { + *size_written_out = 0; + return NANOARROW_OK; Review Comment: Should this be an error? Would this be like writing to a closed file? ########## src/nanoarrow/ipc/encoder.c: ########## @@ -52,11 +52,13 @@ void ArrowIpcEncoderReset(struct ArrowIpcEncoder* encoder) { NANOARROW_DCHECK(encoder != NULL && encoder->private_data != NULL); struct ArrowIpcEncoderPrivate* private = (struct ArrowIpcEncoderPrivate*)encoder->private_data; - flatcc_builder_clear(&private->builder); - ArrowBufferReset(&private->nodes); - ArrowBufferReset(&private->buffers); - ArrowFree(private); - memset(encoder, 0, sizeof(struct ArrowIpcEncoder)); + if (private != NULL) { Review Comment: This has already been checked as a debug check? ########## src/nanoarrow/ipc/writer.c: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> + +#include "nanoarrow/nanoarrow.h" +#include "nanoarrow/nanoarrow_ipc.h" + +void ArrowIpcOutputStreamMove(struct ArrowIpcOutputStream* src, + struct ArrowIpcOutputStream* dst) { + memcpy(dst, src, sizeof(struct ArrowIpcOutputStream)); + src->release = NULL; +} + +struct ArrowIpcOutputStreamBufferPrivate { + struct ArrowBuffer* output; +}; + +static ArrowErrorCode ArrowIpcOutputStreamBufferWrite(struct ArrowIpcOutputStream* stream, + const void* buf, + int64_t buf_size_bytes, + int64_t* size_written_out, + struct ArrowError* error) { + NANOARROW_UNUSED(error); Review Comment: It is a little verbose, but you can use `NANOARROW_RETURN_NOT_OK_WITH_ERROR` (I think that's what it's called) here. In general, we try to guarantee that `error` is always populated if anything other than `NANOARROW_OK` is returned. ########## src/nanoarrow/ipc/writer.c: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> + +#include "nanoarrow/nanoarrow.h" +#include "nanoarrow/nanoarrow_ipc.h" + +void ArrowIpcOutputStreamMove(struct ArrowIpcOutputStream* src, + struct ArrowIpcOutputStream* dst) { + memcpy(dst, src, sizeof(struct ArrowIpcOutputStream)); + src->release = NULL; +} Review Comment: Should this be `static inline`? ########## src/nanoarrow/ipc/writer.c: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> + +#include "nanoarrow/nanoarrow.h" +#include "nanoarrow/nanoarrow_ipc.h" + +void ArrowIpcOutputStreamMove(struct ArrowIpcOutputStream* src, + struct ArrowIpcOutputStream* dst) { + memcpy(dst, src, sizeof(struct ArrowIpcOutputStream)); + src->release = NULL; +} + +struct ArrowIpcOutputStreamBufferPrivate { + struct ArrowBuffer* output; +}; + +static ArrowErrorCode ArrowIpcOutputStreamBufferWrite(struct ArrowIpcOutputStream* stream, + const void* buf, + int64_t buf_size_bytes, + int64_t* size_written_out, + struct ArrowError* error) { + NANOARROW_UNUSED(error); + struct ArrowIpcOutputStreamBufferPrivate* private_data = + (struct ArrowIpcOutputStreamBufferPrivate*)stream->private_data; + NANOARROW_RETURN_NOT_OK(ArrowBufferAppend(private_data->output, buf, buf_size_bytes)); + *size_written_out = buf_size_bytes; + return NANOARROW_OK; +} + +static void ArrowIpcOutputStreamBufferRelease(struct ArrowIpcOutputStream* stream) { + struct ArrowIpcOutputStreamBufferPrivate* private_data = + (struct ArrowIpcOutputStreamBufferPrivate*)stream->private_data; + ArrowFree(private_data); + stream->release = NULL; +} + +ArrowErrorCode ArrowIpcOutputStreamInitBuffer(struct ArrowIpcOutputStream* stream, + struct ArrowBuffer* output) { + struct ArrowIpcOutputStreamBufferPrivate* private_data = + (struct ArrowIpcOutputStreamBufferPrivate*)ArrowMalloc( + sizeof(struct ArrowIpcOutputStreamBufferPrivate)); + if (private_data == NULL) { + return ENOMEM; + } + + private_data->output = output; + stream->write = &ArrowIpcOutputStreamBufferWrite; + stream->release = &ArrowIpcOutputStreamBufferRelease; + stream->private_data = private_data; + + return NANOARROW_OK; +} + +struct ArrowIpcOutputStreamFilePrivate { + FILE* file_ptr; + int stream_finished; + int close_on_release; +}; + +static void ArrowIpcOutputStreamFileRelease(struct ArrowIpcOutputStream* stream) { + struct ArrowIpcOutputStreamFilePrivate* private_data = + (struct ArrowIpcOutputStreamFilePrivate*)stream->private_data; + + if (private_data->file_ptr != NULL && private_data->close_on_release) { + fclose(private_data->file_ptr); + } + + ArrowFree(private_data); + stream->release = NULL; +} + +static ArrowErrorCode ArrowIpcOutputStreamFileWrite(struct ArrowIpcOutputStream* stream, + const void* buf, + int64_t buf_size_bytes, + int64_t* size_written_out, + struct ArrowError* error) { + struct ArrowIpcOutputStreamFilePrivate* private_data = + (struct ArrowIpcOutputStreamFilePrivate*)stream->private_data; + + if (private_data->stream_finished) { + *size_written_out = 0; + return NANOARROW_OK; + } + + // Do the write + int64_t bytes_written = (int64_t)fwrite(buf, 1, buf_size_bytes, private_data->file_ptr); + *size_written_out = bytes_written; + + if (bytes_written != buf_size_bytes) { + private_data->stream_finished = 1; + + // Inspect error + int has_error = !feof(private_data->file_ptr) && ferror(private_data->file_ptr); + + // Try to close the file now + if (private_data->close_on_release) { + if (fclose(private_data->file_ptr) == 0) { + private_data->file_ptr = NULL; + } + } + + // Maybe return error + if (has_error) { Review Comment: Should this return an error if there was an error closing the file? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
