avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mas...@apache.org
Subject svn commit: r901392 - in /hadoop/avro/trunk: CHANGES.txt lang/c/docs/index.txt lang/c/src/avro.h lang/c/src/datum.c lang/c/src/datum.h lang/c/tests/test_avro_data.c lang/c/version.sh
Date Wed, 20 Jan 2010 21:52:36 GMT
Author: massie
Date: Wed Jan 20 21:52:35 2010
New Revision: 901392

URL: http://svn.apache.org/viewvc?rev=901392&view=rev
Log:
AVRO-359.  Add support for encoding/decoding arrays and maps

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/c/docs/index.txt
    hadoop/avro/trunk/lang/c/src/avro.h
    hadoop/avro/trunk/lang/c/src/datum.c
    hadoop/avro/trunk/lang/c/src/datum.h
    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=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Wed Jan 20 21:52:35 2010
@@ -231,6 +231,8 @@
 
     AVRO-353. Publish the C API to avro-doc package when 'dist' target run (massie)
 
+    AVRO-359. Add support for encoding/decoding arrays and maps (massie)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Modified: hadoop/avro/trunk/lang/c/docs/index.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/docs/index.txt?rev=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/docs/index.txt (original)
+++ hadoop/avro/trunk/lang/c/docs/index.txt Wed Jan 20 21:52:35 2010
@@ -41,3 +41,12 @@
 include::../src/avro.h[]
 ----
 
+Another good way to learn how to encode/decode data in +Avro C+ is
+to look at the +test_avro_data.c+ unit test.  This simple unit test
+checks that all the avro types can be encoded/decoded correctly.
+
+[source,c]
+----
+include::../tests/test_avro_data.c[]
+----
+

Modified: hadoop/avro/trunk/lang/c/src/avro.h
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/avro.h?rev=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/avro.h (original)
+++ hadoop/avro/trunk/lang/c/src/avro.h Wed Jan 20 21:52:35 2010
@@ -182,8 +182,13 @@
   avro_datum_t avro_fixed (const char *name, const int64_t len,
 			   const char *bytes);
 
-  avro_datum_t avro_map (const avro_datum_t values);
-  avro_datum_t avro_array (const avro_datum_t items);
+  avro_datum_t avro_map (void);
+  int avro_map_set (const avro_datum_t map, const char *key,
+		    const avro_datum_t value);
+
+  avro_datum_t avro_array (void);
+  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,
@@ -197,10 +202,10 @@
   int avro_datum_equal (avro_datum_t a, avro_datum_t b);
 
   int avro_schema_match (avro_schema_t writers_schema,
-		    avro_schema_t readers_schema);
+			 avro_schema_t readers_schema);
 
   int avro_schema_datum_validate (avro_schema_t expected_schema,
-			     avro_datum_t datum);
+				  avro_datum_t datum);
 
   int avro_read_data (avro_reader_t reader, avro_schema_t writer_schema,
 		      avro_schema_t reader_schema, avro_datum_t * datum);

Modified: hadoop/avro/trunk/lang/c/src/datum.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.c?rev=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum.c Wed Jan 20 21:52:35 2010
@@ -34,6 +34,69 @@
   datum->refcount = 1;
 }
 
+static int
+array_equal (struct avro_array_datum_t *a, struct avro_array_datum_t *b)
+{
+  struct avro_array_element_t *a_el, *b_el;
+  if (a->num_elements != b->num_elements)
+    {
+      return 0;
+    }
+  for (a_el = STAILQ_FIRST (&a->els),
+       b_el = STAILQ_FIRST (&b->els);
+       !(a_el == NULL && a_el == NULL);
+       a_el = STAILQ_NEXT (a_el, els), b_el = STAILQ_NEXT (b_el, els))
+    {
+      if (a_el == NULL || b_el == NULL)
+	{
+	  return 0;		/* different number of elements */
+	}
+      if (!avro_datum_equal (a_el->datum, b_el->datum))
+	{
+	  return 0;
+	}
+    }
+  return 1;
+}
+
+struct map_equal_args
+{
+  int rval;
+  st_table *st;
+};
+
+static int
+map_equal_foreach (char *key, avro_datum_t datum, struct map_equal_args *args)
+{
+  avro_datum_t datum_other = NULL;
+
+  st_lookup (args->st, (st_data_t) key, (st_data_t *) & datum_other);
+  if (!datum_other)
+    {
+      args->rval = 0;
+      return ST_STOP;
+    }
+  if (!avro_datum_equal (datum, datum_other))
+    {
+      args->rval = 0;
+      return ST_STOP;
+    }
+  return ST_CONTINUE;
+}
+
+static int
+map_equal (struct avro_map_datum_t *a, struct avro_map_datum_t *b)
+{
+  struct map_equal_args args = { 1, avro_datum_to_map (b)->map };
+  if (a->map->num_entries != b->map->num_entries)
+    {
+      return 0;
+    }
+  st_foreach (avro_datum_to_map (a)->map,
+	      map_equal_foreach, (st_data_t) & args);
+  return args.rval;
+}
+
 int
 avro_datum_equal (avro_datum_t a, avro_datum_t b)
 {
@@ -67,11 +130,13 @@
       return avro_datum_to_boolean (a)->i == avro_datum_to_boolean (b)->i;
     case AVRO_NULL:
       return 1;
+    case AVRO_ARRAY:
+      return array_equal (avro_datum_to_array (a), avro_datum_to_array (b));
+    case AVRO_MAP:
+      return map_equal (avro_datum_to_map (a), avro_datum_to_map (b));
     case AVRO_RECORD:
     case AVRO_ENUM:
     case AVRO_FIXED:
-    case AVRO_MAP:
-    case AVRO_ARRAY:
     case AVRO_UNION:
     case AVRO_LINK:
       /* TODO */
@@ -283,7 +348,7 @@
 }
 
 avro_datum_t
-avro_map (const avro_datum_t values)
+avro_map (void)
 {
   struct avro_map_datum_t *datum = malloc (sizeof (struct avro_map_datum_t));
   if (!datum)
@@ -295,8 +360,23 @@
   return &datum->obj;
 }
 
+int
+avro_map_set (const avro_datum_t datum, const char *key,
+	      const avro_datum_t value)
+{
+  struct avro_map_datum_t *map;
+  if (!is_avro_datum (datum) || !is_avro_map (datum) || !key
+      || !is_avro_datum (value))
+    {
+      return EINVAL;
+    }
+  map = avro_datum_to_map (datum);
+  st_insert (map->map, (st_data_t) key, (st_data_t) value);
+  return 0;
+}
+
 avro_datum_t
-avro_array (const avro_datum_t items)
+avro_array (void)
 {
   struct avro_array_datum_t *datum =
     malloc (sizeof (struct avro_array_datum_t));
@@ -305,10 +385,36 @@
       return NULL;
     }
   STAILQ_INIT (&datum->els);
+  datum->num_elements = 0;
+
   avro_datum_init (&datum->obj, AVRO_ARRAY);
   return &datum->obj;
 }
 
+int
+avro_array_append_datum (const avro_datum_t array_datum,
+			 const avro_datum_t datum)
+{
+  struct avro_array_datum_t *array;
+  struct avro_array_element_t *el;
+  if (!is_avro_datum (array_datum) || !is_avro_array (array_datum)
+      || !is_avro_datum (datum))
+    {
+      return EINVAL;
+    }
+  array = avro_datum_to_array (array_datum);
+  el = malloc (sizeof (struct avro_array_element_t));
+  if (!el)
+    {
+      return ENOMEM;
+    }
+  el->datum = datum;
+  STAILQ_INSERT_TAIL (&array->els, el, els);
+  array->num_elements++;
+  return 0;
+}
+
+
 avro_datum_t
 avro_datum_incref (avro_datum_t value)
 {
@@ -331,72 +437,67 @@
 int
 avro_schema_match (avro_schema_t writers_schema, avro_schema_t readers_schema)
 {
-  if (is_avro_union (writers_schema) || is_avro_union (readers_schema))
-    {
-      return 1;
-    }
-  /* union */
-  else if (is_avro_primitive (writers_schema)
-	   && is_avro_primitive (readers_schema)
-	   && avro_typeof (writers_schema) == avro_typeof (readers_schema))
-    {
-      return 1;
-    }
-  /* record */
-  else if (is_avro_record (writers_schema) && is_avro_record (readers_schema)
-	   && strcmp (avro_schema_name (writers_schema),
-		      avro_schema_name (readers_schema)) == 0)
+  if (!is_avro_schema (writers_schema) || !is_avro_schema (readers_schema))
     {
-      return 1;
-    }
-  /* fixed */
-  else if (is_avro_fixed (writers_schema) && is_avro_fixed (readers_schema)
-	   && strcmp (avro_schema_name (writers_schema),
-		      avro_schema_name (readers_schema)) == 0
-	   && (avro_schema_to_fixed (writers_schema))->size ==
-	   (avro_schema_to_fixed (readers_schema))->size)
-    {
-      return 1;
-    }
-  /* enum */
-  else if (is_avro_enum (writers_schema) && is_avro_enum (readers_schema)
-	   && strcmp (avro_schema_name (writers_schema),
-		      avro_schema_name (readers_schema)) == 0)
-    {
-      return 1;
-    }
-  /* map */
-  else if (is_avro_map (writers_schema) && is_avro_map (readers_schema)
-	   && avro_typeof ((avro_schema_to_map (writers_schema))->values)
-	   == avro_typeof ((avro_schema_to_map (readers_schema))->values))
-    {
-      return 1;
-    }
-  /* array */
-  else if (is_avro_array (writers_schema) && is_avro_array (readers_schema)
-	   && avro_typeof ((avro_schema_to_array (writers_schema))->items)
-	   == avro_typeof ((avro_schema_to_array (readers_schema))->items))
-    {
-      return 1;
+      return 0;
     }
 
-  /* handle schema promotion */
-  else if (is_avro_int (writers_schema)
-	   && (is_avro_long (readers_schema) || is_avro_float (readers_schema)
-	       || is_avro_double (readers_schema)))
-    {
-      return 1;
-    }
-  else if (is_avro_long (writers_schema)
-	   && (is_avro_float (readers_schema)
-	       && is_avro_double (readers_schema)))
-    {
-      return 1;
-    }
-  else if (is_avro_float (writers_schema) && is_avro_double (readers_schema))
+  switch (avro_typeof (writers_schema))
     {
+    case AVRO_UNION:
       return 1;
+
+    case AVRO_INT:
+      return is_avro_int (readers_schema) || is_avro_long (readers_schema)
+	|| is_avro_float (readers_schema) || is_avro_double (readers_schema);
+
+    case AVRO_LONG:
+      return is_avro_long (readers_schema) || is_avro_float (readers_schema)
+	|| is_avro_double (readers_schema);
+
+    case AVRO_FLOAT:
+      return is_avro_float (readers_schema)
+	|| is_avro_double (readers_schema);
+
+    case AVRO_STRING:
+    case AVRO_BYTES:
+    case AVRO_DOUBLE:
+    case AVRO_BOOLEAN:
+    case AVRO_NULL:
+      return avro_typeof (writers_schema) == avro_typeof (readers_schema);
+
+    case AVRO_RECORD:
+      return is_avro_record (readers_schema)
+	&& strcmp (avro_schema_name (writers_schema),
+		   avro_schema_name (readers_schema)) == 0;
+
+    case AVRO_FIXED:
+      return is_avro_fixed (readers_schema)
+	&& strcmp (avro_schema_name (writers_schema),
+		   avro_schema_name (readers_schema)) == 0
+	&& (avro_schema_to_fixed (writers_schema))->size ==
+	(avro_schema_to_fixed (readers_schema))->size;
+
+    case AVRO_ENUM:
+      return is_avro_enum (readers_schema)
+	&& strcmp (avro_schema_to_enum (writers_schema)->name,
+		   avro_schema_to_enum (readers_schema)->name) == 0;
+
+    case AVRO_MAP:
+      return is_avro_map (readers_schema)
+	&& avro_typeof (avro_schema_to_map (writers_schema)->values)
+	== avro_typeof (avro_schema_to_map (readers_schema)->values);
+
+    case AVRO_ARRAY:
+      return is_avro_array (readers_schema)
+	&& avro_typeof (avro_schema_to_array (writers_schema)->items)
+	== avro_typeof (avro_schema_to_array (readers_schema)->items);
+
+    case AVRO_LINK:
+      /* TODO */
+      break;
     }
+
   return 0;
 }
 
@@ -418,18 +519,121 @@
 
 static int
 read_array (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_array_schema_t *writers_schema,
+	    struct avro_array_schema_t *readers_schema, avro_datum_t * datum)
 {
-  return 1;
+  int rval;
+  int64_t i;
+  int64_t block_count;
+  int64_t block_size;
+  avro_datum_t array_datum;
+
+  rval = enc->read_long (reader, &block_count);
+  if (rval)
+    {
+      return rval;
+    }
+
+  array_datum = avro_array ();
+  while (block_count != 0)
+    {
+      if (block_count < 0)
+	{
+	  block_count = block_count * -1;
+	  rval = enc->read_long (reader, &block_size);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	}
+
+      for (i = 0; i < block_count; i++)
+	{
+	  avro_datum_t datum;
+
+	  rval =
+	    avro_read_data (reader, writers_schema->items,
+			    readers_schema->items, &datum);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	  rval = avro_array_append_datum (array_datum, datum);
+	  if (rval)
+	    {
+	      avro_datum_decref (array_datum);
+	      return rval;
+	    }
+	}
+
+      rval = enc->read_long (reader, &block_count);
+      if (rval)
+	{
+	  return rval;
+	}
+    }
+  *datum = array_datum;
+  return 0;
 }
 
 static int
 read_map (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_map_schema_t *writers_schema,
+	  struct avro_map_schema_t *readers_schema, avro_datum_t * datum)
 {
-  return 1;
+  int rval;
+  int64_t i, block_count;
+  avro_datum_t map = avro_map ();
+
+  rval = enc->read_long (reader, &block_count);
+  if (rval)
+    {
+      return rval;
+    }
+  while (block_count != 0)
+    {
+      int64_t block_size;
+      if (block_count < 0)
+	{
+	  block_count = block_count * -1;
+	  rval = enc->read_long (reader, &block_size);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	}
+      for (i = 0; i < block_count; i++)
+	{
+	  char *key;
+	  avro_datum_t value;
+	  rval = enc->read_string (reader, &key);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	  rval =
+	    avro_read_data (reader,
+			    avro_schema_to_map (writers_schema)->values,
+			    avro_schema_to_map (readers_schema)->values,
+			    &value);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	  rval = avro_map_set (map, key, value);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	}
+      rval = enc->read_long (reader, &block_count);
+      if (rval)
+	{
+	  return rval;
+	}
+    }
+  *datum = map;
+  return 0;
 }
 
 static int
@@ -492,6 +696,7 @@
     {
     case AVRO_NULL:
       rval = enc->read_null (reader);
+      *datum = avro_null ();
       break;
 
     case AVRO_BOOLEAN:
@@ -560,11 +765,15 @@
       break;
 
     case AVRO_ARRAY:
-      rval = read_array (reader, enc, writers_schema, readers_schema, datum);
+      rval =
+	read_array (reader, enc, avro_schema_to_array (writers_schema),
+		    avro_schema_to_array (readers_schema), datum);
       break;
 
     case AVRO_MAP:
-      rval = read_map (reader, enc, writers_schema, readers_schema, datum);
+      rval =
+	read_map (reader, enc, avro_schema_to_map (writers_schema),
+		  avro_schema_to_map (readers_schema), datum);
       break;
 
     case AVRO_UNION:
@@ -689,7 +898,8 @@
     case AVRO_MAP:
       if (is_avro_map (datum))
 	{
-	  struct validate_st vst = { expected_schema, 1 };
+	  struct validate_st vst =
+	    { avro_schema_to_map (expected_schema)->values, 1 };
 	  st_foreach (avro_datum_to_map (datum)->map,
 		      schema_map_validate_foreach, (st_data_t) & vst);
 	  return vst.rval;
@@ -742,8 +952,8 @@
     case AVRO_LINK:
       {
 	return
-	  avro_schema_datum_validate ((avro_schema_to_link (expected_schema))->to,
-				 datum);
+	  avro_schema_datum_validate ((avro_schema_to_link
+				       (expected_schema))->to, datum);
       }
       break;
     }
@@ -774,20 +984,87 @@
   return EINVAL;
 }
 
+struct write_map_args
+{
+  int rval;
+  avro_writer_t writer;
+  const avro_encoding_t *enc;
+  avro_schema_t values_schema;
+};
+
+static int
+write_map_foreach (char *key, avro_datum_t datum, struct write_map_args *args)
+{
+  int rval = args->enc->write_string (args->writer, key);
+  if (rval)
+    {
+      args->rval = rval;
+      return ST_STOP;
+    }
+  rval = avro_write_data (args->writer, args->values_schema, datum);
+  if (rval)
+    {
+      args->rval = rval;
+      return ST_STOP;
+    }
+  return ST_CONTINUE;
+}
+
 static int
 write_map (avro_writer_t writer, const avro_encoding_t * enc,
-	   avro_schema_t writer_schema, avro_datum_t datum)
+	   struct avro_map_schema_t *writer_schema,
+	   struct avro_map_datum_t *datum)
 {
-  /* TODO */
-  return EINVAL;
+  int rval;
+  struct write_map_args args = { 0, writer, enc, writer_schema->values };
+
+  if (datum->map->num_entries)
+    {
+      rval = enc->write_long (writer, datum->map->num_entries);
+      if (rval)
+	{
+	  return rval;
+	}
+      st_foreach (datum->map, write_map_foreach, (st_data_t) & args);
+    }
+  if (!args.rval)
+    {
+      rval = enc->write_long (writer, 0);
+      if (rval)
+	{
+	  return rval;
+	}
+      return 0;
+    }
+  return args.rval;
 }
 
 static int
 write_array (avro_writer_t writer, const avro_encoding_t * enc,
-	     avro_schema_t writer_schema, avro_datum_t datum)
+	     struct avro_array_schema_t *schema,
+	     struct avro_array_datum_t *array)
 {
-  /* TODO */
-  return EINVAL;
+  int rval;
+  struct avro_array_element_t *el;
+
+  if (array->num_elements)
+    {
+      rval = enc->write_long (writer, array->num_elements);
+      if (rval)
+	{
+	  return rval;
+	}
+      for (el = STAILQ_FIRST (&array->els);
+	   el != NULL; el = STAILQ_NEXT (el, els))
+	{
+	  rval = avro_write_data (writer, schema->items, el->datum);
+	  if (rval)
+	    {
+	      return rval;
+	    }
+	}
+    }
+  return enc->write_long (writer, 0);
 }
 
 int
@@ -797,7 +1074,7 @@
   const avro_encoding_t *enc = &avro_binary_encoding;
   int rval = -1;
 
-  if (!(is_avro_schema (writer_schema) && is_avro_datum (datum)))
+  if (!writer || !(is_avro_schema (writer_schema) && is_avro_datum (datum)))
     {
       return EINVAL;
     }
@@ -904,10 +1181,14 @@
       rval = write_fixed (writer, enc, writer_schema, datum);
       break;
     case AVRO_MAP:
-      rval = write_map (writer, enc, writer_schema, datum);
+      rval =
+	write_map (writer, enc, avro_schema_to_map (writer_schema),
+		   avro_datum_to_map (datum));
       break;
     case AVRO_ARRAY:
-      rval = write_array (writer, enc, writer_schema, datum);
+      rval =
+	write_array (writer, enc, avro_schema_to_array (writer_schema),
+		     avro_datum_to_array (datum));
       break;
 
     case AVRO_UNION:

Modified: hadoop/avro/trunk/lang/c/src/datum.h
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.h?rev=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.h (original)
+++ hadoop/avro/trunk/lang/c/src/datum.h Wed Jan 20 21:52:35 2010
@@ -104,6 +104,7 @@
 struct avro_array_datum_t
 {
   struct avro_obj_t obj;
+  int64_t num_elements;
     STAILQ_HEAD (els, avro_array_element_t) els;
 };
 

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=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/tests/test_avro_data.c (original)
+++ hadoop/avro/trunk/lang/c/tests/test_avro_data.c Wed Jan 20 21:52:35 2010
@@ -20,34 +20,42 @@
 #include <stdint.h>
 #include <time.h>
 #include <string.h>
-#include <assert.h>
 #include "avro.h"
-#include "dump.h"
-#include "datum.h"
 
 char buf[4096];
+avro_reader_t reader;
+avro_writer_t writer;
 
 typedef int (*avro_test) (void);
 
-struct test_case
+void
+write_read_check (avro_schema_t writers_schema, avro_schema_t readers_schema,
+		  avro_datum_t datum, char *type)
 {
-  long value;
-  unsigned len;
-  uint8_t bytes[16];
-};
-typedef struct test_case test_case;
-
-struct test_case test_cases[] = {
-  {.value = 0,.len = 1,.bytes = {0x0}},
-  {.value = -1,.len = 1,.bytes = {0x1}},
-  {.value = 1,.len = 1,.bytes = {0x2}},
-  {.value = -2,.len = 1,.bytes = {0x3}},
-  {.value = 2,.len = 1,.bytes = {0x4}},
-  {.value = -64,.len = 1,.bytes = {0x7f}},
-  {.value = 64,.len = 2,.bytes = {0x80, 0x01}},
-  {.value = -65,.len = 2,.bytes = {0x81, 0x01}},
-  {.value = 65,.len = 2,.bytes = {0x82, 0x01}}
-};
+  avro_datum_t datum_out;
+  reader = avro_reader_memory (buf, sizeof (buf));
+  writer = avro_writer_memory (buf, sizeof (buf));
+
+  if (avro_write_data (writer, writers_schema, datum))
+    {
+      fprintf (stderr, "Unable to write %s\n", type);
+      exit (EXIT_FAILURE);
+    }
+  if (avro_read_data (reader, writers_schema, readers_schema, &datum_out))
+    {
+      fprintf (stderr, "Unable to read %s\n", type);
+      exit (EXIT_FAILURE);
+    }
+  if (!avro_datum_equal (datum, datum_out))
+    {
+      fprintf (stderr, "Unable to encode/decode %s\n", type);
+      exit (EXIT_FAILURE);
+    }
+
+  avro_datum_decref (datum_out);
+  avro_reader_free (reader);
+  avro_writer_free (writer);
+}
 
 static int
 test_string (void)
@@ -60,38 +68,10 @@
   };
   for (i = 0; i < sizeof (strings) / sizeof (strings[0]); i++)
     {
-      avro_reader_t reader;
-      avro_writer_t writer;
       avro_schema_t writer_schema = avro_schema_string ();
-      avro_datum_t datum_in = avro_string (strings[i]);
-      avro_datum_t datum_out;
-
-      reader = avro_reader_memory (buf, sizeof (buf));
-      if (!reader)
-	{
-	  assert (0 && "Can't create a memory reader");
-	}
-      writer = avro_writer_memory (buf, sizeof (buf));
-      if (!writer)
-	{
-	  assert (0 && "Can't create a memory writer");
-	}
-      if (avro_write_data (writer, writer_schema, datum_in))
-	{
-	  assert (0 && "Can't write string");
-	}
-      if (avro_read_data (reader, writer_schema, NULL, &datum_out))
-	{
-	  assert (0 && "Can't read string");
-	}
-      if (!avro_datum_equal (datum_in, datum_out))
-	{
-	  assert (0 && "String didn't survive encoding/decoding");
-	}
-      avro_datum_decref (datum_in);
-      avro_datum_decref (datum_out);
-      avro_reader_free (reader);
-      avro_writer_free (writer);
+      avro_datum_t datum = avro_string (strings[i]);
+      write_read_check (writer_schema, NULL, datum, "string");
+      avro_datum_decref (datum);
     }
   return 0;
 }
@@ -100,110 +80,52 @@
 test_bytes (void)
 {
   char bytes[] = { 0xDE, 0xAD, 0xBE, 0xEF };
-  avro_reader_t reader = avro_reader_memory (buf, sizeof (buf));
-  avro_writer_t writer = avro_writer_memory (buf, sizeof (buf));
   avro_schema_t writer_schema = avro_schema_bytes ();
-  avro_datum_t datum_in = avro_bytes (bytes, sizeof (bytes));
-  avro_datum_t datum_out;
+  avro_datum_t datum = avro_bytes (bytes, sizeof (bytes));
 
-  if (avro_write_data (writer, writer_schema, datum_in))
-    {
-      assert (0 && "Unable to write bytes");
-    }
-  if (avro_read_data (reader, writer_schema, NULL, &datum_out))
-    {
-      assert (0 && "Unable to read bytes");
-    }
-  if (!avro_datum_equal (datum_in, datum_out))
-    {
-      assert (0 && "Byte did not encode/decode correctly");
-    }
-  avro_datum_decref (datum_in);
-  avro_datum_decref (datum_out);
-  avro_reader_free (reader);
-  avro_writer_free (writer);
+  write_read_check (writer_schema, NULL, datum, "bytes");
+  avro_datum_decref (datum);
   return 0;
 }
 
 static int
-test_int_long (int long_test)
+test_int (void)
 {
   int i;
   for (i = 0; i < 100; i++)
     {
-      avro_reader_t reader = avro_reader_memory (buf, sizeof (buf));
-      avro_writer_t writer = avro_writer_memory (buf, sizeof (buf));
-      avro_schema_t writer_schema =
-	long_test ? avro_schema_long () : avro_schema_int ();
-      avro_datum_t datum_in = long_test ? avro_long (rand ()) :
-	avro_int (rand ());
-      avro_datum_t datum_out;
-
-      if (avro_write_data (writer, writer_schema, datum_in))
-	{
-	  assert (0 && "Unable to write int/long");
-	}
-      if (avro_read_data (reader, writer_schema, NULL, &datum_out))
-	{
-	  assert (0 && "Unable to read int/long");
-	}
-      if (!avro_datum_equal (datum_in, datum_out))
-	{
-	  assert (0 && "Unable to encode/decode int/long");
-	}
-      avro_datum_decref (datum_in);
-      avro_datum_decref (datum_out);
-      avro_reader_free (reader);
-      avro_writer_free (writer);
+      avro_schema_t writer_schema = avro_schema_int ();
+      avro_datum_t datum = avro_int (rand ());
+      write_read_check (writer_schema, NULL, datum, "int");
+      avro_datum_decref (datum);
     }
   return 0;
 }
 
 static int
-test_int (void)
-{
-  return test_int_long (0);
-}
-
-static int
 test_long (void)
 {
-  return test_int_long (1);
+  int i;
+  for (i = 0; i < 100; i++)
+    {
+      avro_schema_t writer_schema = avro_schema_long ();
+      avro_datum_t datum = avro_long (rand ());
+      write_read_check (writer_schema, NULL, datum, "long");
+      avro_datum_decref (datum);
+    }
+  return 0;
 }
 
 static
-test_float_double (int double_test)
+test_double (void)
 {
   int i;
-
   for (i = 0; i < 100; i++)
     {
-      avro_reader_t reader = avro_reader_memory (buf, sizeof (buf));
-      avro_writer_t writer = avro_writer_memory (buf, sizeof (buf));
-      avro_schema_t schema =
-	double_test ? avro_schema_double () : avro_schema_float ();
-      avro_datum_t datum_in =
-	double_test ? avro_double ((double) (rand ())) :
-	avro_float ((float) (rand ()));
-      avro_datum_t datum_out;
-
-      if (avro_write_data (writer, schema, datum_in))
-	{
-	  assert (0 && "Unable to write float/double");
-	}
-      if (avro_read_data (reader, schema, NULL, &datum_out))
-	{
-	  assert (0 && "Unable to read float/double");
-	}
-      if (!avro_datum_equal (datum_in, datum_out))
-	{
-	  assert (0 && "Unable to encode/decode float/double");
-	}
-
-      avro_datum_decref (datum_in);
-      avro_datum_decref (datum_out);
-      avro_reader_free (reader);
-      avro_writer_free (writer);
+      avro_schema_t schema = avro_schema_double ();
+      avro_datum_t datum = avro_double ((double) (rand ()));
+      write_read_check (schema, NULL, datum, "double");
+      avro_datum_decref (datum);
     }
   return 0;
 }
@@ -211,42 +133,27 @@
 static int
 test_float (void)
 {
-  return test_float_double (0);
-}
-
-static int
-test_double (void)
-{
-  return test_float_double (1);
+  int i;
+  for (i = 0; i < 100; i++)
+    {
+      avro_schema_t schema = avro_schema_double ();
+      avro_datum_t datum = avro_double ((double) (rand ()));
+      write_read_check (schema, NULL, datum, "float");
+      avro_datum_decref (datum);
+    }
+  return 0;
 }
 
 static int
 test_boolean (void)
 {
   int i;
-  for (i = 0; i < 1000; i++)
+  for (i = 0; i < 100; i++)
     {
-      avro_reader_t reader = avro_reader_memory (buf, sizeof (buf));
-      avro_writer_t writer = avro_writer_memory (buf, sizeof (buf));
       avro_schema_t schema = avro_schema_boolean ();
-      avro_datum_t datum_in = avro_boolean (rand () % 2);
-      avro_datum_t datum_out;
-
-      if (avro_write_data (writer, schema, datum_in))
-	{
-	  assert (0 && "Unable to write boolean");
-	}
-      if (avro_read_data (reader, schema, schema, &datum_out))
-	{
-	  assert (0 && "Unable to read boolean");
-	}
-      if (!avro_datum_equal (datum_in, datum_out))
-	{
-	  assert (0 && "Unable to encode/decode boolean");
-	}
-
-      avro_reader_free (reader);
-      avro_writer_free (writer);
+      avro_datum_t datum = avro_boolean (rand () % 2);
+      write_read_check (schema, NULL, datum, "boolean");
+      avro_datum_decref (datum);
     }
   return 0;
 }
@@ -254,6 +161,10 @@
 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");
+  avro_datum_decref (datum);
   return 0;
 }
 
@@ -274,14 +185,38 @@
 int
 test_array (void)
 {
-  /* TODO */
+  int i, rval;
+  avro_schema_t schema = avro_schema_array (avro_schema_int ());
+  avro_datum_t datum = avro_array ();
+
+  for (i = 0; i < 10; i++)
+    {
+      rval = avro_array_append_datum (datum, avro_int (i));
+      if (rval)
+	{
+	  exit (rval);
+	}
+    }
+  write_read_check (schema, NULL, datum, "array");
+  avro_datum_decref (datum);
   return 0;
 }
 
 int
 test_map (void)
 {
-  /* TODO */
+  avro_schema_t schema = avro_schema_map (avro_schema_long ());
+  avro_datum_t datum = avro_map ();
+  int64_t i = 0;
+  char *nums[] =
+    { "zero", "one", "two", "three", "four", "five", "six", NULL };
+  while (nums[i])
+    {
+      avro_map_set (datum, nums[i], avro_long (i));
+      i++;
+    }
+  write_read_check (schema, NULL, datum, "map");
+  avro_datum_decref (datum);
   return 0;
 }
 

Modified: hadoop/avro/trunk/lang/c/version.sh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/version.sh?rev=901392&r1=901391&r2=901392&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/version.sh (original)
+++ hadoop/avro/trunk/lang/c/version.sh Wed Jan 20 21:52:35 2010
@@ -18,9 +18,9 @@
 #         libavro_binary_age = 0
 #         libavro_interface_age = 0
 #
-libavro_micro_version=5
+libavro_micro_version=6
 libavro_interface_age=0
-libavro_binary_age=0
+libavro_binary_age=1
 
 # IGNORE EVERYTHING ELSE FROM HERE DOWN.........
 if test $# != 1; then



Mime
View raw message