]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/c_glib/arrow-glib/ipc-options.cpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / c_glib / arrow-glib / ipc-options.cpp
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 #include <arrow-glib/codec.hpp>
21 #include <arrow-glib/enums.h>
22 #include <arrow-glib/ipc-options.hpp>
23
24 G_BEGIN_DECLS
25
26 /**
27 * SECTION: ipc-options
28 * @section_id: ipc-options-classes
29 * @title: IPC options classes
30 * @include: arrow-glib/arrow-glib.h
31 *
32 * #GArrowReadOptions provides options for reading data.
33 *
34 * #GArrowWriteOptions provides options for writing data.
35 */
36
37 typedef struct GArrowReadOptionsPrivate_ {
38 arrow::ipc::IpcReadOptions options;
39 arrow::ipc::DictionaryMemo dictionary_memo;
40 } GArrowReadOptionsPrivate;
41
42 enum {
43 PROP_READ_OPTIONS_MAX_RECURSION_DEPTH = 1,
44 PROP_READ_OPTIONS_USE_THREADS,
45 };
46
47 G_DEFINE_TYPE_WITH_PRIVATE(GArrowReadOptions,
48 garrow_read_options,
49 G_TYPE_OBJECT);
50
51 #define GARROW_READ_OPTIONS_GET_PRIVATE(obj) \
52 static_cast<GArrowReadOptionsPrivate *>( \
53 garrow_read_options_get_instance_private( \
54 GARROW_READ_OPTIONS(obj)))
55
56 static void
57 garrow_read_options_finalize(GObject *object)
58 {
59 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(object);
60
61 priv->options.~IpcReadOptions();
62 priv->dictionary_memo.~DictionaryMemo();
63
64 G_OBJECT_CLASS(garrow_read_options_parent_class)->finalize(object);
65 }
66
67 static void
68 garrow_read_options_set_property(GObject *object,
69 guint prop_id,
70 const GValue *value,
71 GParamSpec *pspec)
72 {
73 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(object);
74
75 switch (prop_id) {
76 case PROP_READ_OPTIONS_MAX_RECURSION_DEPTH:
77 priv->options.max_recursion_depth = g_value_get_int(value);
78 break;
79 case PROP_READ_OPTIONS_USE_THREADS:
80 priv->options.use_threads = g_value_get_boolean(value);
81 break;
82 default:
83 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
84 break;
85 }
86 }
87
88 static void
89 garrow_read_options_get_property(GObject *object,
90 guint prop_id,
91 GValue *value,
92 GParamSpec *pspec)
93 {
94 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(object);
95
96 switch (prop_id) {
97 case PROP_READ_OPTIONS_MAX_RECURSION_DEPTH:
98 g_value_set_int(value, priv->options.max_recursion_depth);
99 break;
100 case PROP_READ_OPTIONS_USE_THREADS:
101 g_value_set_boolean(value, priv->options.use_threads);
102 break;
103 default:
104 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
105 break;
106 }
107 }
108
109 static void
110 garrow_read_options_init(GArrowReadOptions *object)
111 {
112 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(object);
113 new(&priv->options) arrow::ipc::IpcReadOptions;
114 priv->options = arrow::ipc::IpcReadOptions::Defaults();
115 new(&priv->dictionary_memo) arrow::ipc::DictionaryMemo;
116 }
117
118 static void
119 garrow_read_options_class_init(GArrowReadOptionsClass *klass)
120 {
121 auto gobject_class = G_OBJECT_CLASS(klass);
122
123 gobject_class->finalize = garrow_read_options_finalize;
124 gobject_class->set_property = garrow_read_options_set_property;
125 gobject_class->get_property = garrow_read_options_get_property;
126
127 auto options = arrow::ipc::IpcReadOptions::Defaults();
128
129 GParamSpec *spec;
130
131 /**
132 * GArrowReadOptions:max-recursion-depth:
133 *
134 * The maximum permitted schema nesting depth.
135 *
136 * Since: 1.0.0
137 */
138 spec = g_param_spec_int("max-recursion-depth",
139 "Max recursion depth",
140 "The maximum permitted schema nesting depth",
141 0,
142 arrow::ipc::kMaxNestingDepth,
143 options.max_recursion_depth,
144 static_cast<GParamFlags>(G_PARAM_READWRITE));
145 g_object_class_install_property(gobject_class,
146 PROP_READ_OPTIONS_MAX_RECURSION_DEPTH,
147 spec);
148
149 /**
150 * GArrowReadOptions:use-threads:
151 *
152 * Whether to use the global CPU thread pool.
153 *
154 * Since: 1.0.0
155 */
156 spec = g_param_spec_boolean("use-threads",
157 "Use threads",
158 "Whether to use the global CPU thread pool",
159 options.use_threads,
160 static_cast<GParamFlags>(G_PARAM_READWRITE));
161 g_object_class_install_property(gobject_class,
162 PROP_READ_OPTIONS_USE_THREADS,
163 spec);
164 }
165
166 /**
167 * garrow_read_options_new:
168 *
169 * Returns: A newly created #GArrowReadOptions.
170 *
171 * Since: 1.0.0
172 */
173 GArrowReadOptions *
174 garrow_read_options_new(void)
175 {
176 auto options = g_object_new(GARROW_TYPE_READ_OPTIONS, NULL);
177 return GARROW_READ_OPTIONS(options);
178 }
179
180 /**
181 * garrow_read_options_get_included_fields:
182 * @options: A #GArrowReadOptions.
183 * @n_fields: (out): The number of included fields.
184 *
185 * Returns: (array length=n_fields) (transfer full):
186 * Top-level schema fields to include when deserializing
187 * RecordBatch. If empty, return all deserialized fields.
188 *
189 * It should be freed with g_free() when no longer needed.
190 *
191 * Since: 1.0.0
192 */
193 int *
194 garrow_read_options_get_included_fields(GArrowReadOptions *options,
195 gsize *n_fields)
196 {
197 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(options);
198 if (priv->options.included_fields.empty()) {
199 if (n_fields) {
200 *n_fields = 0;
201 }
202 return NULL;
203 }
204
205 auto n = priv->options.included_fields.size();
206 auto fields = g_new(int, n);
207 if (n_fields) {
208 *n_fields = n;
209 }
210 for (size_t i = 0; i < n; ++i) {
211 fields[i] = priv->options.included_fields[i];
212 }
213 return fields;
214 }
215
216 /**
217 * garrow_read_options_set_included_fields:
218 * @options: A #GArrowReadOptions.
219 * @fields: (array length=n_fields): Top-level schema fields to
220 * include when deserializing RecordBatch. If empty, return all
221 * deserialized fields.
222 * @n_fields: The number of included fields.
223 *
224 * Since: 1.0.0
225 */
226 void
227 garrow_read_options_set_included_fields(GArrowReadOptions *options,
228 int *fields,
229 gsize n_fields)
230 {
231 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(options);
232
233 priv->options.included_fields.resize(n_fields);
234 for (gsize i = 0; i < n_fields; ++i) {
235 priv->options.included_fields[i] = fields[i];
236 }
237 }
238
239
240 typedef struct GArrowWriteOptionsPrivate_ {
241 arrow::ipc::IpcWriteOptions options;
242 GArrowCodec *codec;
243 } GArrowWriteOptionsPrivate;
244
245 enum {
246 PROP_WRITE_OPTIONS_ALLOW_64BIT = 1,
247 PROP_WRITE_OPTIONS_MAX_RECURSION_DEPTH,
248 PROP_WRITE_OPTIONS_ALIGNMENT,
249 PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT,
250 PROP_WRITE_OPTIONS_CODEC,
251 PROP_WRITE_OPTIONS_USE_THREADS,
252 };
253
254 G_DEFINE_TYPE_WITH_PRIVATE(GArrowWriteOptions,
255 garrow_write_options,
256 G_TYPE_OBJECT);
257
258 #define GARROW_WRITE_OPTIONS_GET_PRIVATE(obj) \
259 static_cast<GArrowWriteOptionsPrivate *>( \
260 garrow_write_options_get_instance_private( \
261 GARROW_WRITE_OPTIONS(obj)))
262
263 static void
264 garrow_write_options_dispose(GObject *object)
265 {
266 auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);
267
268 if (priv->codec) {
269 g_object_unref(priv->codec);
270 priv->codec = NULL;
271 }
272
273 G_OBJECT_CLASS(garrow_write_options_parent_class)->dispose(object);
274 }
275
276 static void
277 garrow_write_options_finalize(GObject *object)
278 {
279 auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);
280
281 priv->options.~IpcWriteOptions();
282
283 G_OBJECT_CLASS(garrow_write_options_parent_class)->finalize(object);
284 }
285
286 static void
287 garrow_write_options_set_property(GObject *object,
288 guint prop_id,
289 const GValue *value,
290 GParamSpec *pspec)
291 {
292 auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);
293
294 switch (prop_id) {
295 case PROP_WRITE_OPTIONS_ALLOW_64BIT:
296 priv->options.allow_64bit = g_value_get_boolean(value);
297 break;
298 case PROP_WRITE_OPTIONS_MAX_RECURSION_DEPTH:
299 priv->options.max_recursion_depth = g_value_get_int(value);
300 break;
301 case PROP_WRITE_OPTIONS_ALIGNMENT:
302 priv->options.alignment = g_value_get_int(value);
303 break;
304 case PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT:
305 priv->options.write_legacy_ipc_format = g_value_get_boolean(value);
306 break;
307 case PROP_WRITE_OPTIONS_CODEC:
308 if (priv->codec) {
309 g_object_unref(priv->codec);
310 }
311 priv->codec = GARROW_CODEC(g_value_dup_object(value));
312 priv->options.codec = garrow_codec_get_raw(priv->codec);
313 break;
314 case PROP_WRITE_OPTIONS_USE_THREADS:
315 priv->options.use_threads = g_value_get_boolean(value);
316 break;
317 default:
318 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
319 break;
320 }
321 }
322
323 static void
324 garrow_write_options_get_property(GObject *object,
325 guint prop_id,
326 GValue *value,
327 GParamSpec *pspec)
328 {
329 auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);
330
331 switch (prop_id) {
332 case PROP_WRITE_OPTIONS_ALLOW_64BIT:
333 g_value_set_boolean(value, priv->options.allow_64bit);
334 break;
335 case PROP_WRITE_OPTIONS_MAX_RECURSION_DEPTH:
336 g_value_set_int(value, priv->options.max_recursion_depth);
337 break;
338 case PROP_WRITE_OPTIONS_ALIGNMENT:
339 g_value_set_int(value, priv->options.alignment);
340 break;
341 case PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT:
342 g_value_set_boolean(value, priv->options.write_legacy_ipc_format);
343 break;
344 case PROP_WRITE_OPTIONS_CODEC:
345 g_value_set_object(value, priv->codec);
346 break;
347 case PROP_WRITE_OPTIONS_USE_THREADS:
348 g_value_set_boolean(value, priv->options.use_threads);
349 break;
350 default:
351 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
352 break;
353 }
354 }
355
356 static void
357 garrow_write_options_init(GArrowWriteOptions *object)
358 {
359 auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(object);
360 new(&priv->options) arrow::ipc::IpcWriteOptions;
361 priv->options = arrow::ipc::IpcWriteOptions::Defaults();
362 if (priv->options.codec) {
363 priv->codec = garrow_codec_new_raw(&(priv->options.codec));
364 } else {
365 priv->codec = NULL;
366 }
367 }
368
369 static void
370 garrow_write_options_class_init(GArrowWriteOptionsClass *klass)
371 {
372 auto gobject_class = G_OBJECT_CLASS(klass);
373
374 gobject_class->dispose = garrow_write_options_dispose;
375 gobject_class->finalize = garrow_write_options_finalize;
376 gobject_class->set_property = garrow_write_options_set_property;
377 gobject_class->get_property = garrow_write_options_get_property;
378
379 auto options = arrow::ipc::IpcWriteOptions::Defaults();
380
381 GParamSpec *spec;
382
383 /**
384 * GArrowWriteOptions:allow-64bit:
385 *
386 * Whether to allow field lengths that don't fit in a signed 32-bit
387 * int. Some implementations may not be able to parse such streams.
388 *
389 * Since: 1.0.0
390 */
391 spec = g_param_spec_boolean("allow-64bit",
392 "Allow 64bit",
393 "Whether to allow signed 64-bit int "
394 "for field length",
395 options.allow_64bit,
396 static_cast<GParamFlags>(G_PARAM_READWRITE));
397 g_object_class_install_property(gobject_class,
398 PROP_WRITE_OPTIONS_ALLOW_64BIT,
399 spec);
400
401 /**
402 * GArrowWriteOptions:max-recursion-depth:
403 *
404 * The maximum permitted schema nesting depth.
405 *
406 * Since: 1.0.0
407 */
408 spec = g_param_spec_int("max-recursion-depth",
409 "Max recursion depth",
410 "The maximum permitted schema nesting depth",
411 0,
412 arrow::ipc::kMaxNestingDepth,
413 options.max_recursion_depth,
414 static_cast<GParamFlags>(G_PARAM_READWRITE));
415 g_object_class_install_property(gobject_class,
416 PROP_WRITE_OPTIONS_MAX_RECURSION_DEPTH,
417 spec);
418
419 /**
420 * GArrowWriteOptions:alignment:
421 *
422 * Write padding after memory buffers to this multiple of
423 * bytes. Generally 8 or 64.
424 *
425 * Since: 1.0.0
426 */
427 spec = g_param_spec_int("alignment",
428 "Alignment",
429 "Write padding "
430 "after memory buffers to this multiple of bytes",
431 0,
432 G_MAXINT,
433 options.alignment,
434 static_cast<GParamFlags>(G_PARAM_READWRITE));
435 g_object_class_install_property(gobject_class,
436 PROP_WRITE_OPTIONS_ALIGNMENT,
437 spec);
438
439 /**
440 * GArrowWriteOptions:write-legacy-ipc-format:
441 *
442 * Whether to write the pre-0.15.0 encapsulated IPC message format
443 * consisting of a 4-byte prefix instead of 8 byte.
444 *
445 * Since: 1.0.0
446 */
447 spec = g_param_spec_boolean("write-legacy-ipc-format",
448 "Write legacy IPC format",
449 "Whether to write legacy IPC format",
450 options.write_legacy_ipc_format,
451 static_cast<GParamFlags>(G_PARAM_READWRITE));
452 g_object_class_install_property(gobject_class,
453 PROP_WRITE_OPTIONS_WRITE_LEGACY_IPC_FORMAT,
454 spec);
455
456 /**
457 * GArrowWriteOptions:codec:
458 *
459 * Codec to use for compressing and decompressing record batch body
460 * buffers. This is not part of the Arrow IPC protocol and only for
461 * internal use (e.g. Feather files).
462 *
463 * May only be UNCOMPRESSED, LZ4_FRAME and ZSTD.
464 *
465 * Since: 2.0.0
466 */
467 spec = g_param_spec_object("codec",
468 "Codec",
469 "Codec to use for "
470 "compressing record batch body buffers.",
471 GARROW_TYPE_CODEC,
472 static_cast<GParamFlags>(G_PARAM_READWRITE));
473 g_object_class_install_property(gobject_class,
474 PROP_WRITE_OPTIONS_CODEC,
475 spec);
476
477 /**
478 * GArrowWriteOptions:use-threads:
479 *
480 * Whether to use the global CPU thread pool.
481 *
482 * Since: 1.0.0
483 */
484 spec = g_param_spec_boolean("use-threads",
485 "Use threads",
486 "Whether to use the global CPU thread pool",
487 options.use_threads,
488 static_cast<GParamFlags>(G_PARAM_READWRITE));
489 g_object_class_install_property(gobject_class,
490 PROP_WRITE_OPTIONS_USE_THREADS,
491 spec);
492 }
493
494 /**
495 * garrow_write_options_new:
496 *
497 * Returns: A newly created #GArrowWriteOptions.
498 *
499 * Since: 1.0.0
500 */
501 GArrowWriteOptions *
502 garrow_write_options_new(void)
503 {
504 auto options = g_object_new(GARROW_TYPE_WRITE_OPTIONS, NULL);
505 return GARROW_WRITE_OPTIONS(options);
506 }
507
508 G_END_DECLS
509
510 arrow::ipc::IpcReadOptions *
511 garrow_read_options_get_raw(GArrowReadOptions *options)
512 {
513 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(options);
514 return &(priv->options);
515 }
516
517 arrow::ipc::DictionaryMemo *
518 garrow_read_options_get_dictionary_memo_raw(GArrowReadOptions *options)
519 {
520 auto priv = GARROW_READ_OPTIONS_GET_PRIVATE(options);
521 return &(priv->dictionary_memo);
522 }
523
524 arrow::ipc::IpcWriteOptions *
525 garrow_write_options_get_raw(GArrowWriteOptions *options)
526 {
527 auto priv = GARROW_WRITE_OPTIONS_GET_PRIVATE(options);
528 return &(priv->options);
529 }