示例#1
0
int avro_bin_to_json(avro_schema_t schema,
        const void *val_bin, size_t val_len,
        char **val_out, size_t *val_len_out) {
    if (!val_bin) {
        *val_out = NULL;
        return 0;
    } else if (!schema) {
        log_error("json: got a value where we didn't expect one, and no schema to decode it");
        *val_out = NULL;
        return EINVAL;
    }

    avro_reader_t reader = avro_reader_memory(val_bin, val_len);

    avro_value_iface_t *iface = avro_generic_class_from_schema(schema);
    if (!iface) {
        log_error("json: error in avro_generic_class_from_schema: %s", avro_strerror());
        avro_reader_free(reader);
        return EINVAL;
    }

    int err;

    avro_value_t value;
    err = avro_generic_value_new(iface, &value);
    if (err) {
        log_error("json: error in avro_generic_value_new: %s", avro_strerror());
        avro_value_iface_decref(iface);
        avro_reader_free(reader);
        return err;
    }

    err = avro_value_read(reader, &value);
    if (err) {
        log_error("json: error decoding Avro value: %s", avro_strerror());
        avro_value_decref(&value);
        avro_value_iface_decref(iface);
        avro_reader_free(reader);
        return err;
    }

    err = avro_value_to_json(&value, 1, val_out);
    if (err) {
        log_error("json: error converting Avro value to JSON: %s", avro_strerror());
        avro_value_decref(&value);
        avro_value_iface_decref(iface);
        avro_reader_free(reader);
        return err;
    }

    *val_len_out = strlen(*val_out); // not including null terminator - to librdkafka it's just bytes

    avro_value_decref(&value);
    avro_value_iface_decref(iface);
    avro_reader_free(reader);

    return 0;
}
示例#2
0
文件: datafile.c 项目: fizx/node-avro
static int file_writer_open(const char *path, avro_file_writer_t w)
{
	int rval;
	FILE *fp;
	avro_reader_t reader;

	fp = fopen(path, "r");
	if (!fp) {
		return errno;
	}
	reader = avro_reader_file(fp);
	if (!reader) {
		return ENOMEM;
	}
	rval =
	    file_read_header(reader, &w->writers_schema, w->sync,
			     sizeof(w->sync));
	avro_reader_free(reader);
	/* Position to end of file and get ready to write */
	rval = file_writer_init_fp(path, "a", w);
	if (rval) {
		free(w);
	}
	return rval;
}
示例#3
0
static void test_fixed_deserialize(void **state)
{
    (void)state;

    const uint8_t plain_fixed1[] = { 0x0, 0x1, 0x2, 0x3, 0x4 };
    size_t plain_fixed1_size = sizeof(plain_fixed1) / sizeof(char);

    kaa_bytes_t *kaa_fixed1 = kaa_fixed_copy_create(plain_fixed1, plain_fixed1_size);
    ASSERT_NOT_NULL(kaa_fixed1);

    size_t expected_size = kaa_fixed_get_size(kaa_fixed1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_fixed_serialize(avro_writer, kaa_fixed1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    kaa_bytes_t *kaa_fixed2 = kaa_fixed_deserialize(avro_reader, &expected_size);
    ASSERT_NOT_NULL(kaa_fixed2);

    ASSERT_EQUAL(memcmp(kaa_fixed2->buffer, plain_fixed1, plain_fixed1_size), 0);
    ASSERT_EQUAL(memcmp(kaa_fixed2->buffer, kaa_fixed1->buffer, plain_fixed1_size), 0);

    kaa_fixed_destroy(kaa_fixed2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
    kaa_fixed_destroy(kaa_fixed1);
}
示例#4
0
static void test_string_deserialize(void **state)
{
    (void)state;

    const char *plain_test_str1 = "test";
    kaa_string_t *kaa_str1 = kaa_string_copy_create(plain_test_str1);
    ASSERT_NOT_NULL(kaa_str1);

    size_t expected_size = kaa_string_get_size(kaa_str1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_string_serialize(avro_writer, kaa_str1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    kaa_string_t *kaa_str2 = kaa_string_deserialize(avro_reader);
    ASSERT_NOT_NULL(kaa_str2);

    ASSERT_EQUAL(strcmp(kaa_str2->data, plain_test_str1), 0);
    ASSERT_EQUAL(strcmp(kaa_str2->data, kaa_str1->data), 0);

    kaa_string_destroy(kaa_str2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
    kaa_string_destroy(kaa_str1);
}
示例#5
0
static kaa_root_configuration_t *kaa_configuration_manager_deserialize(const char *buffer, size_t buffer_size)
{
    KAA_RETURN_IF_NIL2(buffer, buffer_size, NULL);

    avro_reader_t reader = avro_reader_memory(buffer, buffer_size);
    KAA_RETURN_IF_NIL(reader, NULL);
    kaa_root_configuration_t *result = KAA_CONFIGURATION_DESERIALIZE(reader);
    avro_reader_free(reader);
    return result;
}
示例#6
0
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);
    }
}
示例#7
0
/* Decrements the reference counts of a schema list entry. */
void schema_list_entry_decrefs(schema_list_entry *entry) {
    avro_reader_free(entry->avro_reader);
    avro_value_decref(&entry->old_value);
    avro_value_decref(&entry->row_value);
    avro_value_iface_decref(entry->row_iface);
    avro_schema_decref(entry->row_schema);

    if (entry->key_schema) {
        avro_value_decref(&entry->key_value);
        avro_value_iface_decref(entry->key_iface);
        avro_schema_decref(entry->key_schema);
    }
}
示例#8
0
static void test_null_deserialize(void **state)
{
    (void)state;

    size_t expected_size = rand() % 10;
    char buffer[expected_size];
    memset(buffer, rand(), expected_size);
    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    ASSERT_NULL(kaa_null_deserialize(avro_reader));

    avro_reader_free(avro_reader);
}
示例#9
0
/* Frees all the memory structures associated with a frame reader. */
void frame_reader_free(frame_reader_t reader) {
    avro_reader_free(reader->avro_reader);
    avro_value_decref(&reader->frame_value);
    avro_value_iface_decref(reader->frame_iface);
    avro_schema_decref(reader->frame_schema);

    for (int i = 0; i < reader->num_schemas; i++) {
        schema_list_entry *entry = reader->schemas[i];
        schema_list_entry_decrefs(entry);
        free(entry);
    }

    free(reader->schemas);
    free(reader);
}
示例#10
0
static int
do_close(AvroDeserializer* self) {
    if (self->flags & DESERIALIZER_READER_OK) {
        avro_reader_free(self->datum_reader);
        self->flags &= ~DESERIALIZER_READER_OK;
    }
    if (self->flags & DESERIALIZER_SCHEMA_OK) {
        avro_schema_decref(self->schema);
        self->flags &= ~DESERIALIZER_SCHEMA_OK;
    }
    if (self->iface != NULL) {
        avro_value_iface_decref(self->iface);
        self->iface = NULL;
    }
    return 0;
}
示例#11
0
static void test_array_deserialize_w_ctx(void **state)
{
    (void)state;

    const uint8_t plain_fixed[] = { 0x0, 0x1, 0x2, 0x3, 0x4 };
    size_t plain_fixed_size = sizeof(plain_fixed) / sizeof(char);

    size_t array_size = 1 + rand() % 10;

    kaa_list_t *avro_array1 = kaa_list_create();
    size_t i = 0;
    for (i = 0; i < array_size; ++i) {
        kaa_list_push_back(avro_array1, kaa_fixed_copy_create(plain_fixed, plain_fixed_size));
    }

    size_t buffer_size = kaa_array_get_size(avro_array1, kaa_fixed_get_size);
    char buffer[buffer_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, buffer_size);

    kaa_array_serialize(avro_writer, avro_array1, kaa_fixed_serialize);

    avro_reader_t avro_reader = avro_reader_memory(buffer, buffer_size);
    kaa_list_t *avro_array2 = kaa_array_deserialize_w_ctx(avro_reader, (deserialize_w_ctx_fn)kaa_fixed_deserialize, &plain_fixed_size);

    ASSERT_NOT_NULL(avro_array2);
    ASSERT_EQUAL(kaa_list_get_size(avro_array2), array_size);

    kaa_list_node_t *it = kaa_list_begin(avro_array2);
    while (it) {
        kaa_bytes_t *fixed = kaa_list_get_data(it);
        ASSERT_NOT_NULL(fixed);
        ASSERT_EQUAL((size_t)fixed->size, plain_fixed_size);
        ASSERT_EQUAL(memcmp(fixed->buffer, plain_fixed, plain_fixed_size), 0);
        it  = kaa_list_next(it);
    }

    kaa_list_destroy(avro_array2, kaa_fixed_destroy);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
    kaa_list_destroy(avro_array1, kaa_fixed_destroy);
}
示例#12
0
static void test_float_deserialize(void **state)
{
    (void)state;

    float float_value1 = rand() / rand();
    size_t expected_size = kaa_float_get_size(&float_value1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_float_serialize(avro_writer, &float_value1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    float *float_value2 = kaa_float_deserialize(avro_reader);

    ASSERT_EQUAL(*float_value2, float_value1);

    kaa_data_destroy(float_value2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
}
示例#13
0
static void test_enum_deserialize(void **state)
{
    (void)state;

    test_enum_t enum_value1 = (test_enum_t)rand() % TEST_VAL_5;
    size_t expected_size = kaa_enum_get_size(&enum_value1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_enum_serialize(avro_writer, &enum_value1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    test_enum_t *enum_value2 = (test_enum_t *)kaa_enum_deserialize(avro_reader);

    ASSERT_EQUAL(*enum_value2, enum_value1);

    kaa_data_destroy(enum_value2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
}
示例#14
0
static void test_long_deserialize(void **state)
{
    (void)state;

    int64_t long_value1 = rand();
    size_t expected_size = kaa_long_get_size(&long_value1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_long_serialize(avro_writer, &long_value1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    int64_t *long_value2 = kaa_long_deserialize(avro_reader);

    ASSERT_EQUAL(*long_value2, long_value1);

    kaa_data_destroy(long_value2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
}
示例#15
0
static void test_int_deserialize(void **state)
{
    (void)state;

    int32_t int_value1 = rand();
    size_t expected_size = kaa_int_get_size(&int_value1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_int_serialize(avro_writer, &int_value1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    int32_t *int_value2 = kaa_int_deserialize(avro_reader);

    ASSERT_EQUAL(*int_value2, int_value1);

    kaa_data_destroy(int_value2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
}
示例#16
0
static void test_boolean_deserialize(void **state)
{
    (void)state;

    int8_t boolean_value1 = true;
    size_t expected_size = kaa_boolean_get_size(&boolean_value1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_boolean_serialize(avro_writer, &boolean_value1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    int8_t *boolean_value2 = kaa_boolean_deserialize(avro_reader);

    ASSERT_EQUAL(*boolean_value2, boolean_value1);

    kaa_data_destroy(boolean_value2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
}
示例#17
0
static void test_double_deserialize(void **state)
{
    (void)state;

    double double_value1 = rand() / rand();
    size_t expected_size = kaa_double_get_size(&double_value1);
    char buffer[expected_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size);

    kaa_double_serialize(avro_writer, &double_value1);

    avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size);

    double *double_value2 = kaa_double_deserialize(avro_reader);

    ASSERT_EQUAL(*double_value2, double_value1);

    kaa_data_destroy(double_value2);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
}
示例#18
0
static void test_array_deserialize_wo_ctx(void **state)
{
    (void)state;

    size_t array_size = 1 + rand() % 10;

    kaa_list_t *avro_array1 = kaa_list_create();
    size_t i = 0;
    for (i = 0; i < array_size; ++i) {
        kaa_list_push_back(avro_array1, create_float());
    }

    size_t buffer_size = kaa_array_get_size(avro_array1, kaa_float_get_size);
    char buffer[buffer_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, buffer_size);

    kaa_array_serialize(avro_writer, avro_array1, kaa_float_serialize);

    avro_reader_t avro_reader = avro_reader_memory(buffer, buffer_size);
    kaa_list_t *avro_array2 = kaa_array_deserialize_wo_ctx(avro_reader, (deserialize_wo_ctx_fn)kaa_float_deserialize);

    ASSERT_NOT_NULL(avro_array2);
    ASSERT_EQUAL(kaa_list_get_size(avro_array2), array_size);

//    /*
//     * Deserialized array has a back order, so reverse the original.
//     */
//    kaa_list_t *reverse_avro_array = NULL;
//    kaa_list_t *it = avro_array1;
//    while (it) {
//        if (reverse_avro_array) {
//            reverse_avro_array = kaa_list_push_front(reverse_avro_array, kaa_list_get_data(it));
//        } else {
//            reverse_avro_array = kaa_list_create(kaa_list_get_data(it));
//        }
//        it  = kaa_list_next(it);
//    }
//
//    kaa_list_destroy(avro_array1, kaa_null_destroy);
//    avro_array1 = reverse_avro_array;

    /*
     * Compare origin and deserialized arrays.
     */
    kaa_list_node_t *it1 = kaa_list_begin(avro_array1);
    kaa_list_node_t *it2 = kaa_list_begin(avro_array2);
    while (it1 && it2) {
        float *float_value1 = kaa_list_get_data(it1);
        float *float_value2 = kaa_list_get_data(it2);

        ASSERT_NOT_NULL(float_value1);
        ASSERT_NOT_NULL(float_value2);
        ASSERT_EQUAL(*float_value2, *float_value1);

        it1  = kaa_list_next(it1);
        it2  = kaa_list_next(it2);
    }

    kaa_list_destroy(avro_array2, kaa_data_destroy);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
    kaa_list_destroy(avro_array1, kaa_data_destroy);
}
示例#19
0
文件: datafile.c 项目: fizx/node-avro
int avro_file_reader_close(avro_file_reader_t reader)
{
	avro_reader_free(reader->reader);
	return 0;
}