bkietz commented on code in PR #585:
URL: https://github.com/apache/arrow-nanoarrow/pull/585#discussion_r1717123550


##########
src/nanoarrow/ipc/json_integration.cc:
##########
@@ -0,0 +1,405 @@
+// 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 <cstdlib>
+#include <fstream>
+
+#include <nanoarrow/nanoarrow_ipc.hpp>
+#include <nanoarrow/nanoarrow_testing.hpp>
+
+#include "flatcc/flatcc_builder.h"
+#include "nanoarrow/ipc/flatcc_generated.h"
+
+struct File {
+  File() = default;
+
+  ~File() {
+    if (file_ != nullptr) {
+      fclose(file_);
+    }
+  }
+
+  ArrowErrorCode open(std::string path, std::string mode, struct ArrowError* 
error) {
+    file_ = fopen(path.c_str(), mode.c_str());
+    if (file_ != nullptr) {
+      return NANOARROW_OK;
+    }
+    ArrowErrorSet(error, "Opening file '%s' failed with errno=%d", 
path.c_str(), errno);
+    return EINVAL;
+  }
+
+  operator FILE*() const { return file_; }
+
+  FILE* file_;
+};
+
+struct Table {
+  nanoarrow::UniqueSchema schema;
+  std::vector<nanoarrow::UniqueArray> batches;
+};
+
+std::string GetEnv(char const* name) {
+  if (char const* val = std::getenv(name)) {
+    return val;
+  }
+  return "";
+}
+
+constexpr char kPaddedMagic[8] = "ARROW1\0";
+
+std::string ReadFileIntoString(std::string const& path) {
+  std::ifstream stream{path};
+  std::string contents(stream.seekg(0, std::ios_base::end).tellg(), '\0');
+  stream.seekg(0).read(contents.data(), contents.size());
+  return contents;
+}
+
+ArrowErrorCode ReadTableFromArrayStream(struct ArrowArrayStream* stream, 
Table* table,
+                                        struct ArrowError* error) {
+  NANOARROW_RETURN_NOT_OK(ArrowArrayStreamGetSchema(stream, 
table->schema.get(), error));
+
+  while (true) {
+    nanoarrow::UniqueArray batch;
+    NANOARROW_RETURN_NOT_OK(ArrowArrayStreamGetNext(stream, batch.get(), 
error));
+    if (batch->release == nullptr) {
+      break;
+    }
+    table->batches.push_back(std::move(batch));
+  }
+
+  return NANOARROW_OK;
+}
+
+ArrowErrorCode ReadTableFromJson(std::string const& json, Table* table,
+                                 struct ArrowError* error) {
+  nanoarrow::testing::TestingJSONReader reader;
+  nanoarrow::UniqueArrayStream array_stream;
+  NANOARROW_RETURN_NOT_OK(
+      reader.ReadDataFile(json, array_stream.get(), reader.kNumBatchReadAll, 
error));
+  return ReadTableFromArrayStream(array_stream.get(), table, error);
+}
+
+ArrowErrorCode ReadTableFromIpcFile(std::string const& path, Table* table,
+                                    struct ArrowError* error) {
+  // FIXME this API needs to be public; it's a bit smelly to pretend that we 
support
+  // reading files when this bespoke program is the only one which can do it
+  //
+  // For now: just check the first 8 bytes of the file and read a stream 
(ignoring the
+  // Footer).
+  File ipc_file;
+  NANOARROW_RETURN_NOT_OK(ipc_file.open(path, "rb", error));
+
+  char prefix[sizeof(kPaddedMagic)] = {};
+  if (fread(&prefix, 1, sizeof(prefix), ipc_file) < sizeof(prefix)) {
+    ArrowErrorSet(error, "Expected file of more than %lu bytes, got %ld", 
sizeof(prefix),
+                  ftell(ipc_file));
+    return EINVAL;
+  }
+
+  if (memcmp(&prefix, kPaddedMagic, sizeof(prefix)) != 0) {
+    ArrowErrorSet(error, "File did not begin with 'ARROW1\\0\\0'");
+    return EINVAL;
+  }
+
+  nanoarrow::ipc::UniqueInputStream input_stream;
+  NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+      ArrowIpcInputStreamInitFile(input_stream.get(), ipc_file,
+                                  /*close_on_release=*/false),
+      error);
+
+  nanoarrow::UniqueArrayStream array_stream;
+  NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+      ArrowIpcArrayStreamReaderInit(array_stream.get(), input_stream.get(),
+                                    /*options=*/nullptr),
+      error);
+
+  return ReadTableFromArrayStream(array_stream.get(), table, error);
+}
+
+ArrowErrorCode WriteTableAsStream(Table const& table,
+                                  struct ArrowIpcOutputStream* output_stream,
+                                  struct ArrowError* error,
+                                  struct ArrowBuffer* blocks = nullptr) {
+  nanoarrow::ipc::UniqueWriter writer;
+  NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowIpcWriterInit(writer.get(), 
output_stream),
+                                     error);
+
+  NANOARROW_RETURN_NOT_OK(
+      ArrowIpcWriterWriteSchema(writer.get(), table.schema.get(), error));
+
+  nanoarrow::UniqueArrayView array_view;
+  NANOARROW_RETURN_NOT_OK(
+      ArrowArrayViewInitFromSchema(array_view.get(), table.schema.get(), 
error));
+
+  for (const auto& batch : table.batches) {
+    NANOARROW_RETURN_NOT_OK(ArrowArrayViewSetArray(array_view.get(), 
batch.get(), error));
+    NANOARROW_RETURN_NOT_OK(
+        ArrowIpcWriterWriteArrayView(writer.get(), array_view.get(), error));
+  }
+
+  if (blocks != nullptr) {
+    struct ArrowIpcWriterPrivate {
+      struct ArrowIpcEncoder encoder;
+      struct ArrowIpcOutputStream output_stream;
+      struct ArrowBuffer buffer;
+      struct ArrowBuffer body_buffer;
+
+      int64_t offset;
+      struct ArrowBuffer blocks;
+    };
+    
ArrowBufferMove(&static_cast<ArrowIpcWriterPrivate*>(writer->private_data)->blocks,
+                    blocks);
+  }
+
+  return ArrowIpcWriterWriteArrayView(writer.get(), nullptr, error);
+}
+
+ArrowErrorCode WriteTableToIpcFile(std::string const& path, Table const& table,
+                                   struct ArrowError* error) {
+  // FIXME this API needs to be public; it's a bit smelly to pretend that we 
support
+  // writing files when this bespoke program is the only one which can do it
+  //
+  // For now: just write the leading magic, the stream + EOS, and a manual 
Footer.
+  File ipc_file;
+  NANOARROW_RETURN_NOT_OK(ipc_file.open(path, "wb", error));
+
+  nanoarrow::ipc::UniqueOutputStream output_stream;
+  NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+      ArrowIpcOutputStreamInitFile(output_stream.get(), ipc_file,
+                                   /*close_on_release=*/false),
+      error);
+
+  struct ArrowBufferView magic = {{kPaddedMagic}, sizeof(kPaddedMagic)};
+  NANOARROW_RETURN_NOT_OK(ArrowIpcOutputStreamWrite(output_stream.get(), 
magic, error));
+
+  nanoarrow::UniqueBuffer blocks;
+  NANOARROW_RETURN_NOT_OK(
+      WriteTableAsStream(table, output_stream.get(), error, blocks.get()));
+
+  nanoarrow::ipc::UniqueEncoder encoder;
+  NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowIpcEncoderInit(encoder.get()), 
error);
+
+#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(org_apache_arrow_flatbuf, x)
+
+#define FLATCC_RETURN_UNLESS_0_NO_NS(x, error)                        \
+  if ((x) != 0) {                                                     \
+    ArrowErrorSet(error, "%s:%d: %s failed", __FILE__, __LINE__, #x); \
+    return ENOMEM;                                                    \
+  }
+
+#define FLATCC_RETURN_UNLESS_0(x, error) FLATCC_RETURN_UNLESS_0_NO_NS(ns(x), 
error)
+
+#define FLATCC_RETURN_IF_NULL(x, error)                                 \
+  if (!(x)) {                                                           \
+    ArrowErrorSet(error, "%s:%d: %s was null", __FILE__, __LINE__, #x); \
+    return ENOMEM;                                                      \
+  }
+
+  struct ArrowIpcEncoderPrivate {
+    flatcc_builder_t builder;
+    struct ArrowBuffer buffers;
+    struct ArrowBuffer nodes;
+    int encoding_footer;
+  };

Review Comment:
   okay
   



-- 
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]

Reply via email to