You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by ma...@apache.org on 2010/01/26 20:07:08 UTC

svn commit: r903369 - in /hadoop/avro/trunk: ./ lang/c/ lang/c/docs/ lang/c/examples/ lang/c/jansson/ lang/c/src/ lang/c/tests/

Author: massie
Date: Tue Jan 26 19:07:07 2010
New Revision: 903369

URL: http://svn.apache.org/viewvc?rev=903369&view=rev
Log:
AVRO-378.  Add example code to the C implementation and update documentation

Added:
    hadoop/avro/trunk/lang/c/examples/
    hadoop/avro/trunk/lang/c/examples/Makefile.am
    hadoop/avro/trunk/lang/c/examples/quickstop.c
    hadoop/avro/trunk/lang/c/jansson/.gitignore
Removed:
    hadoop/avro/trunk/lang/c/jansson/config.guess
    hadoop/avro/trunk/lang/c/jansson/config.sub
    hadoop/avro/trunk/lang/c/jansson/depcomp
    hadoop/avro/trunk/lang/c/jansson/install-sh
    hadoop/avro/trunk/lang/c/jansson/ltmain.sh
    hadoop/avro/trunk/lang/c/jansson/missing
Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/c/Makefile.am
    hadoop/avro/trunk/lang/c/configure.in
    hadoop/avro/trunk/lang/c/docs/index.txt
    hadoop/avro/trunk/lang/c/src/datum.c
    hadoop/avro/trunk/lang/c/src/datum_read.c
    hadoop/avro/trunk/lang/c/src/schema.c
    hadoop/avro/trunk/lang/c/tests/Makefile.am
    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=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Jan 26 19:07:07 2010
@@ -257,6 +257,9 @@
 
     AVRO-377. Add getters and setters for all Avro datum types (massie)
 
+    AVRO-378. Add example code to the C implementation and update 
+              documentation (massie)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Modified: hadoop/avro/trunk/lang/c/Makefile.am
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/Makefile.am?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/Makefile.am (original)
+++ hadoop/avro/trunk/lang/c/Makefile.am Tue Jan 26 19:07:07 2010
@@ -1,5 +1,5 @@
 EXTRA_DIST=version.sh m4
-SUBDIRS = docs jansson src tests
+SUBDIRS = docs jansson src tests examples
 
 # Linux kernel source indent format options
 INDENT_OPTS=-nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 \
@@ -7,7 +7,7 @@
 -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8
 
 pretty:
-	@for dir in src tests; do \
+	@for dir in src tests examples; do \
 	indent $(INDENT_OPTS) $(top_srcdir)/$$dir/*.[c,h]; \
 	mv $(top_srcdir)/$$dir/*~ /tmp; \
 	done

Modified: hadoop/avro/trunk/lang/c/configure.in
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/configure.in?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/configure.in (original)
+++ hadoop/avro/trunk/lang/c/configure.in Tue Jan 26 19:07:07 2010
@@ -41,6 +41,7 @@
    tests/Makefile
    docs/Makefile
    src/Makefile
+   examples/Makefile
 ])
 AC_CONFIG_SUBDIRS([jansson])
 AC_OUTPUT

Modified: hadoop/avro/trunk/lang/c/docs/index.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/docs/index.txt?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/docs/index.txt (original)
+++ hadoop/avro/trunk/lang/c/docs/index.txt Tue Jan 26 19:07:07 2010
@@ -29,18 +29,77 @@
                                    
 ....
 
-The C implementation is still *alpha* is not ready for
-production use.  The current code is being tested on
-+MacOS X+ and +Linux+.
+The C implementation is still not quite ready for production use.
+The current code is being tested on +MacOS X+ and +Linux+.  We're
+always looking for contributions so, if you're a C hacker, please
+feel free to submit patches to the project by visiting
+http://hadoop.apache.org/avro/[the Avro website].
+
+== Examples
+
+Imagine you're a free-lance hacker in Leonardo, New Jersey and you've 
+been approached by the owner of the local *Quick Stop Convenience* store.
+He wants you to create a contact database case he needs to call an employee 
+to work on their day off.
 
-We're making rapid progress and the +avro.h+ header file 
-can give you an idea of what the API will likely look like.
+You might build a simple contact system using Avro C like the following...
+
+[source,c]
+----
+include::../examples/quickstop.c[]
+----
+
+When you compile and run this program, you should get the following output
+
+----
+Successfully added Hicks, Dante id=1
+Successfully added Graves, Randal id=2
+Successfully added Loughran, Veronica id=3
+Successfully added Bree, Caitlin id=4
+Successfully added Silent, Bob id=5
+Successfully added ???, Jay id=6
+
+Avro is compact. Here is the data for all 6 people.
+| 02 0A 44 61 6E 74 65 0A | 48 69 63 6B 73 1C 28 35 |	..Dante.Hicks.(5
+| 35 35 29 20 31 32 33 2D | 34 35 36 37 40 04 0C 52 |	55) 123-4567@..R
+| 61 6E 64 61 6C 0C 47 72 | 61 76 65 73 1C 28 35 35 |	andal.Graves.(55
+| 35 29 20 31 32 33 2D 35 | 36 37 38 3C 06 10 56 65 |	5) 123-5678<..Ve
+| 72 6F 6E 69 63 61 10 4C | 6F 75 67 68 72 61 6E 1C |	ronica.Loughran.
+| 28 35 35 35 29 20 31 32 | 33 2D 30 39 38 37 38 08 |	(555) 123-09878.
+| 0E 43 61 69 74 6C 69 6E | 08 42 72 65 65 1C 28 35 |	.Caitlin.Bree.(5
+| 35 35 29 20 31 32 33 2D | 32 33 32 33 36 0A 06 42 |	55) 123-23236..B
+| 6F 62 0C 53 69 6C 65 6E | 74 1C 28 35 35 35 29 20 |	ob.Silent.(555) 
+| 31 32 33 2D 36 34 32 32 | 3A 0C 06 4A 61 79 06 3F |	123-6422:..Jay.?
+| 3F 3F 1C 28 35 35 35 29 | 20 31 32 33 2D 39 31 38 |	??.(555) 123-918
+| 32 34 .. .. .. .. .. .. | .. .. .. .. .. .. .. .. |	24..............
+
+Now let's read all the records back out
+1 |           Dante |           Hicks |            (555) 123-4567 |   32
+2 |          Randal |          Graves |            (555) 123-5678 |   30
+3 |        Veronica |        Loughran |            (555) 123-0987 |   28
+4 |         Caitlin |            Bree |            (555) 123-2323 |   27
+5 |             Bob |          Silent |            (555) 123-6422 |   29
+6 |             Jay |             ??? |            (555) 123-9182 |   26
+----
+
+The *Quick Stop* store owner was so pleased, he asked you to create a 
+movie database for his *RST Video* store.
+
+== Reference files
+
+=== avro.h
+
+The +avro.h+ header file contains the complete public API
+for +Avro C+.  The documentation is rather sparse right now
+but we'll be adding more information soon.
 
 [source,c]
 ----
 include::../src/avro.h[]
 ----
 
+=== test_avro_data.c 
+
 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.

Added: hadoop/avro/trunk/lang/c/examples/Makefile.am
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/examples/Makefile.am?rev=903369&view=auto
==============================================================================
--- hadoop/avro/trunk/lang/c/examples/Makefile.am (added)
+++ hadoop/avro/trunk/lang/c/examples/Makefile.am Tue Jan 26 19:07:07 2010
@@ -0,0 +1,12 @@
+# -pedantic
+AM_CPPFLAGS=-I$(top_srcdir)/src 
+AM_CFLAGS=-Wall 
+ACLOCAL_AMFLAGS=-I m4
+
+check_PROGRAMS=quickstop
+TESTS=$(check_PROGRAMS)
+
+examples_LDADD=$(top_builddir)/src/libavro.la
+
+quickstop_SOURCES=quickstop.c
+quickstop_LDADD=$(examples_LDADD)

Added: hadoop/avro/trunk/lang/c/examples/quickstop.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/examples/quickstop.c?rev=903369&view=auto
==============================================================================
--- hadoop/avro/trunk/lang/c/examples/quickstop.c (added)
+++ hadoop/avro/trunk/lang/c/examples/quickstop.c Tue Jan 26 19:07:07 2010
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <avro.h>
+
+char buf[4096];
+avro_schema_t person_schema;
+avro_reader_t reader;
+avro_writer_t writer;
+int64_t id = 0;
+
+/* A simple schema for our tutorial */
+#define PERSON_SCHEMA \
+"{\"type\":\"record\",\
+  \"name\":\"Person\",\
+  \"fields\":[\
+     {\"name\": \"ID\", \"type\": \"long\"},\
+     {\"name\": \"First\", \"type\": \"string\"},\
+     {\"name\": \"Last\", \"type\": \"string\"},\
+     {\"name\": \"Phone\", \"type\": \"string\"},\
+     {\"name\": \"Age\", \"type\": \"int\"}]}"
+
+/* Parse schema into a schema data structure */
+void init_schema(void)
+{
+	avro_schema_error_t error;
+	if (avro_schema_from_json(PERSON_SCHEMA, sizeof(PERSON_SCHEMA),
+				  &person_schema, &error)) {
+		fprintf(stderr, "Unable to parse person schema\n");
+		exit(EXIT_FAILURE);
+	}
+}
+
+/* Create a datum to match the person schema and save it */
+void
+add_person(const char *first, const char *last, const char *phone, int32_t age)
+{
+	avro_datum_t person = avro_record("Person");
+
+	avro_datum_t id_datum = avro_int64(++id);
+	avro_datum_t first_datum = avro_string(first);
+	avro_datum_t last_datum = avro_string(last);
+	avro_datum_t age_datum = avro_int32(age);
+	avro_datum_t phone_datum = avro_string(phone);
+
+	if (avro_record_field_set(person, "ID", id_datum)
+	    || avro_record_field_set(person, "First", first_datum)
+	    || avro_record_field_set(person, "Last", last_datum)
+	    || avro_record_field_set(person, "Age", age_datum)
+	    || avro_record_field_set(person, "Phone", phone_datum)) {
+		fprintf(stderr, "Unable to create Person datum structure");
+		exit(EXIT_FAILURE);
+	}
+
+	if (avro_write_data(writer, person_schema, person)) {
+		fprintf(stderr,
+			"Unable to write Person datum to memory buffer");
+		exit(EXIT_FAILURE);
+	}
+
+	/* Decrement all our references to prevent memory from leaking */
+	avro_datum_decref(id_datum);
+	avro_datum_decref(first_datum);
+	avro_datum_decref(last_datum);
+	avro_datum_decref(age_datum);
+	avro_datum_decref(phone_datum);
+	avro_datum_decref(person);
+
+	fprintf(stdout, "Successfully added %s, %s id=%ld\n", last, first, id);
+}
+
+int print_person(void)
+{
+	int rval;
+	avro_datum_t person;
+
+	rval = avro_read_data(reader, person_schema, NULL, &person);
+	if (rval == 0) {
+		int64_t i64;
+		int32_t i32;
+		char *p;
+		avro_datum_t id_datum, first_datum, last_datum, phone_datum,
+		    age_datum;
+
+		id_datum = avro_record_field_get(person, "ID");
+		first_datum = avro_record_field_get(person, "First");
+		last_datum = avro_record_field_get(person, "Last");
+		phone_datum = avro_record_field_get(person, "Phone");
+		age_datum = avro_record_field_get(person, "Age");
+
+		avro_int64_get(id_datum, &i64);
+		fprintf(stdout, "%ld | ", i64);
+		avro_string_get(first_datum, &p);
+		fprintf(stdout, "%15s | ", p);
+		avro_string_get(last_datum, &p);
+		fprintf(stdout, "%15s | ", p);
+		avro_string_get(phone_datum, &p);
+		fprintf(stdout, "%25s | ", p);
+		avro_int32_get(age_datum, &i32);
+		fprintf(stdout, "  %2d\n", i32);
+
+		/* We no longer need this memory */
+		avro_datum_decref(person);
+	}
+	return rval;
+}
+
+int main(void)
+{
+	int64_t i;
+
+	/* Create readers and writers backed by memory */
+	writer = avro_writer_memory(buf, sizeof(buf));
+	reader = avro_reader_memory(buf, sizeof(buf));
+
+	/* Initialize the schema structure from JSON */
+	init_schema();
+
+	/* Add people to the database */
+	add_person("Dante", "Hicks", "(555) 123-4567", 32);
+	add_person("Randal", "Graves", "(555) 123-5678", 30);
+	add_person("Veronica", "Loughran", "(555) 123-0987", 28);
+	add_person("Caitlin", "Bree", "(555) 123-2323", 27);
+	add_person("Bob", "Silent", "(555) 123-6422", 29);
+	add_person("Jay", "???", "(555) 123-9182", 26);
+
+	fprintf(stdout,
+		"\nAvro is compact. Here is the data for all %ld people.\n",
+		id);
+	avro_writer_dump(writer, stdout);
+
+	fprintf(stdout, "\nNow let's read all the records back out\n");
+
+	/* Read all the records and print them */
+	for (i = 0; i < id; i++) {
+		if (print_person()) {
+			fprintf(stderr, "Error printing person\n");
+			exit(EXIT_FAILURE);
+		}
+	}
+
+	/* We don't need this schema anymore */
+	avro_schema_decref(person_schema);
+
+	/* We dont' need the reader/writer anymore */
+	avro_reader_free(reader);
+	avro_writer_free(writer);
+	return 0;
+}

Added: hadoop/avro/trunk/lang/c/jansson/.gitignore
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/jansson/.gitignore?rev=903369&view=auto
==============================================================================
--- hadoop/avro/trunk/lang/c/jansson/.gitignore (added)
+++ hadoop/avro/trunk/lang/c/jansson/.gitignore Tue Jan 26 19:07:07 2010
@@ -0,0 +1,6 @@
+config.guess
+config.sub
+depcomp
+install-sh
+ltmain.sh
+missing

Modified: hadoop/avro/trunk/lang/c/src/datum.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.c?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum.c Tue Jan 26 19:07:07 2010
@@ -434,6 +434,7 @@
 				return ENOMEM;
 			}
 		}
+		avro_datum_incref(field_value);
 		st_insert(record->fields, (st_data_t) key,
 			  (st_data_t) field_value);
 		return 0;
@@ -590,6 +591,7 @@
 			return ENOMEM;
 		}
 	}
+	avro_datum_incref(value);
 	st_insert(map->map, (st_data_t) save_key, (st_data_t) value);
 	return 0;
 }
@@ -623,7 +625,7 @@
 	if (!el) {
 		return ENOMEM;
 	}
-	el->datum = datum;
+	el->datum = avro_datum_incref(datum);
 	STAILQ_INSERT_TAIL(&array->els, el, els);
 	array->num_elements++;
 	return 0;

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=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_read.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_read.c Tue Jan 26 19:07:07 2010
@@ -157,6 +157,7 @@
 				avro_datum_decref(array_datum);
 				return rval;
 			}
+			avro_datum_decref(datum);
 		}
 
 		rval = enc->read_long(reader, &block_count);
@@ -212,6 +213,7 @@
 				free(key);
 				return rval;
 			}
+			avro_datum_decref(value);
 			free(key);
 		}
 		rval = enc->read_long(reader, &block_count);
@@ -282,6 +284,7 @@
 			if (rval) {
 				return rval;
 			}
+			avro_datum_decref(field_datum);
 		} else {
 			/* TODO: skip_record */
 			return -1;

Modified: hadoop/avro/trunk/lang/c/src/schema.c
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.c?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema.c Tue Jan 26 19:07:07 2010
@@ -369,7 +369,7 @@
 {
 	struct avro_record_schema_t *record;
 	struct avro_record_field_t *new_field;
-	if (!record_schema || !field_name || !field_schema
+	if (!field_name || !field_schema || !is_avro_schema(record_schema)
 	    || !is_avro_record(record_schema) || record_schema == field_schema
 	    || !is_avro_id(field_name)) {
 		return EINVAL;
@@ -784,6 +784,8 @@
 
 	root = json_loads(jsontext, &error->json_error);
 	if (!root) {
+		st_free_table(error->named_schemas);
+		free(error);
 		return EINVAL;
 	}
 
@@ -792,6 +794,11 @@
 	 */
 	rval = avro_schema_from_json_t(root, schema, e);
 	json_decref(root);
+	st_free_table(error->named_schemas);
+	if (rval == 0) {
+		/* no need for an error return */
+		free(error);
+	}
 	return rval;
 }
 

Modified: hadoop/avro/trunk/lang/c/tests/Makefile.am
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/tests/Makefile.am?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/tests/Makefile.am (original)
+++ hadoop/avro/trunk/lang/c/tests/Makefile.am Tue Jan 26 19:07:07 2010
@@ -1,5 +1,5 @@
 # -pedantic
-AM_CPPFLAGS=-I$(top_srcdir)/src -I$(top_srcdir)/jansson/src
+AM_CPPFLAGS=-I$(top_srcdir)/src
 AM_CFLAGS=-Wall 
 ACLOCAL_AMFLAGS=-I m4
 

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=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/tests/test_avro_data.c (original)
+++ hadoop/avro/trunk/lang/c/tests/test_avro_data.c Tue Jan 26 19:07:07 2010
@@ -182,16 +182,21 @@
 {
 	avro_schema_t schema = avro_schema_record("person");
 	avro_datum_t datum = avro_record("person");
+	avro_datum_t name_datum, age_datum;
 
 	avro_schema_record_field_append(schema, "name", avro_schema_string());
 	avro_schema_record_field_append(schema, "age", avro_schema_int());
 
-	avro_record_field_set(datum, "name",
-			      avro_wrapstring("Joseph Campbell"));
-	avro_record_field_set(datum, "age", avro_int32(83));
+	name_datum = avro_wrapstring("Joseph Campbell");
+	age_datum = avro_int32(83);
+
+	avro_record_field_set(datum, "name", name_datum);
+	avro_record_field_set(datum, "age", age_datum);
 
 	write_read_check(schema, NULL, datum, "record");
 
+	avro_datum_decref(name_datum);
+	avro_datum_decref(age_datum);
 	avro_datum_decref(datum);
 	avro_schema_decref(schema);
 	return 0;
@@ -221,11 +226,14 @@
 	avro_datum_t datum = avro_array();
 
 	for (i = 0; i < 10; i++) {
-		rval = avro_array_append_datum(datum, avro_int32(i));
+		avro_datum_t i32_datum = avro_int32(i);
+		rval = avro_array_append_datum(datum, i32_datum);
+		avro_datum_decref(i32_datum);
 		if (rval) {
-			exit(rval);
+			exit(EXIT_FAILURE);
 		}
 	}
+
 	write_read_check(schema, NULL, datum, "array");
 	avro_datum_decref(datum);
 	avro_schema_decref(schema);
@@ -240,7 +248,9 @@
 	char *nums[] =
 	    { "zero", "one", "two", "three", "four", "five", "six", NULL };
 	while (nums[i]) {
-		avro_map_set(datum, nums[i], avro_int64(i));
+		avro_datum_t i_datum = avro_int64(i);
+		avro_map_set(datum, nums[i], i_datum);
+		avro_datum_decref(i_datum);
 		i++;
 	}
 	write_read_check(schema, NULL, datum, "map");

Modified: hadoop/avro/trunk/lang/c/version.sh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/version.sh?rev=903369&r1=903368&r2=903369&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/version.sh (original)
+++ hadoop/avro/trunk/lang/c/version.sh Tue Jan 26 19:07:07 2010
@@ -18,9 +18,9 @@
 #         libavro_binary_age = 0
 #         libavro_interface_age = 0
 #
-libavro_micro_version=12
-libavro_interface_age=0
-libavro_binary_age=0
+libavro_micro_version=13
+libavro_interface_age=1
+libavro_binary_age=1
 
 # IGNORE EVERYTHING ELSE FROM HERE DOWN.........
 if test $# != 1; then