Author: dcreager
Date: Fri Feb 25 15:43:50 2011
New Revision: 1074585
URL: http://svn.apache.org/viewvc?rev=1074585&view=rev
Log:
AVRO-751. C: Store schema reference in datum instances
We now keep track of which particular schema an avro_datum_t is an
instance of. For primitive values, there's only one possible schema,
and so we don't store an explicit reference. For compound values, the
datum constructors now take in a schema parameter, which is stored in
the avro_datum_t instance. For records, enums, and fixeds, this means
that we don't need to store the name of the schema type anymore, since
we can get this from the schema.
There were also several functions, which operate on datum instances,
which needed to take in a schema parameter — avro_datum_to_json, as an
example. Those parameters aren't needed anymore, since the datum
carries a reference to its own schema already.
Modified:
avro/trunk/CHANGES.txt
avro/trunk/lang/c/examples/quickstop.c
avro/trunk/lang/c/src/avro.h
avro/trunk/lang/c/src/datum.c
avro/trunk/lang/c/src/datum.h
avro/trunk/lang/c/src/datum_equal.c
avro/trunk/lang/c/src/datum_json.c
avro/trunk/lang/c/src/datum_read.c
avro/trunk/lang/c/src/schema.c
avro/trunk/lang/c/tests/generate_interop_data.c
avro/trunk/lang/c/tests/test_avro_data.c
Modified: avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Feb 25 15:43:50 2011
@@ -4,6 +4,11 @@ Avro 1.5.0 (unreleased)
INCOMPATIBLE CHANGES
+ AVRO-751. C: Each avro_datum_t instance now contains a reference to
+ the schema that the datum is an instance of. As a result, the
+ signatures of several of the functions that operate on avro_datum_t
+ instances have changed.
+
AVRO-647. Java: Break avro.jar up into multiple parts: avro.jar,
avro-compiler.jar, avro-ipc.jar, avro-mapred.jar, avro-tools.jar,
and avro-maven-plugin.jar.
Modified: avro/trunk/lang/c/examples/quickstop.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/examples/quickstop.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/examples/quickstop.c (original)
+++ avro/trunk/lang/c/examples/quickstop.c Fri Feb 25 15:43:50 2011
@@ -51,7 +51,7 @@ void
add_person(avro_file_writer_t db, const char *first, const char *last,
const char *phone, int32_t age)
{
- avro_datum_t person = avro_record("Person", NULL);
+ avro_datum_t person = avro_record(person_schema);
avro_datum_t id_datum = avro_int64(++id);
avro_datum_t first_datum = avro_string(first);
Modified: avro/trunk/lang/c/src/avro.h
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avro.h?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avro.h (original)
+++ avro/trunk/lang/c/src/avro.h Fri Feb 25 15:43:50 2011
@@ -227,7 +227,14 @@ typedef void
void
avro_alloc_free(void *ptr, size_t sz);
-/* constructors */
+/*
+ * Datum constructors. Each datum stores a reference to the schema that
+ * the datum is an instance of. The primitive datum constructors don't
+ * need to take in an explicit avro_schema_t parameter, since there's
+ * only one schema that they could be an instance of. The complex
+ * constructors do need an explicit schema parameter.
+ */
+
typedef struct avro_obj_t *avro_datum_t;
avro_datum_t avro_string(const char *str);
avro_datum_t avro_givestring(const char *str,
@@ -241,16 +248,23 @@ avro_datum_t avro_float(float f);
avro_datum_t avro_double(double d);
avro_datum_t avro_boolean(int8_t i);
avro_datum_t avro_null(void);
-avro_datum_t avro_record(const char *name, const char *space);
-avro_datum_t avro_enum(const char *name, int i);
-avro_datum_t avro_fixed(const char *name, const char *bytes,
- const int64_t size);
-avro_datum_t avro_givefixed(const char *name, const char *bytes,
- const int64_t size,
+avro_datum_t avro_record(avro_schema_t schema);
+avro_datum_t avro_enum(avro_schema_t schema, int i);
+avro_datum_t avro_fixed(avro_schema_t schema,
+ const char *bytes, const int64_t size);
+avro_datum_t avro_givefixed(avro_schema_t schema,
+ const char *bytes, const int64_t size,
avro_free_func_t free);
-avro_datum_t avro_map(void);
-avro_datum_t avro_array(void);
-avro_datum_t avro_union(int64_t discriminant, const avro_datum_t datum);
+avro_datum_t avro_map(avro_schema_t schema);
+avro_datum_t avro_array(avro_schema_t schema);
+avro_datum_t avro_union(avro_schema_t schema,
+ int64_t discriminant, const avro_datum_t datum);
+
+/**
+ * Returns the schema that the datum is an instance of.
+ */
+
+avro_schema_t avro_datum_get_schema(const avro_datum_t datum);
/*
* Constructs a new avro_datum_t instance that's appropriate for holding
@@ -269,8 +283,7 @@ int avro_double_get(avro_datum_t datum,
int avro_boolean_get(avro_datum_t datum, int8_t * i);
int avro_enum_get(const avro_datum_t datum);
-const char *avro_enum_get_name(const avro_datum_t datum,
- const avro_schema_t schema);
+const char *avro_enum_get_name(const avro_datum_t datum);
int avro_fixed_get(avro_datum_t datum, char **bytes, int64_t * size);
int avro_record_get(const avro_datum_t record, const char *field_name,
avro_datum_t * value);
@@ -328,8 +341,7 @@ int avro_double_set(avro_datum_t datum,
int avro_boolean_set(avro_datum_t datum, const int8_t i);
int avro_enum_set(avro_datum_t datum, const int symbol_value);
-int avro_enum_set_name(avro_datum_t datum, avro_schema_t schema,
- const char *symbol_name);
+int avro_enum_set_name(avro_datum_t datum, const char *symbol_name);
int avro_fixed_set(avro_datum_t datum, const char *bytes, const int64_t size);
int avro_givefixed_set(avro_datum_t datum, const char *bytes,
const int64_t size,
@@ -367,7 +379,6 @@ int avro_array_append_datum(avro_datum_t
*/
int avro_union_set_discriminant(avro_datum_t unionp,
- avro_schema_t schema,
int discriminant,
avro_datum_t *branch);
@@ -385,7 +396,7 @@ int avro_datum_equal(avro_datum_t a, avr
* free() function. (*Not* using the custom Avro allocator.)
*/
-int avro_datum_to_json(const avro_datum_t datum, const avro_schema_t schema,
+int avro_datum_to_json(const avro_datum_t datum,
int one_line, char **json_str);
int avro_schema_match(avro_schema_t writers_schema,
Modified: avro/trunk/lang/c/src/datum.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datum.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datum.c (original)
+++ avro/trunk/lang/c/src/datum.c Fri Feb 25 15:43:50 2011
@@ -384,13 +384,19 @@ avro_datum_t avro_null(void)
return avro_datum_incref(&obj);
}
-avro_datum_t avro_union(int64_t discriminant, avro_datum_t value)
+avro_datum_t avro_union(avro_schema_t schema,
+ int64_t discriminant, avro_datum_t value)
{
+ if (!is_avro_schema(schema)) {
+ return NULL;
+ }
+
struct avro_union_datum_t *datum =
avro_new(struct avro_union_datum_t);
if (!datum) {
return NULL;
}
+ datum->schema = avro_schema_incref(schema);
datum->discriminant = discriminant;
datum->value = avro_datum_incref(value);
@@ -409,17 +415,17 @@ avro_datum_t avro_union_current_branch(a
}
int avro_union_set_discriminant(avro_datum_t datum,
- avro_schema_t schema,
int discriminant,
avro_datum_t *branch)
{
- if (!is_avro_union(datum) || !is_avro_union(schema)) {
+ if (!is_avro_union(datum)) {
return EINVAL;
}
struct avro_union_datum_t *unionp =
avro_datum_to_union(datum);
+ avro_schema_t schema = unionp->schema;
avro_schema_t branch_schema =
avro_schema_union_branch(schema, discriminant);
@@ -451,44 +457,30 @@ int avro_union_set_discriminant(avro_dat
return 0;
}
-avro_datum_t avro_record(const char *name, const char *space)
+avro_datum_t avro_record(avro_schema_t schema)
{
+ if (!is_avro_schema(schema)) {
+ return NULL;
+ }
+
struct avro_record_datum_t *datum =
avro_new(struct avro_record_datum_t);
if (!datum) {
return NULL;
}
- datum->name = avro_strdup(name);
- if (!datum->name) {
- avro_freet(struct avro_record_datum_t, datum);
- return NULL;
- }
- datum->space = space ? avro_strdup(space) : NULL;
- if (space && !datum->space) {
- avro_str_free((char *) datum->name);
- avro_freet(struct avro_record_datum_t, datum);
- return NULL;
- }
datum->field_order = st_init_numtable_with_size(DEFAULT_TABLE_SIZE);
if (!datum->field_order) {
- if (space) {
- avro_str_free((char *) datum->space);
- }
- avro_str_free((char *) datum->name);
avro_freet(struct avro_record_datum_t, datum);
return NULL;
}
datum->fields_byname = st_init_strtable_with_size(DEFAULT_TABLE_SIZE);
if (!datum->fields_byname) {
st_free_table(datum->field_order);
- if (space) {
- avro_str_free((char *) datum->space);
- }
- avro_str_free((char *) datum->name);
avro_freet(struct avro_record_datum_t, datum);
return NULL;
}
+ datum->schema = avro_schema_incref(schema);
avro_datum_init(&datum->obj, AVRO_RECORD);
return &datum->obj;
}
@@ -543,14 +535,18 @@ avro_record_set(avro_datum_t datum, cons
return EINVAL;
}
-avro_datum_t avro_enum(const char *name, int i)
+avro_datum_t avro_enum(avro_schema_t schema, int i)
{
+ if (!is_avro_schema(schema)) {
+ return NULL;
+ }
+
struct avro_enum_datum_t *datum =
avro_new(struct avro_enum_datum_t);
if (!datum) {
return NULL;
}
- datum->name = avro_strdup(name);
+ datum->schema = avro_schema_incref(schema);
datum->value = i;
avro_datum_init(&datum->obj, AVRO_ENUM);
@@ -562,10 +558,10 @@ int avro_enum_get(const avro_datum_t dat
return avro_datum_to_enum(datum)->value;
}
-const char *avro_enum_get_name(const avro_datum_t datum,
- const avro_schema_t schema)
+const char *avro_enum_get_name(const avro_datum_t datum)
{
int value = avro_enum_get(datum);
+ avro_schema_t schema = avro_datum_to_enum(datum)->schema;
return avro_schema_enum_get(schema, value);
}
@@ -579,12 +575,12 @@ int avro_enum_set(avro_datum_t datum, co
return 0;
}
-int avro_enum_set_name(avro_datum_t datum, avro_schema_t schema,
- const char *symbol_name)
+int avro_enum_set_name(avro_datum_t datum, const char *symbol_name)
{
- if (!is_avro_enum(datum) || !is_avro_enum(schema)) {
+ if (!is_avro_enum(datum)) {
return EINVAL;
}
+ avro_schema_t schema = avro_datum_to_enum(datum)->schema;
int symbol_value = avro_schema_enum_get_by_name(schema, symbol_name);
if (symbol_value == -1) {
return EINVAL;
@@ -593,16 +589,20 @@ int avro_enum_set_name(avro_datum_t datu
return 0;
}
-static avro_datum_t avro_fixed_private(const char *name, const char *bytes,
- const int64_t size,
+static avro_datum_t avro_fixed_private(avro_schema_t schema,
+ const char *bytes, const int64_t size,
avro_free_func_t fixed_free)
{
+ if (!is_avro_schema(schema)) {
+ return NULL;
+ }
+
struct avro_fixed_datum_t *datum =
avro_new(struct avro_fixed_datum_t);
if (!datum) {
return NULL;
}
- datum->name = avro_strdup(name);
+ datum->schema = avro_schema_incref(schema);
datum->size = size;
datum->bytes = (char *)bytes;
datum->free = fixed_free;
@@ -611,24 +611,26 @@ static avro_datum_t avro_fixed_private(c
return &datum->obj;
}
-avro_datum_t avro_fixed(const char *name, const char *bytes, const int64_t
size)
+avro_datum_t avro_fixed(avro_schema_t schema,
+ const char *bytes, const int64_t size)
{
char *bytes_copy = avro_malloc(size);
if (!bytes_copy) {
return NULL;
}
memcpy(bytes_copy, bytes, size);
- return avro_fixed_private(name, bytes_copy, size, avro_alloc_free);
+ return avro_fixed_private(schema, bytes_copy, size, avro_alloc_free);
}
-avro_datum_t avro_givefixed(const char *name, const char *bytes,
- const int64_t size, avro_free_func_t free)
+avro_datum_t avro_givefixed(avro_schema_t schema,
+ const char *bytes, const int64_t size,
+ avro_free_func_t free)
{
- return avro_fixed_private(name, bytes, size, free);
+ return avro_fixed_private(schema, bytes, size, free);
}
-static int avro_fixed_set_private(avro_datum_t datum, const char *bytes,
- const int64_t size,
+static int avro_fixed_set_private(avro_datum_t datum,
+ const char *bytes, const int64_t size,
avro_free_func_t fixed_free)
{
struct avro_fixed_datum_t *fixed;
@@ -681,8 +683,12 @@ int avro_fixed_get(avro_datum_t datum, c
return 0;
}
-avro_datum_t avro_map(void)
+avro_datum_t avro_map(avro_schema_t schema)
{
+ if (!is_avro_schema(schema)) {
+ return NULL;
+ }
+
struct avro_map_datum_t *datum =
avro_new(struct avro_map_datum_t);
if (!datum) {
@@ -700,6 +706,7 @@ avro_datum_t avro_map(void)
return NULL;
}
+ datum->schema = avro_schema_incref(schema);
avro_datum_init(&datum->obj, AVRO_MAP);
return &datum->obj;
}
@@ -786,8 +793,12 @@ avro_map_set(avro_datum_t datum, const c
return 0;
}
-avro_datum_t avro_array(void)
+avro_datum_t avro_array(avro_schema_t schema)
{
+ if (!is_avro_schema(schema)) {
+ return NULL;
+ }
+
struct avro_array_datum_t *datum =
avro_new(struct avro_array_datum_t);
if (!datum) {
@@ -799,6 +810,7 @@ avro_datum_t avro_array(void)
return NULL;
}
+ datum->schema = avro_schema_incref(schema);
avro_datum_init(&datum->obj, AVRO_ARRAY);
return &datum->obj;
}
@@ -860,6 +872,91 @@ static int array_free_foreach(int i, avr
return ST_DELETE;
}
+avro_schema_t avro_datum_get_schema(const avro_datum_t datum)
+{
+ if (!is_avro_datum(datum)) {
+ return NULL;
+ }
+
+ switch (avro_typeof(datum)) {
+ /*
+ * For the primitive types, which don't store an
+ * explicit reference to their schema, we decref the
+ * schema before returning. This maintains the
+ * invariant that this function doesn't add any
+ * additional references to the schema. The primitive
+ * schemas won't be freed, because there's always at
+ * least 1 reference for their initial static
+ * initializers.
+ */
+
+ case AVRO_STRING:
+ {
+ avro_schema_t result = avro_schema_string();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_BYTES:
+ {
+ avro_schema_t result = avro_schema_bytes();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_INT32:
+ {
+ avro_schema_t result = avro_schema_int();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_INT64:
+ {
+ avro_schema_t result = avro_schema_long();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_FLOAT:
+ {
+ avro_schema_t result = avro_schema_float();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_DOUBLE:
+ {
+ avro_schema_t result = avro_schema_double();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_BOOLEAN:
+ {
+ avro_schema_t result = avro_schema_boolean();
+ avro_schema_decref(result);
+ return result;
+ }
+ case AVRO_NULL:
+ {
+ avro_schema_t result = avro_schema_null();
+ avro_schema_decref(result);
+ return result;
+ }
+
+ case AVRO_RECORD:
+ return avro_datum_to_record(datum)->schema;
+ case AVRO_ENUM:
+ return avro_datum_to_enum(datum)->schema;
+ case AVRO_FIXED:
+ return avro_datum_to_fixed(datum)->schema;
+ case AVRO_MAP:
+ return avro_datum_to_map(datum)->schema;
+ case AVRO_ARRAY:
+ return avro_datum_to_array(datum)->schema;
+ case AVRO_UNION:
+ return avro_datum_to_union(datum)->schema;
+
+ default:
+ return NULL;
+ }
+}
+
static void avro_datum_free(avro_datum_t datum)
{
if (is_avro_datum(datum)) {
@@ -909,10 +1006,7 @@ static void avro_datum_free(avro_datum_t
case AVRO_RECORD:{
struct avro_record_datum_t *record;
record = avro_datum_to_record(datum);
- avro_str_free((char *) record->name);
- if (record->space) {
- avro_str_free((char *) record->space);
- }
+ avro_schema_decref(record->schema);
st_foreach(record->fields_byname,
char_datum_free_foreach, 0);
st_free_table(record->field_order);
@@ -923,14 +1017,14 @@ static void avro_datum_free(avro_datum_t
case AVRO_ENUM:{
struct avro_enum_datum_t *enump;
enump = avro_datum_to_enum(datum);
- avro_str_free((char *) enump->name);
+ avro_schema_decref(enump->schema);
avro_freet(struct avro_enum_datum_t, enump);
}
break;
case AVRO_FIXED:{
struct avro_fixed_datum_t *fixed;
fixed = avro_datum_to_fixed(datum);
- avro_str_free((char *) fixed->name);
+ avro_schema_decref(fixed->schema);
if (fixed->free) {
fixed->free((void *)fixed->bytes,
fixed->size);
@@ -941,6 +1035,7 @@ static void avro_datum_free(avro_datum_t
case AVRO_MAP:{
struct avro_map_datum_t *map;
map = avro_datum_to_map(datum);
+ avro_schema_decref(map->schema);
st_foreach(map->map, char_datum_free_foreach,
0);
st_free_table(map->map);
@@ -951,6 +1046,7 @@ static void avro_datum_free(avro_datum_t
case AVRO_ARRAY:{
struct avro_array_datum_t *array;
array = avro_datum_to_array(datum);
+ avro_schema_decref(array->schema);
st_foreach(array->els, array_free_foreach, 0);
st_free_table(array->els);
avro_freet(struct avro_array_datum_t, array);
@@ -959,6 +1055,7 @@ static void avro_datum_free(avro_datum_t
case AVRO_UNION:{
struct avro_union_datum_t *unionp;
unionp = avro_datum_to_union(datum);
+ avro_schema_decref(unionp->schema);
avro_datum_decref(unionp->value);
avro_freet(struct avro_union_datum_t, unionp);
}
Modified: avro/trunk/lang/c/src/datum.h
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datum.h?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datum.h (original)
+++ avro/trunk/lang/c/src/datum.h Fri Feb 25 15:43:50 2011
@@ -62,7 +62,7 @@ struct avro_boolean_datum_t {
struct avro_fixed_datum_t {
struct avro_obj_t obj;
- char *name;
+ avro_schema_t schema;
char *bytes;
int64_t size;
avro_free_func_t free;
@@ -70,31 +70,33 @@ struct avro_fixed_datum_t {
struct avro_map_datum_t {
struct avro_obj_t obj;
+ avro_schema_t schema;
st_table *map;
st_table *keys_by_index;
};
struct avro_record_datum_t {
struct avro_obj_t obj;
- const char *name;
- const char *space;
+ avro_schema_t schema;
st_table *field_order;
st_table *fields_byname;
};
struct avro_enum_datum_t {
struct avro_obj_t obj;
- const char *name;
+ avro_schema_t schema;
int value;
};
struct avro_array_datum_t {
struct avro_obj_t obj;
+ avro_schema_t schema;
st_table *els;
};
struct avro_union_datum_t {
struct avro_obj_t obj;
+ avro_schema_t schema;
int64_t discriminant;
avro_datum_t value;
};
Modified: avro/trunk/lang/c/src/datum_equal.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datum_equal.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datum_equal.c (original)
+++ avro/trunk/lang/c/src/datum_equal.c Fri Feb 25 15:43:50 2011
@@ -22,6 +22,10 @@
static int
array_equal(struct avro_array_datum_t *a, struct avro_array_datum_t *b)
{
+ if (!avro_schema_equal(a->schema, b->schema)) {
+ return 0;
+ }
+
long i;
if (a->els->num_entries != b->els->num_entries) {
@@ -66,6 +70,10 @@ st_equal_foreach(char *key, avro_datum_t
static int map_equal(struct avro_map_datum_t *a, struct avro_map_datum_t *b)
{
+ if (!avro_schema_equal(a->schema, b->schema)) {
+ return 0;
+ }
+
struct st_equal_args args = { 1, b->map };
if (a->map->num_entries != b->map->num_entries) {
return 0;
@@ -77,20 +85,11 @@ static int map_equal(struct avro_map_dat
static int record_equal(struct avro_record_datum_t *a,
struct avro_record_datum_t *b)
{
- struct st_equal_args args = { 1, b->fields_byname };
- if (strcmp(a->name, b->name)) {
- /* This have different names */
- return 0;
- }
- if (a->space && b->space) {
- /* They have different namespaces */
- if (strcmp(a->space, b->space)) {
- return 0;
- }
- } else if (a->space || b->space) {
- /* One has a namespace, one doesn't */
+ if (!avro_schema_equal(a->schema, b->schema)) {
return 0;
}
+
+ struct st_equal_args args = { 1, b->fields_byname };
if (a->fields_byname->num_entries != b->fields_byname->num_entries) {
return 0;
}
@@ -100,20 +99,27 @@ static int record_equal(struct avro_reco
static int enum_equal(struct avro_enum_datum_t *a, struct avro_enum_datum_t *b)
{
- return strcmp(a->name, b->name) == 0 && a->value == b->value;
+ return avro_schema_equal(a->schema, b->schema) && a->value == b->value;
}
static int fixed_equal(struct avro_fixed_datum_t *a,
struct avro_fixed_datum_t *b)
{
+ if (!avro_schema_equal(a->schema, b->schema)) {
+ return 0;
+ }
+
return a->size == b->size && memcmp(a->bytes, b->bytes, a->size) == 0;
}
static int union_equal(struct avro_union_datum_t *a,
struct avro_union_datum_t *b)
{
- /* XXX: not sure. a->discriminant == b->discriminant important? */
- return avro_datum_equal(a->value, b->value);
+ if (!avro_schema_equal(a->schema, b->schema)) {
+ return 0;
+ }
+
+ return a->discriminant == b->discriminant && avro_datum_equal(a->value,
b->value);
}
int avro_datum_equal(const avro_datum_t a, const avro_datum_t b)
Modified: avro/trunk/lang/c/src/datum_json.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datum_json.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datum_json.c (original)
+++ avro/trunk/lang/c/src/datum_json.c Fri Feb 25 15:43:50 2011
@@ -80,7 +80,7 @@ encode_utf8_bytes(const void *src, size_
}
static json_t *
-avro_datum_to_json_t(const avro_datum_t datum, const avro_schema_t schema)
+avro_datum_to_json_t(const avro_datum_t datum)
{
switch (avro_typeof(datum)) {
case AVRO_BOOLEAN:
@@ -131,7 +131,6 @@ avro_datum_to_json_t(const avro_datum_t
return NULL;
}
- avro_schema_t element_schema =
avro_schema_array_items(schema);
int num_elements = avro_array_size(datum);
int i;
for (i = 0; i < num_elements; i++) {
@@ -141,8 +140,7 @@ avro_datum_to_json_t(const avro_datum_t
return NULL;
}
- json_t *element_json =
- avro_datum_to_json_t(element,
element_schema);
+ json_t *element_json =
avro_datum_to_json_t(element);
if (!element_json) {
json_decref(result);
return NULL;
@@ -158,7 +156,7 @@ avro_datum_to_json_t(const avro_datum_t
}
case AVRO_ENUM:
- return json_string(avro_enum_get_name(datum, schema));
+ return json_string(avro_enum_get_name(datum));
case AVRO_FIXED:
{
@@ -185,7 +183,6 @@ avro_datum_to_json_t(const avro_datum_t
return NULL;
}
- avro_schema_t element_schema =
avro_schema_map_values(schema);
int num_elements = avro_map_size(datum);
int i;
for (i = 0; i < num_elements; i++) {
@@ -201,8 +198,7 @@ avro_datum_to_json_t(const avro_datum_t
return NULL;
}
- json_t *element_json =
- avro_datum_to_json_t(element,
element_schema);
+ json_t *element_json =
avro_datum_to_json_t(element);
if (!element_json) {
json_decref(result);
return NULL;
@@ -224,23 +220,20 @@ avro_datum_to_json_t(const avro_datum_t
return NULL;
}
+ avro_schema_t schema =
avro_datum_to_record(datum)->schema;
int num_fields =
avro_schema_record_size(schema);
int i;
for (i = 0; i < num_fields; i++) {
const char *field_name =
avro_schema_record_field_name(schema, i);
- avro_schema_t field_schema =
-
avro_schema_record_field_get(schema, field_name);
-
avro_datum_t field = NULL;
if (avro_record_get(datum, field_name,
&field)) {
json_decref(result);
return NULL;
}
- json_t *field_json =
- avro_datum_to_json_t(field,
field_schema);
+ json_t *field_json =
avro_datum_to_json_t(field);
if (!field_json) {
json_decref(result);
return NULL;
@@ -259,6 +252,8 @@ avro_datum_to_json_t(const avro_datum_t
{
int64_t discriminant =
avro_union_discriminant(datum);
avro_datum_t branch =
avro_union_current_branch(datum);
+
+ avro_schema_t schema =
avro_datum_to_union(datum)->schema;
avro_schema_t branch_schema =
avro_schema_union_branch(schema,
discriminant);
@@ -271,7 +266,7 @@ avro_datum_to_json_t(const avro_datum_t
return NULL;
}
- json_t *branch_json =
avro_datum_to_json_t(branch, branch_schema);
+ json_t *branch_json =
avro_datum_to_json_t(branch);
if (!branch_json) {
json_decref(result);
return NULL;
@@ -291,14 +286,14 @@ avro_datum_to_json_t(const avro_datum_t
}
}
-int avro_datum_to_json(const avro_datum_t datum, const avro_schema_t schema,
+int avro_datum_to_json(const avro_datum_t datum,
int one_line, char **json_str)
{
- if (!is_avro_datum(datum) || !is_avro_schema(schema) || !json_str) {
+ if (!is_avro_datum(datum) || !json_str) {
return EINVAL;
}
- json_t *json = avro_datum_to_json_t(datum, schema);
+ json_t *json = avro_datum_to_json_t(datum);
if (!json) {
return ENOMEM;
}
Modified: avro/trunk/lang/c/src/datum_read.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datum_read.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datum_read.c (original)
+++ avro/trunk/lang/c/src/datum_read.c Fri Feb 25 15:43:50 2011
@@ -103,10 +103,10 @@ read_enum(avro_reader_t reader, const av
int rval;
int64_t index;
- AVRO_UNUSED(readers_schema);
+ AVRO_UNUSED(writers_schema);
check(rval, enc->read_long(reader, &index));
- *datum = avro_enum(writers_schema->name, index);
+ *datum = avro_enum(&readers_schema->obj, index);
return 0;
}
@@ -126,7 +126,7 @@ read_array(avro_reader_t reader, const a
return rval;
}
- array_datum = avro_array();
+ array_datum = avro_array(&readers_schema->obj);
while (block_count != 0) {
if (block_count < 0) {
block_count = block_count * -1;
@@ -169,7 +169,7 @@ read_map(avro_reader_t reader, const avr
{
int rval;
int64_t i, block_count;
- avro_datum_t map = avro_map();
+ avro_datum_t map = avro_map(&readers_schema->obj);
rval = enc->read_long(reader, &block_count);
if (rval) {
@@ -239,7 +239,7 @@ read_union(avro_reader_t reader, const a
return EILSEQ;
}
check(rval, avro_read_data(reader, val.schema, NULL, &value));
- *datum = avro_union(discriminant, value);
+ *datum = avro_union(&readers_schema->obj, discriminant, value);
avro_datum_decref(value);
return 0;
}
@@ -257,8 +257,7 @@ read_record(avro_reader_t reader, const
AVRO_UNUSED(enc);
- record = *datum =
- avro_record(writers_schema->name, writers_schema->space);
+ record = *datum = avro_record(&readers_schema->obj);
for (i = 0; i < writers_schema->fields->num_entries; i++) {
union {
st_data_t data;
@@ -398,8 +397,6 @@ avro_read_data(avro_reader_t reader, avr
case AVRO_FIXED:{
char *bytes;
- const char *name =
- avro_schema_to_fixed(writers_schema)->name;
int64_t size =
avro_schema_to_fixed(writers_schema)->size;
@@ -409,7 +406,7 @@ avro_read_data(avro_reader_t reader, avr
}
rval = avro_read(reader, bytes, size);
if (!rval) {
- *datum = avro_givefixed(name, bytes, size,
+ *datum = avro_givefixed(readers_schema, bytes,
size,
avro_alloc_free);
}
}
Modified: avro/trunk/lang/c/src/schema.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/src/schema.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/src/schema.c (original)
+++ avro/trunk/lang/c/src/schema.c Fri Feb 25 15:43:50 2011
@@ -1290,9 +1290,7 @@ avro_datum_t avro_datum_from_schema(cons
const struct avro_record_schema_t
*record_schema =
avro_schema_to_record(schema);
- avro_datum_t rec =
- avro_record(record_schema->name,
- record_schema->space);
+ avro_datum_t rec = avro_record(schema);
int i;
for (i = 0; i <
record_schema->fields->num_entries; i++) {
@@ -1312,28 +1310,19 @@ avro_datum_t avro_datum_from_schema(cons
}
case AVRO_ENUM:
- {
- const struct avro_enum_schema_t *enum_schema =
- avro_schema_to_enum(schema);
- return avro_enum(enum_schema->name, 0);
- }
+ return avro_enum(schema, 0);
case AVRO_FIXED:
- {
- const struct avro_fixed_schema_t *fixed_schema =
- avro_schema_to_fixed(schema);
- return avro_givefixed(fixed_schema->name,
- "", 0, NULL);
- }
+ return avro_givefixed(schema, "", 0, NULL);
case AVRO_MAP:
- return avro_map();
+ return avro_map(schema);
case AVRO_ARRAY:
- return avro_array();
+ return avro_array(schema);
case AVRO_UNION:
- return avro_union(-1, NULL);
+ return avro_union(schema, -1, NULL);
case AVRO_LINK:
{
Modified: avro/trunk/lang/c/tests/generate_interop_data.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/generate_interop_data.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/generate_interop_data.c (original)
+++ avro/trunk/lang/c/tests/generate_interop_data.c Fri Feb 25 15:43:50 2011
@@ -56,7 +56,7 @@ int main(int argc, char *argv[])
check(rval, avro_file_writer_create(outpath, schema, &file_writer));
/* TODO: create a method for generating random data from schema */
- interop = avro_record("Interop", "org.apache.avro");
+ interop = avro_record(schema);
avro_record_set(interop, "intField", avro_int32(42));
avro_record_set(interop, "longField", avro_int64(4242));
avro_record_set(interop, "stringField",
@@ -67,23 +67,33 @@ int main(int argc, char *argv[])
avro_record_set(interop, "bytesField", avro_bytes("abcd", 4));
avro_record_set(interop, "nullField", avro_null());
- array_datum = avro_array();
+ avro_schema_t array_schema = avro_schema_get_subschema(schema,
"arrayField");
+ array_datum = avro_array(array_schema);
avro_array_append_datum(array_datum, avro_double(1.0));
avro_array_append_datum(array_datum, avro_double(2.0));
avro_array_append_datum(array_datum, avro_double(3.0));
avro_record_set(interop, "arrayField", array_datum);
- avro_record_set(interop, "mapField", avro_map());
- union_datum = avro_union(1, avro_double(1.61803399));
+ avro_schema_t map_schema = avro_schema_get_subschema(schema,
"mapField");
+ avro_record_set(interop, "mapField", avro_map(map_schema));
+
+ avro_schema_t union_schema = avro_schema_get_subschema(schema,
"unionField");
+ union_datum = avro_union(union_schema, 1, avro_double(1.61803399));
avro_record_set(interop, "unionField", union_datum);
- avro_record_set(interop, "enumField", avro_enum("Kind", KIND_A));
+
+ avro_schema_t enum_schema = avro_schema_get_subschema(schema,
"enumField");
+ avro_record_set(interop, "enumField", avro_enum(enum_schema, KIND_A));
+
+ avro_schema_t fixed_schema = avro_schema_get_subschema(schema,
"fixedField");
avro_record_set(interop, "fixedField",
- avro_fixed("MD5", "1234567890123456", 16));
+ avro_fixed(fixed_schema, "1234567890123456", 16));
- node_datum = avro_record("Node", NULL);
+ avro_schema_t node_schema = avro_schema_get_subschema(schema,
"recordField");
+ node_datum = avro_record(node_schema);
avro_record_set(node_datum, "label",
avro_givestring("If you label me, you negate me.",
NULL));
- avro_record_set(node_datum, "children", avro_array());
+ avro_schema_t children_schema = avro_schema_get_subschema(node_schema,
"children");
+ avro_record_set(node_datum, "children", avro_array(children_schema));
avro_record_set(interop, "recordField", node_datum);
rval = avro_file_writer_append(file_writer, interop);
Modified: avro/trunk/lang/c/tests/test_avro_data.c
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/test_avro_data.c?rev=1074585&r1=1074584&r2=1074585&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/test_avro_data.c (original)
+++ avro/trunk/lang/c/tests/test_avro_data.c Fri Feb 25 15:43:50 2011
@@ -130,11 +130,10 @@ write_read_check(avro_schema_t writers_s
}
}
-static void test_json(avro_datum_t datum, avro_schema_t schema,
- const char *expected)
+static void test_json(avro_datum_t datum, const char *expected)
{
char *json = NULL;
- avro_datum_to_json(datum, schema, 1, &json);
+ avro_datum_to_json(datum, 1, &json);
if (strcmp(json, expected) != 0) {
fprintf(stderr, "Unexpected JSON encoding: %s\n", json);
exit(EXIT_FAILURE);
@@ -158,8 +157,7 @@ static int test_string(void)
}
avro_datum_t datum = avro_givestring(strings[0], NULL);
- test_json(datum, writer_schema,
- "\"Four score and seven years ago\"");
+ test_json(datum, "\"Four score and seven years ago\"");
avro_datum_decref(datum);
// The following should bork if we don't copy the string value
@@ -182,8 +180,7 @@ static int test_bytes(void)
datum = avro_givebytes(bytes, sizeof(bytes), NULL);
write_read_check(writer_schema, NULL, datum, "bytes");
- test_json(datum, writer_schema,
- "\"\\u00de\\u00ad\\u00be\\u00ef\"");
+ test_json(datum, "\"\\u00de\\u00ad\\u00be\\u00ef\"");
avro_datum_decref(datum);
avro_schema_decref(writer_schema);
@@ -220,7 +217,7 @@ static int test_int32(void)
}
avro_datum_t datum = avro_int32(10000);
- test_json(datum, writer_schema, "10000");
+ test_json(datum, "10000");
avro_datum_decref(datum);
avro_schema_decref(writer_schema);
@@ -238,7 +235,7 @@ static int test_int64(void)
}
avro_datum_t datum = avro_int64(10000);
- test_json(datum, writer_schema, "10000");
+ test_json(datum, "10000");
avro_datum_decref(datum);
avro_schema_decref(writer_schema);
@@ -256,7 +253,7 @@ static int test_double(void)
}
avro_datum_t datum = avro_double(2000.0);
- test_json(datum, schema, "2000.0");
+ test_json(datum, "2000.0");
avro_datum_decref(datum);
avro_schema_decref(schema);
@@ -274,7 +271,7 @@ static int test_float(void)
}
avro_datum_t datum = avro_float(2000.0);
- test_json(datum, schema, "2000.0");
+ test_json(datum, "2000.0");
avro_datum_decref(datum);
avro_schema_decref(schema);
@@ -289,7 +286,7 @@ static int test_boolean(void)
for (i = 0; i <= 1; i++) {
avro_datum_t datum = avro_boolean(i);
write_read_check(schema, NULL, datum, "boolean");
- test_json(datum, schema, expected_json[i]);
+ test_json(datum, expected_json[i]);
avro_datum_decref(datum);
}
avro_schema_decref(schema);
@@ -301,7 +298,7 @@ static int test_null(void)
avro_schema_t schema = avro_schema_null();
avro_datum_t datum = avro_null();
write_read_check(schema, NULL, datum, "null");
- test_json(datum, schema, "null");
+ test_json(datum, "null");
avro_datum_decref(datum);
return 0;
}
@@ -309,7 +306,7 @@ static int test_null(void)
static int test_record(void)
{
avro_schema_t schema = avro_schema_record("person", NULL);
- avro_datum_t datum = avro_record("person", NULL);
+ avro_datum_t datum = avro_record(schema);
avro_datum_t name_datum, age_datum;
avro_schema_record_field_append(schema, "name", avro_schema_string());
@@ -322,8 +319,7 @@ static int test_record(void)
avro_record_set(datum, "age", age_datum);
write_read_check(schema, NULL, datum, "record");
- test_json(datum, schema,
- "{\"name\": \"Joseph Campbell\", \"age\": 83}");
+ test_json(datum, "{\"name\": \"Joseph Campbell\", \"age\": 83}");
int rc;
avro_record_set_field_value(rc, datum, int32, "age", 104);
@@ -352,7 +348,7 @@ static int test_enum(void)
AVRO_JAVA
};
avro_schema_t schema = avro_schema_enum("language");
- avro_datum_t datum = avro_enum("language", AVRO_C);
+ avro_datum_t datum = avro_enum(schema, AVRO_C);
avro_schema_enum_symbol_append(schema, "C");
avro_schema_enum_symbol_append(schema, "C++");
@@ -365,31 +361,31 @@ static int test_enum(void)
exit(EXIT_FAILURE);
}
- if (strcmp(avro_enum_get_name(datum, schema), "C") != 0) {
+ if (strcmp(avro_enum_get_name(datum), "C") != 0) {
fprintf(stderr, "Unexpected enum value name C\n");
exit(EXIT_FAILURE);
}
write_read_check(schema, NULL, datum, "enum");
- test_json(datum, schema, "\"C\"");
+ test_json(datum, "\"C\"");
avro_enum_set(datum, AVRO_CPP);
- if (strcmp(avro_enum_get_name(datum, schema), "C++") != 0) {
+ if (strcmp(avro_enum_get_name(datum), "C++") != 0) {
fprintf(stderr, "Unexpected enum value name C++\n");
exit(EXIT_FAILURE);
}
write_read_check(schema, NULL, datum, "enum");
- test_json(datum, schema, "\"C++\"");
+ test_json(datum, "\"C++\"");
- avro_enum_set_name(datum, schema, "Python");
+ avro_enum_set_name(datum, "Python");
if (avro_enum_get(datum) != AVRO_PYTHON) {
fprintf(stderr, "Unexpected enum value AVRO_PYTHON\n");
exit(EXIT_FAILURE);
}
write_read_check(schema, NULL, datum, "enum");
- test_json(datum, schema, "\"Python\"");
+ test_json(datum, "\"Python\"");
avro_datum_decref(datum);
avro_schema_decref(schema);
@@ -400,7 +396,7 @@ static int test_array(void)
{
int i, rval;
avro_schema_t schema = avro_schema_array(avro_schema_int());
- avro_datum_t datum = avro_array();
+ avro_datum_t datum = avro_array(schema);
for (i = 0; i < 10; i++) {
avro_datum_t i32_datum = avro_int32(i);
@@ -417,7 +413,7 @@ static int test_array(void)
}
write_read_check(schema, NULL, datum, "array");
- test_json(datum, schema, "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
+ test_json(datum, "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
avro_datum_decref(datum);
avro_schema_decref(schema);
return 0;
@@ -426,7 +422,7 @@ static int test_array(void)
static int test_map(void)
{
avro_schema_t schema = avro_schema_map(avro_schema_long());
- avro_datum_t datum = avro_map();
+ avro_datum_t datum = avro_map(schema);
int64_t i = 0;
char *nums[] =
{ "zero", "one", "two", "three", "four", "five", "six", NULL };
@@ -455,7 +451,7 @@ static int test_map(void)
}
write_read_check(schema, NULL, datum, "map");
- test_json(datum, schema,
+ test_json(datum,
"{\"zero\": 0, \"one\": 1, \"two\": 2, \"three\": 3, "
"\"four\": 4, \"five\": 5, \"six\": 6}");
avro_datum_decref(datum);
@@ -476,7 +472,7 @@ static int test_union(void)
avro_schema_union_append(schema, avro_schema_null());
datum = avro_givestring("Follow your bliss.", NULL);
- union_datum = avro_union(0, datum);
+ union_datum = avro_union(schema, 0, datum);
if (avro_union_discriminant(union_datum) != 0) {
fprintf(stderr, "Unexpected union discriminant\n");
@@ -489,7 +485,7 @@ static int test_union(void)
}
union_datum1 = avro_datum_from_schema(schema);
- avro_union_set_discriminant(union_datum1, schema, 0, &datum1);
+ avro_union_set_discriminant(union_datum1, 0, &datum1);
avro_givestring_set(datum1, "Follow your bliss.", NULL);
if (!avro_datum_equal(datum, datum1)) {
@@ -498,12 +494,11 @@ static int test_union(void)
}
write_read_check(schema, NULL, union_datum, "union");
- test_json(union_datum, schema,
- "{\"string\": \"Follow your bliss.\"}");
+ test_json(union_datum, "{\"string\": \"Follow your bliss.\"}");
avro_datum_decref(datum);
- avro_union_set_discriminant(union_datum, schema, 2, &datum);
- test_json(union_datum, schema, "null");
+ avro_union_set_discriminant(union_datum, 2, &datum);
+ test_json(union_datum, "null");
avro_datum_decref(union_datum);
avro_datum_decref(datum);
@@ -519,14 +514,14 @@ static int test_fixed(void)
avro_datum_t datum;
avro_datum_t expected_datum;
- datum = avro_givefixed("msg", bytes, sizeof(bytes), NULL);
+ datum = avro_givefixed(schema, bytes, sizeof(bytes), NULL);
write_read_check(schema, NULL, datum, "fixed");
- test_json(datum, schema, "\"\\r\\n\\r\\n\\u000b\\n\\u000b\\n\"");
+ test_json(datum, "\"\\r\\n\\r\\n\\u000b\\n\\u000b\\n\"");
avro_datum_decref(datum);
- datum = avro_givefixed("msg", NULL, 0, NULL);
+ datum = avro_givefixed(schema, NULL, 0, NULL);
avro_givefixed_set(datum, bytes, sizeof(bytes), NULL);
- expected_datum = avro_givefixed("msg", bytes, sizeof(bytes), NULL);
+ expected_datum = avro_givefixed(schema, bytes, sizeof(bytes), NULL);
if (!avro_datum_equal(datum, expected_datum)) {
fprintf(stderr,
"Expected equal fixed instances.\n");
@@ -538,7 +533,7 @@ static int test_fixed(void)
// The following should bork if we don't copy the fixed value
// correctly (since we'll try to free a static string).
- datum = avro_fixed("msg", "original", 8);
+ datum = avro_fixed(schema, "original", 8);
avro_fixed_set(datum, "alsothis", 8);
avro_datum_decref(datum);