http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/io-writeable-file.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/io-writeable-file.cpp b/c_glib/arrow-glib/io-writeable-file.cpp new file mode 100644 index 0000000..3de42dd --- /dev/null +++ b/c_glib/arrow-glib/io-writeable-file.cpp @@ -0,0 +1,84 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow/api.h> + +#include <arrow-glib/error.hpp> +#include <arrow-glib/io-writeable-file.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: io-writeable-file + * @title: GArrowIOWriteableFile + * @short_description: File output interface + * + * #GArrowIOWriteableFile is an interface for file output. + */ + +G_DEFINE_INTERFACE(GArrowIOWriteableFile, + garrow_io_writeable_file, + G_TYPE_OBJECT) + +static void +garrow_io_writeable_file_default_init (GArrowIOWriteableFileInterface *iface) +{ +} + +/** + * garrow_io_writeable_file_write_at: + * @writeable_file: A #GArrowIOWriteableFile. + * @position: The write start position. + * @data: (array length=n_bytes): The data to be written. + * @n_bytes: The number of bytes to be written. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_io_writeable_file_write_at(GArrowIOWriteableFile *writeable_file, + gint64 position, + const guint8 *data, + gint64 n_bytes, + GError **error) +{ + const auto arrow_writeable_file = + garrow_io_writeable_file_get_raw(writeable_file); + + auto status = arrow_writeable_file->WriteAt(position, data, n_bytes); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[io][writeable-file][write-at]"); + return FALSE; + } +} + +G_END_DECLS + +std::shared_ptr<arrow::io::WriteableFileInterface> +garrow_io_writeable_file_get_raw(GArrowIOWriteableFile *writeable_file) +{ + auto *iface = GARROW_IO_WRITEABLE_FILE_GET_IFACE(writeable_file); + return iface->get_raw(writeable_file); +}
http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/io-writeable-file.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/io-writeable-file.h b/c_glib/arrow-glib/io-writeable-file.h new file mode 100644 index 0000000..4a4dee5 --- /dev/null +++ b/c_glib/arrow-glib/io-writeable-file.h @@ -0,0 +1,51 @@ +/* + * 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. + */ + +#pragma once + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define GARROW_IO_TYPE_WRITEABLE_FILE \ + (garrow_io_writeable_file_get_type()) +#define GARROW_IO_WRITEABLE_FILE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_IO_TYPE_WRITEABLE_FILE, \ + GArrowIOWriteableFileInterface)) +#define GARROW_IO_IS_WRITEABLE_FILE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_IO_TYPE_WRITEABLE_FILE)) +#define GARROW_IO_WRITEABLE_FILE_GET_IFACE(obj) \ + (G_TYPE_INSTANCE_GET_INTERFACE((obj), \ + GARROW_IO_TYPE_WRITEABLE_FILE, \ + GArrowIOWriteableFileInterface)) + +typedef struct _GArrowIOWriteableFile GArrowIOWriteableFile; +typedef struct _GArrowIOWriteableFileInterface GArrowIOWriteableFileInterface; + +GType garrow_io_writeable_file_get_type(void) G_GNUC_CONST; + +gboolean garrow_io_writeable_file_write_at(GArrowIOWriteableFile *writeable_file, + gint64 position, + const guint8 *data, + gint64 n_bytes, + GError **error); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/io-writeable-file.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/io-writeable-file.hpp b/c_glib/arrow-glib/io-writeable-file.hpp new file mode 100644 index 0000000..2043007 --- /dev/null +++ b/c_glib/arrow-glib/io-writeable-file.hpp @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/io/interfaces.h> + +#include <arrow-glib/io-writeable-file.h> + +/** + * GArrowIOWriteableFileInterface: + * + * It wraps `arrow::io::WriteableFileInterface`. + */ +struct _GArrowIOWriteableFileInterface +{ + GTypeInterface parent_iface; + + std::shared_ptr<arrow::io::WriteableFileInterface> (*get_raw)(GArrowIOWriteableFile *file); +}; + +std::shared_ptr<arrow::io::WriteableFileInterface> garrow_io_writeable_file_get_raw(GArrowIOWriteableFile *writeable_file); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/io-writeable.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/io-writeable.cpp b/c_glib/arrow-glib/io-writeable.cpp new file mode 100644 index 0000000..9ea69e3 --- /dev/null +++ b/c_glib/arrow-glib/io-writeable.cpp @@ -0,0 +1,106 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow/api.h> + +#include <arrow-glib/error.hpp> +#include <arrow-glib/io-writeable.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: io-writeable + * @title: GArrowIOWriteable + * @short_description: Output interface + * + * #GArrowIOWriteable is an interface for output. Output must be + * writeable. + */ + +G_DEFINE_INTERFACE(GArrowIOWriteable, + garrow_io_writeable, + G_TYPE_OBJECT) + +static void +garrow_io_writeable_default_init (GArrowIOWriteableInterface *iface) +{ +} + +/** + * garrow_io_writeable_write: + * @writeable: A #GArrowIOWriteable. + * @data: (array length=n_bytes): The data to be written. + * @n_bytes: The number of bytes to be written. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_io_writeable_write(GArrowIOWriteable *writeable, + const guint8 *data, + gint64 n_bytes, + GError **error) +{ + const auto arrow_writeable = garrow_io_writeable_get_raw(writeable); + + auto status = arrow_writeable->Write(data, n_bytes); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[io][writeable][write]"); + return FALSE; + } +} + +/** + * garrow_io_writeable_flush: + * @writeable: A #GArrowIOWriteable. + * @error: (nullable): Return location for a #GError or %NULL. + * + * It ensures writing all data on memory to storage. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_io_writeable_flush(GArrowIOWriteable *writeable, + GError **error) +{ + const auto arrow_writeable = garrow_io_writeable_get_raw(writeable); + + auto status = arrow_writeable->Flush(); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[io][writeable][flush]"); + return FALSE; + } +} + +G_END_DECLS + +std::shared_ptr<arrow::io::Writeable> +garrow_io_writeable_get_raw(GArrowIOWriteable *writeable) +{ + auto *iface = GARROW_IO_WRITEABLE_GET_IFACE(writeable); + return iface->get_raw(writeable); +} http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/io-writeable.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/io-writeable.h b/c_glib/arrow-glib/io-writeable.h new file mode 100644 index 0000000..f5c5e91 --- /dev/null +++ b/c_glib/arrow-glib/io-writeable.h @@ -0,0 +1,52 @@ +/* + * 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. + */ + +#pragma once + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define GARROW_IO_TYPE_WRITEABLE \ + (garrow_io_writeable_get_type()) +#define GARROW_IO_WRITEABLE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_IO_TYPE_WRITEABLE, \ + GArrowIOWriteableInterface)) +#define GARROW_IO_IS_WRITEABLE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_IO_TYPE_WRITEABLE)) +#define GARROW_IO_WRITEABLE_GET_IFACE(obj) \ + (G_TYPE_INSTANCE_GET_INTERFACE((obj), \ + GARROW_IO_TYPE_WRITEABLE, \ + GArrowIOWriteableInterface)) + +typedef struct _GArrowIOWriteable GArrowIOWriteable; +typedef struct _GArrowIOWriteableInterface GArrowIOWriteableInterface; + +GType garrow_io_writeable_get_type(void) G_GNUC_CONST; + +gboolean garrow_io_writeable_write(GArrowIOWriteable *writeable, + const guint8 *data, + gint64 n_bytes, + GError **error); +gboolean garrow_io_writeable_flush(GArrowIOWriteable *writeable, + GError **error); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/io-writeable.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/io-writeable.hpp b/c_glib/arrow-glib/io-writeable.hpp new file mode 100644 index 0000000..f833924 --- /dev/null +++ b/c_glib/arrow-glib/io-writeable.hpp @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/io/interfaces.h> + +#include <arrow-glib/io-writeable.h> + +/** + * GArrowIOWriteableInterface: + * + * It wraps `arrow::io::Writeable`. + */ +struct _GArrowIOWriteableInterface +{ + GTypeInterface parent_iface; + + std::shared_ptr<arrow::io::Writeable> (*get_raw)(GArrowIOWriteable *file); +}; + +std::shared_ptr<arrow::io::Writeable> garrow_io_writeable_get_raw(GArrowIOWriteable *writeable); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-enums.c.template ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-enums.c.template b/c_glib/arrow-glib/ipc-enums.c.template new file mode 100644 index 0000000..c938f77 --- /dev/null +++ b/c_glib/arrow-glib/ipc-enums.c.template @@ -0,0 +1,56 @@ +/*** BEGIN file-header ***/ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/arrow-ipc-glib.h> +/*** END file-header ***/ + +/*** BEGIN file-production ***/ + +/* enumerations from "@filename@" */ +/*** END file-production ***/ + +/*** BEGIN value-header ***/ +GType +@enum_name@_get_type(void) +{ + static GType etype = 0; + if (G_UNLIKELY(etype == 0)) { + static const G@Type@Value values[] = { +/*** END value-header ***/ + +/*** BEGIN value-production ***/ + {@VALUENAME@, "@VALUENAME@", "@valuenick@"}, +/*** END value-production ***/ + +/*** BEGIN value-tail ***/ + {0, NULL, NULL} + }; + etype = g_@type@_register_static(g_intern_static_string("@EnumName@"), values); + } + return etype; +} +/*** END value-tail ***/ + +/*** BEGIN file-tail ***/ +/*** END file-tail ***/ http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-enums.h.template ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-enums.h.template b/c_glib/arrow-glib/ipc-enums.h.template new file mode 100644 index 0000000..e103c5b --- /dev/null +++ b/c_glib/arrow-glib/ipc-enums.h.template @@ -0,0 +1,41 @@ +/*** BEGIN file-header ***/ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/ipc-metadata-version.h> + +G_BEGIN_DECLS +/*** END file-header ***/ + +/*** BEGIN file-production ***/ + +/* enumerations from "@filename@" */ +/*** END file-production ***/ + +/*** BEGIN value-header ***/ +GType @enum_name@_get_type(void) G_GNUC_CONST; +#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type()) +/*** END value-header ***/ + +/*** BEGIN file-tail ***/ + +G_END_DECLS +/*** END file-tail ***/ http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-file-reader.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-file-reader.cpp b/c_glib/arrow-glib/ipc-file-reader.cpp new file mode 100644 index 0000000..b9e408c --- /dev/null +++ b/c_glib/arrow-glib/ipc-file-reader.cpp @@ -0,0 +1,247 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow/ipc/api.h> + +#include <arrow-glib/error.hpp> +#include <arrow-glib/record-batch.hpp> +#include <arrow-glib/schema.hpp> + +#include <arrow-glib/io-readable-file.hpp> + +#include <arrow-glib/ipc-file-reader.hpp> +#include <arrow-glib/ipc-metadata-version.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: ipc-file-reader + * @short_description: File reader class + * + * #GArrowIPCFileReader is a class for receiving data by file based IPC. + */ + +typedef struct GArrowIPCFileReaderPrivate_ { + std::shared_ptr<arrow::ipc::FileReader> file_reader; +} GArrowIPCFileReaderPrivate; + +enum { + PROP_0, + PROP_FILE_READER +}; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowIPCFileReader, + garrow_ipc_file_reader, + G_TYPE_OBJECT); + +#define GARROW_IPC_FILE_READER_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ + GARROW_IPC_TYPE_FILE_READER, \ + GArrowIPCFileReaderPrivate)) + +static void +garrow_ipc_file_reader_finalize(GObject *object) +{ + GArrowIPCFileReaderPrivate *priv; + + priv = GARROW_IPC_FILE_READER_GET_PRIVATE(object); + + priv->file_reader = nullptr; + + G_OBJECT_CLASS(garrow_ipc_file_reader_parent_class)->finalize(object); +} + +static void +garrow_ipc_file_reader_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GArrowIPCFileReaderPrivate *priv; + + priv = GARROW_IPC_FILE_READER_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_FILE_READER: + priv->file_reader = + *static_cast<std::shared_ptr<arrow::ipc::FileReader> *>(g_value_get_pointer(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_ipc_file_reader_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_ipc_file_reader_init(GArrowIPCFileReader *object) +{ +} + +static void +garrow_ipc_file_reader_class_init(GArrowIPCFileReaderClass *klass) +{ + GObjectClass *gobject_class; + GParamSpec *spec; + + gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = garrow_ipc_file_reader_finalize; + gobject_class->set_property = garrow_ipc_file_reader_set_property; + gobject_class->get_property = garrow_ipc_file_reader_get_property; + + spec = g_param_spec_pointer("file-reader", + "ipc::FileReader", + "The raw std::shared<arrow::ipc::FileReader> *", + static_cast<GParamFlags>(G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_FILE_READER, spec); +} + +/** + * garrow_ipc_file_reader_open: + * @file: The file to be read. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): A newly opened + * #GArrowIPCFileReader or %NULL on error. + */ +GArrowIPCFileReader * +garrow_ipc_file_reader_open(GArrowIOReadableFile *file, + GError **error) +{ + std::shared_ptr<arrow::ipc::FileReader> arrow_file_reader; + auto status = + arrow::ipc::FileReader::Open(garrow_io_readable_file_get_raw(file), + &arrow_file_reader); + if (status.ok()) { + return garrow_ipc_file_reader_new_raw(&arrow_file_reader); + } else { + garrow_error_set(error, status, "[ipc][file-reader][open]"); + return NULL; + } +} + +/** + * garrow_ipc_file_reader_get_schema: + * @file_reader: A #GArrowIPCFileReader. + * + * Returns: (transfer full): The schema in the file. + */ +GArrowSchema * +garrow_ipc_file_reader_get_schema(GArrowIPCFileReader *file_reader) +{ + auto arrow_file_reader = + garrow_ipc_file_reader_get_raw(file_reader); + auto arrow_schema = arrow_file_reader->schema(); + return garrow_schema_new_raw(&arrow_schema); +} + +/** + * garrow_ipc_file_reader_get_n_record_batches: + * @file_reader: A #GArrowIPCFileReader. + * + * Returns: The number of record batches in the file. + */ +guint +garrow_ipc_file_reader_get_n_record_batches(GArrowIPCFileReader *file_reader) +{ + auto arrow_file_reader = + garrow_ipc_file_reader_get_raw(file_reader); + return arrow_file_reader->num_record_batches(); +} + +/** + * garrow_ipc_file_reader_get_version: + * @file_reader: A #GArrowIPCFileReader. + * + * Returns: The format version in the file. + */ +GArrowIPCMetadataVersion +garrow_ipc_file_reader_get_version(GArrowIPCFileReader *file_reader) +{ + auto arrow_file_reader = + garrow_ipc_file_reader_get_raw(file_reader); + auto arrow_version = arrow_file_reader->version(); + return garrow_ipc_metadata_version_from_raw(arrow_version); +} + +/** + * garrow_ipc_file_reader_get_record_batch: + * @file_reader: A #GArrowIPCFileReader. + * @i: The index of the target record batch. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): + * The i-th record batch in the file or %NULL on error. + */ +GArrowRecordBatch * +garrow_ipc_file_reader_get_record_batch(GArrowIPCFileReader *file_reader, + guint i, + GError **error) +{ + auto arrow_file_reader = + garrow_ipc_file_reader_get_raw(file_reader); + std::shared_ptr<arrow::RecordBatch> arrow_record_batch; + auto status = arrow_file_reader->GetRecordBatch(i, &arrow_record_batch); + + if (status.ok()) { + return garrow_record_batch_new_raw(&arrow_record_batch); + } else { + garrow_error_set(error, status, "[ipc][file-reader][get-record-batch]"); + return NULL; + } +} + +G_END_DECLS + +GArrowIPCFileReader * +garrow_ipc_file_reader_new_raw(std::shared_ptr<arrow::ipc::FileReader> *arrow_file_reader) +{ + auto file_reader = + GARROW_IPC_FILE_READER(g_object_new(GARROW_IPC_TYPE_FILE_READER, + "file-reader", arrow_file_reader, + NULL)); + return file_reader; +} + +std::shared_ptr<arrow::ipc::FileReader> +garrow_ipc_file_reader_get_raw(GArrowIPCFileReader *file_reader) +{ + GArrowIPCFileReaderPrivate *priv; + + priv = GARROW_IPC_FILE_READER_GET_PRIVATE(file_reader); + return priv->file_reader; +} http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-file-reader.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-file-reader.h b/c_glib/arrow-glib/ipc-file-reader.h new file mode 100644 index 0000000..22915f8 --- /dev/null +++ b/c_glib/arrow-glib/ipc-file-reader.h @@ -0,0 +1,83 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/record-batch.h> +#include <arrow-glib/schema.h> + +#include <arrow-glib/io-readable-file.h> + +#include <arrow-glib/ipc-metadata-version.h> + +G_BEGIN_DECLS + +#define GARROW_IPC_TYPE_FILE_READER \ + (garrow_ipc_file_reader_get_type()) +#define GARROW_IPC_FILE_READER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_IPC_TYPE_FILE_READER, \ + GArrowIPCFileReader)) +#define GARROW_IPC_FILE_READER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_IPC_TYPE_FILE_READER, \ + GArrowIPCFileReaderClass)) +#define GARROW_IPC_IS_FILE_READER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_IPC_TYPE_FILE_READER)) +#define GARROW_IPC_IS_FILE_READER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_IPC_TYPE_FILE_READER)) +#define GARROW_IPC_FILE_READER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_IPC_TYPE_FILE_READER, \ + GArrowIPCFileReaderClass)) + +typedef struct _GArrowIPCFileReader GArrowIPCFileReader; +typedef struct _GArrowIPCFileReaderClass GArrowIPCFileReaderClass; + +/** + * GArrowIPCFileReader: + * + * It wraps `arrow::ipc::FileReader`. + */ +struct _GArrowIPCFileReader +{ + /*< private >*/ + GObject parent_instance; +}; + +struct _GArrowIPCFileReaderClass +{ + GObjectClass parent_class; +}; + +GType garrow_ipc_file_reader_get_type(void) G_GNUC_CONST; + +GArrowIPCFileReader *garrow_ipc_file_reader_open(GArrowIOReadableFile *file, + GError **error); + +GArrowSchema *garrow_ipc_file_reader_get_schema(GArrowIPCFileReader *file_reader); +guint garrow_ipc_file_reader_get_n_record_batches(GArrowIPCFileReader *file_reader); +GArrowIPCMetadataVersion garrow_ipc_file_reader_get_version(GArrowIPCFileReader *file_reader); +GArrowRecordBatch *garrow_ipc_file_reader_get_record_batch(GArrowIPCFileReader *file_reader, + guint i, + GError **error); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-file-reader.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-file-reader.hpp b/c_glib/arrow-glib/ipc-file-reader.hpp new file mode 100644 index 0000000..66cd45d --- /dev/null +++ b/c_glib/arrow-glib/ipc-file-reader.hpp @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/api.h> +#include <arrow/ipc/api.h> + +#include <arrow-glib/ipc-file-reader.h> + +GArrowIPCFileReader *garrow_ipc_file_reader_new_raw(std::shared_ptr<arrow::ipc::FileReader> *arrow_file_reader); +std::shared_ptr<arrow::ipc::FileReader> garrow_ipc_file_reader_get_raw(GArrowIPCFileReader *file_reader); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-file-writer.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-file-writer.cpp b/c_glib/arrow-glib/ipc-file-writer.cpp new file mode 100644 index 0000000..d8b3c2e --- /dev/null +++ b/c_glib/arrow-glib/ipc-file-writer.cpp @@ -0,0 +1,158 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow/ipc/api.h> + +#include <arrow-glib/array.hpp> +#include <arrow-glib/error.hpp> +#include <arrow-glib/record-batch.hpp> +#include <arrow-glib/schema.hpp> + +#include <arrow-glib/io-output-stream.hpp> + +#include <arrow-glib/ipc-stream-writer.hpp> +#include <arrow-glib/ipc-file-writer.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: ipc-file-writer + * @short_description: File writer class + * + * #GArrowIPCFileWriter is a class for sending data by file based IPC. + */ + +G_DEFINE_TYPE(GArrowIPCFileWriter, + garrow_ipc_file_writer, + GARROW_IPC_TYPE_STREAM_WRITER); + +static void +garrow_ipc_file_writer_init(GArrowIPCFileWriter *object) +{ +} + +static void +garrow_ipc_file_writer_class_init(GArrowIPCFileWriterClass *klass) +{ +} + +/** + * garrow_ipc_file_writer_open: + * @sink: The output of the writer. + * @schema: The schema of the writer. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): A newly opened + * #GArrowIPCFileWriter or %NULL on error. + */ +GArrowIPCFileWriter * +garrow_ipc_file_writer_open(GArrowIOOutputStream *sink, + GArrowSchema *schema, + GError **error) +{ + std::shared_ptr<arrow::ipc::FileWriter> arrow_file_writer; + auto status = + arrow::ipc::FileWriter::Open(garrow_io_output_stream_get_raw(sink).get(), + garrow_schema_get_raw(schema), + &arrow_file_writer); + if (status.ok()) { + return garrow_ipc_file_writer_new_raw(&arrow_file_writer); + } else { + garrow_error_set(error, status, "[ipc][file-writer][open]"); + return NULL; + } +} + +/** + * garrow_ipc_file_writer_write_record_batch: + * @file_writer: A #GArrowIPCFileWriter. + * @record_batch: The record batch to be written. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_ipc_file_writer_write_record_batch(GArrowIPCFileWriter *file_writer, + GArrowRecordBatch *record_batch, + GError **error) +{ + auto arrow_file_writer = + garrow_ipc_file_writer_get_raw(file_writer); + auto arrow_record_batch = + garrow_record_batch_get_raw(record_batch); + auto arrow_record_batch_raw = + arrow_record_batch.get(); + + auto status = arrow_file_writer->WriteRecordBatch(*arrow_record_batch_raw); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[ipc][file-writer][write-record-batch]"); + return FALSE; + } +} + +/** + * garrow_ipc_file_writer_close: + * @file_writer: A #GArrowIPCFileWriter. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_ipc_file_writer_close(GArrowIPCFileWriter *file_writer, + GError **error) +{ + auto arrow_file_writer = + garrow_ipc_file_writer_get_raw(file_writer); + + auto status = arrow_file_writer->Close(); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[ipc][file-writer][close]"); + return FALSE; + } +} + +G_END_DECLS + +GArrowIPCFileWriter * +garrow_ipc_file_writer_new_raw(std::shared_ptr<arrow::ipc::FileWriter> *arrow_file_writer) +{ + auto file_writer = + GARROW_IPC_FILE_WRITER(g_object_new(GARROW_IPC_TYPE_FILE_WRITER, + "stream-writer", arrow_file_writer, + NULL)); + return file_writer; +} + +arrow::ipc::FileWriter * +garrow_ipc_file_writer_get_raw(GArrowIPCFileWriter *file_writer) +{ + auto arrow_stream_writer = + garrow_ipc_stream_writer_get_raw(GARROW_IPC_STREAM_WRITER(file_writer)); + auto arrow_file_writer_raw = + dynamic_cast<arrow::ipc::FileWriter *>(arrow_stream_writer.get()); + return arrow_file_writer_raw; +} http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-file-writer.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-file-writer.h b/c_glib/arrow-glib/ipc-file-writer.h new file mode 100644 index 0000000..732d942 --- /dev/null +++ b/c_glib/arrow-glib/ipc-file-writer.h @@ -0,0 +1,78 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/ipc-stream-writer.h> + +G_BEGIN_DECLS + +#define GARROW_IPC_TYPE_FILE_WRITER \ + (garrow_ipc_file_writer_get_type()) +#define GARROW_IPC_FILE_WRITER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_IPC_TYPE_FILE_WRITER, \ + GArrowIPCFileWriter)) +#define GARROW_IPC_FILE_WRITER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_IPC_TYPE_FILE_WRITER, \ + GArrowIPCFileWriterClass)) +#define GARROW_IPC_IS_FILE_WRITER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_IPC_TYPE_FILE_WRITER)) +#define GARROW_IPC_IS_FILE_WRITER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_IPC_TYPE_FILE_WRITER)) +#define GARROW_IPC_FILE_WRITER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_IPC_TYPE_FILE_WRITER, \ + GArrowIPCFileWriterClass)) + +typedef struct _GArrowIPCFileWriter GArrowIPCFileWriter; +typedef struct _GArrowIPCFileWriterClass GArrowIPCFileWriterClass; + +/** + * GArrowIPCFileWriter: + * + * It wraps `arrow::ipc::FileWriter`. + */ +struct _GArrowIPCFileWriter +{ + /*< private >*/ + GArrowIPCStreamWriter parent_instance; +}; + +struct _GArrowIPCFileWriterClass +{ + GObjectClass parent_class; +}; + +GType garrow_ipc_file_writer_get_type(void) G_GNUC_CONST; + +GArrowIPCFileWriter *garrow_ipc_file_writer_open(GArrowIOOutputStream *sink, + GArrowSchema *schema, + GError **error); + +gboolean garrow_ipc_file_writer_write_record_batch(GArrowIPCFileWriter *file_writer, + GArrowRecordBatch *record_batch, + GError **error); +gboolean garrow_ipc_file_writer_close(GArrowIPCFileWriter *file_writer, + GError **error); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-file-writer.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-file-writer.hpp b/c_glib/arrow-glib/ipc-file-writer.hpp new file mode 100644 index 0000000..b8ae113 --- /dev/null +++ b/c_glib/arrow-glib/ipc-file-writer.hpp @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/api.h> +#include <arrow/ipc/api.h> + +#include <arrow-glib/ipc-file-writer.h> + +GArrowIPCFileWriter *garrow_ipc_file_writer_new_raw(std::shared_ptr<arrow::ipc::FileWriter> *arrow_file_writer); +arrow::ipc::FileWriter *garrow_ipc_file_writer_get_raw(GArrowIPCFileWriter *file_writer); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-metadata-version.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-metadata-version.cpp b/c_glib/arrow-glib/ipc-metadata-version.cpp new file mode 100644 index 0000000..c5cc8d3 --- /dev/null +++ b/c_glib/arrow-glib/ipc-metadata-version.cpp @@ -0,0 +1,59 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/ipc-metadata-version.hpp> + +/** + * SECTION: ipc-metadata-version + * @title: GArrowIPCMetadataVersion + * @short_description: Metadata version mapgging between Arrow and arrow-glib + * + * #GArrowIPCMetadataVersion provides metadata versions corresponding + * to `arrow::ipc::MetadataVersion::type` values. + */ + +GArrowIPCMetadataVersion +garrow_ipc_metadata_version_from_raw(arrow::ipc::MetadataVersion::type version) +{ + switch (version) { + case arrow::ipc::MetadataVersion::type::V1: + return GARROW_IPC_METADATA_VERSION_V1; + case arrow::ipc::MetadataVersion::type::V2: + return GARROW_IPC_METADATA_VERSION_V2; + default: + return GARROW_IPC_METADATA_VERSION_V2; + } +} + +arrow::ipc::MetadataVersion::type +garrow_ipc_metadata_version_to_raw(GArrowIPCMetadataVersion version) +{ + switch (version) { + case GARROW_IPC_METADATA_VERSION_V1: + return arrow::ipc::MetadataVersion::type::V1; + case GARROW_IPC_METADATA_VERSION_V2: + return arrow::ipc::MetadataVersion::type::V2; + default: + return arrow::ipc::MetadataVersion::type::V2; + } +} http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-metadata-version.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-metadata-version.h b/c_glib/arrow-glib/ipc-metadata-version.h new file mode 100644 index 0000000..ccfd52a --- /dev/null +++ b/c_glib/arrow-glib/ipc-metadata-version.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#pragma once + +#include <glib-object.h> + +G_BEGIN_DECLS + +/** + * GArrowIPCMetadataVersion: + * @GARROW_IPC_METADATA_VERSION_V1: Version 1. + * @GARROW_IPC_METADATA_VERSION_V2: Version 2. + * + * They are corresponding to `arrow::ipc::MetadataVersion::type` + * values. + */ +typedef enum { + GARROW_IPC_METADATA_VERSION_V1, + GARROW_IPC_METADATA_VERSION_V2 +} GArrowIPCMetadataVersion; + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-metadata-version.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-metadata-version.hpp b/c_glib/arrow-glib/ipc-metadata-version.hpp new file mode 100644 index 0000000..2a7e8cf --- /dev/null +++ b/c_glib/arrow-glib/ipc-metadata-version.hpp @@ -0,0 +1,27 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/ipc/api.h> + +#include <arrow-glib/ipc-metadata-version.h> + +GArrowIPCMetadataVersion garrow_ipc_metadata_version_from_raw(arrow::ipc::MetadataVersion::type version); +arrow::ipc::MetadataVersion::type garrow_ipc_metadata_version_to_raw(GArrowIPCMetadataVersion version); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-stream-reader.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-stream-reader.cpp b/c_glib/arrow-glib/ipc-stream-reader.cpp new file mode 100644 index 0000000..4804784 --- /dev/null +++ b/c_glib/arrow-glib/ipc-stream-reader.cpp @@ -0,0 +1,221 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow/ipc/api.h> + +#include <arrow-glib/error.hpp> +#include <arrow-glib/record-batch.hpp> +#include <arrow-glib/schema.hpp> + +#include <arrow-glib/io-input-stream.hpp> + +#include <arrow-glib/ipc-metadata-version.hpp> +#include <arrow-glib/ipc-stream-reader.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: ipc-stream-reader + * @short_description: Stream reader class + * + * #GArrowIPCStreamReader is a class for receiving data by stream + * based IPC. + */ + +typedef struct GArrowIPCStreamReaderPrivate_ { + std::shared_ptr<arrow::ipc::StreamReader> stream_reader; +} GArrowIPCStreamReaderPrivate; + +enum { + PROP_0, + PROP_STREAM_READER +}; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowIPCStreamReader, + garrow_ipc_stream_reader, + G_TYPE_OBJECT); + +#define GARROW_IPC_STREAM_READER_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ + GARROW_IPC_TYPE_STREAM_READER, \ + GArrowIPCStreamReaderPrivate)) + +static void +garrow_ipc_stream_reader_finalize(GObject *object) +{ + GArrowIPCStreamReaderPrivate *priv; + + priv = GARROW_IPC_STREAM_READER_GET_PRIVATE(object); + + priv->stream_reader = nullptr; + + G_OBJECT_CLASS(garrow_ipc_stream_reader_parent_class)->finalize(object); +} + +static void +garrow_ipc_stream_reader_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GArrowIPCStreamReaderPrivate *priv; + + priv = GARROW_IPC_STREAM_READER_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_STREAM_READER: + priv->stream_reader = + *static_cast<std::shared_ptr<arrow::ipc::StreamReader> *>(g_value_get_pointer(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_ipc_stream_reader_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_ipc_stream_reader_init(GArrowIPCStreamReader *object) +{ +} + +static void +garrow_ipc_stream_reader_class_init(GArrowIPCStreamReaderClass *klass) +{ + GObjectClass *gobject_class; + GParamSpec *spec; + + gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = garrow_ipc_stream_reader_finalize; + gobject_class->set_property = garrow_ipc_stream_reader_set_property; + gobject_class->get_property = garrow_ipc_stream_reader_get_property; + + spec = g_param_spec_pointer("stream-reader", + "ipc::StreamReader", + "The raw std::shared<arrow::ipc::StreamReader> *", + static_cast<GParamFlags>(G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_STREAM_READER, spec); +} + +/** + * garrow_ipc_stream_reader_open: + * @stream: The stream to be read. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): A newly opened + * #GArrowIPCStreamReader or %NULL on error. + */ +GArrowIPCStreamReader * +garrow_ipc_stream_reader_open(GArrowIOInputStream *stream, + GError **error) +{ + std::shared_ptr<arrow::ipc::StreamReader> arrow_stream_reader; + auto status = + arrow::ipc::StreamReader::Open(garrow_io_input_stream_get_raw(stream), + &arrow_stream_reader); + if (status.ok()) { + return garrow_ipc_stream_reader_new_raw(&arrow_stream_reader); + } else { + garrow_error_set(error, status, "[ipc][stream-reader][open]"); + return NULL; + } +} + +/** + * garrow_ipc_stream_reader_get_schema: + * @stream_reader: A #GArrowIPCStreamReader. + * + * Returns: (transfer full): The schema in the stream. + */ +GArrowSchema * +garrow_ipc_stream_reader_get_schema(GArrowIPCStreamReader *stream_reader) +{ + auto arrow_stream_reader = + garrow_ipc_stream_reader_get_raw(stream_reader); + auto arrow_schema = arrow_stream_reader->schema(); + return garrow_schema_new_raw(&arrow_schema); +} + +/** + * garrow_ipc_stream_reader_get_next_record_batch: + * @stream_reader: A #GArrowIPCStreamReader. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): + * The next record batch in the stream or %NULL on end of stream. + */ +GArrowRecordBatch * +garrow_ipc_stream_reader_get_next_record_batch(GArrowIPCStreamReader *stream_reader, + GError **error) +{ + auto arrow_stream_reader = + garrow_ipc_stream_reader_get_raw(stream_reader); + std::shared_ptr<arrow::RecordBatch> arrow_record_batch; + auto status = arrow_stream_reader->GetNextRecordBatch(&arrow_record_batch); + + if (status.ok()) { + if (arrow_record_batch == nullptr) { + return NULL; + } else { + return garrow_record_batch_new_raw(&arrow_record_batch); + } + } else { + garrow_error_set(error, status, "[ipc][stream-reader][get-next-record-batch]"); + return NULL; + } +} + +G_END_DECLS + +GArrowIPCStreamReader * +garrow_ipc_stream_reader_new_raw(std::shared_ptr<arrow::ipc::StreamReader> *arrow_stream_reader) +{ + auto stream_reader = + GARROW_IPC_STREAM_READER(g_object_new(GARROW_IPC_TYPE_STREAM_READER, + "stream-reader", arrow_stream_reader, + NULL)); + return stream_reader; +} + +std::shared_ptr<arrow::ipc::StreamReader> +garrow_ipc_stream_reader_get_raw(GArrowIPCStreamReader *stream_reader) +{ + GArrowIPCStreamReaderPrivate *priv; + + priv = GARROW_IPC_STREAM_READER_GET_PRIVATE(stream_reader); + return priv->stream_reader; +} http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-stream-reader.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-stream-reader.h b/c_glib/arrow-glib/ipc-stream-reader.h new file mode 100644 index 0000000..993cd85 --- /dev/null +++ b/c_glib/arrow-glib/ipc-stream-reader.h @@ -0,0 +1,80 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/record-batch.h> +#include <arrow-glib/schema.h> + +#include <arrow-glib/io-input-stream.h> + +#include <arrow-glib/ipc-metadata-version.h> + +G_BEGIN_DECLS + +#define GARROW_IPC_TYPE_STREAM_READER \ + (garrow_ipc_stream_reader_get_type()) +#define GARROW_IPC_STREAM_READER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_IPC_TYPE_STREAM_READER, \ + GArrowIPCStreamReader)) +#define GARROW_IPC_STREAM_READER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_IPC_TYPE_STREAM_READER, \ + GArrowIPCStreamReaderClass)) +#define GARROW_IPC_IS_STREAM_READER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_IPC_TYPE_STREAM_READER)) +#define GARROW_IPC_IS_STREAM_READER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_IPC_TYPE_STREAM_READER)) +#define GARROW_IPC_STREAM_READER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_IPC_TYPE_STREAM_READER, \ + GArrowIPCStreamReaderClass)) + +typedef struct _GArrowIPCStreamReader GArrowIPCStreamReader; +typedef struct _GArrowIPCStreamReaderClass GArrowIPCStreamReaderClass; + +/** + * GArrowIPCStreamReader: + * + * It wraps `arrow::ipc::StreamReader`. + */ +struct _GArrowIPCStreamReader +{ + /*< private >*/ + GObject parent_instance; +}; + +struct _GArrowIPCStreamReaderClass +{ + GObjectClass parent_class; +}; + +GType garrow_ipc_stream_reader_get_type(void) G_GNUC_CONST; + +GArrowIPCStreamReader *garrow_ipc_stream_reader_open(GArrowIOInputStream *stream, + GError **error); + +GArrowSchema *garrow_ipc_stream_reader_get_schema(GArrowIPCStreamReader *stream_reader); +GArrowRecordBatch *garrow_ipc_stream_reader_get_next_record_batch(GArrowIPCStreamReader *stream_reader, + GError **error); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-stream-reader.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-stream-reader.hpp b/c_glib/arrow-glib/ipc-stream-reader.hpp new file mode 100644 index 0000000..a35bdab --- /dev/null +++ b/c_glib/arrow-glib/ipc-stream-reader.hpp @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/api.h> +#include <arrow/ipc/api.h> + +#include <arrow-glib/ipc-stream-reader.h> + +GArrowIPCStreamReader *garrow_ipc_stream_reader_new_raw(std::shared_ptr<arrow::ipc::StreamReader> *arrow_stream_reader); +std::shared_ptr<arrow::ipc::StreamReader> garrow_ipc_stream_reader_get_raw(GArrowIPCStreamReader *stream_reader); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-stream-writer.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-stream-writer.cpp b/c_glib/arrow-glib/ipc-stream-writer.cpp new file mode 100644 index 0000000..e2455a4 --- /dev/null +++ b/c_glib/arrow-glib/ipc-stream-writer.cpp @@ -0,0 +1,232 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow/ipc/api.h> + +#include <arrow-glib/array.hpp> +#include <arrow-glib/error.hpp> +#include <arrow-glib/record-batch.hpp> +#include <arrow-glib/schema.hpp> + +#include <arrow-glib/io-output-stream.hpp> + +#include <arrow-glib/ipc-stream-writer.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: ipc-stream-writer + * @short_description: Stream writer class + * + * #GArrowIPCStreamWriter is a class for sending data by stream based + * IPC. + */ + +typedef struct GArrowIPCStreamWriterPrivate_ { + std::shared_ptr<arrow::ipc::StreamWriter> stream_writer; +} GArrowIPCStreamWriterPrivate; + +enum { + PROP_0, + PROP_STREAM_WRITER +}; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowIPCStreamWriter, + garrow_ipc_stream_writer, + G_TYPE_OBJECT); + +#define GARROW_IPC_STREAM_WRITER_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ + GARROW_IPC_TYPE_STREAM_WRITER, \ + GArrowIPCStreamWriterPrivate)) + +static void +garrow_ipc_stream_writer_finalize(GObject *object) +{ + GArrowIPCStreamWriterPrivate *priv; + + priv = GARROW_IPC_STREAM_WRITER_GET_PRIVATE(object); + + priv->stream_writer = nullptr; + + G_OBJECT_CLASS(garrow_ipc_stream_writer_parent_class)->finalize(object); +} + +static void +garrow_ipc_stream_writer_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GArrowIPCStreamWriterPrivate *priv; + + priv = GARROW_IPC_STREAM_WRITER_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_STREAM_WRITER: + priv->stream_writer = + *static_cast<std::shared_ptr<arrow::ipc::StreamWriter> *>(g_value_get_pointer(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_ipc_stream_writer_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_ipc_stream_writer_init(GArrowIPCStreamWriter *object) +{ +} + +static void +garrow_ipc_stream_writer_class_init(GArrowIPCStreamWriterClass *klass) +{ + GObjectClass *gobject_class; + GParamSpec *spec; + + gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = garrow_ipc_stream_writer_finalize; + gobject_class->set_property = garrow_ipc_stream_writer_set_property; + gobject_class->get_property = garrow_ipc_stream_writer_get_property; + + spec = g_param_spec_pointer("stream-writer", + "ipc::StreamWriter", + "The raw std::shared<arrow::ipc::StreamWriter> *", + static_cast<GParamFlags>(G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_STREAM_WRITER, spec); +} + +/** + * garrow_ipc_stream_writer_open: + * @sink: The output of the writer. + * @schema: The schema of the writer. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): A newly opened + * #GArrowIPCStreamWriter or %NULL on error. + */ +GArrowIPCStreamWriter * +garrow_ipc_stream_writer_open(GArrowIOOutputStream *sink, + GArrowSchema *schema, + GError **error) +{ + std::shared_ptr<arrow::ipc::StreamWriter> arrow_stream_writer; + auto status = + arrow::ipc::StreamWriter::Open(garrow_io_output_stream_get_raw(sink).get(), + garrow_schema_get_raw(schema), + &arrow_stream_writer); + if (status.ok()) { + return garrow_ipc_stream_writer_new_raw(&arrow_stream_writer); + } else { + garrow_error_set(error, status, "[ipc][stream-writer][open]"); + return NULL; + } +} + +/** + * garrow_ipc_stream_writer_write_record_batch: + * @stream_writer: A #GArrowIPCStreamWriter. + * @record_batch: The record batch to be written. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_ipc_stream_writer_write_record_batch(GArrowIPCStreamWriter *stream_writer, + GArrowRecordBatch *record_batch, + GError **error) +{ + auto arrow_stream_writer = + garrow_ipc_stream_writer_get_raw(stream_writer); + auto arrow_record_batch = + garrow_record_batch_get_raw(record_batch); + auto arrow_record_batch_raw = + arrow_record_batch.get(); + + auto status = arrow_stream_writer->WriteRecordBatch(*arrow_record_batch_raw); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[ipc][stream-writer][write-record-batch]"); + return FALSE; + } +} + +/** + * garrow_ipc_stream_writer_close: + * @stream_writer: A #GArrowIPCStreamWriter. + * @error: (nullable): Return locatipcn for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + */ +gboolean +garrow_ipc_stream_writer_close(GArrowIPCStreamWriter *stream_writer, + GError **error) +{ + auto arrow_stream_writer = + garrow_ipc_stream_writer_get_raw(stream_writer); + + auto status = arrow_stream_writer->Close(); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[ipc][stream-writer][close]"); + return FALSE; + } +} + +G_END_DECLS + +GArrowIPCStreamWriter * +garrow_ipc_stream_writer_new_raw(std::shared_ptr<arrow::ipc::StreamWriter> *arrow_stream_writer) +{ + auto stream_writer = + GARROW_IPC_STREAM_WRITER(g_object_new(GARROW_IPC_TYPE_STREAM_WRITER, + "stream-writer", arrow_stream_writer, + NULL)); + return stream_writer; +} + +std::shared_ptr<arrow::ipc::StreamWriter> +garrow_ipc_stream_writer_get_raw(GArrowIPCStreamWriter *stream_writer) +{ + GArrowIPCStreamWriterPrivate *priv; + + priv = GARROW_IPC_STREAM_WRITER_GET_PRIVATE(stream_writer); + return priv->stream_writer; +} http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-stream-writer.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-stream-writer.h b/c_glib/arrow-glib/ipc-stream-writer.h new file mode 100644 index 0000000..4488204 --- /dev/null +++ b/c_glib/arrow-glib/ipc-stream-writer.h @@ -0,0 +1,82 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/array.h> +#include <arrow-glib/record-batch.h> +#include <arrow-glib/schema.h> + +#include <arrow-glib/io-output-stream.h> + +G_BEGIN_DECLS + +#define GARROW_IPC_TYPE_STREAM_WRITER \ + (garrow_ipc_stream_writer_get_type()) +#define GARROW_IPC_STREAM_WRITER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_IPC_TYPE_STREAM_WRITER, \ + GArrowIPCStreamWriter)) +#define GARROW_IPC_STREAM_WRITER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_IPC_TYPE_STREAM_WRITER, \ + GArrowIPCStreamWriterClass)) +#define GARROW_IPC_IS_STREAM_WRITER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_IPC_TYPE_STREAM_WRITER)) +#define GARROW_IPC_IS_STREAM_WRITER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_IPC_TYPE_STREAM_WRITER)) +#define GARROW_IPC_STREAM_WRITER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_IPC_TYPE_STREAM_WRITER, \ + GArrowIPCStreamWriterClass)) + +typedef struct _GArrowIPCStreamWriter GArrowIPCStreamWriter; +typedef struct _GArrowIPCStreamWriterClass GArrowIPCStreamWriterClass; + +/** + * GArrowIPCStreamWriter: + * + * It wraps `arrow::ipc::StreamWriter`. + */ +struct _GArrowIPCStreamWriter +{ + /*< private >*/ + GObject parent_instance; +}; + +struct _GArrowIPCStreamWriterClass +{ + GObjectClass parent_class; +}; + +GType garrow_ipc_stream_writer_get_type(void) G_GNUC_CONST; + +GArrowIPCStreamWriter *garrow_ipc_stream_writer_open(GArrowIOOutputStream *sink, + GArrowSchema *schema, + GError **error); + +gboolean garrow_ipc_stream_writer_write_record_batch(GArrowIPCStreamWriter *stream_writer, + GArrowRecordBatch *record_batch, + GError **error); +gboolean garrow_ipc_stream_writer_close(GArrowIPCStreamWriter *stream_writer, + GError **error); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/ipc-stream-writer.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/ipc-stream-writer.hpp b/c_glib/arrow-glib/ipc-stream-writer.hpp new file mode 100644 index 0000000..9d09740 --- /dev/null +++ b/c_glib/arrow-glib/ipc-stream-writer.hpp @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/api.h> +#include <arrow/ipc/api.h> + +#include <arrow-glib/ipc-stream-writer.h> + +GArrowIPCStreamWriter *garrow_ipc_stream_writer_new_raw(std::shared_ptr<arrow::ipc::StreamWriter> *arrow_stream_writer); +std::shared_ptr<arrow::ipc::StreamWriter> garrow_ipc_stream_writer_get_raw(GArrowIPCStreamWriter *stream_writer); http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/list-array-builder.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/list-array-builder.cpp b/c_glib/arrow-glib/list-array-builder.cpp new file mode 100644 index 0000000..6c8f53d --- /dev/null +++ b/c_glib/arrow-glib/list-array-builder.cpp @@ -0,0 +1,173 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/array-builder.hpp> +#include <arrow-glib/list-array-builder.h> +#include <arrow-glib/error.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: list-array-builder + * @short_description: List array builder class + * @include: arrow-glib/arrow-glib.h + * + * #GArrowListArrayBuilder is the class to create a new + * #GArrowListArray. + */ + +G_DEFINE_TYPE(GArrowListArrayBuilder, + garrow_list_array_builder, + GARROW_TYPE_ARRAY_BUILDER) + +static void +garrow_list_array_builder_init(GArrowListArrayBuilder *builder) +{ +} + +static void +garrow_list_array_builder_class_init(GArrowListArrayBuilderClass *klass) +{ +} + +/** + * garrow_list_array_builder_new: + * @value_builder: A #GArrowArrayBuilder for value array. + * + * Returns: A newly created #GArrowListArrayBuilder. + */ +GArrowListArrayBuilder * +garrow_list_array_builder_new(GArrowArrayBuilder *value_builder) +{ + auto memory_pool = arrow::default_memory_pool(); + auto arrow_value_builder = garrow_array_builder_get_raw(value_builder); + auto arrow_list_builder = + std::make_shared<arrow::ListBuilder>(memory_pool, arrow_value_builder); + std::shared_ptr<arrow::ArrayBuilder> arrow_builder = arrow_list_builder; + auto builder = garrow_array_builder_new_raw(&arrow_builder); + return GARROW_LIST_ARRAY_BUILDER(builder); +} + +/** + * garrow_list_array_builder_append: + * @builder: A #GArrowListArrayBuilder. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + * + * It appends a new list element. To append a new list element, you + * need to call this function then append list element values to + * `value_builder`. `value_builder` is the #GArrowArrayBuilder + * specified to constructor. You can get `value_builder` by + * garrow_list_array_builder_get_value_builder(). + * + * |[<!-- language="C" --> + * GArrowInt8ArrayBuilder *value_builder; + * GArrowListArrayBuilder *builder; + * + * value_builder = garrow_int8_array_builder_new(); + * builder = garrow_list_array_builder_new(value_builder, NULL); + * + * // Start 0th list element: [1, 0, -1] + * garrow_list_array_builder_append(builder, NULL); + * garrow_int8_array_builder_append(value_builder, 1); + * garrow_int8_array_builder_append(value_builder, 0); + * garrow_int8_array_builder_append(value_builder, -1); + * + * // Start 1st list element: [-29, 29] + * garrow_list_array_builder_append(builder, NULL); + * garrow_int8_array_builder_append(value_builder, -29); + * garrow_int8_array_builder_append(value_builder, 29); + * + * { + * // [[1, 0, -1], [-29, 29]] + * GArrowArray *array = garrow_array_builder_finish(builder); + * // Now, builder is needless. + * g_object_unref(builder); + * g_object_unref(value_builder); + * + * // Use array... + * g_object_unref(array); + * } + * ]| + */ +gboolean +garrow_list_array_builder_append(GArrowListArrayBuilder *builder, + GError **error) +{ + auto arrow_builder = + static_cast<arrow::ListBuilder *>( + garrow_array_builder_get_raw(GARROW_ARRAY_BUILDER(builder)).get()); + + auto status = arrow_builder->Append(); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[list-array-builder][append]"); + return FALSE; + } +} + +/** + * garrow_list_array_builder_append_null: + * @builder: A #GArrowListArrayBuilder. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + * + * It appends a new NULL element. + */ +gboolean +garrow_list_array_builder_append_null(GArrowListArrayBuilder *builder, + GError **error) +{ + auto arrow_builder = + static_cast<arrow::ListBuilder *>( + garrow_array_builder_get_raw(GARROW_ARRAY_BUILDER(builder)).get()); + + auto status = arrow_builder->AppendNull(); + if (status.ok()) { + return TRUE; + } else { + garrow_error_set(error, status, "[list-array-builder][append-null]"); + return FALSE; + } +} + +/** + * garrow_list_array_builder_get_value_builder: + * @builder: A #GArrowListArrayBuilder. + * + * Returns: (transfer full): The #GArrowArrayBuilder for values. + */ +GArrowArrayBuilder * +garrow_list_array_builder_get_value_builder(GArrowListArrayBuilder *builder) +{ + auto arrow_builder = + static_cast<arrow::ListBuilder *>( + garrow_array_builder_get_raw(GARROW_ARRAY_BUILDER(builder)).get()); + auto arrow_value_builder = arrow_builder->value_builder(); + return garrow_array_builder_new_raw(&arrow_value_builder); +} + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/list-array-builder.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/list-array-builder.h b/c_glib/arrow-glib/list-array-builder.h new file mode 100644 index 0000000..2c2e58e --- /dev/null +++ b/c_glib/arrow-glib/list-array-builder.h @@ -0,0 +1,77 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/array-builder.h> + +G_BEGIN_DECLS + +#define GARROW_TYPE_LIST_ARRAY_BUILDER \ + (garrow_list_array_builder_get_type()) +#define GARROW_LIST_ARRAY_BUILDER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_TYPE_LIST_ARRAY_BUILDER, \ + GArrowListArrayBuilder)) +#define GARROW_LIST_ARRAY_BUILDER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_TYPE_LIST_ARRAY_BUILDER, \ + GArrowListArrayBuilderClass)) +#define GARROW_IS_LIST_ARRAY_BUILDER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_TYPE_LIST_ARRAY_BUILDER)) +#define GARROW_IS_LIST_ARRAY_BUILDER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_TYPE_LIST_ARRAY_BUILDER)) +#define GARROW_LIST_ARRAY_BUILDER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_TYPE_LIST_ARRAY_BUILDER, \ + GArrowListArrayBuilderClass)) + +typedef struct _GArrowListArrayBuilder GArrowListArrayBuilder; +typedef struct _GArrowListArrayBuilderClass GArrowListArrayBuilderClass; + +/** + * GArrowListArrayBuilder: + * + * It wraps `arrow::ListBuilder`. + */ +struct _GArrowListArrayBuilder +{ + /*< private >*/ + GArrowArrayBuilder parent_instance; +}; + +struct _GArrowListArrayBuilderClass +{ + GArrowArrayBuilderClass parent_class; +}; + +GType garrow_list_array_builder_get_type(void) G_GNUC_CONST; + +GArrowListArrayBuilder *garrow_list_array_builder_new(GArrowArrayBuilder *value_builder); + +gboolean garrow_list_array_builder_append(GArrowListArrayBuilder *builder, + GError **error); +gboolean garrow_list_array_builder_append_null(GArrowListArrayBuilder *builder, + GError **error); + +GArrowArrayBuilder *garrow_list_array_builder_get_value_builder(GArrowListArrayBuilder *builder); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/list-array.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/list-array.cpp b/c_glib/arrow-glib/list-array.cpp new file mode 100644 index 0000000..2b3fb31 --- /dev/null +++ b/c_glib/arrow-glib/list-array.cpp @@ -0,0 +1,92 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/array.hpp> +#include <arrow-glib/data-type.hpp> +#include <arrow-glib/list-array.h> + +G_BEGIN_DECLS + +/** + * SECTION: list-array + * @short_description: List array class + * @include: arrow-glib/arrow-glib.h + * + * #GArrowListArray is a class for list array. It can store zero + * or more list data. + * + * #GArrowListArray is immutable. You need to use + * #GArrowListArrayBuilder to create a new array. + */ + +G_DEFINE_TYPE(GArrowListArray, \ + garrow_list_array, \ + GARROW_TYPE_ARRAY) + +static void +garrow_list_array_init(GArrowListArray *object) +{ +} + +static void +garrow_list_array_class_init(GArrowListArrayClass *klass) +{ +} + +/** + * garrow_list_array_get_value_type: + * @array: A #GArrowListArray. + * + * Returns: (transfer full): The data type of value in each list. + */ +GArrowDataType * +garrow_list_array_get_value_type(GArrowListArray *array) +{ + auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array)); + auto arrow_list_array = + static_cast<arrow::ListArray *>(arrow_array.get()); + auto arrow_value_type = arrow_list_array->value_type(); + return garrow_data_type_new_raw(&arrow_value_type); +} + +/** + * garrow_list_array_get_value: + * @array: A #GArrowListArray. + * @i: The index of the target value. + * + * Returns: (transfer full): The i-th list. + */ +GArrowArray * +garrow_list_array_get_value(GArrowListArray *array, + gint64 i) +{ + auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array)); + auto arrow_list_array = + static_cast<arrow::ListArray *>(arrow_array.get()); + auto arrow_list = + arrow_list_array->values()->Slice(arrow_list_array->value_offset(i), + arrow_list_array->value_length(i)); + return garrow_array_new_raw(&arrow_list); +} + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/39c7274f/c_glib/arrow-glib/list-array.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/list-array.h b/c_glib/arrow-glib/list-array.h new file mode 100644 index 0000000..c49aed1 --- /dev/null +++ b/c_glib/arrow-glib/list-array.h @@ -0,0 +1,73 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/array.h> +#include <arrow-glib/data-type.h> + +G_BEGIN_DECLS + +#define GARROW_TYPE_LIST_ARRAY \ + (garrow_list_array_get_type()) +#define GARROW_LIST_ARRAY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_TYPE_LIST_ARRAY, \ + GArrowListArray)) +#define GARROW_LIST_ARRAY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_TYPE_LIST_ARRAY, \ + GArrowListArrayClass)) +#define GARROW_IS_LIST_ARRAY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_TYPE_LIST_ARRAY)) +#define GARROW_IS_LIST_ARRAY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_TYPE_LIST_ARRAY)) +#define GARROW_LIST_ARRAY_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_TYPE_LIST_ARRAY, \ + GArrowListArrayClass)) + +typedef struct _GArrowListArray GArrowListArray; +typedef struct _GArrowListArrayClass GArrowListArrayClass; + +/** + * GArrowListArray: + * + * It wraps `arrow::ListArray`. + */ +struct _GArrowListArray +{ + /*< private >*/ + GArrowArray parent_instance; +}; + +struct _GArrowListArrayClass +{ + GArrowArrayClass parent_class; +}; + +GType garrow_list_array_get_type(void) G_GNUC_CONST; + +GArrowDataType *garrow_list_array_get_value_type(GArrowListArray *array); +GArrowArray *garrow_list_array_get_value(GArrowListArray *array, + gint64 i); + +G_END_DECLS