1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <arrow-glib/codec.hpp>
25 #include <arrow-glib/error.hpp>
26
27 G_BEGIN_DECLS
28
29 /**
30 * SECTION: codec
31 * @title: Codec related type and class
32 * @include: arrow-glib/arrow-glib.h
33 *
34 * #GArrowCompressionType provides compression types corresponding to
35 * `arrow::Compression::type`.
36 *
37 * #GArrowCodec is a class for compressing and decompressing data.
38 */
39
40 typedef struct GArrowCodecPrivate_ {
41 arrow::util::Codec *codec;
42 } GArrowCodecPrivate;
43
44 enum {
45 PROP_CODEC = 1
46 };
47
G_DEFINE_TYPE_WITH_PRIVATE(GArrowCodec,garrow_codec,G_TYPE_OBJECT)48 G_DEFINE_TYPE_WITH_PRIVATE(GArrowCodec, garrow_codec, G_TYPE_OBJECT)
49
50 #define GARROW_CODEC_GET_PRIVATE(object) \
51 static_cast<GArrowCodecPrivate *>( \
52 garrow_codec_get_instance_private( \
53 GARROW_CODEC(object)))
54
55 static void
56 garrow_codec_finalize(GObject *object)
57 {
58 auto priv = GARROW_CODEC_GET_PRIVATE(object);
59
60 delete priv->codec;
61
62 G_OBJECT_CLASS(garrow_codec_parent_class)->finalize(object);
63 }
64
65 static void
garrow_codec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)66 garrow_codec_set_property(GObject *object,
67 guint prop_id,
68 const GValue *value,
69 GParamSpec *pspec)
70 {
71 auto priv = GARROW_CODEC_GET_PRIVATE(object);
72
73 switch (prop_id) {
74 case PROP_CODEC:
75 priv->codec = static_cast<arrow::util::Codec *>(g_value_get_pointer(value));
76 break;
77 default:
78 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
79 break;
80 }
81 }
82
83 static void
garrow_codec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)84 garrow_codec_get_property(GObject *object,
85 guint prop_id,
86 GValue *value,
87 GParamSpec *pspec)
88 {
89 switch (prop_id) {
90 default:
91 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
92 break;
93 }
94 }
95
96 static void
garrow_codec_init(GArrowCodec * object)97 garrow_codec_init(GArrowCodec *object)
98 {
99 }
100
101 static void
garrow_codec_class_init(GArrowCodecClass * klass)102 garrow_codec_class_init(GArrowCodecClass *klass)
103 {
104 GParamSpec *spec;
105
106 auto gobject_class = G_OBJECT_CLASS(klass);
107
108 gobject_class->finalize = garrow_codec_finalize;
109 gobject_class->set_property = garrow_codec_set_property;
110 gobject_class->get_property = garrow_codec_get_property;
111
112 spec = g_param_spec_pointer("codec",
113 "Codec",
114 "The raw arrow::util::Codec *",
115 static_cast<GParamFlags>(G_PARAM_WRITABLE |
116 G_PARAM_CONSTRUCT_ONLY));
117 g_object_class_install_property(gobject_class, PROP_CODEC, spec);
118 }
119
120 /**
121 * garrow_codec_new:
122 * @type: A #GArrowCompressionType.
123 * @error: (nullable): Return location for a #GError or %NULL.
124 *
125 * Returns: A newly created #GArrowCodec on success, %NULL on error.
126 *
127 * Since: 0.12.0
128 */
129 GArrowCodec *
garrow_codec_new(GArrowCompressionType type,GError ** error)130 garrow_codec_new(GArrowCompressionType type,
131 GError **error)
132 {
133 auto arrow_type = garrow_compression_type_to_raw(type);
134 auto arrow_codec = arrow::util::Codec::Create(arrow_type);
135 if (garrow::check(error, arrow_codec, "[codec][new]")) {
136 return garrow_codec_new_raw(arrow_codec.ValueOrDie().release());
137 } else {
138 return NULL;
139 }
140 }
141
142 /**
143 * garrow_codec_get_name:
144 * @codec: A #GArrowCodec.
145 *
146 * Returns: The name of the codec.
147 *
148 * Since: 0.12.0
149 */
150 const gchar *
garrow_codec_get_name(GArrowCodec * codec)151 garrow_codec_get_name(GArrowCodec *codec)
152 {
153 auto arrow_codec = garrow_codec_get_raw(codec);
154 return arrow_codec->name();
155 }
156
157 G_END_DECLS
158
159 GArrowCompressionType
garrow_compression_type_from_raw(arrow::Compression::type arrow_type)160 garrow_compression_type_from_raw(arrow::Compression::type arrow_type)
161 {
162 switch (arrow_type) {
163 case arrow::Compression::type::UNCOMPRESSED:
164 return GARROW_COMPRESSION_TYPE_UNCOMPRESSED;
165 case arrow::Compression::type::SNAPPY:
166 return GARROW_COMPRESSION_TYPE_SNAPPY;
167 case arrow::Compression::type::GZIP:
168 return GARROW_COMPRESSION_TYPE_GZIP;
169 case arrow::Compression::type::BROTLI:
170 return GARROW_COMPRESSION_TYPE_BROTLI;
171 case arrow::Compression::type::ZSTD:
172 return GARROW_COMPRESSION_TYPE_ZSTD;
173 case arrow::Compression::type::LZ4:
174 return GARROW_COMPRESSION_TYPE_LZ4;
175 case arrow::Compression::type::LZO:
176 return GARROW_COMPRESSION_TYPE_LZO;
177 case arrow::Compression::type::BZ2:
178 return GARROW_COMPRESSION_TYPE_BZ2;
179 default:
180 return GARROW_COMPRESSION_TYPE_UNCOMPRESSED;
181 }
182 }
183
184 arrow::Compression::type
garrow_compression_type_to_raw(GArrowCompressionType type)185 garrow_compression_type_to_raw(GArrowCompressionType type)
186 {
187 switch (type) {
188 case GARROW_COMPRESSION_TYPE_UNCOMPRESSED:
189 return arrow::Compression::type::UNCOMPRESSED;
190 case GARROW_COMPRESSION_TYPE_SNAPPY:
191 return arrow::Compression::type::SNAPPY;
192 case GARROW_COMPRESSION_TYPE_GZIP:
193 return arrow::Compression::type::GZIP;
194 case GARROW_COMPRESSION_TYPE_BROTLI:
195 return arrow::Compression::type::BROTLI;
196 case GARROW_COMPRESSION_TYPE_ZSTD:
197 return arrow::Compression::type::ZSTD;
198 case GARROW_COMPRESSION_TYPE_LZ4:
199 return arrow::Compression::type::LZ4;
200 case GARROW_COMPRESSION_TYPE_LZO:
201 return arrow::Compression::type::LZO;
202 case GARROW_COMPRESSION_TYPE_BZ2:
203 return arrow::Compression::type::BZ2;
204 default:
205 return arrow::Compression::type::UNCOMPRESSED;
206 }
207 }
208
209 GArrowCodec *
garrow_codec_new_raw(arrow::util::Codec * arrow_codec)210 garrow_codec_new_raw(arrow::util::Codec *arrow_codec)
211 {
212 auto codec = GARROW_CODEC(g_object_new(GARROW_TYPE_CODEC,
213 "codec", arrow_codec,
214 NULL));
215 return codec;
216 }
217
218 arrow::util::Codec *
garrow_codec_get_raw(GArrowCodec * codec)219 garrow_codec_get_raw(GArrowCodec *codec)
220 {
221 auto priv = GARROW_CODEC_GET_PRIVATE(codec);
222 return priv->codec;
223 }
224