avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mas...@apache.org
Subject svn commit: r902315 - in /hadoop/avro/trunk: CHANGES.txt lang/c/src/avro.h lang/c/src/datum.c lang/c/src/datum_equal.c lang/c/src/datum_read.c lang/c/src/datum_write.c lang/c/tests/test_avro_data.c lang/c/version.sh
Date Sat, 23 Jan 2010 00:07:18 GMT
Author: massie
Date: Sat Jan 23 00:07:16 2010
New Revision: 902315

URL: http://svn.apache.org/viewvc?rev=902315&view=rev
Log:
AVRO-371.  Add support for encoding/decoding unions

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/c/src/avro.h
    hadoop/avro/trunk/lang/c/src/datum.c
    hadoop/avro/trunk/lang/c/src/datum_equal.c
    hadoop/avro/trunk/lang/c/src/datum_read.c
    hadoop/avro/trunk/lang/c/src/datum_write.c
    hadoop/avro/trunk/lang/c/tests/test_avro_data.c
    hadoop/avro/trunk/lang/c/version.sh

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Sat Jan 23 00:07:16 2010
@@ -250,6 +250,8 @@
 
     AVRO-370. Add support for encoding/decoding fixed data (massie)
 
+    AVRO-371. Add support for encoding/decoding unions (massie)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Modified: hadoop/avro/trunk/lang/c/src/avro.h
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/avro.h?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/avro.h (original)
+++ hadoop/avro/trunk/lang/c/src/avro.h Sat Jan 23 00:07:16 2010
@@ -203,8 +203,7 @@
 int avro_array_append_datum(const avro_datum_t array_datum,
 			    const avro_datum_t datum);
 
-avro_datum_t avro_union(void);
-int avro_union_append(const avro_datum_t union_value, const avro_datum_t value);
+avro_datum_t avro_union(const avro_schema_t schema, const avro_datum_t datum);
 
 avro_datum_t avro_datum_incref(avro_datum_t value);
 void avro_datum_decref(avro_datum_t value);

Modified: hadoop/avro/trunk/lang/c/src/datum.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.c?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum.c Sat Jan 23 00:07:16 2010
@@ -479,9 +479,7 @@
 				free(array);
 			}
 			break;
-		case AVRO_UNION:{
-				/* TODO */
-			}
+		case AVRO_UNION:
 			break;
 		case AVRO_LINK:{
 				/* TODO */

Modified: hadoop/avro/trunk/lang/c/src/datum_equal.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum_equal.c?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_equal.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_equal.c Sat Jan 23 00:07:16 2010
@@ -144,6 +144,7 @@
 				   avro_datum_to_fixed(b));
 
 	case AVRO_UNION:
+		break;
 	case AVRO_LINK:
 		/*
 		 * TODO 

Modified: hadoop/avro/trunk/lang/c/src/datum_read.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum_read.c?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_read.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_read.c Sat Jan 23 00:07:16 2010
@@ -225,10 +225,26 @@
 
 static int
 read_union(avro_reader_t reader, const avro_encoding_t * enc,
-	   avro_schema_t writers_schema, avro_schema_t readers_schema,
-	   avro_datum_t * datum)
+	   struct avro_union_schema_t *writers_schema,
+	   struct avro_union_schema_t *readers_schema, avro_datum_t * datum)
 {
-	return 1;
+	int rval;
+	int64_t i, index;
+	struct avro_union_branch_t *branch;
+
+	rval = enc->read_long(reader, &index);
+	if (rval) {
+		return rval;
+	}
+
+	branch = STAILQ_FIRST(&writers_schema->branches);
+	for (i = 0; i != index && branch != NULL;
+	     branch = STAILQ_NEXT(branch, branches)) {
+	}
+	if (!branch) {
+		return EILSEQ;
+	}
+	return avro_read_data(reader, branch->schema, NULL, datum);
 }
 
 /* TODO: handle default values in fields */
@@ -427,8 +443,9 @@
 
 	case AVRO_UNION:
 		rval =
-		    read_union(reader, enc, writers_schema, readers_schema,
-			       datum);
+		    read_union(reader, enc,
+			       avro_schema_to_union(writers_schema),
+			       avro_schema_to_union(readers_schema), datum);
 		break;
 
 	case AVRO_RECORD:

Modified: hadoop/avro/trunk/lang/c/src/datum_write.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum_write.c?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_write.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_write.c Sat Jan 23 00:07:16 2010
@@ -128,6 +128,29 @@
 	return enc->write_long(writer, 0);
 }
 
+static int
+write_union(avro_writer_t writer, const avro_encoding_t * enc,
+	    struct avro_union_schema_t *schema, avro_datum_t datum)
+{
+	int rval;
+	int64_t index;
+	struct avro_union_branch_t *branch = STAILQ_FIRST(&schema->branches);
+	for (index = 0; branch != NULL;
+	     branch = STAILQ_NEXT(branch, branches), index++) {
+		if (avro_schema_datum_validate(branch->schema, datum)) {
+			break;
+		}
+	}
+	if (!branch) {
+		return EINVAL;
+	}
+	rval = enc->write_long(writer, index);
+	if (rval) {
+		return rval;
+	}
+	return avro_write_data(writer, branch->schema, datum);
+}
+
 int
 avro_write_data(avro_writer_t writer, avro_schema_t writer_schema,
 		avro_datum_t datum)
@@ -242,9 +265,9 @@
 		break;
 
 	case AVRO_UNION:
-		{
-			assert(0 && "Bug in schema validation code");
-		}
+		rval =
+		    write_union(writer, enc,
+				avro_schema_to_union(writer_schema), datum);
 		break;
 
 	case AVRO_LINK:

Modified: hadoop/avro/trunk/lang/c/tests/test_avro_data.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/tests/test_avro_data.c?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/tests/test_avro_data.c (original)
+++ hadoop/avro/trunk/lang/c/tests/test_avro_data.c Sat Jan 23 00:07:16 2010
@@ -229,9 +229,18 @@
 
 static int test_union(void)
 {
-	/*
-	 * TODO 
-	 */
+	avro_schema_t schema = avro_schema_union();
+	avro_datum_t datum;
+
+	avro_schema_union_append(schema, avro_schema_string());
+	avro_schema_union_append(schema, avro_schema_int());
+	avro_schema_union_append(schema, avro_schema_null());
+
+	datum = avro_wrapstring("Follow your bliss.");
+
+	write_read_check(schema, NULL, datum, "union");
+	avro_datum_decref(datum);
+	avro_schema_decref(schema);
 	return 0;
 }
 
@@ -266,7 +275,8 @@
 		"enum", test_enum}, {
 		"array", test_array}, {
 		"map", test_map}, {
-		"fixed", test_fixed}
+		"fixed", test_fixed}, {
+		"union", test_union}
 	};
 
 	srandom(time(NULL));

Modified: hadoop/avro/trunk/lang/c/version.sh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/version.sh?rev=902315&r1=902314&r2=902315&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/version.sh (original)
+++ hadoop/avro/trunk/lang/c/version.sh Sat Jan 23 00:07:16 2010
@@ -18,7 +18,7 @@
 #         libavro_binary_age = 0
 #         libavro_interface_age = 0
 #
-libavro_micro_version=10
+libavro_micro_version=11
 libavro_interface_age=0
 libavro_binary_age=0
 



Mime
View raw message