Repository: arrow Updated Branches: refs/heads/master eaf2118ef -> f00e2ab59
ARROW-890: [GLib] Add GArrowMutableBuffer Author: Kouhei Sutou <k...@clear-code.com> Closes #596 from kou/glib-mutable-buffer and squashes the following commits: 73c2663 [Kouhei Sutou] [GLib] Support running tests on Ubuntu 14.04 d211a22 [Kouhei Sutou] [GLib] Add GArrowMutableBuffer Project: http://git-wip-us.apache.org/repos/asf/arrow/repo Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/f00e2ab5 Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/f00e2ab5 Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/f00e2ab5 Branch: refs/heads/master Commit: f00e2ab590ad8f04409e7bc09f70622e73ebd741 Parents: eaf2118 Author: Kouhei Sutou <k...@clear-code.com> Authored: Tue Apr 25 08:45:01 2017 +0200 Committer: Uwe L. Korn <uw...@xhochy.com> Committed: Tue Apr 25 08:45:01 2017 +0200 ---------------------------------------------------------------------- c_glib/arrow-glib/buffer.cpp | 97 ++++++++++++++++++++++++++++++++- c_glib/arrow-glib/buffer.h | 52 ++++++++++++++++++ c_glib/arrow-glib/buffer.hpp | 2 + c_glib/test/test-buffer.rb | 7 +++ c_glib/test/test-mutable-buffer.rb | 38 +++++++++++++ 5 files changed, 195 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/arrow/blob/f00e2ab5/c_glib/arrow-glib/buffer.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/buffer.cpp b/c_glib/arrow-glib/buffer.cpp index 9853e89..5fc3b07 100644 --- a/c_glib/arrow-glib/buffer.cpp +++ b/c_glib/arrow-glib/buffer.cpp @@ -28,10 +28,16 @@ G_BEGIN_DECLS /** * SECTION: buffer - * @short_description: Buffer class + * @section_id: buffer-classes + * @title: Buffer classes + * @include: arrow-glib/arrow-glib.h * * #GArrowBuffer is a class for keeping data. Other classes such as * #GArrowArray and #GArrowTensor can use data in buffer. + * + * #GArrowBuffer is immutable. + * + * #GArrowMutableBuffer is mutable. */ typedef struct GArrowBufferPrivate_ { @@ -183,6 +189,27 @@ garrow_buffer_get_data(GArrowBuffer *buffer) } /** + * garrow_buffer_get_mutable_data: + * @buffer: A #GArrowBuffer. + * + * Returns: (transfer full) (nullable): The data of the buffer. If the + * buffer is imutable, it returns %NULL. The data is owned by the + * buffer. You should not free the data. + * + * Since: 0.3.0 + */ +GBytes * +garrow_buffer_get_mutable_data(GArrowBuffer *buffer) +{ + auto arrow_buffer = garrow_buffer_get_raw(buffer); + if (!arrow_buffer->is_mutable()) { + return NULL; + } + return g_bytes_new_static(arrow_buffer->mutable_data(), + arrow_buffer->size()); +} + +/** * garrow_buffer_get_size: * @buffer: A #GArrowBuffer. * @@ -271,6 +298,65 @@ garrow_buffer_slice(GArrowBuffer *buffer, gint64 offset, gint64 size) return garrow_buffer_new_raw(&arrow_buffer); } + +G_DEFINE_TYPE(GArrowMutableBuffer, \ + garrow_mutable_buffer, \ + GARROW_TYPE_BUFFER) + +static void +garrow_mutable_buffer_init(GArrowMutableBuffer *object) +{ +} + +static void +garrow_mutable_buffer_class_init(GArrowMutableBufferClass *klass) +{ +} + +/** + * garrow_mutable_buffer_new: + * @data: (array length=size): Data for the buffer. + * They aren't owned by the new buffer. + * You must not free the data while the new buffer is alive. + * @size: The number of bytes of the data. + * + * Returns: A newly created #GArrowMutableBuffer. + * + * Since: 0.3.0 + */ +GArrowMutableBuffer * +garrow_mutable_buffer_new(guint8 *data, gint64 size) +{ + auto arrow_buffer = std::make_shared<arrow::MutableBuffer>(data, size); + return garrow_mutable_buffer_new_raw(&arrow_buffer); +} + +/** + * garrow_mutable_buffer_slice: + * @buffer: A #GArrowMutableBuffer. + * @offset: An offset in the buffer data in byte. + * @size: The number of bytes of the sliced data. + * + * Returns: (transfer full): A newly created #GArrowMutableBuffer that + * shares data of the base #GArrowMutableBuffer. The created + * #GArrowMutableBuffer has data start with offset from the base + * buffer data and are the specified bytes size. + * + * Since: 0.3.0 + */ +GArrowMutableBuffer * +garrow_mutable_buffer_slice(GArrowMutableBuffer *buffer, + gint64 offset, + gint64 size) +{ + auto arrow_parent_buffer = garrow_buffer_get_raw(GARROW_BUFFER(buffer)); + auto arrow_buffer = + std::make_shared<arrow::MutableBuffer>(arrow_parent_buffer, + offset, + size); + return garrow_mutable_buffer_new_raw(&arrow_buffer); +} + G_END_DECLS GArrowBuffer * @@ -288,3 +374,12 @@ garrow_buffer_get_raw(GArrowBuffer *buffer) auto priv = GARROW_BUFFER_GET_PRIVATE(buffer); return priv->buffer; } + +GArrowMutableBuffer * +garrow_mutable_buffer_new_raw(std::shared_ptr<arrow::MutableBuffer> *arrow_buffer) +{ + auto buffer = GARROW_MUTABLE_BUFFER(g_object_new(GARROW_TYPE_MUTABLE_BUFFER, + "buffer", arrow_buffer, + NULL)); + return buffer; +} http://git-wip-us.apache.org/repos/asf/arrow/blob/f00e2ab5/c_glib/arrow-glib/buffer.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/buffer.h b/c_glib/arrow-glib/buffer.h index 83e1d0d..5334614 100644 --- a/c_glib/arrow-glib/buffer.h +++ b/c_glib/arrow-glib/buffer.h @@ -62,6 +62,7 @@ GArrowBuffer *garrow_buffer_new (const guint8 *data, gboolean garrow_buffer_is_mutable (GArrowBuffer *buffer); gint64 garrow_buffer_get_capacity (GArrowBuffer *buffer); GBytes *garrow_buffer_get_data (GArrowBuffer *buffer); +GBytes *garrow_buffer_get_mutable_data(GArrowBuffer *buffer); gint64 garrow_buffer_get_size (GArrowBuffer *buffer); GArrowBuffer *garrow_buffer_get_parent (GArrowBuffer *buffer); @@ -73,4 +74,55 @@ GArrowBuffer *garrow_buffer_slice (GArrowBuffer *buffer, gint64 offset, gint64 size); + +#define GARROW_TYPE_MUTABLE_BUFFER \ + (garrow_mutable_buffer_get_type()) +#define GARROW_MUTABLE_BUFFER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_TYPE_MUTABLE_BUFFER, \ + GArrowMutableBuffer)) +#define GARROW_MUTABLE_BUFFER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_TYPE_MUTABLE_BUFFER, \ + GArrowMutableBufferClass)) +#define GARROW_IS_MUTABLE_BUFFER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GARROW_TYPE_MUTABLE_BUFFER)) +#define GARROW_IS_MUTABLE_BUFFER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GARROW_TYPE_MUTABLE_BUFFER)) +#define GARROW_MUTABLE_BUFFER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_TYPE_MUTABLE_BUFFER, \ + GArrowMutableBufferClass)) + +typedef struct _GArrowMutableBuffer GArrowMutableBuffer; +#ifndef __GTK_DOC_IGNORE__ +typedef struct _GArrowMutableBufferClass GArrowMutableBufferClass; +#endif + +/** + * GArrowMutableBuffer: + * + * It wraps `arrow::MutableBuffer`. + */ +struct _GArrowMutableBuffer +{ + /*< private >*/ + GArrowBuffer parent_instance; +}; + +#ifndef __GTK_DOC_IGNORE__ +struct _GArrowMutableBufferClass +{ + GArrowBufferClass parent_class; +}; +#endif + +GType garrow_mutable_buffer_get_type(void) G_GNUC_CONST; + +GArrowMutableBuffer *garrow_mutable_buffer_new (guint8 *data, + gint64 size); +GArrowMutableBuffer *garrow_mutable_buffer_slice(GArrowMutableBuffer *buffer, + gint64 offset, + gint64 size); + G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/f00e2ab5/c_glib/arrow-glib/buffer.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/buffer.hpp b/c_glib/arrow-glib/buffer.hpp index 00dd3de..1337d9e 100644 --- a/c_glib/arrow-glib/buffer.hpp +++ b/c_glib/arrow-glib/buffer.hpp @@ -25,3 +25,5 @@ GArrowBuffer *garrow_buffer_new_raw(std::shared_ptr<arrow::Buffer> *arrow_buffer); std::shared_ptr<arrow::Buffer> garrow_buffer_get_raw(GArrowBuffer *buffer); + +GArrowMutableBuffer *garrow_mutable_buffer_new_raw(std::shared_ptr<arrow::MutableBuffer> *arrow_buffer); http://git-wip-us.apache.org/repos/asf/arrow/blob/f00e2ab5/c_glib/test/test-buffer.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/test-buffer.rb b/c_glib/test/test-buffer.rb index 6bb9671..9f76a80 100644 --- a/c_glib/test/test-buffer.rb +++ b/c_glib/test/test-buffer.rb @@ -16,6 +16,8 @@ # under the License. class TestBuffer < Test::Unit::TestCase + include Helper::Omittable + def setup @data = "Hello" @buffer = Arrow::Buffer.new(@data) @@ -35,6 +37,11 @@ class TestBuffer < Test::Unit::TestCase assert_equal(@data, @buffer.data.to_s) end + def test_mutable_data + require_gi(3, 1, 2) + assert_nil(@buffer.mutable_data) + end + def test_size assert_equal(@data.bytesize, @buffer.size) end http://git-wip-us.apache.org/repos/asf/arrow/blob/f00e2ab5/c_glib/test/test-mutable-buffer.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/test-mutable-buffer.rb b/c_glib/test/test-mutable-buffer.rb new file mode 100644 index 0000000..df62dcf --- /dev/null +++ b/c_glib/test/test-mutable-buffer.rb @@ -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. + +class TestMutableBuffer < Test::Unit::TestCase + def setup + @data = "Hello" + @buffer = Arrow::MutableBuffer.new(@data) + end + + def test_mutable? + assert do + @buffer.mutable? + end + end + + def test_mutable_data + assert_equal(@data, @buffer.mutable_data.to_s) + end + + def test_slice + sliced_buffer = @buffer.slice(1, 3) + assert_equal(@data[1, 3], sliced_buffer.data.to_s) + end +end