Author: massie
Date: Wed Feb  3 23:59:40 2010
New Revision: 906293

URL: http://svn.apache.org/viewvc?rev=906293&view=rev
Log:
AVRO-394. Simplify and consolidate all data structures into hash tables

Removed:
    hadoop/avro/trunk/lang/c/src/queue.h
Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/c/examples/   (props changed)
    hadoop/avro/trunk/lang/c/jansson/   (props changed)
    hadoop/avro/trunk/lang/c/src/Makefile.am
    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_skip.c
    hadoop/avro/trunk/lang/c/src/datum_validate.c
    hadoop/avro/trunk/lang/c/src/datum_write.c
    hadoop/avro/trunk/lang/c/src/schema.c
    hadoop/avro/trunk/lang/c/src/schema.h
    hadoop/avro/trunk/lang/c/src/schema_equal.c
    hadoop/avro/trunk/lang/c/src/schema_printf.c

Modified: hadoop/avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Wed Feb  3 23:59:40 2010
@@ -281,6 +281,8 @@
 
     AVRO-386. Python implementaiton of compression (philz)
 
+    AVRO-394. Simplify and consolidate all data structures into hash tables 
(massie)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Propchange: hadoop/avro/trunk/lang/c/examples/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Feb  3 23:59:40 2010
@@ -0,0 +1 @@
+Makefile.in

Propchange: hadoop/avro/trunk/lang/c/jansson/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb  3 23:59:40 2010
@@ -3,3 +3,9 @@
 autom4te.cache
 aclocal.m4
 configure
+depcomp
+config.guess
+ltmain.sh
+config.sub
+missing
+install-sh

Modified: hadoop/avro/trunk/lang/c/src/Makefile.am
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/Makefile.am?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/Makefile.am (original)
+++ hadoop/avro/trunk/lang/c/src/Makefile.am Wed Feb  3 23:59:40 2010
@@ -9,7 +9,7 @@
 libavro_la_SOURCES = st.c st.h schema.c schema.h schema_printf.c 
schema_equal.c \
 datum.c datum_equal.c datum_validate.c datum_read.c datum_skip.c datum_write.c 
datum.h \
 io.c dump.c dump.h encoding_binary.c \
-container_of.h queue.h encoding.h
+container_of.h encoding.h
 libavro_la_LIBADD = $(top_builddir)/jansson/src/.libs/libjansson.a
 libavro_la_LDFLAGS = \
         -version-info $(LIBAVRO_VERSION) \

Modified: hadoop/avro/trunk/lang/c/src/datum.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.c?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum.c Wed Feb  3 23:59:40 2010
@@ -21,6 +21,8 @@
 #include "datum.h"
 #include "encoding.h"
 
+#define DEFAULT_TABLE_SIZE 32
+
 static void avro_datum_init(avro_datum_t datum, avro_type_t type)
 {
        datum->type = type;
@@ -386,7 +388,16 @@
                return NULL;
        }
        datum->name = strdup(name);
-       datum->fields = st_init_strtable();
+       if (!datum->name) {
+               free(datum);
+               return NULL;
+       }
+       datum->fields = st_init_strtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!datum->fields) {
+               free(datum->name);
+               free(datum);
+               return NULL;
+       }
 
        avro_datum_init(&datum->obj, AVRO_RECORD);
        return &datum->obj;
@@ -555,7 +566,12 @@
        if (!datum) {
                return NULL;
        }
-       datum->map = st_init_strtable();
+       datum->map = st_init_strtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!datum->map) {
+               free(datum);
+               return NULL;
+       }
+
        avro_datum_init(&datum->obj, AVRO_MAP);
        return &datum->obj;
 }
@@ -616,8 +632,11 @@
        if (!datum) {
                return NULL;
        }
-       STAILQ_INIT(&datum->els);
-       datum->num_elements = 0;
+       datum->els = st_init_numtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!datum->els) {
+               free(datum);
+               return NULL;
+       }
 
        avro_datum_init(&datum->obj, AVRO_ARRAY);
        return &datum->obj;
@@ -628,19 +647,13 @@
                        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 = avro_datum_incref(datum);
-       STAILQ_INSERT_TAIL(&array->els, el, els);
-       array->num_elements++;
+       st_insert(array->els, array->els->num_entries,
+                 (st_data_t) avro_datum_incref(datum));
        return 0;
 }
 
@@ -651,6 +664,12 @@
        return ST_DELETE;
 }
 
+static int array_free_foreach(int i, avro_datum_t datum, void *arg)
+{
+       avro_datum_decref(datum);
+       return ST_DELETE;
+}
+
 static void avro_datum_free(avro_datum_t datum)
 {
        if (is_avro_datum(datum)) {
@@ -747,13 +766,8 @@
                case AVRO_ARRAY:{
                                struct avro_array_datum_t *array;
                                array = avro_datum_to_array(datum);
-                               while (!STAILQ_EMPTY(&array->els)) {
-                                       struct avro_array_element_t *el;
-                                       el = STAILQ_FIRST(&array->els);
-                                       STAILQ_REMOVE_HEAD(&array->els, els);
-                                       avro_datum_decref(el->datum);
-                                       free(el);
-                               }
+                               st_foreach(array->els, array_free_foreach, 0);
+                               st_free_table(array->els);
                                free(array);
                        }
                        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=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.h (original)
+++ hadoop/avro/trunk/lang/c/src/datum.h Wed Feb  3 23:59:40 2010
@@ -20,7 +20,6 @@
 #include "avro.h"              /* for avro_schema_t */
 #include "container_of.h"
 #include "st.h"
-#include "queue.h"
 
 struct avro_string_datum_t {
        struct avro_obj_t obj;
@@ -85,15 +84,9 @@
        const char *symbol;
 };
 
-struct avro_array_element_t {
-       avro_datum_t datum;
-        STAILQ_ENTRY(avro_array_element_t) els;
-};
-
 struct avro_array_datum_t {
        struct avro_obj_t obj;
-       int64_t num_elements;
-        STAILQ_HEAD(els, avro_array_element_t) els;
+       st_table *els;
 };
 
 #define avro_datum_to_string(datum_)    (container_of(datum_, struct 
avro_string_datum_t, obj))

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=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_equal.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_equal.c Wed Feb  3 23:59:40 2010
@@ -21,18 +21,19 @@
 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) {
+       long i;
+
+       if (a->els->num_entries != b->els->num_entries) {
                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)) {
+       for (i = 0; i < a->els->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       avro_datum_t datum;
+               } ael, bel;
+               st_lookup(a->els, i, &ael.data);
+               st_lookup(b->els, i, &bel.data);
+               if (!avro_datum_equal(ael.datum, bel.datum)) {
                        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=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_read.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_read.c Wed Feb  3 23:59:40 2010
@@ -98,22 +98,21 @@
          struct avro_enum_schema_t *readers_schema, avro_datum_t * datum)
 {
        int rval;
-       int64_t i, index;
-       struct avro_enum_symbol_t *sym;
+       int64_t index;
+       union {
+               st_data_t data;
+               char *sym;
+       } val;
 
        rval = enc->read_long(reader, &index);
        if (rval) {
                return rval;
        }
 
-       sym = STAILQ_FIRST(&writers_schema->symbols);
-       for (i = 0; i != index && sym != NULL;
-            sym = STAILQ_NEXT(sym, symbols), i++) {
-       }
-       if (!sym) {
+       if (!st_lookup(writers_schema->symbols, index, &val.data)) {
                return EINVAL;
        }
-       *datum = avro_enum(writers_schema->name, sym->symbol);
+       *datum = avro_enum(writers_schema->name, val.sym);
        return 0;
 }
 
@@ -231,22 +230,21 @@
           struct avro_union_schema_t *readers_schema, avro_datum_t * datum)
 {
        int rval;
-       int64_t i, index;
-       struct avro_union_branch_t *branch;
+       int64_t index;
+       union {
+               st_data_t data;
+               avro_schema_t schema;
+       } val;
 
        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) {
+       if (!st_lookup(writers_schema->branches, index, &val.data)) {
                return EILSEQ;
        }
-       return avro_read_data(reader, branch->schema, NULL, datum);
+       return avro_read_data(reader, val.schema, NULL, datum);
 }
 
 /* TODO: handle default values in fields */
@@ -256,36 +254,35 @@
            struct avro_record_schema_t *readers_schema, avro_datum_t * datum)
 {
        int rval;
-       struct avro_record_field_t *reader_field;
-       struct avro_record_field_t *field;
+       long i;
        avro_datum_t record;
        avro_datum_t field_datum;
 
        record = *datum = avro_record(writers_schema->name);
-       for (field = STAILQ_FIRST(&writers_schema->fields);
-            field != NULL; field = STAILQ_NEXT(field, fields)) {
-               for (reader_field = STAILQ_FIRST(&readers_schema->fields);
-                    reader_field != NULL;
-                    reader_field = STAILQ_NEXT(reader_field, fields)) {
-                       if (strcmp(field->name, reader_field->name) == 0) {
-                               break;
-                       }
-               }
-               if (reader_field) {
+       for (i = 0; i < writers_schema->fields->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       struct avro_record_field_t *field;
+               } rfield, wfield;
+               st_lookup(writers_schema->fields, i, &wfield.data);
+               if (st_lookup
+                   (readers_schema->fields_byname,
+                    (st_data_t) wfield.field->name, &rfield.data)) {
                        rval =
-                           avro_read_data(reader, field->type,
-                                          reader_field->type, &field_datum);
+                           avro_read_data(reader, wfield.field->type,
+                                          rfield.field->type, &field_datum);
                        if (rval) {
                                return rval;
                        }
                        rval =
-                           avro_record_set(record, field->name, field_datum);
+                           avro_record_set(record, wfield.field->name,
+                                           field_datum);
                        if (rval) {
                                return rval;
                        }
                        avro_datum_decref(field_datum);
                } else {
-                       rval = avro_skip_data(reader, field->type);
+                       rval = avro_skip_data(reader, wfield.field->type);
                        if (rval) {
                                return rval;
                        }
@@ -299,6 +296,7 @@
               avro_schema_t readers_schema, avro_datum_t * datum)
 {
        int rval = EINVAL;
+       long i;
        const avro_encoding_t *enc = &avro_binary_encoding;
 
        if (!reader || !is_avro_schema(writers_schema) || !datum) {
@@ -315,15 +313,18 @@
         * schema resolution 
         */
        if (!is_avro_union(writers_schema) && is_avro_union(readers_schema)) {
-               struct avro_union_branch_t *branch;
                struct avro_union_schema_t *union_schema =
                    avro_schema_to_union(readers_schema);
 
-               for (branch = STAILQ_FIRST(&union_schema->branches);
-                    branch != NULL; branch = STAILQ_NEXT(branch, branches)) {
-                       if (avro_schema_match(writers_schema, branch->schema)) {
+               for (i = 0; i < union_schema->branches->num_entries; i++) {
+                       union {
+                               st_data_t data;
+                               avro_schema_t schema;
+                       } val;
+                       st_lookup(union_schema->branches, i, &val.data);
+                       if (avro_schema_match(writers_schema, val.schema)) {
                                return avro_read_data(reader, writers_schema,
-                                                     branch->schema, datum);
+                                                     val.schema, datum);
                        }
                }
                return EINVAL;

Modified: hadoop/avro/trunk/lang/c/src/datum_skip.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum_skip.c?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_skip.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_skip.c Wed Feb  3 23:59:40 2010
@@ -101,33 +101,35 @@
                      struct avro_union_schema_t *writers_schema)
 {
        int rval;
-       int64_t i, index;
-       struct avro_union_branch_t *branch;
+       int64_t index;
+       union {
+               st_data_t data;
+               avro_schema_t schema;
+       } val;
 
        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) {
+       if (!st_lookup(writers_schema->branches, index, &val.data)) {
                return EILSEQ;
        }
-       return avro_skip_data(reader, branch->schema);
+       return avro_skip_data(reader, val.schema);
 }
 
 static int skip_record(avro_reader_t reader, const avro_encoding_t * enc,
                       struct avro_record_schema_t *writers_schema)
 {
        int rval;
-       struct avro_record_field_t *field;
+       long i;
 
-       for (field = STAILQ_FIRST(&writers_schema->fields);
-            field != NULL; field = STAILQ_NEXT(field, fields)) {
-               rval = avro_skip_data(reader, field->type);
+       for (i = 0; i < writers_schema->fields->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       struct avro_record_field_t *field;
+               } val;
+               st_lookup(writers_schema->fields, i, &val.data);
+               rval = avro_skip_data(reader, val.field->type);
                if (rval) {
                        return rval;
                }

Modified: hadoop/avro/trunk/lang/c/src/datum_validate.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum_validate.c?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_validate.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_validate.c Wed Feb  3 23:59:40 2010
@@ -40,6 +40,9 @@
 int
 avro_schema_datum_validate(avro_schema_t expected_schema, avro_datum_t datum)
 {
+       int rval;
+       long i;
+
        if (!is_avro_schema(expected_schema) || !is_avro_datum(datum)) {
                return EINVAL;
        }
@@ -80,49 +83,45 @@
                            avro_datum_to_fixed(datum)->size));
 
        case AVRO_ENUM:
-               {
+               if (is_avro_enum(datum)) {
                        struct avro_enum_schema_t *enump =
                            avro_schema_to_enum(expected_schema);
-                       struct avro_enum_symbol_t *symbol =
-                           STAILQ_FIRST(&enump->symbols);
-                       while (symbol) {
-                               if (!strcmp
-                                   (symbol->symbol,
-                                    avro_datum_to_enum(datum)->symbol)) {
-                                       return 1;
-                               }
-                               symbol = STAILQ_NEXT(symbol, symbols);
-                       }
-                       return 0;
+                       struct avro_enum_datum_t *d = avro_datum_to_enum(datum);
+                       union {
+                               st_data_t data;
+                               long idx;
+                       } val;
+                       return st_lookup(enump->symbols_byname,
+                                        (st_data_t) d->symbol, &val.data);
                }
-               break;
+               return 0;
 
        case AVRO_ARRAY:
-               {
-                       if (is_avro_array(datum)) {
-                               struct avro_array_datum_t *array =
-                                   avro_datum_to_array(datum);
-                               struct avro_array_element_t *el =
-                                   STAILQ_FIRST(&array->els);
-                               while (el) {
-                                       if (!avro_schema_datum_validate
-                                           ((avro_schema_to_array
-                                             (expected_schema))->items,
-                                            el->datum)) {
-                                               return 0;
-                                       }
-                                       el = STAILQ_NEXT(el, els);
+               if (is_avro_array(datum)) {
+                       struct avro_array_datum_t *array =
+                           avro_datum_to_array(datum);
+
+                       for (i = 0; i < array->els->num_entries; i++) {
+                               union {
+                                       st_data_t data;
+                                       avro_datum_t datum;
+                               } val;
+                               st_lookup(array->els, i, &val.data);
+                               if (!avro_schema_datum_validate
+                                   ((avro_schema_to_array
+                                     (expected_schema))->items, val.datum)) {
+                                       return 0;
                                }
-                               return 1;
                        }
-                       return 0;
+                       return 1;
                }
-               break;
+               return 0;
 
        case AVRO_MAP:
                if (is_avro_map(datum)) {
                        struct validate_st vst =
-                           { avro_schema_to_map(expected_schema)->values, 1 };
+                           { avro_schema_to_map(expected_schema)->values, 1
+                       };
                        st_foreach(avro_datum_to_map(datum)->map,
                                   schema_map_validate_foreach,
                                   (st_data_t) & vst);
@@ -134,41 +133,45 @@
                {
                        struct avro_union_schema_t *union_schema =
                            avro_schema_to_union(expected_schema);
-                       struct avro_union_branch_t *branch;
 
-                       for (branch = STAILQ_FIRST(&union_schema->branches);
-                            branch != NULL;
-                            branch = STAILQ_NEXT(branch, branches)) {
+                       for (i = 0; i < union_schema->branches->num_entries;
+                            i++) {
+                               union {
+                                       st_data_t data;
+                                       avro_schema_t schema;
+                               } val;
+                               st_lookup(union_schema->branches, i, &val.data);
                                if (avro_schema_datum_validate
-                                   (branch->schema, datum)) {
+                                   (val.schema, datum)) {
                                        return 1;
                                }
                        }
-                       return 0;
                }
-               break;
+               return 0;
 
        case AVRO_RECORD:
                if (is_avro_record(datum)) {
                        struct avro_record_schema_t *record_schema =
                            avro_schema_to_record(expected_schema);
-                       struct avro_record_field_t *field;
-                       for (field = STAILQ_FIRST(&record_schema->fields);
-                            field != NULL;
-                            field = STAILQ_NEXT(field, fields)) {
-                               int field_rval;
+                       for (i = 0; i < record_schema->fields->num_entries; 
i++) {
                                avro_datum_t field_datum;
-                               field_rval =
-                                   avro_record_get(datum, field->name,
+                               union {
+                                       st_data_t data;
+                                       struct avro_record_field_t *field;
+                               } val;
+                               st_lookup(record_schema->fields, i, &val.data);
+
+                               rval =
+                                   avro_record_get(datum, val.field->name,
                                                    &field_datum);
-                               if (field_rval) {
+                               if (rval) {
                                        /*
                                         * TODO: check for default values 
                                         */
-                                       return field_rval;
+                                       return rval;
                                }
                                if (!avro_schema_datum_validate
-                                   (field->type, field_datum)) {
+                                   (val.field->type, field_datum)) {
                                        return 0;
                                }
                        }

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=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum_write.c (original)
+++ hadoop/avro/trunk/lang/c/src/datum_write.c Wed Feb  3 23:59:40 2010
@@ -26,15 +26,20 @@
             struct avro_record_schema_t *record, avro_datum_t datum)
 {
        int rval;
-       struct avro_record_field_t *field = STAILQ_FIRST(&record->fields);
-       for (; field != NULL; field = STAILQ_NEXT(field, fields)) {
-               int field_rval;
+       long i;
+
+       for (i = 0; i < record->fields->num_entries; i++) {
                avro_datum_t field_datum;
-               field_rval = avro_record_get(datum, field->name, &field_datum);
-               if (field_rval) {
-                       return field_rval;
+               union {
+                       st_data_t data;
+                       struct avro_record_field_t *field;
+               } val;
+               st_lookup(record->fields, i, &val.data);
+               rval = avro_record_get(datum, val.field->name, &field_datum);
+               if (rval) {
+                       return rval;
                }
-               rval = avro_write_data(writer, field->type, field_datum);
+               rval = avro_write_data(writer, val.field->type, field_datum);
                if (rval) {
                        return rval;
                }
@@ -46,17 +51,15 @@
 write_enum(avro_writer_t writer, const avro_encoding_t * enc,
           struct avro_enum_schema_t *enump, struct avro_enum_datum_t *datum)
 {
-       int64_t index;
-       struct avro_enum_symbol_t *sym = STAILQ_FIRST(&enump->symbols);
-       for (index = 0; sym != NULL; sym = STAILQ_NEXT(sym, symbols), index++) {
-               if (strcmp(sym->symbol, datum->symbol) == 0) {
-                       break;
-               }
-       }
-       if (!sym) {
+       union {
+               st_data_t data;
+               long idx;
+       } val;
+       if (!st_lookup
+           (enump->symbols_byname, (st_data_t) datum->symbol, &val.data)) {
                return EINVAL;
        }
-       return enc->write_long(writer, index);
+       return enc->write_long(writer, val.idx);
 }
 
 struct write_map_args {
@@ -113,17 +116,21 @@
            struct avro_array_datum_t *array)
 {
        int rval;
-       struct avro_array_element_t *el;
+       long i;
 
-       if (array->num_elements) {
-               rval = enc->write_long(writer, array->num_elements);
+       if (array->els->num_entries) {
+               rval = enc->write_long(writer, array->els->num_entries);
                if (rval) {
                        return rval;
                }
-               for (el = STAILQ_FIRST(&array->els);
-                    el != NULL; el = STAILQ_NEXT(el, els)) {
+               for (i = 0; i < array->els->num_entries; i++) {
+                       union {
+                               st_data_t data;
+                               avro_datum_t datum;
+                       } val;
+                       st_lookup(array->els, i, &val.data);
                        rval =
-                           avro_write_data(writer, schema->items, el->datum);
+                           avro_write_data(writer, schema->items, val.datum);
                        if (rval) {
                                return rval;
                        }
@@ -137,22 +144,23 @@
            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;
+       long i;
+
+       for (i = 0; i < schema->branches->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       avro_schema_t schema;
+               } val;
+               st_lookup(schema->branches, i, &val.data);
+               if (avro_schema_datum_validate(val.schema, datum)) {
+                       rval = enc->write_long(writer, i);
+                       if (rval) {
+                               return rval;
+                       }
+                       return avro_write_data(writer, val.schema, datum);
                }
        }
-       if (!branch) {
-               return EINVAL;
-       }
-       rval = enc->write_long(writer, index);
-       if (rval) {
-               return rval;
-       }
-       return avro_write_data(writer, branch->schema, datum);
+       return EINVAL;
 }
 
 int

Modified: hadoop/avro/trunk/lang/c/src/schema.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.c?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema.c Wed Feb  3 23:59:40 2010
@@ -24,6 +24,8 @@
 #include "st.h"
 #include "schema.h"
 
+#define DEFAULT_TABLE_SIZE 32
+
 struct avro_schema_error_t {
        st_table *named_schemas;
        json_error_t json_error;
@@ -58,6 +60,27 @@
        return 0;
 }
 
+static int record_free_foreach(int i, struct avro_record_field_t *field,
+                              void *arg)
+{
+       free(field->name);
+       avro_schema_decref(field->type);
+       free(field);
+       return ST_DELETE;
+}
+
+static int enum_free_foreach(int i, char *sym, void *arg)
+{
+       free(sym);
+       return ST_DELETE;
+}
+
+static int union_free_foreach(int i, avro_schema_t schema, void *arg)
+{
+       avro_schema_decref(schema);
+       return ST_DELETE;
+}
+
 static void avro_schema_free(avro_schema_t schema)
 {
        if (is_avro_schema(schema)) {
@@ -77,15 +100,10 @@
                                struct avro_record_schema_t *record;
                                record = avro_schema_to_record(schema);
                                free(record->name);
-                               while (!STAILQ_EMPTY(&record->fields)) {
-                                       struct avro_record_field_t *field;
-                                       field = STAILQ_FIRST(&record->fields);
-                                       STAILQ_REMOVE_HEAD(&record->fields,
-                                                          fields);
-                                       avro_schema_decref(field->type);
-                                       free(field->name);
-                                       free(field);
-                               }
+                               st_foreach(record->fields, record_free_foreach,
+                                          0);
+                               st_free_table(record->fields_byname);
+                               st_free_table(record->fields);
                                free(record);
                        }
                        break;
@@ -94,14 +112,10 @@
                                struct avro_enum_schema_t *enump;
                                enump = avro_schema_to_enum(schema);
                                free(enump->name);
-                               while (!STAILQ_EMPTY(&enump->symbols)) {
-                                       struct avro_enum_symbol_t *symbol;
-                                       symbol = STAILQ_FIRST(&enump->symbols);
-                                       STAILQ_REMOVE_HEAD(&enump->symbols,
-                                                          symbols);
-                                       free(symbol->symbol);
-                                       free(symbol);
-                               }
+                               st_foreach(enump->symbols, enum_free_foreach,
+                                          0);
+                               st_free_table(enump->symbols);
+                               st_free_table(enump->symbols_byname);
                                free(enump);
                        }
                        break;
@@ -132,15 +146,9 @@
                case AVRO_UNION:{
                                struct avro_union_schema_t *unionp;
                                unionp = avro_schema_to_union(schema);
-                               while (!STAILQ_EMPTY(&unionp->branches)) {
-                                       struct avro_union_branch_t *branch;
-                                       branch =
-                                           STAILQ_FIRST(&unionp->branches);
-                                       STAILQ_REMOVE_HEAD(&unionp->branches,
-                                                          branches);
-                                       avro_schema_decref(branch->schema);
-                                       free(branch);
-                               }
+                               st_foreach(unionp->branches, union_free_foreach,
+                                          0);
+                               st_free_table(unionp->branches);
                                free(unionp);
                        }
                        break;
@@ -275,7 +283,12 @@
        if (!schema) {
                return NULL;
        }
-       STAILQ_INIT(&schema->branches);
+       schema->branches = st_init_numtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!schema->branches) {
+               free(schema);
+               return NULL;
+       }
+
        avro_schema_init(&schema->obj, AVRO_UNION);
        return &schema->obj;
 }
@@ -285,18 +298,13 @@
                         const avro_schema_t schema)
 {
        struct avro_union_schema_t *unionp;
-       struct avro_union_branch_t *s;
-
        if (!union_schema || !schema || !is_avro_union(union_schema)) {
                return EINVAL;
        }
        unionp = avro_schema_to_union(union_schema);
-       s = malloc(sizeof(struct avro_union_branch_t));
-       if (!s) {
-               return ENOMEM;
-       }
-       s->schema = avro_schema_incref(schema);
-       STAILQ_INSERT_TAIL(&unionp->branches, s, branches);
+       st_insert(unionp->branches, unionp->branches->num_entries,
+                 (st_data_t) schema);
+       avro_schema_incref(schema);
        return 0;
 }
 
@@ -326,19 +334,33 @@
 
 avro_schema_t avro_schema_enum(const char *name)
 {
-       struct avro_enum_schema_t *enump =
-           malloc(sizeof(struct avro_enum_schema_t));
-       if (!enump) {
+       struct avro_enum_schema_t *enump;
+
+       if (!is_avro_id(name)) {
                return NULL;
        }
-       if (!is_avro_id(name)) {
+       enump = malloc(sizeof(struct avro_enum_schema_t));
+       if (!enump) {
                return NULL;
        }
        enump->name = strdup(name);
        if (!enump->name) {
+               free(enump);
+               return NULL;
+       }
+       enump->symbols = st_init_numtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!enump->symbols) {
+               free(enump->name);
+               free(enump);
+               return NULL;
+       }
+       enump->symbols_byname = st_init_strtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!enump->symbols_byname) {
+               st_free_table(enump->symbols);
+               free(enump->name);
+               free(enump);
                return NULL;
        }
-       STAILQ_INIT(&enump->symbols);
        avro_schema_init(&enump->obj, AVRO_ENUM);
        return &enump->obj;
 }
@@ -348,17 +370,19 @@
                               const char *symbol)
 {
        struct avro_enum_schema_t *enump;
-       struct avro_enum_symbol_t *enum_symbol;
+       char *sym;
+       long idx;
        if (!enum_schema || !symbol || !is_avro_enum(enum_schema)) {
                return EINVAL;
        }
        enump = avro_schema_to_enum(enum_schema);
-       enum_symbol = malloc(sizeof(struct avro_enum_symbol_t));
-       if (!enum_symbol) {
+       sym = strdup(symbol);
+       if (!sym) {
                return ENOMEM;
        }
-       enum_symbol->symbol = strdup(symbol);
-       STAILQ_INSERT_TAIL(&enump->symbols, enum_symbol, symbols);
+       idx = enump->symbols->num_entries;
+       st_insert(enump->symbols, (st_data_t) idx, (st_data_t) sym);
+       st_insert(enump->symbols_byname, (st_data_t) sym, (st_data_t) idx);
        return 0;
 }
 
@@ -381,22 +405,42 @@
        }
        new_field->name = strdup(field_name);
        new_field->type = avro_schema_incref(field_schema);
-       STAILQ_INSERT_TAIL(&record->fields, new_field, fields);
+       st_insert(record->fields, record->fields->num_entries,
+                 (st_data_t) new_field);
+       st_insert(record->fields_byname, (st_data_t) new_field->name,
+                 (st_data_t) new_field);
        return 0;
 }
 
 avro_schema_t avro_schema_record(const char *name)
 {
-       struct avro_record_schema_t *record =
-           malloc(sizeof(struct avro_record_schema_t));
-       if (!record) {
+       struct avro_record_schema_t *record;
+       if (!is_avro_id(name)) {
                return NULL;
        }
-       if (!is_avro_id(name)) {
+       record = malloc(sizeof(struct avro_record_schema_t));
+       if (!record) {
                return NULL;
        }
        record->name = strdup(name);
-       STAILQ_INIT(&record->fields);
+       if (!record->name) {
+               free(record);
+               return NULL;
+       }
+       record->fields = st_init_numtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!record->fields) {
+               free(record->name);
+               free(record);
+               return NULL;
+       }
+       record->fields_byname = st_init_strtable_with_size(DEFAULT_TABLE_SIZE);
+       if (!record->fields_byname) {
+               st_free_table(record->fields);
+               free(record->name);
+               free(record);
+               return NULL;
+       }
+
        avro_schema_init(&record->obj, AVRO_RECORD);
        return &record->obj;
 }
@@ -776,7 +820,7 @@
        }
        *e = error;
 
-       error->named_schemas = st_init_strtable();
+       error->named_schemas = st_init_strtable_with_size(DEFAULT_TABLE_SIZE);
        if (!error->named_schemas) {
                free(error);
                return ENOMEM;
@@ -804,6 +848,7 @@
 
 avro_schema_t avro_schema_copy(avro_schema_t schema)
 {
+       long i;
        avro_schema_t new_schema = NULL;
        if (!schema) {
                return NULL;
@@ -827,22 +872,18 @@
                {
                        struct avro_record_schema_t *record_schema =
                            avro_schema_to_record(schema);
-                       struct avro_record_field_t *field;
                        new_schema = avro_schema_record(record_schema->name);
-                       for (field = STAILQ_FIRST(&record_schema->fields);
-                            field != NULL;
-                            field = STAILQ_NEXT(field, fields)) {
-                               avro_schema_t field_copy =
-                                   avro_schema_copy(field->type);
-                               if (!field_copy) {
-                                       avro_schema_decref(new_schema);
-                                       return NULL;
-                               }
-                               if (avro_schema_record_field_append
-                                   (new_schema, field->name, field_copy)) {
-                                       avro_schema_decref(new_schema);
-                                       return NULL;
-                               }
+                       for (i = 0; i < record_schema->fields->num_entries; 
i++) {
+                               union {
+                                       st_data_t data;
+                                       struct avro_record_field_t *field;
+                               } val;
+                               st_lookup(record_schema->fields, i, &val.data);
+                               avro_schema_t type_copy =
+                                   avro_schema_copy(val.field->type);
+                               avro_schema_record_field_append(new_schema,
+                                                               val.field->name,
+                                                               type_copy);
                        }
                }
                break;
@@ -852,15 +893,14 @@
                        struct avro_enum_schema_t *enum_schema =
                            avro_schema_to_enum(schema);
                        new_schema = avro_schema_enum(enum_schema->name);
-                       struct avro_enum_symbol_t *enum_symbol;
-                       for (enum_symbol = STAILQ_FIRST(&enum_schema->symbols);
-                            enum_symbol != NULL;
-                            enum_symbol = STAILQ_NEXT(enum_symbol, symbols)) {
-                               if (avro_schema_enum_symbol_append
-                                   (new_schema, enum_symbol->symbol)) {
-                                       avro_schema_decref(new_schema);
-                                       return NULL;
-                               }
+                       for (i = 0; i < enum_schema->symbols->num_entries; i++) 
{
+                               union {
+                                       st_data_t data;
+                                       char *sym;
+                               } val;
+                               st_lookup(enum_schema->symbols, i, &val.data);
+                               avro_schema_enum_symbol_append(new_schema,
+                                                              val.sym);
                        }
                }
                break;
@@ -905,14 +945,17 @@
                {
                        struct avro_union_schema_t *union_schema =
                            avro_schema_to_union(schema);
-                       struct avro_union_branch_t *s;
 
                        new_schema = avro_schema_union();
-
-                       for (s = STAILQ_FIRST(&union_schema->branches);
-                            s != NULL; s = STAILQ_NEXT(s, branches)) {
-                               avro_schema_t schema_copy =
-                                   avro_schema_copy(s->schema);
+                       for (i = 0; i < union_schema->branches->num_entries;
+                            i++) {
+                               avro_schema_t schema_copy;
+                               union {
+                                       st_data_t data;
+                                       avro_schema_t schema;
+                               } val;
+                               st_lookup(union_schema->branches, i, &val.data);
+                               schema_copy = avro_schema_copy(val.schema);
                                if (avro_schema_union_append
                                    (new_schema, schema_copy)) {
                                        avro_schema_decref(new_schema);

Modified: hadoop/avro/trunk/lang/c/src/schema.h
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.h?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.h (original)
+++ hadoop/avro/trunk/lang/c/src/schema.h Wed Feb  3 23:59:40 2010
@@ -19,7 +19,7 @@
 
 #include "avro.h"
 #include "container_of.h"
-#include "queue.h"
+#include "st.h"
 
 struct avro_record_field_t {
        char *name;
@@ -27,25 +27,20 @@
        /*
         * TODO: default values 
         */
-        STAILQ_ENTRY(avro_record_field_t) fields;
 };
 
 struct avro_record_schema_t {
        struct avro_obj_t obj;
        char *name;
-       /* TODO: st_table of names for faster lookup on record_read() */
-        STAILQ_HEAD(fields, avro_record_field_t) fields;
-};
-
-struct avro_enum_symbol_t {
-       char *symbol;
-        STAILQ_ENTRY(avro_enum_symbol_t) symbols;
+       st_table *fields;
+       st_table *fields_byname;
 };
 
 struct avro_enum_schema_t {
        struct avro_obj_t obj;
        char *name;
-        STAILQ_HEAD(symbols, avro_enum_symbol_t) symbols;
+       st_table *symbols;
+       st_table *symbols_byname;
 };
 
 struct avro_array_schema_t {
@@ -58,14 +53,9 @@
        avro_schema_t values;
 };
 
-struct avro_union_branch_t {
-       avro_schema_t schema;
-        STAILQ_ENTRY(avro_union_branch_t) branches;
-};
-
 struct avro_union_schema_t {
        struct avro_obj_t obj;
-        STAILQ_HEAD(branch, avro_union_branch_t) branches;
+       st_table *branches;
 };
 
 struct avro_fixed_schema_t {

Modified: hadoop/avro/trunk/lang/c/src/schema_equal.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema_equal.c?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema_equal.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema_equal.c Wed Feb  3 23:59:40 2010
@@ -22,29 +22,29 @@
 schema_record_equal(struct avro_record_schema_t *a,
                    struct avro_record_schema_t *b)
 {
-       struct avro_record_field_t *field_a, *field_b;
+       long i;
        if (strcmp(a->name, b->name)) {
                /*
                 * They have different names 
                 */
                return 0;
        }
-
-       for (field_a = STAILQ_FIRST(&a->fields),
-            field_b = STAILQ_FIRST(&b->fields);
-            !(field_a == NULL && field_b == NULL);
-            field_a = STAILQ_NEXT(field_a, fields),
-            field_b = STAILQ_NEXT(field_b, fields)) {
-               if (field_a == NULL || field_b == NULL) {
-                       return 0;       /* different num fields */
+       for (i = 0; i < a->fields->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       struct avro_record_field_t *f;
+               } fa, fb;
+               st_lookup(a->fields, i, &fa.data);
+               if (!st_lookup(b->fields, i, &fb.data)) {
+                       return 0;
                }
-               if (strcmp(field_a->name, field_b->name)) {
+               if (strcmp(fa.f->name, fb.f->name)) {
                        /*
                         * They have fields with different names 
                         */
                        return 0;
                }
-               if (!avro_schema_equal(field_a->type, field_b->type)) {
+               if (!avro_schema_equal(fa.f->type, fb.f->type)) {
                        /*
                         * They have fields with different schemas 
                         */
@@ -57,23 +57,23 @@
 static int
 schema_enum_equal(struct avro_enum_schema_t *a, struct avro_enum_schema_t *b)
 {
-       struct avro_enum_symbol_t *sym_a, *sym_b;
-
+       long i;
        if (strcmp(a->name, b->name)) {
                /*
                 * They have different names 
                 */
                return 0;
        }
-       for (sym_a = STAILQ_FIRST(&a->symbols),
-            sym_b = STAILQ_FIRST(&b->symbols);
-            !(sym_a == NULL && sym_b == NULL);
-            sym_a = STAILQ_NEXT(sym_a, symbols),
-            sym_b = STAILQ_NEXT(sym_b, symbols)) {
-               if (sym_a == NULL || sym_b == NULL) {
-                       return 0;       /* different num symbols */
+       for (i = 0; i < a->symbols->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       char *sym;
+               } sa, sb;
+               st_lookup(a->symbols, i, &sa.data);
+               if (!st_lookup(b->symbols, i, &sb.data)) {
+                       return 0;
                }
-               if (strcmp(sym_a->symbol, sym_b->symbol)) {
+               if (strcmp(sa.sym, sb.sym) != 0) {
                        /*
                         * They have different symbol names 
                         */
@@ -110,17 +110,17 @@
 static int
 schema_union_equal(struct avro_union_schema_t *a, struct avro_union_schema_t 
*b)
 {
-       struct avro_union_branch_t *branch_a, *branch_b;
-
-       for (branch_a = STAILQ_FIRST(&a->branches),
-            branch_b = STAILQ_FIRST(&b->branches);
-            !(branch_a == NULL && branch_b == NULL);
-            branch_a = STAILQ_NEXT(branch_a, branches),
-            branch_b = STAILQ_NEXT(branch_b, branches)) {
-               if (branch_a == NULL || branch_b == NULL) {
-                       return 0;       /* different num symbols */
+       long i;
+       for (i = 0; i < a->branches->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       avro_schema_t schema;
+               } ab, bb;
+               st_lookup(a->branches, i, &ab.data);
+               if (!st_lookup(b->branches, i, &bb.data)) {
+                       return 0;
                }
-               if (!avro_schema_equal(branch_a->schema, branch_b->schema)) {
+               if (!avro_schema_equal(ab.schema, bb.schema)) {
                        /*
                         * They don't have the same schema types 
                         */

Modified: hadoop/avro/trunk/lang/c/src/schema_printf.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema_printf.c?rev=906293&r1=906292&r2=906293&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema_printf.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema_printf.c Wed Feb  3 23:59:40 2010
@@ -92,13 +92,17 @@
 avro_schema_record_print(struct avro_record_schema_t *record,
                         struct avro_schema_args_t *args)
 {
-       struct avro_record_field_t *field;
+       long i;
        avro_schema_printf_indent(args);
        fprintf(args->fp, "record \"%s\"\n", record->name);
        args->depth++;
-       for (field = STAILQ_FIRST(&record->fields);
-            field != NULL; field = STAILQ_NEXT(field, fields)) {
-               avro_schema_record_field_print(field, args);
+       for (i = 0; i < record->fields->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       struct avro_record_field_t *field;
+               } val;
+               st_lookup(record->fields, i, &val.data);
+               avro_schema_record_field_print(val.field, args);
        }
        args->depth--;
 }


Reply via email to