int avro_file_reader_read(avro_file_reader_t r, avro_schema_t readers_schema, avro_datum_t * datum) { int rval; char sync[16]; if (!r || !datum) { return EINVAL; } check(rval, avro_read_data(r->reader, r->writers_schema, readers_schema, datum)); r->blocks_read++; if (r->blocks_read == r->blocks_total) { check(rval, avro_read(r->reader, sync, sizeof(sync))); if (memcmp(r->sync, sync, sizeof(r->sync)) != 0) { /* wrong sync bytes */ return EILSEQ; } /* For now, ignore errors (e.g. EOF) */ file_read_block_count(r); } return 0; }
static int file_read_header(avro_reader_t reader, avro_schema_t * writers_schema, char *sync, int synclen) { int rval; avro_schema_t meta_schema; avro_schema_t meta_values_schema; avro_datum_t meta; char magic[4]; avro_datum_t schema_bytes; char *p; int64_t len; avro_schema_error_t schema_error; check(rval, avro_read(reader, magic, sizeof(magic))); if (magic[0] != 'O' || magic[1] != 'b' || magic[2] != 'j' || magic[3] != 1) { return EILSEQ; } meta_values_schema = avro_schema_bytes(); meta_schema = avro_schema_map(meta_values_schema); rval = avro_read_data(reader, meta_schema, NULL, &meta); if (rval) { return EILSEQ; } check(rval, avro_map_get(meta, "avro.schema", &schema_bytes)); avro_bytes_get(schema_bytes, &p, &len); check(rval, avro_schema_from_json(p, len, writers_schema, &schema_error)); avro_schema_decref(meta); return avro_read(reader, sync, synclen); }
void write_read_check(avro_schema_t writers_schema, avro_datum_t datum, avro_schema_t readers_schema, avro_datum_t expected, char *type) { avro_datum_t datum_out; int validate; for (validate = 0; validate <= 1; validate++) { reader = avro_reader_memory(buf, sizeof(buf)); writer = avro_writer_memory(buf, sizeof(buf)); if (!expected) { expected = datum; } /* Validating read/write */ if (avro_write_data (writer, validate ? writers_schema : NULL, datum)) { fprintf(stderr, "Unable to write %s validate=%d\n %s\n", type, validate, avro_strerror()); exit(EXIT_FAILURE); } int64_t size = avro_size_data(writer, validate ? writers_schema : NULL, datum); if (size != avro_writer_tell(writer)) { fprintf(stderr, "Unable to calculate size %s validate=%d " "(%"PRId64" != %"PRId64")\n %s\n", type, validate, size, avro_writer_tell(writer), avro_strerror()); exit(EXIT_FAILURE); } if (avro_read_data (reader, writers_schema, readers_schema, &datum_out)) { fprintf(stderr, "Unable to read %s validate=%d\n %s\n", type, validate, avro_strerror()); fprintf(stderr, " %s\n", avro_strerror()); exit(EXIT_FAILURE); } if (!avro_datum_equal(expected, datum_out)) { fprintf(stderr, "Unable to encode/decode %s validate=%d\n %s\n", type, validate, avro_strerror()); exit(EXIT_FAILURE); } avro_reader_dump(reader, stderr); avro_datum_decref(datum_out); avro_reader_free(reader); avro_writer_free(writer); } }