| 1 |
Avro C |
| 2 |
====== |
| 3 |
|
| 4 |
The current version of Avro is +{avro_version}+. The current version of +libavro+ is +{libavro_version}+. |
| 5 |
This document was created +{docdate}+. |
| 6 |
|
| 7 |
== Introduction to Avro |
| 8 |
|
| 9 |
Avro is a data serialization system. |
| 10 |
|
| 11 |
Avro provides: |
| 12 |
|
| 13 |
* Rich data structures. |
| 14 |
* A compact, fast, binary data format. |
| 15 |
* A container file, to store persistent data. |
| 16 |
* Remote procedure call (RPC). |
| 17 |
|
| 18 |
This document will focus on the C implementation of Avro. To learn more about |
| 19 |
Avro in general, https://avro.apache.org/[visit the Avro website]. |
| 20 |
|
| 21 |
== Introduction to Avro C |
| 22 |
|
| 23 |
.... |
| 24 |
___ ______ |
| 25 |
/ |_ ___________ / ____/ |
| 26 |
/ /| | | / / ___/ __ \ / / |
| 27 |
/ ___ | |/ / / / /_/ / / /___ |
| 28 |
/_/ |_|___/_/ \____/ \____/ |
| 29 |
|
| 30 |
.... |
| 31 |
|
| 32 |
[quote,Waldi Ravens,(walra%moacs11 @ nl.net) 94/03/18] |
| 33 |
____ |
| 34 |
A C program is like a fast dance on a newly waxed dance floor by people carrying razors. |
| 35 |
____ |
| 36 |
|
| 37 |
The C implementation has been tested on +MacOSX+ and +Linux+ but, over |
| 38 |
time, the number of support OSes should grow. Please let us know if |
| 39 |
you're using +Avro C+ on other systems. |
| 40 |
|
| 41 |
Avro depends on the http://www.digip.org/jansson/[Jansson JSON parser], |
| 42 |
version 2.3 or higher. On many operating systems this library is |
| 43 |
available through your package manager (for example, |
| 44 |
+apt-get install libjansson-dev+ on Ubuntu/Debian, and |
| 45 |
+brew install jansson+ on Mac OS). If not, please download and install |
| 46 |
it from source. |
| 47 |
|
| 48 |
The C implementation supports: |
| 49 |
|
| 50 |
* binary encoding/decoding of all primitive and complex data types |
| 51 |
* storage to an Avro Object Container File |
| 52 |
* schema resolution, promotion and projection |
| 53 |
* validating and non-validating mode for writing Avro data |
| 54 |
|
| 55 |
The C implementation is lacking: |
| 56 |
|
| 57 |
* RPC |
| 58 |
|
| 59 |
To learn about the API, take a look at the examples and reference files |
| 60 |
later in this document. |
| 61 |
|
| 62 |
We're always looking for contributions so, if you're a C hacker, please |
| 63 |
feel free to https://avro.apache.org/[submit patches to the project]. |
| 64 |
|
| 65 |
|
| 66 |
== Error reporting |
| 67 |
|
| 68 |
Most functions in the Avro C library return a single +int+ status code. |
| 69 |
Following the POSIX _errno.h_ convention, a status code of 0 indicates |
| 70 |
success. Non-zero codes indicate an error condition. Some functions |
| 71 |
return a pointer value instead of an +int+ status code; for these |
| 72 |
functions, a +NULL+ pointer indicates an error. |
| 73 |
|
| 74 |
You can retrieve |
| 75 |
a string description of the most recent error using the +avro_strerror+ |
| 76 |
function: |
| 77 |
|
| 78 |
[source,c] |
| 79 |
---- |
| 80 |
avro_schema_t schema = avro_schema_string(); |
| 81 |
if (schema == NULL) { |
| 82 |
fprintf(stderr, "Error was %s\n", avro_strerror()); |
| 83 |
} |
| 84 |
---- |
| 85 |
|
| 86 |
|
| 87 |
== Avro values |
| 88 |
|
| 89 |
Starting with version 1.6.0, the Avro C library has a new API for |
| 90 |
handling Avro data. To help distinguish between the two APIs, we refer |
| 91 |
to the old one as the _legacy_ or _datum_ API, and the new one as the |
| 92 |
_value_ API. (These names come from the names of the C types used to |
| 93 |
represent Avro data in the corresponding API — +avro_datum_t+ and |
| 94 |
+avro_value_t+.) The legacy API is still present, but it's deprecated — |
| 95 |
you shouldn't use the +avro_datum_t+ type or the +avro_datum_*+ |
| 96 |
functions in new code. |
| 97 |
|
| 98 |
One main benefit of the new value API is that you can treat any existing |
| 99 |
C type as an Avro value; you just have to provide a custom |
| 100 |
implementation of the value interface. In addition, we provide a |
| 101 |
_generic_ value implementation; “generic”, in this sense, meaning that |
| 102 |
this single implementation works for instances of any Avro schema type. |
| 103 |
Finally, we also provide a wrapper implementation for the deprecated |
| 104 |
+avro_datum_t+ type, which lets you gradually transition to the new |
| 105 |
value API. |
| 106 |
|
| 107 |
|
| 108 |
=== Avro value interface |
| 109 |
|
| 110 |
You interact with Avro values using the _value interface_, which defines |
| 111 |
methods for setting and retrieving the contents of an Avro value. An |
| 112 |
individual value is represented by an instance of the +avro_value_t+ |
| 113 |
type. |
| 114 |
|
| 115 |
This section provides an overview of the methods that you can call on an |
| 116 |
+avro_value_t+ instance. There are quite a few methods in the value |
| 117 |
interface, but not all of them make sense for all Avro schema types. |
| 118 |
For instance, you won't be able to call +avro_value_set_boolean+ on an |
| 119 |
Avro array value. If you try to call an inappropriate method, we'll |
| 120 |
return an +EINVAL+/+AVRO_INVALID+ error code. |
| 121 |
|
| 122 |
Note that the functions in this section apply to _all_ Avro values, |
| 123 |
regardless of which value implementation is used under the covers. This |
| 124 |
section doesn't describe how to _create_ value instances, since those |
| 125 |
constructors will be specific to a particular value implementation. |
| 126 |
|
| 127 |
|
| 128 |
==== Common methods |
| 129 |
|
| 130 |
There are a handful of methods that can be used with any value, |
| 131 |
regardless of which Avro schema it's an instance of: |
| 132 |
|
| 133 |
[source,c] |
| 134 |
---- |
| 135 |
#include <stdint.h> |
| 136 |
#include <avro.h> |
| 137 |
|
| 138 |
avro_type_t avro_value_get_type(const avro_value_t *value); |
| 139 |
avro_schema_t avro_value_get_schema(const avro_value_t *value); |
| 140 |
|
| 141 |
int avro_value_equal(const avro_value_t *v1, const avro_value_t *v2); |
| 142 |
int avro_value_equal_fast(const avro_value_t *v1, const avro_value_t *v2); |
| 143 |
|
| 144 |
int avro_value_copy(avro_value_t *dest, const avro_value_t *src); |
| 145 |
int avro_value_copy_fast(avro_value_t *dest, const avro_value_t *src); |
| 146 |
|
| 147 |
uint32_t avro_value_hash(avro_value_t *value); |
| 148 |
|
| 149 |
int avro_value_reset(avro_value_t *value); |
| 150 |
---- |
| 151 |
|
| 152 |
The +get_type+ and +get_schema+ methods can be used to get information |
| 153 |
about what kind of Avro value a given +avro_value_t+ instance |
| 154 |
represents. (For +get_schema+, you borrow the value's reference to the |
| 155 |
schema; if you need to save it and ensure that it outlives the value, |
| 156 |
you need to call +avro_schema_incref+ on it.) |
| 157 |
|
| 158 |
The +equal+ and +equal_fast+ methods compare two values for equality. |
| 159 |
The two values do _not_ have to have the same value implementations, but |
| 160 |
they _do_ have to be instances of the same schema. (Not _equivalent_ |
| 161 |
schemas; the _same_ schema.) The +equal+ method checks that the schemas |
| 162 |
match; the +equal_fast+ method assumes that they do. |
| 163 |
|
| 164 |
The +copy+ and +copy_fast+ methods copy the contents of one Avro value |
| 165 |
into another. (Where possible, this is done without copying the actual |
| 166 |
content of a +bytes+, +string+, or +fixed+ value, using the |
| 167 |
+avro_wrapped_buffer_t+ functions described in the next section.) Like |
| 168 |
+equal+, the two values must have the same schema; +copy+ checks this, |
| 169 |
while +copy_fast+ assumes it. |
| 170 |
|
| 171 |
The +hash+ method returns a hash value for the given Avro value. This |
| 172 |
can be used to construct hash tables that use Avro values as keys. The |
| 173 |
function works correctly even with maps; it produces a hash that doesn't |
| 174 |
depend on the ordering of the elements of the map. Hash values are only |
| 175 |
meaningful for comparing values of exactly the same schema. Hash values |
| 176 |
are _not_ guaranteed to be consistent across different platforms, or |
| 177 |
different versions of the Avro library. That means that it's really |
| 178 |
only safe to use these hash values internally within the context of a |
| 179 |
single execution of a single application. |
| 180 |
|
| 181 |
The +reset+ method “clears out” an +avro_value_t+ instance, making sure |
| 182 |
that it's ready to accept the contents of a new value. For scalars, |
| 183 |
this is usually a no-op, since the new value will just overwrite the old |
| 184 |
one. For arrays and maps, this removes any existing elements from the |
| 185 |
container, so that we can append the elements of the new value. For |
| 186 |
records and unions, this just recursively resets the fields or current |
| 187 |
branch. |
| 188 |
|
| 189 |
|
| 190 |
==== Scalar values |
| 191 |
|
| 192 |
The simplest case is handling instances of the scalar Avro schema types. |
| 193 |
In Avro, the scalars are all of the primitive schema types, as well as |
| 194 |
+enum+ and +fixed+ — i.e., anything that can't contain another Avro |
| 195 |
value. Note that we use standard C99 types to represent the primitive |
| 196 |
contents of an Avro scalar. |
| 197 |
|
| 198 |
To retrieve the contents of an Avro scalar, you can use one of the |
| 199 |
_getter_ methods: |
| 200 |
|
| 201 |
[source,c] |
| 202 |
---- |
| 203 |
#include <stdint.h> |
| 204 |
#include <stdlib.h> |
| 205 |
#include <avro.h> |
| 206 |
|
| 207 |
int avro_value_get_boolean(const avro_value_t *value, int *dest); |
| 208 |
int avro_value_get_bytes(const avro_value_t *value, |
| 209 |
const void **dest, size_t *size); |
| 210 |
int avro_value_get_double(const avro_value_t *value, double *dest); |
| 211 |
int avro_value_get_float(const avro_value_t *value, float *dest); |
| 212 |
int avro_value_get_int(const avro_value_t *value, int32_t *dest); |
| 213 |
int avro_value_get_long(const avro_value_t *value, int64_t *dest); |
| 214 |
int avro_value_get_null(const avro_value_t *value); |
| 215 |
int avro_value_get_string(const avro_value_t *value, |
| 216 |
const char **dest, size_t *size); |
| 217 |
int avro_value_get_enum(const avro_value_t *value, int *dest); |
| 218 |
int avro_value_get_fixed(const avro_value_t *value, |
| 219 |
const void **dest, size_t *size); |
| 220 |
---- |
| 221 |
|
| 222 |
For the most part, these should be self-explanatory. For +bytes+, |
| 223 |
+string+, and +fixed+ values, the pointer to the underlying content is |
| 224 |
+const+ — you aren't allowed to modify the contents directly. We |
| 225 |
guarantee that the content of a +string+ will be NUL-terminated, so you |
| 226 |
can use it as a C string as you'd expect. The +size+ returned for a |
| 227 |
+string+ object will include the NUL terminator; it will be one more |
| 228 |
than you'd get from calling +strlen+ on the content. |
| 229 |
|
| 230 |
Also, for +bytes+, +string+, and +fixed+, the +dest+ and +size+ |
| 231 |
parameters are optional; if you only want to determine the length of a |
| 232 |
+bytes+ value, you can use: |
| 233 |
|
| 234 |
[source,c] |
| 235 |
---- |
| 236 |
avro_value_t *value = /* from somewhere */; |
| 237 |
size_t size; |
| 238 |
avro_value_get_bytes(value, NULL, &size); |
| 239 |
---- |
| 240 |
|
| 241 |
To set the contents of an Avro scalar, you can use one of the _setter_ |
| 242 |
methods: |
| 243 |
|
| 244 |
[source,c] |
| 245 |
---- |
| 246 |
#include <stdint.h> |
| 247 |
#include <stdlib.h> |
| 248 |
#include <avro.h> |
| 249 |
|
| 250 |
int avro_value_set_boolean(avro_value_t *value, int src); |
| 251 |
int avro_value_set_bytes(avro_value_t *value, |
| 252 |
void *buf, size_t size); |
| 253 |
int avro_value_set_double(avro_value_t *value, double src); |
| 254 |
int avro_value_set_float(avro_value_t *value, float src); |
| 255 |
int avro_value_set_int(avro_value_t *value, int32_t src); |
| 256 |
int avro_value_set_long(avro_value_t *value, int64_t src); |
| 257 |
int avro_value_set_null(avro_value_t *value); |
| 258 |
int avro_value_set_string(avro_value_t *value, const char *src); |
| 259 |
int avro_value_set_string_len(avro_value_t *value, |
| 260 |
const char *src, size_t size); |
| 261 |
int avro_value_set_enum(avro_value_t *value, int src); |
| 262 |
int avro_value_set_fixed(avro_value_t *value, |
| 263 |
void *buf, size_t size); |
| 264 |
---- |
| 265 |
|
| 266 |
These are also straightforward. For +bytes+, +string+, and +fixed+ |
| 267 |
values, the +set+ methods will make a copy of the underlying data. For |
| 268 |
+string+ values, the content must be NUL-terminated. You can use |
| 269 |
+set_string_len+ if you already know the length of the string content; |
| 270 |
the length you pass in should include the NUL terminator. If you call |
| 271 |
+set_string+, then we'll use +strlen+ to calculate the length. |
| 272 |
|
| 273 |
For +fixed+ values, the +size+ must match what's expected by the value's |
| 274 |
underlying +fixed+ schema; if the sizes don't match, you'll get an error |
| 275 |
code. |
| 276 |
|
| 277 |
If you don't want to copy the contents of a +bytes+, +string+, or |
| 278 |
+fixed+ value, you can use the _giver_ and _grabber_ functions: |
| 279 |
|
| 280 |
[source,c] |
| 281 |
---- |
| 282 |
#include <stdint.h> |
| 283 |
#include <stdlib.h> |
| 284 |
#include <avro.h> |
| 285 |
|
| 286 |
typedef void |
| 287 |
(*avro_buf_free_t)(void *ptr, size_t sz, void *user_data); |
| 288 |
|
| 289 |
int avro_value_give_bytes(avro_value_t *value, avro_wrapped_buffer_t *src); |
| 290 |
int avro_value_give_string_len(avro_value_t *value, avro_wrapped_buffer_t *src); |
| 291 |
int avro_value_give_fixed(avro_value_t *value, avro_wrapped_buffer_t *src); |
| 292 |
|
| 293 |
int avro_value_grab_bytes(const avro_value_t *value, avro_wrapped_buffer_t *dest); |
| 294 |
int avro_value_grab_string(const avro_value_t *value, avro_wrapped_buffer_t *dest); |
| 295 |
int avro_value_grab_fixed(const avro_value_t *value, avro_wrapped_buffer_t *dest); |
| 296 |
|
| 297 |
typedef struct avro_wrapped_buffer { |
| 298 |
const void *buf; |
| 299 |
size_t size; |
| 300 |
void (*free)(avro_wrapped_buffer_t *self); |
| 301 |
int (*copy)(avro_wrapped_buffer_t *dest, |
| 302 |
const avro_wrapped_buffer_t *src, |
| 303 |
size_t offset, size_t length); |
| 304 |
int (*slice)(avro_wrapped_buffer_t *self, |
| 305 |
size_t offset, size_t length); |
| 306 |
} avro_wrapped_buffer_t; |
| 307 |
|
| 308 |
void |
| 309 |
avro_wrapped_buffer_free(avro_wrapped_buffer_t *buf); |
| 310 |
|
| 311 |
int |
| 312 |
avro_wrapped_buffer_copy(avro_wrapped_buffer_t *dest, |
| 313 |
const avro_wrapped_buffer_t *src, |
| 314 |
size_t offset, size_t length); |
| 315 |
|
| 316 |
int |
| 317 |
avro_wrapped_buffer_slice(avro_wrapped_buffer_t *self, |
| 318 |
size_t offset, size_t length); |
| 319 |
---- |
| 320 |
|
| 321 |
The +give+ functions give control of an existing buffer to the value. |
| 322 |
(You should *not* try to free the +src+ wrapped buffer after calling |
| 323 |
this method.) The +grab+ function fills in a wrapped buffer with a |
| 324 |
pointer to the contents of an Avro value. (You *should* free the +dest+ |
| 325 |
wrapped buffer when you're done with it.) |
| 326 |
|
| 327 |
The +avro_wrapped_buffer_t+ struct encapsulates the location and size of |
| 328 |
the existing buffer. It also includes several methods. The +free+ |
| 329 |
method will be called when the content of the buffer is no longer |
| 330 |
needed. The +slice+ method will be called when the wrapped buffer needs |
| 331 |
to be updated to point at a subset of what it pointed at before. (This |
| 332 |
doesn't create a new wrapped buffer; it updates an existing one.) The |
| 333 |
+copy+ method will be called if the content needs to be copied. Note |
| 334 |
that if you're wrapping a buffer with nice reference counting features, |
| 335 |
you don't need to perform an actual copy; you just need to ensure that |
| 336 |
the +free+ function can be called on both the original and the copy, and |
| 337 |
not have things blow up. |
| 338 |
|
| 339 |
The “generic” value implementation takes advantage of this feature; if |
| 340 |
you pass in a wrapped buffer with a +give+ method, and then retrieve it |
| 341 |
later with a +grab+ method, then we'll use the wrapped buffer's +copy+ |
| 342 |
method to fill in the +dest+ parameter. If your wrapped buffer |
| 343 |
implements a +slice+ method that updates reference counts instead of |
| 344 |
actually copying, then you've got nice zero-copy access to the contents |
| 345 |
of an Avro value. |
| 346 |
|
| 347 |
|
| 348 |
==== Compound values |
| 349 |
|
| 350 |
The following sections describe the getter and setter methods for |
| 351 |
handling compound Avro values. All of the compound values are |
| 352 |
responsible for the storage of their children; this means that there |
| 353 |
isn't a method, for instance, that lets you add an existing |
| 354 |
+avro_value_t+ to an array. Instead, there's a method that creates a |
| 355 |
new, empty +avro_value_t+ of the appropriate type, adds it to the array, |
| 356 |
and returns it for you to fill in as needed. |
| 357 |
|
| 358 |
You also shouldn't try to free the child elements that are created this |
| 359 |
way; the container value is responsible for their life cycle. The child |
| 360 |
element is guaranteed to be valid for as long as the container value |
| 361 |
is. You'll usually define an +avro_value_t+ in the stack, and let it |
| 362 |
fall out of scope when you're done with it: |
| 363 |
|
| 364 |
[source,c] |
| 365 |
---- |
| 366 |
avro_value_t *array = /* from somewhere else */; |
| 367 |
|
| 368 |
{ |
| 369 |
avro_value_t child; |
| 370 |
avro_value_get_by_index(array, 0, &child, NULL); |
| 371 |
/* do something interesting with the array element */ |
| 372 |
} |
| 373 |
---- |
| 374 |
|
| 375 |
|
| 376 |
==== Arrays |
| 377 |
|
| 378 |
There are three methods that can be used with array values: |
| 379 |
|
| 380 |
[source,c] |
| 381 |
---- |
| 382 |
#include <stdlib.h> |
| 383 |
#include <avro.h> |
| 384 |
|
| 385 |
int avro_value_get_size(const avro_value_t *array, size_t *size); |
| 386 |
int avro_value_get_by_index(const avro_value_t *array, size_t index, |
| 387 |
avro_value_t *element, const char **unused); |
| 388 |
int avro_value_append(avro_value_t *array, avro_value_t *element, |
| 389 |
size_t *new_index); |
| 390 |
---- |
| 391 |
|
| 392 |
The +get_size+ method returns the number of elements currently in the |
| 393 |
array. The +get_by_index+ method fills in +element+ to point at the |
| 394 |
array element with the given index. (You should use +NULL+ for the |
| 395 |
+unused+ parameter; it's ignored for array values.) |
| 396 |
|
| 397 |
The +append+ method creates a new value, appends it to the array, and |
| 398 |
returns it in +element+. If +new_index+ is given, then it will be |
| 399 |
filled in with the index of the new element. |
| 400 |
|
| 401 |
|
| 402 |
==== Maps |
| 403 |
|
| 404 |
There are four methods that can be used with map values: |
| 405 |
|
| 406 |
[source,c] |
| 407 |
---- |
| 408 |
#include <stdlib.h> |
| 409 |
#include <avro.h> |
| 410 |
|
| 411 |
int avro_value_get_size(const avro_value_t *map, size_t *size); |
| 412 |
int avro_value_get_by_name(const avro_value_t *map, const char *key, |
| 413 |
avro_value_t *element, size_t *index); |
| 414 |
int avro_value_get_by_index(const avro_value_t *map, size_t index, |
| 415 |
avro_value_t *element, const char **key); |
| 416 |
int avro_value_add(avro_value_t *map, |
| 417 |
const char *key, avro_value_t *element, |
| 418 |
size_t *index, int *is_new); |
| 419 |
---- |
| 420 |
|
| 421 |
The +get_size+ method returns the number of elements currently in the |
| 422 |
map. Map elements can be retrieved either by their key (+get_by_name+) |
| 423 |
or by their numeric index (+get_by_index+). (Numeric indices in a map |
| 424 |
are based on the order that the elements were added to the map.) In |
| 425 |
either case, the method takes in an optional output parameter that let |
| 426 |
you retrieve the index associated with a key, and vice versa. |
| 427 |
|
| 428 |
The +add+ method will add a new value to the map, if the given key isn't |
| 429 |
already present. If the key is present, then the existing value with be |
| 430 |
returned. The +index+ parameter, if given, will be filled in the |
| 431 |
element's index. The +is_new+ parameter, if given, can be used to |
| 432 |
determine whether the mapped value is new or not. |
| 433 |
|
| 434 |
|
| 435 |
==== Records |
| 436 |
|
| 437 |
There are three methods that can be used with record values: |
| 438 |
|
| 439 |
[source,c] |
| 440 |
---- |
| 441 |
#include <stdlib.h> |
| 442 |
#include <avro.h> |
| 443 |
|
| 444 |
int avro_value_get_size(const avro_value_t *record, size_t *size); |
| 445 |
int avro_value_get_by_index(const avro_value_t *record, size_t index, |
| 446 |
avro_value_t *element, const char **field_name); |
| 447 |
int avro_value_get_by_name(const avro_value_t *record, const char *field_name, |
| 448 |
avro_value_t *element, size_t *index); |
| 449 |
---- |
| 450 |
|
| 451 |
The +get_size+ method returns the number of fields in the record. (You |
| 452 |
can also get this by querying the value's schema, but for some |
| 453 |
implementations, this method can be faster.) |
| 454 |
|
| 455 |
The +get_by_index+ and +get_by_name+ functions can be used to retrieve |
| 456 |
one of the fields in the record, either by its ordinal position within |
| 457 |
the record, or by the name of the underlying field. Like with maps, the |
| 458 |
methods take in an additional parameter that let you retrieve the index |
| 459 |
associated with a field name, and vice versa. |
| 460 |
|
| 461 |
When possible, it's recommended that you access record fields by their |
| 462 |
numeric index, rather than by their field name. For most |
| 463 |
implementations, this will be more efficient. |
| 464 |
|
| 465 |
|
| 466 |
==== Unions |
| 467 |
|
| 468 |
There are three methods that can be used with union values: |
| 469 |
|
| 470 |
[source,c] |
| 471 |
---- |
| 472 |
#include <avro.h> |
| 473 |
|
| 474 |
int avro_value_get_discriminant(const avro_value_t *union_val, int *disc); |
| 475 |
int avro_value_get_current_branch(const avro_value_t *union_val, avro_value_t *branch); |
| 476 |
int avro_value_set_branch(avro_value_t *union_val, |
| 477 |
int discriminant, avro_value_t *branch); |
| 478 |
---- |
| 479 |
|
| 480 |
The +get_discriminant+ and +get_current_branch+ methods return the |
| 481 |
current state of the union value, without modifying which branch is |
| 482 |
currently selected. The +set_branch+ method can be used to choose the |
| 483 |
active branch, filling in the +branch+ value to point at the branch's |
| 484 |
value instance. (Most implementations will be smart enough to detect |
| 485 |
when the desired branch is already selected, so you should always call |
| 486 |
this method unless you can _guarantee_ that the right branch is already |
| 487 |
current.) |
| 488 |
|
| 489 |
|
| 490 |
=== Creating value instances |
| 491 |
|
| 492 |
Okay, so we've described how to interact with a value that you already |
| 493 |
have a pointer to, but how do you create one in the first place? Each |
| 494 |
implementation of the value interface must provide its own functions for |
| 495 |
creating +avro_value_t+ instances for that class. The 10,000-foot view |
| 496 |
is to: |
| 497 |
|
| 498 |
1. Get an _implementation struct_ for the value implementation that you |
| 499 |
want to use. (This is represented by an +avro_value_iface_t+ |
| 500 |
pointer.) |
| 501 |
|
| 502 |
2. Use the implementation's constructor function to allocate instances |
| 503 |
of that value implementation. |
| 504 |
|
| 505 |
3. Do whatever you need to the value (using the +avro_value_t+ methods |
| 506 |
described in the previous section). |
| 507 |
|
| 508 |
4. Free the value instance, if necessary, using the implementation's |
| 509 |
destructor function. |
| 510 |
|
| 511 |
5. Free the implementation struct when you're done creating value |
| 512 |
instances. |
| 513 |
|
| 514 |
These steps use the following functions: |
| 515 |
|
| 516 |
[source,c] |
| 517 |
---- |
| 518 |
#include <avro.h> |
| 519 |
|
| 520 |
avro_value_iface_t *avro_value_iface_incref(avro_value_iface_t *iface); |
| 521 |
void avro_value_iface_decref(avro_value_iface_t *iface); |
| 522 |
---- |
| 523 |
|
| 524 |
Note that for most value implementations, it's fine to reuse a single |
| 525 |
+avro_value_t+ instance for multiple values, using the |
| 526 |
+avro_value_reset+ function before filling in the instance for each |
| 527 |
value. (This helps reduce the number of +malloc+ and +free+ calls that |
| 528 |
your application will make.) |
| 529 |
|
| 530 |
We provide a “generic” value implementation that will work (efficiently) |
| 531 |
for any Avro schema. |
| 532 |
|
| 533 |
|
| 534 |
For most applications, you won't need to write your own value |
| 535 |
implementation; the Avro C library provides an efficient “generic” |
| 536 |
implementation, which supports the full range of Avro schema types. |
| 537 |
There's a good chance that you just want to use this implementation, |
| 538 |
rather than rolling your own. (The primary reason for rolling your own |
| 539 |
would be if you want to access the elements of a compound value using C |
| 540 |
syntax — for instance, translating an Avro record into a C struct.) You |
| 541 |
can use the following functions to create and work with a generic value |
| 542 |
implementation for a particular schema: |
| 543 |
|
| 544 |
[source,c] |
| 545 |
---- |
| 546 |
#include <avro.h> |
| 547 |
|
| 548 |
avro_value_iface_t *avro_generic_class_from_schema(avro_schema_t schema); |
| 549 |
int avro_generic_value_new(const avro_value_iface_t *iface, avro_value_t *dest); |
| 550 |
void avro_generic_value_free(avro_value_t *self); |
| 551 |
---- |
| 552 |
|
| 553 |
Combining all of this together, you might have the following snippet of |
| 554 |
code: |
| 555 |
|
| 556 |
[source,c] |
| 557 |
---- |
| 558 |
avro_schema_t schema = avro_schema_long(); |
| 559 |
avro_value_iface_t *iface = avro_generic_class_from_schema(schema); |
| 560 |
|
| 561 |
avro_value_t val; |
| 562 |
avro_generic_value_new(iface, &val); |
| 563 |
|
| 564 |
/* Generate Avro longs from 0-499 */ |
| 565 |
int i; |
| 566 |
for (i = 0; i < 500; i++) { |
| 567 |
avro_value_reset(&val); |
| 568 |
avro_value_set_long(&val, i); |
| 569 |
/* do something with the value */ |
| 570 |
} |
| 571 |
|
| 572 |
avro_generic_value_free(&val); |
| 573 |
avro_value_iface_decref(iface); |
| 574 |
avro_schema_decref(schema); |
| 575 |
---- |
| 576 |
|
| 577 |
|
| 578 |
== Reference Counting |
| 579 |
|
| 580 |
+Avro C+ does reference counting for all schema and data objects. |
| 581 |
When the number of references drops to zero, the memory is freed. |
| 582 |
|
| 583 |
For example, to create and free a string, you would use: |
| 584 |
---- |
| 585 |
avro_datum_t string = avro_string("This is my string"); |
| 586 |
|
| 587 |
... |
| 588 |
avro_datum_decref(string); |
| 589 |
---- |
| 590 |
|
| 591 |
Things get a little more complicated when you consider more elaborate |
| 592 |
schema and data structures. |
| 593 |
|
| 594 |
For example, let's say that you create a record with a single |
| 595 |
string field: |
| 596 |
---- |
| 597 |
avro_datum_t example = avro_record("Example"); |
| 598 |
avro_datum_t solo_field = avro_string("Example field value"); |
| 599 |
|
| 600 |
avro_record_set(example, "solo", solo_field); |
| 601 |
|
| 602 |
... |
| 603 |
avro_datum_decref(example); |
| 604 |
---- |
| 605 |
|
| 606 |
In this example, the +solo_field+ datum would *not* be freed since it |
| 607 |
has two references: the original reference and a reference inside |
| 608 |
the +Example+ record. The +avro_datum_decref(example)+ call drops |
| 609 |
the number of reference to one. If you are finished with the +solo_field+ |
| 610 |
schema, then you need to +avro_schema_decref(solo_field)+ to |
| 611 |
completely dereference the +solo_field+ datum and free it. |
| 612 |
|
| 613 |
== Wrap It and Give It |
| 614 |
|
| 615 |
You'll notice that some datatypes can be "wrapped" and "given". This |
| 616 |
allows C programmers the freedom to decide who is responsible for |
| 617 |
the memory. Let's take strings for example. |
| 618 |
|
| 619 |
To create a string datum, you have three different methods: |
| 620 |
---- |
| 621 |
avro_datum_t avro_string(const char *str); |
| 622 |
avro_datum_t avro_wrapstring(const char *str); |
| 623 |
avro_datum_t avro_givestring(const char *str); |
| 624 |
---- |
| 625 |
|
| 626 |
If you use, +avro_string+ then +Avro C+ will make a copy of your |
| 627 |
string and free it when the datum is dereferenced. In some cases, |
| 628 |
especially when dealing with large amounts of data, you want |
| 629 |
to avoid this memory copy. That's where +avro_wrapstring+ and |
| 630 |
+avro_givestring+ can help. |
| 631 |
|
| 632 |
If you use, +avro_wrapstring+ then +Avro C+ will do no memory |
| 633 |
management at all. It will just save a pointer to your data and |
| 634 |
it's your responsibility to free the string. |
| 635 |
|
| 636 |
WARNING: When using +avro_wrapstring+, do not free the string |
| 637 |
before you dereference the string datum with +avro_datum_decref()+. |
| 638 |
|
| 639 |
Lastly, if you use +avro_givestring+ then +Avro C+ will free the |
| 640 |
string later when the datum is dereferenced. In a sense, you |
| 641 |
are "giving" responsibility for freeing the string to +Avro C+. |
| 642 |
|
| 643 |
[WARNING] |
| 644 |
=============================== |
| 645 |
Don't "give" +Avro C+ a string that you haven't allocated from the heap with e.g. +malloc+ or +strdup+. |
| 646 |
|
| 647 |
For example, *don't* do this: |
| 648 |
---- |
| 649 |
avro_datum_t bad_idea = avro_givestring("This isn't allocated on the heap"); |
| 650 |
---- |
| 651 |
=============================== |
| 652 |
|
| 653 |
== Schema Validation |
| 654 |
|
| 655 |
If you want to write a datum, you would use the following function |
| 656 |
|
| 657 |
[source,c] |
| 658 |
---- |
| 659 |
int avro_write_data(avro_writer_t writer, |
| 660 |
avro_schema_t writers_schema, avro_datum_t datum); |
| 661 |
---- |
| 662 |
|
| 663 |
If you pass in a +writers_schema+, then you +datum+ will be validated *before* |
| 664 |
it is sent to the +writer+. This check ensures that your data has the |
| 665 |
correct format. If you are certain your datum is correct, you can pass |
| 666 |
a +NULL+ value for +writers_schema+ and +Avro C+ will not validate before |
| 667 |
writing. |
| 668 |
|
| 669 |
NOTE: Data written to an Avro File Object Container is always validated. |
| 670 |
|
| 671 |
== Examples |
| 672 |
|
| 673 |
[quote,Dante Hicks] |
| 674 |
____ |
| 675 |
I'm not even supposed to be here today! |
| 676 |
____ |
| 677 |
|
| 678 |
Imagine you're a free-lance hacker in Leonardo, New Jersey and you've |
| 679 |
been approached by the owner of the local *Quick Stop Convenience* store. |
| 680 |
He wants you to create a contact database case he needs to call employees |
| 681 |
to work on their day off. |
| 682 |
|
| 683 |
You might build a simple contact system using Avro C like the following... |
| 684 |
|
| 685 |
[source,c] |
| 686 |
---- |
| 687 |
include::../examples/quickstop.c[] |
| 688 |
---- |
| 689 |
|
| 690 |
When you compile and run this program, you should get the following output |
| 691 |
|
| 692 |
---- |
| 693 |
Successfully added Hicks, Dante id=1 |
| 694 |
Successfully added Graves, Randal id=2 |
| 695 |
Successfully added Loughran, Veronica id=3 |
| 696 |
Successfully added Bree, Caitlin id=4 |
| 697 |
Successfully added Silent, Bob id=5 |
| 698 |
Successfully added ???, Jay id=6 |
| 699 |
|
| 700 |
Avro is compact. Here is the data for all 6 people. |
| 701 |
| 02 0A 44 61 6E 74 65 0A | 48 69 63 6B 73 1C 28 35 | ..Dante.Hicks.(5 |
| 702 |
| 35 35 29 20 31 32 33 2D | 34 35 36 37 40 04 0C 52 | 55) [email protected] |
| 703 |
| 61 6E 64 61 6C 0C 47 72 | 61 76 65 73 1C 28 35 35 | andal.Graves.(55 |
| 704 |
| 35 29 20 31 32 33 2D 35 | 36 37 38 3C 06 10 56 65 | 5) 123-5678<..Ve |
| 705 |
| 72 6F 6E 69 63 61 10 4C | 6F 75 67 68 72 61 6E 1C | ronica.Loughran. |
| 706 |
| 28 35 35 35 29 20 31 32 | 33 2D 30 39 38 37 38 08 | (555) 123-09878. |
| 707 |
| 0E 43 61 69 74 6C 69 6E | 08 42 72 65 65 1C 28 35 | .Caitlin.Bree.(5 |
| 708 |
| 35 35 29 20 31 32 33 2D | 32 33 32 33 36 0A 06 42 | 55) 123-23236..B |
| 709 |
| 6F 62 0C 53 69 6C 65 6E | 74 1C 28 35 35 35 29 20 | ob.Silent.(555) |
| 710 |
| 31 32 33 2D 36 34 32 32 | 3A 0C 06 4A 61 79 06 3F | 123-6422:..Jay.? |
| 711 |
| 3F 3F 1C 28 35 35 35 29 | 20 31 32 33 2D 39 31 38 | ??.(555) 123-918 |
| 712 |
| 32 34 .. .. .. .. .. .. | .. .. .. .. .. .. .. .. | 24.............. |
| 713 |
|
| 714 |
Now let's read all the records back out |
| 715 |
1 | Dante | Hicks | (555) 123-4567 | 32 |
| 716 |
2 | Randal | Graves | (555) 123-5678 | 30 |
| 717 |
3 | Veronica | Loughran | (555) 123-0987 | 28 |
| 718 |
4 | Caitlin | Bree | (555) 123-2323 | 27 |
| 719 |
5 | Bob | Silent | (555) 123-6422 | 29 |
| 720 |
6 | Jay | ??? | (555) 123-9182 | 26 |
| 721 |
|
| 722 |
|
| 723 |
Use projection to print only the First name and phone numbers |
| 724 |
Dante | (555) 123-4567 | |
| 725 |
Randal | (555) 123-5678 | |
| 726 |
Veronica | (555) 123-0987 | |
| 727 |
Caitlin | (555) 123-2323 | |
| 728 |
Bob | (555) 123-6422 | |
| 729 |
Jay | (555) 123-9182 | |
| 730 |
---- |
| 731 |
|
| 732 |
The *Quick Stop* owner was so pleased, he asked you to create a |
| 733 |
movie database for his *RST Video* store. |
| 734 |
|
| 735 |
== Reference files |
| 736 |
|
| 737 |
=== avro.h |
| 738 |
|
| 739 |
The +avro.h+ header file contains the complete public API |
| 740 |
for +Avro C+. The documentation is rather sparse right now |
| 741 |
but we'll be adding more information soon. |
| 742 |
|
| 743 |
[source,c] |
| 744 |
---- |
| 745 |
include::../src/avro.h[] |
| 746 |
---- |
| 747 |
|
| 748 |
=== test_avro_data.c |
| 749 |
|
| 750 |
Another good way to learn how to encode/decode data in +Avro C+ is |
| 751 |
to look at the +test_avro_data.c+ unit test. This simple unit test |
| 752 |
checks that all the avro types can be encoded/decoded correctly. |
| 753 |
|
| 754 |
[source,c] |
| 755 |
---- |
| 756 |
include::../tests/test_avro_data.c[] |
| 757 |
---- |
| 758 |
|