File: send-network.c

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (177 lines) | stat: -rw-r--r-- 5,930 bytes parent folder | download | duplicates (6)
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
/*
 * 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 <stdlib.h>

#include <arrow-glib/arrow-glib.h>

static GArrowSchema *
build_schema(void)
{
  GList *fields = NULL;
  GArrowBooleanDataType *boolean_data_type = garrow_boolean_data_type_new();
  fields =
    g_list_append(fields,
                  garrow_field_new("boolean", GARROW_DATA_TYPE(boolean_data_type)));
  GArrowInt32DataType *int32_data_type = garrow_int32_data_type_new();
  fields =
    g_list_append(fields, garrow_field_new("int32", GARROW_DATA_TYPE(int32_data_type)));
  GArrowSchema *schema = garrow_schema_new(fields);
  g_list_free_full(fields, g_object_unref);

  return schema;
}

static GArrowRecordBatch *
build_record_batch(void)
{
  GArrowSchema *schema = build_schema();
  if (!schema) {
    return NULL;
  }
  GError *error = NULL;
  GArrowRecordBatchBuilder *builder = garrow_record_batch_builder_new(schema, &error);
  g_object_unref(schema);
  if (!builder) {
    g_print("failed to build record batch builder: %s\n", error->message);
    g_error_free(error);
    return NULL;
  }

  const gint64 n_records = 3;
  GArrowBooleanArrayBuilder *boolean_builder = GARROW_BOOLEAN_ARRAY_BUILDER(
    garrow_record_batch_builder_get_column_builder(builder, 0));
  gboolean boolean_values[] = {TRUE, TRUE, FALSE};
  gboolean boolean_is_valids[] = {TRUE, FALSE, TRUE};
  if (!garrow_boolean_array_builder_append_values(boolean_builder,
                                                  boolean_values,
                                                  n_records,
                                                  boolean_is_valids,
                                                  n_records,
                                                  &error)) {
    g_print("failed to append boolean values: %s\n", error->message);
    g_error_free(error);
    g_object_unref(boolean_builder);
    g_object_unref(builder);
    return NULL;
  }

  GArrowInt32ArrayBuilder *int32_builder = GARROW_INT32_ARRAY_BUILDER(
    garrow_record_batch_builder_get_column_builder(builder, 1));
  gint32 int32_values[] = {1, 11, 111};
  gint32 int32_is_valids[] = {FALSE, TRUE, TRUE};
  if (!garrow_int32_array_builder_append_values(int32_builder,
                                                int32_values,
                                                n_records,
                                                int32_is_valids,
                                                n_records,
                                                &error)) {
    g_print("failed to append int32 values: %s\n", error->message);
    g_error_free(error);
    g_object_unref(int32_builder);
    g_object_unref(builder);
    return NULL;
  }

  GArrowRecordBatch *record_batch = garrow_record_batch_builder_flush(builder, &error);
  if (!record_batch) {
    g_print("failed to build record batch: %s\n", error->message);
    g_error_free(error);
    g_object_unref(builder);
    return NULL;
  }

  g_object_unref(builder);

  return record_batch;
}

int
main(int argc, char **argv)
{
  if (argc != 2) {
    g_print("Usage: %s PORT\n", argv[0]);
    g_print(" e.g.: %s 2929\n", argv[0]);
    return EXIT_FAILURE;
  }

  guint port = atoi(argv[1]);

  GSocketClient *client = g_socket_client_new();
  GSocketAddress *address = g_inet_socket_address_new_from_string("127.0.0.1", port);
  GError *error = NULL;
  GSocketConnection *connection =
    g_socket_client_connect(client, G_SOCKET_CONNECTABLE(address), NULL, &error);
  if (!connection) {
    g_print("failed to connect: %s\n", error->message);
    g_error_free(error);
    return EXIT_FAILURE;
  }

  GArrowSchema *schema = build_schema();
  if (!schema) {
    return EXIT_FAILURE;
  }
  GArrowGIOOutputStream *output =
    garrow_gio_output_stream_new(g_io_stream_get_output_stream(G_IO_STREAM(connection)));
  GArrowRecordBatchStreamWriter *writer =
    garrow_record_batch_stream_writer_new(GARROW_OUTPUT_STREAM(output), schema, &error);
  g_object_unref(schema);
  if (!writer) {
    g_print("failed to create writer: %s\n", error->message);
    g_error_free(error);
    g_object_unref(output);
    g_object_unref(connection);
    g_object_unref(client);
    return EXIT_FAILURE;
  }

  gsize n_record_batches = 5;
  gsize i;
  for (i = 0; i < n_record_batches; i++) {
    GArrowRecordBatch *record_batch = build_record_batch();
    if (!record_batch) {
      g_object_unref(writer);
      g_object_unref(output);
      g_object_unref(connection);
      g_object_unref(client);
      return EXIT_FAILURE;
    }
    gboolean success =
      garrow_record_batch_writer_write_record_batch(GARROW_RECORD_BATCH_WRITER(writer),
                                                    record_batch,
                                                    &error);
    g_object_unref(record_batch);
    if (!success) {
      g_print("failed to write record batch: %s\n", error->message);
      g_error_free(error);
      g_object_unref(output);
      g_object_unref(connection);
      g_object_unref(client);
      return EXIT_FAILURE;
    }
  }

  g_object_unref(writer);
  g_object_unref(output);
  g_object_unref(connection);
  g_object_unref(client);

  return EXIT_SUCCESS;
}