Пример #1
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);
}
Пример #2
0
static int test_map(void)
{
    avro_schema_t schema = avro_schema_map(avro_schema_long());
    avro_datum_t datum = avro_map(schema);
    int64_t i = 0;
    char *nums[] =
    { "zero", "one", "two", "three", "four", "five", "six", NULL };
    while (nums[i]) {
        avro_datum_t i_datum = avro_int64(i);
        avro_map_set(datum, nums[i], i_datum);
        avro_datum_decref(i_datum);
        i++;
    }

    if (avro_array_size(datum) != 7) {
        fprintf(stderr, "Unexpected map size\n");
        exit(EXIT_FAILURE);
    }

    avro_datum_t value;
    const char  *key;
    avro_map_get_key(datum, 2, &key);
    avro_map_get(datum, key, &value);
    int64_t  val;
    avro_int64_get(value, &val);

    if (val != 2) {
        fprintf(stderr, "Unexpected map value 2\n");
        exit(EXIT_FAILURE);
    }

    int  index;
    if (avro_map_get_index(datum, "two", &index)) {
        fprintf(stderr, "Can't get index for key \"two\": %s\n",
                avro_strerror());
        exit(EXIT_FAILURE);
    }
    if (index != 2) {
        fprintf(stderr, "Unexpected index for key \"two\"\n");
        exit(EXIT_FAILURE);
    }
    if (!avro_map_get_index(datum, "foobar", &index)) {
        fprintf(stderr, "Unexpected index for key \"foobar\"\n");
        exit(EXIT_FAILURE);
    }

    write_read_check(schema, datum, NULL, NULL, "map");
    test_json(datum,
              "{\"zero\": 0, \"one\": 1, \"two\": 2, \"three\": 3, "
              "\"four\": 4, \"five\": 5, \"six\": 6}");
    avro_datum_decref(datum);
    avro_schema_decref(schema);
    return 0;
}
Пример #3
0
static int
avro_datum_value_add(const avro_value_iface_t *iface,
		     void *vself, const char *key,
		     avro_value_t *child, size_t *index, int *is_new)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	if (!is_avro_map(self)) {
		avro_set_error("Can only add to map");
		return EINVAL;
	}

	int  rval;
	avro_datum_t  child_datum;

	if (avro_map_get(self, key, &child_datum) == 0) {
		/* key already exists */
		if (is_new != NULL) {
			*is_new = 0;
		}
		if (index != NULL) {
			int  real_index;
			avro_map_get_index(self, key, &real_index);
			*index = real_index;
		}
		return avro_datum_as_child_value(child, child_datum);
	}

	/* key is new */
	avro_schema_t  map_schema = avro_datum_get_schema(self);
	avro_schema_t  child_schema = avro_schema_map_values(map_schema);
	child_datum = avro_datum_from_schema(child_schema);
	if (child_datum == NULL) {
		return ENOMEM;
	}

	rval = avro_map_set(self, key, child_datum);
	avro_datum_decref(child_datum);
	if (rval != 0) {
		return rval;
	}

	if (is_new != NULL) {
		*is_new = 1;
	}
	if (index != NULL) {
		*index = avro_map_size(self) - 1;
	}

	return avro_datum_as_child_value(child, child_datum);
}
Пример #4
0
static int
avro_datum_value_get_by_index(const avro_value_iface_t *iface,
			      const void *vself, size_t index,
			      avro_value_t *child, const char **name)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	avro_datum_t  child_datum;

	if (is_avro_array(self)) {
		check(rval, avro_array_get(self, index, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	if (is_avro_map(self)) {
		const char  *real_key;
		check(rval, avro_map_get_key(self, index, &real_key));
		if (name != NULL) {
			*name = real_key;
		}
		check(rval, avro_map_get(self, real_key, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	if (is_avro_record(self)) {
		avro_schema_t  schema = avro_datum_get_schema(self);
		const char  *field_name =
		    avro_schema_record_field_name(schema, index);
		if (field_name == NULL) {
			return EINVAL;
		}
		if (name != NULL) {
			*name = field_name;
		}
		check(rval, avro_record_get(self, field_name, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	avro_set_error("Can only get by index from array, map, or record");
	return EINVAL;
}
Пример #5
0
static int
avro_datum_value_get_by_name(const avro_value_iface_t *iface,
			     const void *vself, const char *name,
			     avro_value_t *child, size_t *index)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	avro_datum_t  child_datum;

	if (is_avro_map(self)) {
		if (index != NULL) {
			int  real_index;
			check(rval, avro_map_get_index(self, name, &real_index));
			*index = real_index;
		}

		check(rval, avro_map_get(self, name, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	if (is_avro_record(self)) {
		if (index != NULL) {
			avro_schema_t  schema = avro_datum_get_schema(self);
			*index = avro_schema_record_field_get_index(schema, name);
		}

		check(rval, avro_record_get(self, name, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	avro_set_error("Can only get by name from map or record");
	return EINVAL;
}