]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/c_glib/arrow-flight-glib/client.cpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / c_glib / arrow-flight-glib / client.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/arrow-glib.hpp>
21
22 #include <arrow-flight-glib/client.hpp>
23 #include <arrow-flight-glib/common.hpp>
24
25 G_BEGIN_DECLS
26
27 /**
28 * SECTION: client
29 * @section_id: client
30 * @title: Client related classes
31 * @include: arrow-flight-glib/arrow-flight-glib.h
32 *
33 * #GAFlightStreamReader is a class for reading record batches from a
34 * server.
35 *
36 * #GAFlightCallOptions is a class for options of each call.
37 *
38 * #GAFlightClientOptions is a class for options of each client.
39 *
40 * #GAFlightClient is a class for Apache Arrow Flight client.
41 *
42 * Since: 5.0.0
43 */
44
45 G_DEFINE_TYPE(GAFlightStreamReader,
46 gaflight_stream_reader,
47 GAFLIGHT_TYPE_RECORD_BATCH_READER)
48
49 static void
50 gaflight_stream_reader_init(GAFlightStreamReader *object)
51 {
52 }
53
54 static void
55 gaflight_stream_reader_class_init(GAFlightStreamReaderClass *klass)
56 {
57 }
58
59 typedef struct GAFlightCallOptionsPrivate_ {
60 arrow::flight::FlightCallOptions options;
61 } GAFlightCallOptionsPrivate;
62
63 G_DEFINE_TYPE_WITH_PRIVATE(GAFlightCallOptions,
64 gaflight_call_options,
65 G_TYPE_OBJECT)
66
67 #define GAFLIGHT_CALL_OPTIONS_GET_PRIVATE(obj) \
68 static_cast<GAFlightCallOptionsPrivate *>( \
69 gaflight_call_options_get_instance_private( \
70 GAFLIGHT_CALL_OPTIONS(obj)))
71
72 static void
73 gaflight_call_options_finalize(GObject *object)
74 {
75 auto priv = GAFLIGHT_CALL_OPTIONS_GET_PRIVATE(object);
76
77 priv->options.~FlightCallOptions();
78
79 G_OBJECT_CLASS(gaflight_call_options_parent_class)->finalize(object);
80 }
81
82 static void
83 gaflight_call_options_init(GAFlightCallOptions *object)
84 {
85 auto priv = GAFLIGHT_CALL_OPTIONS_GET_PRIVATE(object);
86 new(&priv->options) arrow::flight::FlightCallOptions;
87 }
88
89 static void
90 gaflight_call_options_class_init(GAFlightCallOptionsClass *klass)
91 {
92 auto gobject_class = G_OBJECT_CLASS(klass);
93
94 gobject_class->finalize = gaflight_call_options_finalize;
95 }
96
97 /**
98 * gaflight_call_options_new:
99 *
100 * Returns: The newly created options for a call.
101 *
102 * Since: 5.0.0
103 */
104 GAFlightCallOptions *
105 gaflight_call_options_new(void)
106 {
107 return static_cast<GAFlightCallOptions *>(
108 g_object_new(GAFLIGHT_TYPE_CALL_OPTIONS, NULL));
109 }
110
111
112 typedef struct GAFlightClientOptionsPrivate_ {
113 arrow::flight::FlightClientOptions options;
114 } GAFlightClientOptionsPrivate;
115
116 G_DEFINE_TYPE_WITH_PRIVATE(GAFlightClientOptions,
117 gaflight_client_options,
118 G_TYPE_OBJECT)
119
120 #define GAFLIGHT_CLIENT_OPTIONS_GET_PRIVATE(obj) \
121 static_cast<GAFlightClientOptionsPrivate *>( \
122 gaflight_client_options_get_instance_private( \
123 GAFLIGHT_CLIENT_OPTIONS(obj)))
124
125 static void
126 gaflight_client_options_finalize(GObject *object)
127 {
128 auto priv = GAFLIGHT_CLIENT_OPTIONS_GET_PRIVATE(object);
129
130 priv->options.~FlightClientOptions();
131
132 G_OBJECT_CLASS(gaflight_client_options_parent_class)->finalize(object);
133 }
134
135 static void
136 gaflight_client_options_init(GAFlightClientOptions *object)
137 {
138 auto priv = GAFLIGHT_CLIENT_OPTIONS_GET_PRIVATE(object);
139 new(&(priv->options)) arrow::flight::FlightClientOptions;
140 priv->options = arrow::flight::FlightClientOptions::Defaults();
141 }
142
143 static void
144 gaflight_client_options_class_init(GAFlightClientOptionsClass *klass)
145 {
146 auto gobject_class = G_OBJECT_CLASS(klass);
147
148 gobject_class->finalize = gaflight_client_options_finalize;
149 }
150
151 /**
152 * gaflight_client_options_new:
153 *
154 * Returns: The newly created options for a client.
155 *
156 * Since: 5.0.0
157 */
158 GAFlightClientOptions *
159 gaflight_client_options_new(void)
160 {
161 return static_cast<GAFlightClientOptions *>(
162 g_object_new(GAFLIGHT_TYPE_CLIENT_OPTIONS, NULL));
163 }
164
165
166 typedef struct GAFlightClientPrivate_ {
167 arrow::flight::FlightClient *client;
168 } GAFlightClientPrivate;
169
170 enum {
171 PROP_CLIENT = 1,
172 };
173
174 G_DEFINE_TYPE_WITH_PRIVATE(GAFlightClient,
175 gaflight_client,
176 G_TYPE_OBJECT)
177
178 #define GAFLIGHT_CLIENT_GET_PRIVATE(obj) \
179 static_cast<GAFlightClientPrivate *>( \
180 gaflight_client_get_instance_private( \
181 GAFLIGHT_CLIENT(obj)))
182
183 static void
184 gaflight_client_finalize(GObject *object)
185 {
186 auto priv = GAFLIGHT_CLIENT_GET_PRIVATE(object);
187
188 delete priv->client;
189
190 G_OBJECT_CLASS(gaflight_client_parent_class)->finalize(object);
191 }
192
193 static void
194 gaflight_client_set_property(GObject *object,
195 guint prop_id,
196 const GValue *value,
197 GParamSpec *pspec)
198 {
199 auto priv = GAFLIGHT_CLIENT_GET_PRIVATE(object);
200
201 switch (prop_id) {
202 case PROP_CLIENT:
203 priv->client =
204 static_cast<arrow::flight::FlightClient *>(g_value_get_pointer(value));
205 break;
206 default:
207 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
208 break;
209 }
210 }
211
212 static void
213 gaflight_client_init(GAFlightClient *object)
214 {
215 }
216
217 static void
218 gaflight_client_class_init(GAFlightClientClass *klass)
219 {
220 auto gobject_class = G_OBJECT_CLASS(klass);
221
222 gobject_class->finalize = gaflight_client_finalize;
223 gobject_class->set_property = gaflight_client_set_property;
224
225 GParamSpec *spec;
226 spec = g_param_spec_pointer("client",
227 "Client",
228 "The raw arrow::flight::FlightClient *",
229 static_cast<GParamFlags>(G_PARAM_WRITABLE |
230 G_PARAM_CONSTRUCT_ONLY));
231 g_object_class_install_property(gobject_class, PROP_CLIENT, spec);
232 }
233
234 /**
235 * gaflight_client_new:
236 * @location: A #GAFlightLocation to be connected.
237 * @options: (nullable): A #GAFlightClientOptions.
238 * @error: (nullable): Return location for a #GError or %NULL.
239 *
240 * Returns: (nullable): The newly created client, %NULL on error.
241 *
242 * Since: 5.0.0
243 */
244 GAFlightClient *
245 gaflight_client_new(GAFlightLocation *location,
246 GAFlightClientOptions *options,
247 GError **error)
248 {
249 const auto flight_location = gaflight_location_get_raw(location);
250 std::unique_ptr<arrow::flight::FlightClient> flight_client;
251 arrow::Status status;
252 if (options) {
253 const auto flight_options = gaflight_client_options_get_raw(options);
254 status = arrow::flight::FlightClient::Connect(*flight_location,
255 *flight_options,
256 &flight_client);
257 } else {
258 status = arrow::flight::FlightClient::Connect(*flight_location,
259 &flight_client);
260 }
261 if (garrow::check(error, status, "[flight-client][new]")) {
262 return gaflight_client_new_raw(flight_client.release());
263 } else {
264 return NULL;
265 }
266 }
267
268 /**
269 * gaflight_client_list_flights:
270 * @client: A #GAFlightClient.
271 * @criteria: (nullable): A #GAFlightCriteria.
272 * @options: (nullable): A #GAFlightCallOptions.
273 * @error: (nullable): Return location for a #GError or %NULL.
274 *
275 * Returns: (nullable) (element-type GAFlightInfo) (transfer full):
276 * The returned list of #GAFlightInfo on success, %NULL on error.
277 *
278 * Since: 5.0.0
279 */
280 GList *
281 gaflight_client_list_flights(GAFlightClient *client,
282 GAFlightCriteria *criteria,
283 GAFlightCallOptions *options,
284 GError **error)
285 {
286 auto flight_client = gaflight_client_get_raw(client);
287 arrow::flight::Criteria flight_default_criteria;
288 auto flight_criteria = &flight_default_criteria;
289 if (criteria) {
290 flight_criteria = gaflight_criteria_get_raw(criteria);
291 }
292 arrow::flight::FlightCallOptions flight_default_options;
293 auto flight_options = &flight_default_options;
294 if (options) {
295 flight_options = gaflight_call_options_get_raw(options);
296 }
297 std::unique_ptr<arrow::flight::FlightListing> flight_listing;
298 auto status = flight_client->ListFlights(*flight_options,
299 *flight_criteria,
300 &flight_listing);
301 if (!garrow::check(error,
302 status,
303 "[flight-client][list-flights]")) {
304 return NULL;
305 }
306 GList *listing = NULL;
307 std::unique_ptr<arrow::flight::FlightInfo> flight_info;
308 while (true) {
309 status = flight_listing->Next(&flight_info);
310 if (!garrow::check(error,
311 status,
312 "[flight-client][list-flights]")) {
313 g_list_free_full(listing, g_object_unref);
314 return NULL;
315 }
316 if (!flight_info) {
317 break;
318 }
319 auto info = gaflight_info_new_raw(flight_info.release());
320 listing = g_list_prepend(listing, info);
321 }
322 return g_list_reverse(listing);
323 }
324
325 /**
326 * gaflight_client_do_get:
327 * @client: A #GAFlightClient.
328 * @ticket: A #GAFlightTicket.
329 * @options: (nullable): A #GAFlightCallOptions.
330 * @error: (nullable): Return location for a #GError or %NULL.
331 *
332 * Returns: (nullable) (transfer full):
333 * The #GAFlightStreamReader to read record batched from the server
334 * on success, %NULL on error.
335 *
336 * Since: 6.0.0
337 */
338 GAFlightStreamReader *
339 gaflight_client_do_get(GAFlightClient *client,
340 GAFlightTicket *ticket,
341 GAFlightCallOptions *options,
342 GError **error)
343 {
344 auto flight_client = gaflight_client_get_raw(client);
345 const auto flight_ticket = gaflight_ticket_get_raw(ticket);
346 arrow::flight::FlightCallOptions flight_default_options;
347 auto flight_options = &flight_default_options;
348 if (options) {
349 flight_options = gaflight_call_options_get_raw(options);
350 }
351 std::unique_ptr<arrow::flight::FlightStreamReader> flight_reader;
352 auto status = flight_client->DoGet(*flight_options,
353 *flight_ticket,
354 &flight_reader);
355 if (garrow::check(error,
356 status,
357 "[flight-client][do-get]")) {
358 return gaflight_stream_reader_new_raw(flight_reader.release());
359 } else {
360 return NULL;
361 }
362 }
363
364
365 G_END_DECLS
366
367
368 GAFlightStreamReader *
369 gaflight_stream_reader_new_raw(
370 arrow::flight::FlightStreamReader *flight_reader)
371 {
372 return GAFLIGHT_STREAM_READER(
373 g_object_new(GAFLIGHT_TYPE_STREAM_READER,
374 "reader", flight_reader,
375 NULL));
376 }
377
378 arrow::flight::FlightCallOptions *
379 gaflight_call_options_get_raw(GAFlightCallOptions *options)
380 {
381 auto priv = GAFLIGHT_CALL_OPTIONS_GET_PRIVATE(options);
382 return &(priv->options);
383 }
384
385 arrow::flight::FlightClientOptions *
386 gaflight_client_options_get_raw(GAFlightClientOptions *options)
387 {
388 auto priv = GAFLIGHT_CLIENT_OPTIONS_GET_PRIVATE(options);
389 return &(priv->options);
390 }
391
392 arrow::flight::FlightClient *
393 gaflight_client_get_raw(GAFlightClient *client)
394 {
395 auto priv = GAFLIGHT_CLIENT_GET_PRIVATE(client);
396 return priv->client;
397 }
398
399 GAFlightClient *
400 gaflight_client_new_raw(arrow::flight::FlightClient *flight_client)
401 {
402 return GAFLIGHT_CLIENT(g_object_new(GAFLIGHT_TYPE_CLIENT,
403 "client", flight_client,
404 NULL));
405 }