summaryrefslogtreecommitdiffstats
path: root/src/arrow/c_glib/arrow-glib/codec.cpp
blob: fecf9770487ccdbdc79151244a533240ffa93e27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * 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.
 */

#include <arrow-glib/codec.hpp>
#include <arrow-glib/error.hpp>

G_BEGIN_DECLS

/**
 * SECTION: codec
 * @title: Codec related type and class
 * @include: arrow-glib/arrow-glib.h
 *
 * #GArrowCompressionType provides compression types corresponding to
 * `arrow::Compression::type`.
 *
 * #GArrowCodec is a class for compressing and decompressing data.
 */

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

enum {
  PROP_CODEC = 1
};

G_DEFINE_TYPE_WITH_PRIVATE(GArrowCodec, garrow_codec, G_TYPE_OBJECT)

#define GARROW_CODEC_GET_PRIVATE(object)        \
  static_cast<GArrowCodecPrivate *>(            \
    garrow_codec_get_instance_private(          \
      GARROW_CODEC(object)))

static void
garrow_codec_finalize(GObject *object)
{
  auto priv = GARROW_CODEC_GET_PRIVATE(object);

  priv->codec.~shared_ptr();

  G_OBJECT_CLASS(garrow_codec_parent_class)->finalize(object);
}

static void
garrow_codec_set_property(GObject *object,
                          guint prop_id,
                          const GValue *value,
                          GParamSpec *pspec)
{
  auto priv = GARROW_CODEC_GET_PRIVATE(object);

  switch (prop_id) {
  case PROP_CODEC:
    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);
    break;
  }
}

static void
garrow_codec_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_codec_init(GArrowCodec *object)
{
  auto priv = GARROW_CODEC_GET_PRIVATE(object);
  new(&priv->codec) std::shared_ptr<arrow::util::Codec>;
}

static void
garrow_codec_class_init(GArrowCodecClass *klass)
{
  GParamSpec *spec;

  auto gobject_class = G_OBJECT_CLASS(klass);

  gobject_class->finalize     = garrow_codec_finalize;
  gobject_class->set_property = garrow_codec_set_property;
  gobject_class->get_property = garrow_codec_get_property;

  spec = g_param_spec_pointer("codec",
                              "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);
}

/**
 * garrow_codec_new:
 * @type: A #GArrowCompressionType.
 * @error: (nullable): Return location for a #GError or %NULL.
 *
 * Returns: A newly created #GArrowCodec on success, %NULL on error.
 *
 * Since: 0.12.0
 */
GArrowCodec *
garrow_codec_new(GArrowCompressionType type,
                 GError **error)
{
  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]")) {
    std::shared_ptr<arrow::util::Codec> arrow_codec_shared =
      std::move(*arrow_codec);
    return garrow_codec_new_raw(&arrow_codec_shared);
  } else {
    return NULL;
  }
}

/**
 * garrow_codec_get_name:
 * @codec: A #GArrowCodec.
 *
 * Returns: The name of the codec.
 *
 * Since: 0.12.0
 */
const gchar *
garrow_codec_get_name(GArrowCodec *codec)
{
  auto arrow_codec = garrow_codec_get_raw(codec);
  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

GArrowCompressionType
garrow_compression_type_from_raw(arrow::Compression::type arrow_type)
{
  switch (arrow_type) {
  case arrow::Compression::type::UNCOMPRESSED:
    return GARROW_COMPRESSION_TYPE_UNCOMPRESSED;
  case arrow::Compression::type::SNAPPY:
    return GARROW_COMPRESSION_TYPE_SNAPPY;
  case arrow::Compression::type::GZIP:
    return GARROW_COMPRESSION_TYPE_GZIP;
  case arrow::Compression::type::BROTLI:
    return GARROW_COMPRESSION_TYPE_BROTLI;
  case arrow::Compression::type::ZSTD:
    return GARROW_COMPRESSION_TYPE_ZSTD;
  case arrow::Compression::type::LZ4:
    return GARROW_COMPRESSION_TYPE_LZ4;
  case arrow::Compression::type::LZO:
    return GARROW_COMPRESSION_TYPE_LZO;
  case arrow::Compression::type::BZ2:
    return GARROW_COMPRESSION_TYPE_BZ2;
  default:
    return GARROW_COMPRESSION_TYPE_UNCOMPRESSED;
  }
}

arrow::Compression::type
garrow_compression_type_to_raw(GArrowCompressionType type)
{
  switch (type) {
  case GARROW_COMPRESSION_TYPE_UNCOMPRESSED:
    return arrow::Compression::type::UNCOMPRESSED;
  case GARROW_COMPRESSION_TYPE_SNAPPY:
    return arrow::Compression::type::SNAPPY;
  case GARROW_COMPRESSION_TYPE_GZIP:
    return arrow::Compression::type::GZIP;
  case GARROW_COMPRESSION_TYPE_BROTLI:
    return arrow::Compression::type::BROTLI;
  case GARROW_COMPRESSION_TYPE_ZSTD:
    return arrow::Compression::type::ZSTD;
  case GARROW_COMPRESSION_TYPE_LZ4:
    return arrow::Compression::type::LZ4;
  case GARROW_COMPRESSION_TYPE_LZO:
    return arrow::Compression::type::LZO;
  case GARROW_COMPRESSION_TYPE_BZ2:
    return arrow::Compression::type::BZ2;
  default:
    return arrow::Compression::type::UNCOMPRESSED;
  }
}

GArrowCodec *
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;
}

std::shared_ptr<arrow::util::Codec>
garrow_codec_get_raw(GArrowCodec *codec)
{
  auto priv = GARROW_CODEC_GET_PRIVATE(codec);
  return priv->codec;
}