avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mas...@apache.org
Subject svn commit: r902297 - in /hadoop/avro/trunk: CHANGES.txt lang/c/src/avro.h lang/c/src/datum.c lang/c/src/datum.h 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 Fri, 22 Jan 2010 22:38:10 GMT
Author: massie
Date: Fri Jan 22 22:38:09 2010
New Revision: 902297

URL: http://svn.apache.org/viewvc?rev=902297&view=rev
Log:
AVRO-370.  Add support for encoding/decoding fixed data

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.h
    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=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Fri Jan 22 22:38:09 2010
@@ -246,6 +246,8 @@
 
     AVRO-369. Add support for encoding/decoding enum values (massie)
 
+    AVRO-370. Add support for encoding/decoding fixed data (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=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/avro.h (original)
+++ hadoop/avro/trunk/lang/c/src/avro.h Fri Jan 22 22:38:09 2010
@@ -188,7 +188,12 @@
 
 avro_datum_t avro_enum(const char *name, const char *symbol);
 
-avro_datum_t avro_fixed(const char *name, const int64_t len, const char *bytes);
+avro_datum_t avro_fixed(const char *name, const char *bytes,
+			const int64_t size);
+avro_datum_t avro_wrapfixed(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_map(void);
 int avro_map_set(const avro_datum_t map, const char *key,

Modified: hadoop/avro/trunk/lang/c/src/datum.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.c?rev=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum.c Fri Jan 22 22:38:09 2010
@@ -249,7 +249,9 @@
 	return &datum->obj;
 }
 
-avro_datum_t avro_fixed(const char *name, const int64_t size, const char *bytes)
+static avro_datum_t avro_fixed_private(const char *name, const char *bytes,
+				       const int64_t size,
+				       void (*fixed_free) (void *ptr))
 {
 	struct avro_fixed_datum_t *datum =
 	    malloc(sizeof(struct avro_fixed_datum_t));
@@ -258,16 +260,35 @@
 	}
 	datum->name = strdup(name);
 	datum->size = size;
-	datum->bytes = malloc(size);
-	if (datum->bytes) {
-		free(datum);
-		return NULL;
-	}
-	memcpy(datum->bytes, bytes, size);
+	datum->bytes = (char *)bytes;
+	datum->free = fixed_free;
+
 	avro_datum_init(&datum->obj, AVRO_FIXED);
 	return &datum->obj;
 }
 
+avro_datum_t avro_fixed(const char *name, const char *bytes, const int64_t size)
+{
+	char *bytes_copy = malloc(size);
+	if (!bytes_copy) {
+		return NULL;
+	}
+	memcpy(bytes_copy, bytes, size);
+	return avro_fixed_private(name, bytes, size, free);
+}
+
+avro_datum_t avro_wrapfixed(const char *name, const char *bytes,
+			    const int64_t size)
+{
+	return avro_fixed_private(name, bytes, size, NULL);
+}
+
+avro_datum_t avro_givefixed(const char *name, const char *bytes,
+			    const int64_t size)
+{
+	return avro_fixed_private(name, bytes, size, free);
+}
+
 avro_datum_t avro_map(void)
 {
 	struct avro_map_datum_t *datum =
@@ -430,7 +451,9 @@
 				struct avro_fixed_datum_t *fixed;
 				fixed = avro_datum_to_fixed(datum);
 				free((void *)fixed->name);
-				free((void *)fixed->bytes);
+				if (fixed->free) {
+					fixed->free((void *)fixed->bytes);
+				}
 				free(fixed);
 			}
 			break;

Modified: hadoop/avro/trunk/lang/c/src/datum.h
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.h?rev=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.h (original)
+++ hadoop/avro/trunk/lang/c/src/datum.h Fri Jan 22 22:38:09 2010
@@ -65,6 +65,7 @@
 	char *name;
 	char *bytes;
 	int64_t size;
+	void (*free) (void *ptr);
 };
 
 struct avro_map_datum_t {

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=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_equal.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_equal.c Fri Jan 22 22:38:09 2010
@@ -89,6 +89,12 @@
 	    && strcmp(a->symbol, b->symbol) == 0;
 }
 
+static int fixed_equal(struct avro_fixed_datum_t *a,
+		       struct avro_fixed_datum_t *b)
+{
+	return a->size == b->size && memcmp(a->bytes, b->bytes, a->size)
== 0;
+}
+
 int avro_datum_equal(avro_datum_t a, avro_datum_t b)
 {
 	if (!(is_avro_datum(a) && is_avro_datum(b))) {
@@ -134,6 +140,9 @@
 		return enum_equal(avro_datum_to_enum(a), avro_datum_to_enum(b));
 
 	case AVRO_FIXED:
+		return fixed_equal(avro_datum_to_fixed(a),
+				   avro_datum_to_fixed(b));
+
 	case AVRO_UNION:
 	case AVRO_LINK:
 		/*

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=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_read.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_read.c Fri Jan 22 22:38:09 2010
@@ -93,14 +93,6 @@
 }
 
 static int
-read_fixed(avro_reader_t reader, const avro_encoding_t * enc,
-	   avro_schema_t writers_schema, avro_schema_t readers_schema,
-	   avro_datum_t * datum)
-{
-	return 1;
-}
-
-static int
 read_enum(avro_reader_t reader, const avro_encoding_t * enc,
 	  struct avro_enum_schema_t *writers_schema,
 	  struct avro_enum_schema_t *readers_schema, avro_datum_t * datum)
@@ -320,14 +312,18 @@
 	switch (avro_typeof(writers_schema)) {
 	case AVRO_NULL:
 		rval = enc->read_null(reader);
-		*datum = avro_null();
+		if (!rval) {
+			*datum = avro_null();
+		}
 		break;
 
 	case AVRO_BOOLEAN:
 		{
 			int8_t b;
 			rval = enc->read_boolean(reader, &b);
-			*datum = avro_boolean(b);
+			if (!rval) {
+				*datum = avro_boolean(b);
+			}
 		}
 		break;
 
@@ -335,7 +331,9 @@
 		{
 			char *s;
 			rval = enc->read_string(reader, &s);
-			*datum = avro_givestring(s);
+			if (!rval) {
+				*datum = avro_givestring(s);
+			}
 		}
 		break;
 
@@ -343,7 +341,9 @@
 		{
 			int32_t i;
 			rval = enc->read_int(reader, &i);
-			*datum = avro_int(i);
+			if (!rval) {
+				*datum = avro_int(i);
+			}
 		}
 		break;
 
@@ -351,7 +351,9 @@
 		{
 			int64_t l;
 			rval = enc->read_long(reader, &l);
-			*datum = avro_long(l);
+			if (!rval) {
+				*datum = avro_long(l);
+			}
 		}
 		break;
 
@@ -359,7 +361,9 @@
 		{
 			float f;
 			rval = enc->read_float(reader, &f);
-			*datum = avro_float(f);
+			if (!rval) {
+				*datum = avro_float(f);
+			}
 		}
 		break;
 
@@ -367,7 +371,9 @@
 		{
 			double d;
 			rval = enc->read_double(reader, &d);
-			*datum = avro_double(d);
+			if (!rval) {
+				*datum = avro_double(d);
+			}
 		}
 		break;
 
@@ -376,14 +382,28 @@
 			char *bytes;
 			int64_t len;
 			rval = enc->read_bytes(reader, &bytes, &len);
-			*datum = avro_givebytes(bytes, len);
+			if (!rval) {
+				*datum = avro_givebytes(bytes, len);
+			}
 		}
 		break;
 
-	case AVRO_FIXED:
-		rval =
-		    read_fixed(reader, enc, writers_schema, readers_schema,
-			       datum);
+	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;
+
+			bytes = malloc(size);
+			if (!bytes) {
+				return ENOMEM;
+			}
+			rval = avro_read(reader, bytes, size);
+			if (!rval) {
+				*datum = avro_givefixed(name, bytes, size);
+			}
+		}
 		break;
 
 	case AVRO_ENUM:

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=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_write.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_write.c Fri Jan 22 22:38:09 2010
@@ -55,16 +55,6 @@
 	return enc->write_long(writer, index);
 }
 
-static int
-write_fixed(avro_writer_t writer, const avro_encoding_t * enc,
-	    avro_schema_t writer_schema, avro_datum_t datum)
-{
-	/*
-	 * TODO 
-	 */
-	return EINVAL;
-}
-
 struct write_map_args {
 	int rval;
 	avro_writer_t writer;
@@ -228,14 +218,17 @@
 		    write_record(writer, enc,
 				 avro_schema_to_record(writer_schema), datum);
 		break;
+
 	case AVRO_ENUM:
 		rval =
 		    write_enum(writer, enc, avro_schema_to_enum(writer_schema),
 			       avro_datum_to_enum(datum));
 		break;
+
 	case AVRO_FIXED:
-		rval = write_fixed(writer, enc, writer_schema, datum);
-		break;
+		return avro_write(writer, avro_datum_to_fixed(datum)->bytes,
+				  avro_datum_to_fixed(datum)->size);
+
 	case AVRO_MAP:
 		rval =
 		    write_map(writer, enc, avro_schema_to_map(writer_schema),

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=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/tests/test_avro_data.c (original)
+++ hadoop/avro/trunk/lang/c/tests/test_avro_data.c Fri Jan 22 22:38:09 2010
@@ -237,9 +237,12 @@
 
 static int test_fixed(void)
 {
-	/*
-	 * TODO 
-	 */
+	char bytes[] = { 0xD, 0xA, 0xD, 0xA, 0xB, 0xA, 0xB, 0xA };
+	avro_schema_t schema = avro_schema_fixed("msg", sizeof(bytes));
+	avro_datum_t datum = avro_wrapfixed("msg", bytes, sizeof(bytes));
+	write_read_check(schema, NULL, datum, "fixed");
+	avro_datum_decref(datum);
+	avro_schema_decref(schema);
 	return 0;
 }
 

Modified: hadoop/avro/trunk/lang/c/version.sh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/version.sh?rev=902297&r1=902296&r2=902297&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/version.sh (original)
+++ hadoop/avro/trunk/lang/c/version.sh Fri Jan 22 22:38:09 2010
@@ -18,9 +18,9 @@
 #         libavro_binary_age = 0
 #         libavro_interface_age = 0
 #
-libavro_micro_version=9
+libavro_micro_version=10
 libavro_interface_age=0
-libavro_binary_age=4
+libavro_binary_age=0
 
 # IGNORE EVERYTHING ELSE FROM HERE DOWN.........
 if test $# != 1; then



Mime
View raw message