2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to you under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
* implied. See the License for the specific language governing
15
* permissions and limitations under the License.
18
#ifndef AVRO_CONSUMER_H
19
#define AVRO_CONSUMER_H
22
#define CLOSE_EXTERN }
27
#include <avro/platform.h>
31
#include <avro/schema.h>
34
/*---------------------------------------------------------------------
39
* A <i>consumer</i> is an object that knows how to process Avro data.
40
* There are consumer methods for each type of Avro data. The
41
* <code>avro_consumer_t</code> struct is an abstract superclass, which
42
* you don't instantiate directly. Later in this file, we define
43
* several consumer classes that know how to process Avro data in
46
* For compound Avro values (records, arrays, maps, and unions), the
47
* consumer callbacks provide a nested consumer that should be used to
48
* process subvalues. Each consumer instance, including these
49
* "subconsumers", contains a reference to the schema of the data that
50
* it expects to process. This means that the functions that produce
51
* Avro data (such as avro_consume_binary) don't need to maintain their
52
* own references to any schemas, since they'll be encapsulated in the
53
* consumer that they pass their data off to.
56
typedef struct avro_consumer_t avro_consumer_t;
58
struct avro_consumer_t {
60
* The schema of the data that this consumer expects to process.
66
* Called when this consumer is freed. This function should
67
* free any additional resources acquired by a consumer
71
void (*free)(avro_consumer_t *consumer);
73
/* PRIMITIVE VALUES */
76
* Called when a boolean value is encountered.
79
int (*boolean_value)(avro_consumer_t *consumer,
84
* Called when a bytes value is encountered. The @ref value
85
* pointer is only guaranteed to be valid for the duration of
86
* the callback function. If you need to save the data for
87
* processing later, you must copy it into another buffer.
90
int (*bytes_value)(avro_consumer_t *consumer,
91
const void *value, size_t value_len,
95
* Called when a double value is encountered.
98
int (*double_value)(avro_consumer_t *consumer,
103
* Called when a float value is encountered.
106
int (*float_value)(avro_consumer_t *consumer,
111
* Called when an int value is encountered.
114
int (*int_value)(avro_consumer_t *consumer,
119
* Called when a long value is encountered.
122
int (*long_value)(avro_consumer_t *consumer,
127
* Called when a null value is encountered.
130
int (*null_value)(avro_consumer_t *consumer, void *user_data);
133
* Called when a string value is encountered. The @ref value
134
* pointer will point at UTF-8 encoded data. (If the data
135
* you're representing isn't a UTF-8 Unicode string, you
136
* should use the bytes type.) The @ref value_len parameter
137
* gives the length of the data in bytes, not in Unicode
138
* characters. The @ref value pointer is only guaranteed to
139
* be valid for the duration of the callback function. If you
140
* need to save the data for processing later, you must copy
141
* it into another buffer.
144
int (*string_value)(avro_consumer_t *consumer,
145
const void *value, size_t value_len,
148
/* COMPOUND VALUES */
151
* Called when the beginning of an array block is encountered.
152
* The @ref block_count parameter will contain the number of
153
* elements in this block.
156
int (*array_start_block)(avro_consumer_t *consumer,
158
unsigned int block_count,
162
* Called before each individual element of an array is
163
* processed. The index of the current element is passed into
164
* the callback. The callback should fill in @ref
165
* element_consumer and @ref element_user_data with the consumer
166
* and <code>user_data</code> pointer to use to process the
170
int (*array_element)(avro_consumer_t *consumer,
172
avro_consumer_t **element_consumer,
173
void **element_user_data,
177
* Called when an enum value is encountered.
180
int (*enum_value)(avro_consumer_t *consumer, int value,
184
* Called when a fixed value is encountered. The @ref value
185
* pointer is only guaranteed to be valid for the duration of
186
* the callback function. If you need to save the data for
187
* processing later, you must copy it into another buffer.
190
int (*fixed_value)(avro_consumer_t *consumer,
191
const void *value, size_t value_len,
195
* Called when the beginning of a map block is encountered.
196
* The @ref block_count parameter will contain the number of
197
* elements in this block.
200
int (*map_start_block)(avro_consumer_t *consumer,
202
unsigned int block_count,
206
* Called before each individual element of a map is
207
* processed. The index and key of the current element is
208
* passed into the callback. The key is only guaranteed to be
209
* valid for the duration of the map_element_start callback,
210
* and the map's subschema callback. If you need to save it for
211
* later use, you must copy the key into another memory
212
* location. The callback should fill in @ref value_consumer
213
* and @ref value_user_data with the consumer and
214
* <code>user_data</code> pointer to use to process the value.
217
int (*map_element)(avro_consumer_t *consumer,
220
avro_consumer_t **value_consumer,
221
void **value_user_data,
225
* Called when the beginning of a record is encountered.
228
int (*record_start)(avro_consumer_t *consumer,
232
* Called before each individual field of a record is
233
* processed. The index and name of the current field is
234
* passed into the callback. The name is only guaranteed to
235
* be valid for the duration of the record_field_start
236
* callback, and the field's subschema callback. If you need to
237
* save it for later use, you must copy the key into another
238
* memory location. The callback should fill in @ref
239
* field_consumer and @ref field_user_data with the consumer
240
* <code>user_data</code> pointer to use to process the field.
243
int (*record_field)(avro_consumer_t *consumer,
245
avro_consumer_t **field_consumer,
246
void **field_user_data,
250
* Called when a union value is encountered. The callback
251
* should fill in @ref branch_consumer and @ref branch_user_data
252
* with the consumer <code>user_data</code> pointer to use to
253
* process the branch.
256
int (*union_branch)(avro_consumer_t *consumer,
257
unsigned int discriminant,
258
avro_consumer_t **branch_consumer,
259
void **branch_user_data,
265
* Calls the given callback in consumer, if it's present. If the
266
* callback is NULL, it just returns a success code.
269
#define avro_consumer_call(consumer, callback, ...) \
270
(((consumer)->callback == NULL)? 0: \
271
(consumer)->callback((consumer), __VA_ARGS__))
275
* Frees an @ref avro_consumer_t instance. (This function works on
276
* consumer subclasses, too.)
279
void avro_consumer_free(avro_consumer_t *consumer);
282
/*---------------------------------------------------------------------
287
* A <i>resolver</i> is a special kind of consumer that knows how to
288
* implement Avro's schema resolution rules to translate between a
289
* writer schema and a reader schema. The consumer callbacks line up
290
* with the writer schema; as each element of the datum is produced, the
291
* resolver fills in the contents of an @ref avro_datum_t instance.
292
* (The datum is provided as the user_data when you use the consumer.)
296
avro_resolver_new(avro_schema_t writer_schema,
297
avro_schema_t reader_schema);
300
/*---------------------------------------------------------------------
305
* Reads an Avro datum from the given @ref avro_reader_t. As the
306
* datum is read, each portion of it is passed off to the appropriate
307
* callback in @ref consumer.
311
avro_consume_binary(avro_reader_t reader,
312
avro_consumer_t *consumer,