Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions c_glib/arrow-glib/codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ G_BEGIN_DECLS
*/

typedef struct GArrowCodecPrivate_ {
arrow::util::Codec *codec;
std::shared_ptr<arrow::util::Codec> codec;
} GArrowCodecPrivate;

enum {
Expand All @@ -57,7 +57,7 @@ garrow_codec_finalize(GObject *object)
{
auto priv = GARROW_CODEC_GET_PRIVATE(object);

delete priv->codec;
priv->codec.~shared_ptr();

G_OBJECT_CLASS(garrow_codec_parent_class)->finalize(object);
}
Expand All @@ -72,7 +72,8 @@ garrow_codec_set_property(GObject *object,

switch (prop_id) {
case PROP_CODEC:
priv->codec = static_cast<arrow::util::Codec *>(g_value_get_pointer(value));
priv->codec =
*static_cast<std::shared_ptr<arrow::util::Codec> *>(g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Expand All @@ -96,6 +97,8 @@ garrow_codec_get_property(GObject *object,
static void
garrow_codec_init(GArrowCodec *object)
{
auto priv = GARROW_CODEC_GET_PRIVATE(object);
new(&priv->codec) std::shared_ptr<arrow::util::Codec>;
}

static void
Expand All @@ -111,7 +114,7 @@ garrow_codec_class_init(GArrowCodecClass *klass)

spec = g_param_spec_pointer("codec",
"Codec",
"The raw arrow::util::Codec *",
"The raw std::shared_ptr<arrow::util::Codec> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_CODEC, spec);
Expand All @@ -133,7 +136,9 @@ garrow_codec_new(GArrowCompressionType type,
auto arrow_type = garrow_compression_type_to_raw(type);
auto arrow_codec = arrow::util::Codec::Create(arrow_type);
if (garrow::check(error, arrow_codec, "[codec][new]")) {
return garrow_codec_new_raw(arrow_codec.ValueOrDie().release());
std::shared_ptr<arrow::util::Codec> arrow_codec_shared =
std::move(*arrow_codec);
return garrow_codec_new_raw(&arrow_codec_shared);
} else {
return NULL;
}
Expand All @@ -151,7 +156,46 @@ const gchar *
garrow_codec_get_name(GArrowCodec *codec)
{
auto arrow_codec = garrow_codec_get_raw(codec);
return arrow_codec->name();
if (!arrow_codec) {
return NULL;
}
return arrow_codec->name().c_str();
}

/**
* garrow_codec_get_compression_type:
* @codec: A #GArrowCodec.
*
* Returns: The compression type of the codec.
*
* Since: 2.0.0
*/
GArrowCompressionType
garrow_codec_get_compression_type(GArrowCodec *codec)
{
auto arrow_codec = garrow_codec_get_raw(codec);
if (!arrow_codec) {
return GARROW_COMPRESSION_TYPE_UNCOMPRESSED;
}
return garrow_compression_type_from_raw(arrow_codec->compression_type());
}

/**
* garrow_codec_get_compression_level:
* @codec: A #GArrowCodec.
*
* Returns: The compression level of the codec.
*
* Since: 2.0.0
*/
gint
garrow_codec_get_compression_level(GArrowCodec *codec)
{
auto arrow_codec = garrow_codec_get_raw(codec);
if (!arrow_codec) {
return arrow::util::Codec::UseDefaultCompressionLevel();
}
return arrow_codec->compression_level();
}

G_END_DECLS
Expand Down Expand Up @@ -207,15 +251,15 @@ garrow_compression_type_to_raw(GArrowCompressionType type)
}

GArrowCodec *
garrow_codec_new_raw(arrow::util::Codec *arrow_codec)
garrow_codec_new_raw(std::shared_ptr<arrow::util::Codec> *arrow_codec)
{
auto codec = GARROW_CODEC(g_object_new(GARROW_TYPE_CODEC,
"codec", arrow_codec,
NULL));
return codec;
}

arrow::util::Codec *
std::shared_ptr<arrow::util::Codec>
garrow_codec_get_raw(GArrowCodec *codec)
{
auto priv = GARROW_CODEC_GET_PRIVATE(codec);
Expand Down
7 changes: 7 additions & 0 deletions c_glib/arrow-glib/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <arrow-glib/gobject-type.h>
#include <arrow-glib/version.h>

G_BEGIN_DECLS

Expand Down Expand Up @@ -63,5 +64,11 @@ GArrowCodec *garrow_codec_new(GArrowCompressionType type,
GError **error);

const gchar *garrow_codec_get_name(GArrowCodec *codec);
GARROW_AVAILABLE_IN_2_0
GArrowCompressionType
garrow_codec_get_compression_type(GArrowCodec *codec);
GARROW_AVAILABLE_IN_2_0
gint
garrow_codec_get_compression_level(GArrowCodec *codec);

G_END_DECLS
4 changes: 2 additions & 2 deletions c_glib/arrow-glib/codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ arrow::Compression::type
garrow_compression_type_to_raw(GArrowCompressionType type);

GArrowCodec *
garrow_codec_new_raw(arrow::util::Codec *arrow_codec);
arrow::util::Codec *
garrow_codec_new_raw(std::shared_ptr<arrow::util::Codec> *arrow_codec);
std::shared_ptr<arrow::util::Codec>
garrow_codec_get_raw(GArrowCodec *codec);
2 changes: 1 addition & 1 deletion c_glib/arrow-glib/input-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ garrow_compressed_input_stream_new(GArrowCodec *codec,
GArrowInputStream *raw,
GError **error)
{
auto arrow_codec = garrow_codec_get_raw(codec);
auto arrow_codec = garrow_codec_get_raw(codec).get();
auto arrow_raw = garrow_input_stream_get_raw(raw);
auto arrow_stream =
arrow::io::CompressedInputStream::Make(arrow_codec, arrow_raw);
Expand Down
83 changes: 41 additions & 42 deletions c_glib/arrow-glib/ipc-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# include <config.h>
#endif

#include <arrow-glib/codec.hpp>
#include <arrow-glib/enums.h>
#include <arrow-glib/ipc-options.hpp>

Expand Down Expand Up @@ -242,15 +243,15 @@ garrow_read_options_set_included_fields(GArrowReadOptions *options,

typedef struct GArrowWriteOptionsPrivate_ {
arrow::ipc::IpcWriteOptions options;
GArrowCodec *codec;
} GArrowWriteOptionsPrivate;

enum {
PROP_WRITE_OPTIONS_ALLOW_64BIT = 1,
PROP_WRITE_OPTIONS_MAX_RECURSION_DEPTH,
PROP_WRITE_OPTIONS_ALIGNMENT,
PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT,
PROP_WRITE_OPTIONS_COMPRESSION,
PROP_WRITE_OPTIONS_COMPRESSION_LEVEL,
PROP_WRITE_OPTIONS_CODEC,
PROP_WRITE_OPTIONS_USE_THREADS,
};

Expand All @@ -263,6 +264,19 @@ G_DEFINE_TYPE_WITH_PRIVATE(GArrowWriteOptions,
garrow_write_options_get_instance_private( \
GARROW_WRITE_OPTIONS(obj)))

static void
garrow_write_options_dispose(GObject *object)
{
auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);

if (priv->codec) {
g_object_unref(priv->codec);
priv->codec = NULL;
}

G_OBJECT_CLASS(garrow_write_options_parent_class)->dispose(object);
}

static void
garrow_write_options_finalize(GObject *object)
{
Expand Down Expand Up @@ -294,12 +308,12 @@ garrow_write_options_set_property(GObject *object,
case PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT:
priv->options.write_legacy_ipc_format = g_value_get_boolean(value);
break;
case PROP_WRITE_OPTIONS_COMPRESSION:
priv->options.compression =
static_cast<arrow::Compression::type>(g_value_get_enum(value));
break;
case PROP_WRITE_OPTIONS_COMPRESSION_LEVEL:
priv->options.compression_level = g_value_get_int(value);
case PROP_WRITE_OPTIONS_CODEC:
if (priv->codec) {
g_object_unref(priv->codec);
}
priv->codec = GARROW_CODEC(g_value_dup_object(value));
priv->options.codec = garrow_codec_get_raw(priv->codec);
break;
case PROP_WRITE_OPTIONS_USE_THREADS:
priv->options.use_threads = g_value_get_boolean(value);
Expand Down Expand Up @@ -331,11 +345,8 @@ garrow_write_options_get_property(GObject *object,
case PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT:
g_value_set_boolean(value, priv->options.write_legacy_ipc_format);
break;
case PROP_WRITE_OPTIONS_COMPRESSION:
g_value_set_enum(value, priv->options.compression);
break;
case PROP_WRITE_OPTIONS_COMPRESSION_LEVEL:
g_value_set_int(value, priv->options.compression_level);
case PROP_WRITE_OPTIONS_CODEC:
g_value_set_object(value, priv->codec);
break;
case PROP_WRITE_OPTIONS_USE_THREADS:
g_value_set_boolean(value, priv->options.use_threads);
Expand All @@ -352,13 +363,19 @@ garrow_write_options_init(GArrowWriteOptions *object)
auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);
new(&priv->options) arrow::ipc::IpcWriteOptions;
priv->options = arrow::ipc::IpcWriteOptions::Defaults();
if (priv->options.codec) {
priv->codec = garrow_codec_new_raw(&(priv->options.codec));
} else {
priv->codec = NULL;
}
}

static void
garrow_write_options_class_init(GArrowWriteOptionsClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);

gobject_class->dispose = garrow_write_options_dispose;
gobject_class->finalize = garrow_write_options_finalize;
gobject_class->set_property = garrow_write_options_set_property;
gobject_class->get_property = garrow_write_options_get_property;
Expand Down Expand Up @@ -441,42 +458,24 @@ garrow_write_options_class_init(GArrowWriteOptionsClass *klass)
spec);

/**
* GArrowWriteOptions:compression:
* GArrowWriteOptions:codec:
*
* Codec to use for compressing and decompressing record batch body
* buffers. This is not part of the Arrow IPC protocol and only for
* internal use (e.g. Feather files). May only be LZ4_FRAME and
* ZSTD.
* internal use (e.g. Feather files).
*
* Since: 1.0.0
*/
spec = g_param_spec_enum("compression",
"Compression",
"Codec to use for "
"compressing record batch body buffers.",
GARROW_TYPE_COMPRESSION_TYPE,
options.compression,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class,
PROP_WRITE_OPTIONS_COMPRESSION,
spec);

/**
* GArrowWriteOptions:compression-level:
*
* The level for compression.
* May only be UNCOMPRESSED, LZ4_FRAME and ZSTD.
*
* Since: 1.0.0
* Since: 2.0.0
*/
spec = g_param_spec_int("compression-level",
"Compression level",
"The level for compression",
G_MININT,
G_MAXINT,
options.compression_level,
static_cast<GParamFlags>(G_PARAM_READWRITE));
spec = g_param_spec_object("codec",
"Codec",
"Codec to use for "
"compressing record batch body buffers.",
GARROW_TYPE_CODEC,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class,
PROP_WRITE_OPTIONS_COMPRESSION_LEVEL,
PROP_WRITE_OPTIONS_CODEC,
spec);

/**
Expand Down
2 changes: 1 addition & 1 deletion c_glib/arrow-glib/output-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ garrow_compressed_output_stream_new(GArrowCodec *codec,
GArrowOutputStream *raw,
GError **error)
{
auto arrow_codec = garrow_codec_get_raw(codec);
auto arrow_codec = garrow_codec_get_raw(codec).get();
auto arrow_raw = garrow_output_stream_get_raw(raw);
auto arrow_stream = arrow::io::CompressedOutputStream::Make(arrow_codec,
arrow_raw);
Expand Down
10 changes: 10 additions & 0 deletions c_glib/test/test-codec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ def test_name
codec = Arrow::Codec.new(:gzip)
assert_equal("gzip", codec.name)
end

def test_compression_type
codec = Arrow::Codec.new(:gzip)
assert_equal(Arrow::CompressionType::GZIP, codec.compression_type)
end

def test_compression_level
codec = Arrow::Codec.new(:gzip)
assert_equal(9, codec.compression_level)
end
end
22 changes: 5 additions & 17 deletions c_glib/test/test-write-options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,15 @@ def test_accessor
end
end

sub_test_case("compression") do
sub_test_case("codec") do
def test_default
assert_equal(Arrow::CompressionType::UNCOMPRESSED,
@options.compression)
assert_nil(@options.codec)
end

def test_accessor
@options.compression = :zstd
assert_equal(Arrow::CompressionType::ZSTD,
@options.compression)
end
end

sub_test_case("compression-level") do
def test_default
assert_equal(-(2 ** 31), @options.compression_level)
end

def test_accessor
@options.compression_level = 8
assert_equal(8, @options.compression_level)
@options.codec = Arrow::Codec.new(:zstd)
assert_equal("zstd",
@options.codec.name)
end
end

Expand Down
Loading