Skip to content

Commit

Permalink
ARROW-3319: [GLib] Add align() to GArrowInputStream and GArrowOutputS…
Browse files Browse the repository at this point in the history
…tream

Author: Kouhei Sutou <[email protected]>

Closes apache#2626 from kou/glib-add-align-to-stream and squashes the following commits:

9bc5d0b <Kouhei Sutou> Fix wrong method call
73f3476 <Kouhei Sutou>  Add align() to GArrowInputStream and GArrowOutputStream
  • Loading branch information
kou committed Sep 26, 2018
1 parent b3cd616 commit 9d007b1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
42 changes: 42 additions & 0 deletions c_glib/arrow-glib/input-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,47 @@ garrow_input_stream_class_init(GArrowInputStreamClass *klass)
g_object_class_install_property(gobject_class, PROP_INPUT_STREAM, spec);
}

/**
* garrow_input_stream_advance:
* @input_stream: A #GArrowInputStream.
* @n_bytes: The number of bytes to be advanced.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 0.11.0
*/
gboolean
garrow_input_stream_advance(GArrowInputStream *input_stream,
gint64 n_bytes,
GError **error)
{
auto arrow_input_stream = garrow_input_stream_get_raw(input_stream);
auto status = arrow_input_stream->Advance(n_bytes);
return garrow_error_check(error, status, "[input-stream][advance]");
}

/**
* garrow_input_stream_align:
* @input_stream: A #GArrowInputStream.
* @alignment: The byte multiple for the metadata prefix, usually 8
* or 64, to ensure the body starts on a multiple of that alignment.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 0.11.0
*/
gboolean
garrow_input_stream_align(GArrowInputStream *input_stream,
gint32 alignment,
GError **error)
{
auto arrow_input_stream = garrow_input_stream_get_raw(input_stream);
auto status = arrow::ipc::AlignStream(arrow_input_stream.get(),
alignment);
return garrow_error_check(error, status, "[input-stream][align]");
}

/**
* garrow_input_stream_read_tensor:
Expand Down Expand Up @@ -204,6 +245,7 @@ garrow_input_stream_read_tensor(GArrowInputStream *input_stream,
}
}


G_DEFINE_TYPE(GArrowSeekableInputStream, \
garrow_seekable_input_stream, \
GARROW_TYPE_INPUT_STREAM);
Expand Down
6 changes: 6 additions & 0 deletions c_glib/arrow-glib/input-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ struct _GArrowInputStreamClass
GObjectClass parent_class;
};

gboolean garrow_input_stream_advance(GArrowInputStream *input_stream,
gint64 n_bytes,
GError **error);
gboolean garrow_input_stream_align(GArrowInputStream *input_stream,
gint32 alignment,
GError **error);
GArrowTensor *garrow_input_stream_read_tensor(GArrowInputStream *input_stream,
GError **error);

Expand Down
21 changes: 21 additions & 0 deletions c_glib/arrow-glib/output-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ garrow_output_stream_class_init(GArrowOutputStreamClass *klass)
g_object_class_install_property(gobject_class, PROP_OUTPUT_STREAM, spec);
}

/**
* garrow_output_stream_align:
* @stream: A #GArrowWritable.
* @alignment: The byte multiple for the metadata prefix, usually 8
* or 64, to ensure the body starts on a multiple of that alignment.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 0.11.0
*/
gboolean
garrow_output_stream_align(GArrowOutputStream *stream,
gint32 alignment,
GError **error)
{
auto arrow_stream = garrow_output_stream_get_raw(stream);
auto status = arrow::ipc::AlignStream(arrow_stream.get(), alignment);
return garrow_error_check(error, status, "[output-stream][align]");
}

/**
* garrow_output_stream_write_tensor:
* @stream: A #GArrowWritable.
Expand Down
3 changes: 3 additions & 0 deletions c_glib/arrow-glib/output-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ struct _GArrowOutputStreamClass
GObjectClass parent_class;
};

gboolean garrow_output_stream_align(GArrowOutputStream *stream,
gint32 alignment,
GError **error);
gint64 garrow_output_stream_write_tensor(GArrowOutputStream *stream,
GArrowTensor *tensor,
GError **error);
Expand Down
17 changes: 17 additions & 0 deletions c_glib/test/test-buffer-input-stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ def test_read
read_buffer = buffer_input_stream.read(5)
assert_equal("Hello", read_buffer.data.to_s)
end

def test_advance
buffer = Arrow::Buffer.new("Hello World")
buffer_input_stream = Arrow::BufferInputStream.new(buffer)
buffer_input_stream.advance(6)
read_buffer = buffer_input_stream.read(5)
assert_equal("World", read_buffer.data.to_s)
end

def test_align
buffer = Arrow::Buffer.new("Hello World")
buffer_input_stream = Arrow::BufferInputStream.new(buffer)
buffer_input_stream.advance(3)
buffer_input_stream.align(8)
read_buffer = buffer_input_stream.read(3)
assert_equal("rld", read_buffer.data.to_s)
end
end
9 changes: 9 additions & 0 deletions c_glib/test/test-buffer-output-stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ def test_new
output_stream.close
assert_equal("Hello", buffer.data.to_s)
end

def test_align
buffer = Arrow::ResizableBuffer.new(0)
output_stream = Arrow::BufferOutputStream.new(buffer)
output_stream.write("Hello")
output_stream.align(8)
output_stream.close
assert_equal("Hello\x00\x00\x00", buffer.data.to_s)
end
end
1 change: 0 additions & 1 deletion c_glib/test/test-memory-mapped-input-stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def test_read_at
end
end


def test_mode
tempfile = Tempfile.open("arrow-memory-mapped-input-stream")
tempfile.write("Hello World")
Expand Down

0 comments on commit 9d007b1

Please sign in to comment.