]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/c_glib/arrow-glib/writer.cpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / c_glib / arrow-glib / writer.cpp
CommitLineData
1d09f67e
TL
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/array.hpp>
21#include <arrow-glib/error.hpp>
22#include <arrow-glib/record-batch.hpp>
23#include <arrow-glib/schema.hpp>
24#include <arrow-glib/table.hpp>
25
26#include <arrow-glib/output-stream.hpp>
27
28#include <arrow-glib/writer.hpp>
29
30G_BEGIN_DECLS
31
32/**
33 * SECTION: writer
34 * @section_id: writer-classes
35 * @title: Writer classes
36 * @include: arrow-glib/arrow-glib.h
37 *
38 * #GArrowRecordBatchWriter is a base class for writing record batches
39 * in stream format into output.
40 *
41 * #GArrowRecordBatchStreamWriter is a base class for writing record
42 * batches in stream format into output synchronously.
43 *
44 * #GArrowRecordBatchFileWriter is a class for writing record
45 * batches in file format into output.
46 */
47
48typedef struct GArrowRecordBatchWriterPrivate_ {
49 std::shared_ptr<arrow::ipc::RecordBatchWriter> record_batch_writer;
50} GArrowRecordBatchWriterPrivate;
51
52enum {
53 PROP_0,
54 PROP_RECORD_BATCH_WRITER
55};
56
57G_DEFINE_TYPE_WITH_PRIVATE(GArrowRecordBatchWriter,
58 garrow_record_batch_writer,
59 G_TYPE_OBJECT);
60
61#define GARROW_RECORD_BATCH_WRITER_GET_PRIVATE(obj) \
62 static_cast<GArrowRecordBatchWriterPrivate *>( \
63 garrow_record_batch_writer_get_instance_private( \
64 GARROW_RECORD_BATCH_WRITER(obj)))
65
66static void
67garrow_record_batch_writer_finalize(GObject *object)
68{
69 auto priv = GARROW_RECORD_BATCH_WRITER_GET_PRIVATE(object);
70
71 priv->record_batch_writer.~shared_ptr();
72
73 G_OBJECT_CLASS(garrow_record_batch_writer_parent_class)->finalize(object);
74}
75
76static void
77garrow_record_batch_writer_set_property(GObject *object,
78 guint prop_id,
79 const GValue *value,
80 GParamSpec *pspec)
81{
82 auto priv = GARROW_RECORD_BATCH_WRITER_GET_PRIVATE(object);
83
84 switch (prop_id) {
85 case PROP_RECORD_BATCH_WRITER:
86 priv->record_batch_writer =
87 *static_cast<std::shared_ptr<arrow::ipc::RecordBatchWriter> *>(g_value_get_pointer(value));
88 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
91 break;
92 }
93}
94
95static void
96garrow_record_batch_writer_get_property(GObject *object,
97 guint prop_id,
98 GValue *value,
99 GParamSpec *pspec)
100{
101 switch (prop_id) {
102 default:
103 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
104 break;
105 }
106}
107
108static void
109garrow_record_batch_writer_init(GArrowRecordBatchWriter *object)
110{
111 auto priv = GARROW_RECORD_BATCH_WRITER_GET_PRIVATE(object);
112 new(&priv->record_batch_writer) std::shared_ptr<arrow::ipc::RecordBatchWriter>;
113}
114
115static void
116garrow_record_batch_writer_class_init(GArrowRecordBatchWriterClass *klass)
117{
118 GObjectClass *gobject_class;
119 GParamSpec *spec;
120
121 gobject_class = G_OBJECT_CLASS(klass);
122
123 gobject_class->finalize = garrow_record_batch_writer_finalize;
124 gobject_class->set_property = garrow_record_batch_writer_set_property;
125 gobject_class->get_property = garrow_record_batch_writer_get_property;
126
127 spec = g_param_spec_pointer("record-batch-writer",
128 "arrow::ipc::RecordBatchWriter",
129 "The raw std::shared<arrow::ipc::RecordBatchWriter> *",
130 static_cast<GParamFlags>(G_PARAM_WRITABLE |
131 G_PARAM_CONSTRUCT_ONLY));
132 g_object_class_install_property(gobject_class, PROP_RECORD_BATCH_WRITER, spec);
133}
134
135/**
136 * garrow_record_batch_writer_write_record_batch:
137 * @writer: A #GArrowRecordBatchWriter.
138 * @record_batch: The record batch to be written.
139 * @error: (nullable): Return location for a #GError or %NULL.
140 *
141 * Returns: %TRUE on success, %FALSE if there was an error.
142 *
143 * Since: 0.4.0
144 */
145gboolean
146garrow_record_batch_writer_write_record_batch(GArrowRecordBatchWriter *writer,
147 GArrowRecordBatch *record_batch,
148 GError **error)
149{
150 auto arrow_writer = garrow_record_batch_writer_get_raw(writer);
151 auto arrow_record_batch = garrow_record_batch_get_raw(record_batch);
152 auto arrow_record_batch_raw = arrow_record_batch.get();
153
154 auto status = arrow_writer->WriteRecordBatch(*arrow_record_batch_raw);
155 return garrow_error_check(error,
156 status,
157 "[record-batch-writer][write-record-batch]");
158}
159
160/**
161 * garrow_record_batch_writer_write_table:
162 * @writer: A #GArrowRecordBatchWriter.
163 * @table: The table to be written.
164 * @error: (nullable): Return location for a #GError or %NULL.
165 *
166 * Returns: %TRUE on success, %FALSE if there was an error.
167 *
168 * Since: 0.8.0
169 */
170gboolean
171garrow_record_batch_writer_write_table(GArrowRecordBatchWriter *writer,
172 GArrowTable *table,
173 GError **error)
174{
175 auto arrow_writer = garrow_record_batch_writer_get_raw(writer);
176 auto arrow_table = garrow_table_get_raw(table);
177
178 auto status = arrow_writer->WriteTable(*arrow_table);
179 return garrow_error_check(error,
180 status,
181 "[record-batch-writer][write-table]");
182}
183
184/**
185 * garrow_record_batch_writer_close:
186 * @writer: A #GArrowRecordBatchWriter.
187 * @error: (nullable): Return location for a #GError or %NULL.
188 *
189 * Returns: %TRUE on success, %FALSE if there was an error.
190 *
191 * Since: 0.4.0
192 */
193gboolean
194garrow_record_batch_writer_close(GArrowRecordBatchWriter *writer,
195 GError **error)
196{
197 auto arrow_writer = garrow_record_batch_writer_get_raw(writer);
198
199 auto status = arrow_writer->Close();
200 return garrow_error_check(error, status, "[record-batch-writer][close]");
201}
202
203
204G_DEFINE_TYPE(GArrowRecordBatchStreamWriter,
205 garrow_record_batch_stream_writer,
206 GARROW_TYPE_RECORD_BATCH_WRITER);
207
208static void
209garrow_record_batch_stream_writer_init(GArrowRecordBatchStreamWriter *object)
210{
211}
212
213static void
214garrow_record_batch_stream_writer_class_init(GArrowRecordBatchStreamWriterClass *klass)
215{
216}
217
218/**
219 * garrow_record_batch_stream_writer_new:
220 * @sink: The output of the writer.
221 * @schema: The schema of the writer.
222 * @error: (nullable): Return location for a #GError or %NULL.
223 *
224 * Returns: (nullable): A newly created #GArrowRecordBatchStreamWriter
225 * or %NULL on error.
226 *
227 * Since: 0.4.0
228 */
229GArrowRecordBatchStreamWriter *
230garrow_record_batch_stream_writer_new(GArrowOutputStream *sink,
231 GArrowSchema *schema,
232 GError **error)
233{
234 auto arrow_sink = garrow_output_stream_get_raw(sink);
235 auto arrow_schema = garrow_schema_get_raw(schema);
236 auto arrow_writer_result =
237 arrow::ipc::MakeStreamWriter(arrow_sink, arrow_schema);
238 if (garrow::check(error,
239 arrow_writer_result,
240 "[record-batch-stream-writer][open]")) {
241 auto arrow_writer = *arrow_writer_result;
242 return garrow_record_batch_stream_writer_new_raw(&arrow_writer);
243 } else {
244 return NULL;
245 }
246}
247
248
249G_DEFINE_TYPE(GArrowRecordBatchFileWriter,
250 garrow_record_batch_file_writer,
251 GARROW_TYPE_RECORD_BATCH_STREAM_WRITER);
252
253static void
254garrow_record_batch_file_writer_init(GArrowRecordBatchFileWriter *object)
255{
256}
257
258static void
259garrow_record_batch_file_writer_class_init(GArrowRecordBatchFileWriterClass *klass)
260{
261}
262
263/**
264 * garrow_record_batch_file_writer_new:
265 * @sink: The output of the writer.
266 * @schema: The schema of the writer.
267 * @error: (nullable): Return location for a #GError or %NULL.
268 *
269 * Returns: (nullable): A newly created #GArrowRecordBatchFileWriter
270 * or %NULL on error.
271 *
272 * Since: 0.4.0
273 */
274GArrowRecordBatchFileWriter *
275garrow_record_batch_file_writer_new(GArrowOutputStream *sink,
276 GArrowSchema *schema,
277 GError **error)
278{
279 auto arrow_sink = garrow_output_stream_get_raw(sink);
280 auto arrow_schema = garrow_schema_get_raw(schema);
281 std::shared_ptr<arrow::ipc::RecordBatchWriter> arrow_writer;
282 auto arrow_writer_result =
283 arrow::ipc::MakeFileWriter(arrow_sink, arrow_schema);
284 if (garrow::check(error,
285 arrow_writer_result,
286 "[record-batch-file-writer][open]")) {
287 auto arrow_writer = *arrow_writer_result;
288 return garrow_record_batch_file_writer_new_raw(&arrow_writer);
289 } else {
290 return NULL;
291 }
292}
293
294G_END_DECLS
295
296GArrowRecordBatchWriter *
297garrow_record_batch_writer_new_raw(std::shared_ptr<arrow::ipc::RecordBatchWriter> *arrow_writer)
298{
299 auto writer =
300 GARROW_RECORD_BATCH_WRITER(
301 g_object_new(GARROW_TYPE_RECORD_BATCH_WRITER,
302 "record-batch-writer", arrow_writer,
303 NULL));
304 return writer;
305}
306
307std::shared_ptr<arrow::ipc::RecordBatchWriter>
308garrow_record_batch_writer_get_raw(GArrowRecordBatchWriter *writer)
309{
310 auto priv = GARROW_RECORD_BATCH_WRITER_GET_PRIVATE(writer);
311 return priv->record_batch_writer;
312}
313
314GArrowRecordBatchStreamWriter *
315garrow_record_batch_stream_writer_new_raw(std::shared_ptr<arrow::ipc::RecordBatchWriter> *arrow_writer)
316{
317 auto writer =
318 GARROW_RECORD_BATCH_STREAM_WRITER(
319 g_object_new(GARROW_TYPE_RECORD_BATCH_STREAM_WRITER,
320 "record-batch-writer", arrow_writer,
321 NULL));
322 return writer;
323}
324
325GArrowRecordBatchFileWriter *
326garrow_record_batch_file_writer_new_raw(std::shared_ptr<arrow::ipc::RecordBatchWriter> *arrow_writer)
327{
328 auto writer =
329 GARROW_RECORD_BATCH_FILE_WRITER(
330 g_object_new(GARROW_TYPE_RECORD_BATCH_FILE_WRITER,
331 "record-batch-writer", arrow_writer,
332 NULL));
333 return writer;
334}