Author: massie
Date: Sat Feb  6 03:39:08 2010
New Revision: 907169

URL: http://svn.apache.org/viewvc?rev=907169&view=rev
Log:
AVRO-409. Update contact database example to use a file object container for C 
implementation

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/c/examples/quickstop.c

Modified: hadoop/avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=907169&r1=907168&r2=907169&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Sat Feb  6 03:39:08 2010
@@ -313,6 +313,9 @@
 
     AVRO-400. Adding warning for unused parameters (Eli Collins via massie)
 
+    AVRO-409. Update contact database example to use a file object 
+              container for C implementation (massie)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Modified: hadoop/avro/trunk/lang/c/examples/quickstop.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/c/examples/quickstop.c?rev=907169&r1=907168&r2=907169&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/c/examples/quickstop.c (original)
+++ hadoop/avro/trunk/lang/c/examples/quickstop.c Sat Feb  6 03:39:08 2010
@@ -16,9 +16,9 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <avro.h>
 
-char buf[4096];
 avro_schema_t person_schema;
 int64_t id = 0;
 
@@ -46,7 +46,7 @@
 
 /* Create a datum to match the person schema and save it */
 void
-add_person(avro_writer_t writer, const char *first, const char *last,
+add_person(avro_file_writer_t db, const char *first, const char *last,
           const char *phone, int32_t age)
 {
        avro_datum_t person = avro_record("Person");
@@ -66,7 +66,7 @@
                exit(EXIT_FAILURE);
        }
 
-       if (avro_write_data(writer, person_schema, person)) {
+       if (avro_file_writer_append(db, person)) {
                fprintf(stderr,
                        "Unable to write Person datum to memory buffer");
                exit(EXIT_FAILURE);
@@ -83,12 +83,12 @@
        fprintf(stdout, "Successfully added %s, %s id=%ld\n", last, first, id);
 }
 
-int print_person(avro_reader_t reader, avro_schema_t reader_schema)
+int print_person(avro_file_reader_t db, avro_schema_t reader_schema)
 {
        int rval;
        avro_datum_t person;
 
-       rval = avro_read_data(reader, person_schema, reader_schema, &person);
+       rval = avro_file_reader_read(db, reader_schema, &person);
        if (rval == 0) {
                int64_t i64;
                int32_t i32;
@@ -126,40 +126,44 @@
 
 int main(void)
 {
-       avro_reader_t reader;
-       avro_writer_t writer;
+       int rval;
+       avro_file_reader_t dbreader;
+       avro_file_writer_t db;
        avro_schema_t projection_schema, first_name_schema, phone_schema;
        int64_t i;
+       const char *dbname = "quickstop.db";
 
        /* Initialize the schema structure from JSON */
        init_schema();
 
+       /* Delete the database if it exists */
+       unlink(dbname);
+       /* Create a new database */
+       rval = avro_file_writer_create(dbname, person_schema, &db);
+       if (rval) {
+               fprintf(stderr, "There was an error creating %s\n", dbname);
+               exit(EXIT_FAILURE);
+       }
        /* Add people to the database */
-       writer = avro_writer_memory(buf, sizeof(buf));
-       add_person(writer, "Dante", "Hicks", "(555) 123-4567", 32);
-       add_person(writer, "Randal", "Graves", "(555) 123-5678", 30);
-       add_person(writer, "Veronica", "Loughran", "(555) 123-0987", 28);
-       add_person(writer, "Caitlin", "Bree", "(555) 123-2323", 27);
-       add_person(writer, "Bob", "Silent", "(555) 123-6422", 29);
-       add_person(writer, "Jay", "???", "(555) 123-9182", 26);
-       avro_writer_free(writer);
-
-       fprintf(stdout,
-               "\nAvro is compact. Here is the data for all %ld people.\n",
-               id);
-       avro_writer_dump(writer, stdout);
+       add_person(db, "Dante", "Hicks", "(555) 123-4567", 32);
+       add_person(db, "Randal", "Graves", "(555) 123-5678", 30);
+       add_person(db, "Veronica", "Loughran", "(555) 123-0987", 28);
+       add_person(db, "Caitlin", "Bree", "(555) 123-2323", 27);
+       add_person(db, "Bob", "Silent", "(555) 123-6422", 29);
+       add_person(db, "Jay", "???", "(555) 123-9182", 26);
+       avro_file_writer_close(db);
 
        fprintf(stdout, "\nNow let's read all the records back out\n");
 
        /* Read all the records and print them */
-       reader = avro_reader_memory(buf, sizeof(buf));
+       avro_file_reader(dbname, &dbreader);
        for (i = 0; i < id; i++) {
-               if (print_person(reader, NULL)) {
+               if (print_person(dbreader, NULL)) {
                        fprintf(stderr, "Error printing person\n");
                        exit(EXIT_FAILURE);
                }
        }
-       avro_reader_free(reader);
+       avro_file_reader_close(dbreader);
 
        /* You can also use projection, to only decode only the data you are
           interested in.  This is particularly useful when you have 
@@ -176,14 +180,14 @@
        /* Read only the record you're interested in */
        fprintf(stdout,
                "\n\nUse projection to print only the First name and phone 
numbers\n");
-       reader = avro_reader_memory(buf, sizeof(buf));
+       avro_file_reader(dbname, &dbreader);
        for (i = 0; i < id; i++) {
-               if (print_person(reader, projection_schema)) {
+               if (print_person(dbreader, projection_schema)) {
                        fprintf(stderr, "Error printing person\n");
                        exit(EXIT_FAILURE);
                }
        }
-       avro_reader_free(reader);
+       avro_file_reader_close(dbreader);
        avro_schema_decref(first_name_schema);
        avro_schema_decref(phone_schema);
        avro_schema_decref(projection_schema);


Reply via email to