summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/avro/tests/test_avro_1906.c
blob: 7188aec04afb9c32096bd825566e588613c56511 (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
/*
 * 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
 *
 * https://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 <stdio.h>
#include <sys/stat.h>
#include "avro.h"

static const char *filename = "avro_file.dat";

static const char PERSON_SCHEMA[] =
	"{"
	"    \"type\":\"record\","
	"    \"name\":\"Person\","
	"    \"fields\": ["
	"        {\"name\": \"ab\", \"type\": \"int\"}"
	"    ]"
	"}";

static int read_data() {
	int rval;
	int records_read = 0;

	avro_file_reader_t reader;
	avro_value_iface_t *iface;
	avro_value_t value;

	fprintf(stderr, "\nReading...\n");

	rval = avro_file_reader(filename, &reader);

	if (rval) {
		fprintf(stderr, "Error: %s\n", avro_strerror());
		return -1;
	}

	avro_schema_t schema = avro_file_reader_get_writer_schema(reader);

	iface = avro_generic_class_from_schema(schema);
	avro_generic_value_new(iface, &value);

	while ((rval = avro_file_reader_read_value(reader, &value)) == 0) {
		avro_value_t field;
		int32_t val;
		avro_value_get_by_index(&value, 0, &field, NULL);
		avro_value_get_int(&field, &val);
		fprintf(stderr, "value = %d\n", val);
		records_read++;
		avro_value_reset(&value);
	}

	avro_value_decref(&value);
	avro_value_iface_decref(iface);
	avro_schema_decref(schema);
	avro_file_reader_close(reader);

	fprintf(stderr, "read %d records.\n", records_read);

	if (rval != EOF) {
		fprintf(stderr, "Error: %s\n", avro_strerror());
		return -1;
	}

	return records_read;
}

static int read_data_datum() {
	int rval;
	int records_read = 0;

	avro_file_reader_t reader;
	avro_datum_t datum;

	fprintf(stderr, "\nReading...\n");

	rval = avro_file_reader(filename, &reader);

	if (rval) {
		fprintf(stderr, "Error using 'datum': %s\n", avro_strerror());
		return -1;
	}

	avro_schema_t schema = avro_file_reader_get_writer_schema(reader);

	while ((rval = avro_file_reader_read(reader, schema, &datum)) == 0) {
		avro_datum_t val_datum;
		int32_t val;
		if (avro_record_get(datum, "ab", &val_datum)) {
			fprintf(stderr, "Error getting value: %s\n", avro_strerror());
			return -1;
		}
		avro_int32_get(val_datum, &val);
		fprintf(stderr, "value = %d\n", val);
		records_read++;
		avro_datum_decref(datum);
	}

	avro_schema_decref(schema);
	avro_file_reader_close(reader);

	fprintf(stderr, "read %d records using 'datum'.\n", records_read);

	if (rval != EOF) {
		fprintf(stderr, "Error using 'datum': %s\n", avro_strerror());
		return -1;
	}

	return records_read;
}

static int write_data(int n_records) {
	int  i;
	avro_schema_t schema;
	avro_schema_error_t error;
	avro_file_writer_t writer;
	avro_value_iface_t *iface;
	avro_value_t value;

	fprintf(stderr, "\nWriting...\n");

	if (avro_schema_from_json(PERSON_SCHEMA, 0, &schema, &error)) {
		fprintf(stderr, "Unable to parse schema\n");
		return -1;
	}

	if (avro_file_writer_create(filename, schema, &writer)) {
		fprintf(stderr, "There was an error creating file: %s\n", avro_strerror());
		return -1;
	}

	iface = avro_generic_class_from_schema(schema);
	avro_generic_value_new(iface, &value);

	avro_value_t field;

	avro_value_get_by_index(&value, 0, &field, NULL);
	avro_value_set_int(&field, 123);

	for (i = 0; i < n_records; i++) {
		if (avro_file_writer_append_value(writer, &value)) {
			fprintf(stderr, "There was an error writing file: %s\n", avro_strerror());
			return -1;
		}
	}

	if (avro_file_writer_close(writer)) {
		fprintf(stderr, "There was an error creating file: %s\n", avro_strerror());
		return -1;
	}

	avro_value_decref(&value);
	avro_value_iface_decref(iface);
	avro_schema_decref(schema);

	return n_records;
}

static int test_n_records(int n_records) {
	int res = 0;

	if (write_data(n_records) != n_records) {
		remove(filename);
		return -1;
	}

	if (read_data() != n_records) {
		remove(filename);
		return -1;
	}

	if (read_data_datum() != n_records) {
		remove(filename);
		return -1;
	}

	remove(filename);
	return 0;
}

int main()
{
	if (test_n_records(1) < 0) {
		return EXIT_FAILURE;
	}

	if (test_n_records(0) < 0) {
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}