Author: massie
Date: Thu Feb  4 21:41:02 2010
New Revision: 906668

URL: http://svn.apache.org/viewvc?rev=906668&view=rev
Log:
AVRO-402. Add method for writing avro_schema_t structure to an avro_writer_t

Added:
    hadoop/avro/trunk/lang/c/src/avro_private.h
Removed:
    hadoop/avro/trunk/lang/c/src/container_of.h
    hadoop/avro/trunk/lang/c/src/schema_printf.c
Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/c/src/Makefile.am
    hadoop/avro/trunk/lang/c/src/avro.h
    hadoop/avro/trunk/lang/c/src/datum.h
    hadoop/avro/trunk/lang/c/src/io.c
    hadoop/avro/trunk/lang/c/src/schema.c
    hadoop/avro/trunk/lang/c/src/schema.h
    hadoop/avro/trunk/lang/c/tests/test_avro_schema.c

Modified: hadoop/avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Thu Feb  4 21:41:02 2010
@@ -304,6 +304,9 @@
 
     AVRO-321. Restore Java RPC interop tests. (cutting)
 
+    AVRO-402. Add method for writing avro_schema_t structure to an 
+              avro_writer_t (massie)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Modified: hadoop/avro/trunk/lang/c/src/Makefile.am
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/Makefile.am?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/Makefile.am (original)
+++ hadoop/avro/trunk/lang/c/src/Makefile.am Thu Feb  4 21:41:02 2010
@@ -6,7 +6,7 @@
 include_HEADERS = avro.h
 
 lib_LTLIBRARIES = libavro.la
-libavro_la_SOURCES = st.c st.h schema.c schema.h schema_printf.c 
schema_equal.c \
+libavro_la_SOURCES = st.c st.h schema.c schema.h 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 encoding.h

Modified: hadoop/avro/trunk/lang/c/src/avro.h
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/avro.h?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/avro.h (original)
+++ hadoop/avro/trunk/lang/c/src/avro.h Thu Feb  4 21:41:02 2010
@@ -90,6 +90,9 @@
 #define is_avro_complex_type(obj) (!(is_avro_primitive(obj))
 #define is_avro_link(obj)     (obj && avro_typeof(obj) == AVRO_LINK)
 
+typedef struct avro_reader_t *avro_reader_t;
+typedef struct avro_writer_t *avro_writer_t;
+
 /*
  * schema 
  */
@@ -129,6 +132,7 @@
 int avro_schema_from_json(const char *jsontext,
                          const int32_t len,
                          avro_schema_t * schema, avro_schema_error_t * error);
+int avro_schema_to_json(avro_schema_t schema, avro_writer_t out);
 
 int avro_schema_to_specific(avro_schema_t schema, const char *prefix);
 
@@ -139,14 +143,9 @@
 avro_schema_t avro_schema_incref(avro_schema_t schema);
 void avro_schema_decref(avro_schema_t schema);
 
-void avro_schema_printf(avro_schema_t schema, FILE * fp);
-
 /*
  * io 
  */
-typedef struct avro_reader_t *avro_reader_t;
-typedef struct avro_writer_t *avro_writer_t;
-
 avro_reader_t avro_reader_file(FILE * fp);
 avro_writer_t avro_writer_file(FILE * fp);
 avro_reader_t avro_reader_memory(const char *buf, int64_t len);

Added: hadoop/avro/trunk/lang/c/src/avro_private.h
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/avro_private.h?rev=906668&view=auto
==============================================================================
--- hadoop/avro/trunk/lang/c/src/avro_private.h (added)
+++ hadoop/avro/trunk/lang/c/src/avro_private.h Thu Feb  4 21:41:02 2010
@@ -0,0 +1,27 @@
+/*
+ * 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. 
+ */
+#ifndef AVRO_PRIVATE_H
+#define AVRO_PRIVATE_H
+
+#define sys_call(rval, call) do { rval = call; } while(rval < 0 && errno == 
EINTR)
+
+#define check(rval, call) { rval = call; if(rval) return rval; }
+
+#define container_of(ptr_, type_, member_)  \
+    ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
+
+#endif

Modified: hadoop/avro/trunk/lang/c/src/datum.h
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/datum.h?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/datum.h (original)
+++ hadoop/avro/trunk/lang/c/src/datum.h Thu Feb  4 21:41:02 2010
@@ -18,7 +18,7 @@
 #ifndef AVRO_DATUM_H
 #define AVRO_DATUM_H
 #include "avro.h"              /* for avro_schema_t */
-#include "container_of.h"
+#include "avro_private.h"
 #include "st.h"
 
 struct avro_string_datum_t {

Modified: hadoop/avro/trunk/lang/c/src/io.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/io.c?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/io.c (original)
+++ hadoop/avro/trunk/lang/c/src/io.c Thu Feb  4 21:41:02 2010
@@ -20,7 +20,7 @@
 #include <errno.h>
 #include <string.h>
 #include "avro.h"
-#include "container_of.h"
+#include "avro_private.h"
 #include "dump.h"
 
 enum avro_io_type_t {

Modified: hadoop/avro/trunk/lang/c/src/schema.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.c?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.c (original)
+++ hadoop/avro/trunk/lang/c/src/schema.c Thu Feb  4 21:41:02 2010
@@ -983,3 +983,187 @@
        }
        return new_schema;
 }
+
+const char *avro_schema_name(const avro_schema_t schema)
+{
+       if (is_avro_record(schema)) {
+               return (avro_schema_to_record(schema))->name;
+       } else if (is_avro_enum(schema)) {
+               return (avro_schema_to_enum(schema))->name;
+       } else if (is_avro_fixed(schema)) {
+               return (avro_schema_to_fixed(schema))->name;
+       }
+       return NULL;
+}
+
+/* simple helper for writing strings */
+static int avro_write_str(avro_writer_t out, const char *str)
+{
+       return avro_write(out, (char *)str, strlen(str));
+}
+
+static int write_field(avro_writer_t out, struct avro_record_field_t *field)
+{
+       int rval;
+       check(rval, avro_write_str(out, "{\"name\":\""));
+       check(rval, avro_write_str(out, field->name));
+       check(rval, avro_write_str(out, "\",\"type\":"));
+       check(rval, avro_schema_to_json(field->type, out));
+       return avro_write_str(out, "}");
+}
+
+static int write_record(avro_writer_t out, struct avro_record_schema_t *record)
+{
+       int rval;
+       long i;
+
+       check(rval, avro_write_str(out, "{\"type\":\"record\",\"name\":\""));
+       check(rval, avro_write_str(out, record->name));
+       check(rval, avro_write_str(out, "\",\"fields\":["));
+       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);
+               if (i) {
+                       check(rval, avro_write_str(out, ","));
+               }
+               check(rval, write_field(out, val.field));
+       }
+       return avro_write_str(out, "]}");
+}
+
+static int write_enum(avro_writer_t out, struct avro_enum_schema_t *enump)
+{
+       int rval;
+       long i;
+       check(rval, avro_write_str(out, "{\"type\":\"enum\",\"name\":\""));
+       check(rval, avro_write_str(out, enump->name));
+       check(rval, avro_write_str(out, "\",\"symbols\":["));
+
+       for (i = 0; i < enump->symbols->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       char *sym;
+               } val;
+               st_lookup(enump->symbols, i, &val.data);
+               if (i) {
+                       check(rval, avro_write_str(out, ","));
+               }
+               check(rval, avro_write_str(out, "\""));
+               check(rval, avro_write_str(out, val.sym));
+               check(rval, avro_write_str(out, "\""));
+       }
+       return avro_write_str(out, "]}");
+}
+static int write_fixed(avro_writer_t out, struct avro_fixed_schema_t *fixed)
+{
+       int rval;
+       char size[16];
+       check(rval, avro_write_str(out, "{\"type\":\"fixed\",\"name\":\""));
+       check(rval, avro_write_str(out, fixed->name));
+       check(rval, avro_write_str(out, "\",\"size\":"));
+       snprintf(size, sizeof(size), "%lld", fixed->size);
+       check(rval, avro_write_str(out, size));
+       return avro_write_str(out, "}");
+}
+static int write_map(avro_writer_t out, struct avro_map_schema_t *map)
+{
+       int rval;
+       check(rval, avro_write_str(out, "{\"type\":\"map\",\"values\":"));
+       check(rval, avro_schema_to_json(map->values, out));
+       return avro_write_str(out, "}");
+}
+static int write_array(avro_writer_t out, struct avro_array_schema_t *array)
+{
+       int rval;
+       check(rval, avro_write_str(out, "{\"type\":\"array\",\"items\":"));
+       check(rval, avro_schema_to_json(array->items, out));
+       return avro_write_str(out, "}");
+}
+static int write_union(avro_writer_t out, struct avro_union_schema_t *unionp)
+{
+       int rval;
+       long i;
+       check(rval, avro_write_str(out, "["));
+
+       for (i = 0; i < unionp->branches->num_entries; i++) {
+               union {
+                       st_data_t data;
+                       avro_schema_t schema;
+               } val;
+               st_lookup(unionp->branches, i, &val.data);
+               if (i) {
+                       check(rval, avro_write_str(out, ","));
+               }
+               check(rval, avro_schema_to_json(val.schema, out));
+       }
+       return avro_write_str(out, "]");
+}
+static int write_link(avro_writer_t out, struct avro_link_schema_t *link)
+{
+       int rval;
+       check(rval, avro_write_str(out, "\""));
+       check(rval, avro_write_str(out, avro_schema_name(link->to)));
+       return avro_write_str(out, "\"");
+}
+
+int avro_schema_to_json(avro_schema_t schema, avro_writer_t out)
+{
+       int rval;
+
+       if (!is_avro_schema(schema) || !out) {
+               return EINVAL;
+       }
+
+       if (is_avro_primitive(schema)) {
+               check(rval, avro_write_str(out, "{\"type\":\""));
+       }
+
+       switch (avro_typeof(schema)) {
+       case AVRO_STRING:
+               check(rval, avro_write_str(out, "string"));
+               break;
+       case AVRO_BYTES:
+               check(rval, avro_write_str(out, "bytes"));
+               break;
+       case AVRO_INT32:
+               check(rval, avro_write_str(out, "int"));
+               break;
+       case AVRO_INT64:
+               check(rval, avro_write_str(out, "long"));
+               break;
+       case AVRO_FLOAT:
+               check(rval, avro_write_str(out, "float"));
+               break;
+       case AVRO_DOUBLE:
+               check(rval, avro_write_str(out, "double"));
+               break;
+       case AVRO_BOOLEAN:
+               check(rval, avro_write_str(out, "boolean"));
+               break;
+       case AVRO_NULL:
+               check(rval, avro_write_str(out, "null"));
+               break;
+       case AVRO_RECORD:
+               return write_record(out, avro_schema_to_record(schema));
+       case AVRO_ENUM:
+               return write_enum(out, avro_schema_to_enum(schema));
+       case AVRO_FIXED:
+               return write_fixed(out, avro_schema_to_fixed(schema));
+       case AVRO_MAP:
+               return write_map(out, avro_schema_to_map(schema));
+       case AVRO_ARRAY:
+               return write_array(out, avro_schema_to_array(schema));
+       case AVRO_UNION:
+               return write_union(out, avro_schema_to_union(schema));
+       case AVRO_LINK:
+               return write_link(out, avro_schema_to_link(schema));
+       }
+
+       if (is_avro_primitive(schema)) {
+               return avro_write_str(out, "\"}");
+       }
+       return EINVAL;
+}

Modified: hadoop/avro/trunk/lang/c/src/schema.h
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/src/schema.h?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/src/schema.h (original)
+++ hadoop/avro/trunk/lang/c/src/schema.h Thu Feb  4 21:41:02 2010
@@ -18,7 +18,7 @@
 #define AVRO_SCHEMA_H
 
 #include "avro.h"
-#include "container_of.h"
+#include "avro_private.h"
 #include "st.h"
 
 struct avro_record_field_t {

Modified: hadoop/avro/trunk/lang/c/tests/test_avro_schema.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/tests/test_avro_schema.c?rev=906668&r1=906667&r2=906668&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/tests/test_avro_schema.c (original)
+++ hadoop/avro/trunk/lang/c/tests/test_avro_schema.c Thu Feb  4 21:41:02 2010
@@ -22,6 +22,7 @@
 #include "avro.h"
 
 int test_cases = 0;
+avro_writer_t avro_stderr;
 
 static void run_tests(char *dirpath, int should_pass)
 {
@@ -63,7 +64,9 @@
                                        avro_schema_t schema_copy =
                                            avro_schema_copy(schema);
                                        fprintf(stderr, "pass\n");
-                                       avro_schema_printf(schema, stderr);
+                                       avro_schema_to_json(schema,
+                                                           avro_stderr);
+                                       fprintf(stderr, "\n");
                                        if (!avro_schema_equal
                                            (schema, schema_copy)) {
                                                fprintf(stderr,
@@ -103,6 +106,8 @@
                srcdir = ".";
        }
 
+       avro_stderr = avro_writer_file(stderr);
+
        /*
         * Run the tests that should pass 
         */
@@ -119,5 +124,6 @@
                test_cases);
        fprintf(stderr, "==================================================\n");
 
+       avro_writer_free(avro_stderr);
        return EXIT_SUCCESS;
 }


Reply via email to