You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2009/08/18 23:07:08 UTC

svn commit: r805580 - in /hadoop/avro/trunk: ./ src/c++/ src/c++/api/ src/c++/impl/ src/c++/jsonschemas/ src/c++/parser/ src/c++/scripts/ src/c++/test/

Author: cutting
Date: Tue Aug 18 21:07:08 2009
New Revision: 805580

URL: http://svn.apache.org/viewvc?rev=805580&view=rev
Log:
AVRO-97.  Fix various C++ bugs.  Contributed by Scott Banachowski.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/c++/Makefile
    hadoop/avro/trunk/src/c++/api/AvroParse.hh
    hadoop/avro/trunk/src/c++/api/Exception.hh
    hadoop/avro/trunk/src/c++/api/Reader.hh
    hadoop/avro/trunk/src/c++/impl/ValidSchema.cc
    hadoop/avro/trunk/src/c++/jsonschemas/bigrecord
    hadoop/avro/trunk/src/c++/parser/avro.y
    hadoop/avro/trunk/src/c++/scripts/gen.py
    hadoop/avro/trunk/src/c++/test/testgen.cc
    hadoop/avro/trunk/src/c++/test/unittest.cc

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Aug 18 21:07:08 2009
@@ -58,6 +58,8 @@
     AVRO-61. Add Python support for reading blocked data.
     (Ravi Gummadi via cutting)
 
+    AVRO-97.  Fix various C++ bugs.  (Scott Banachowski via cutting)
+
 Avro 1.0.0 -- 9 July 2009
 
   INCOMPATIBLE CHANGES

Modified: hadoop/avro/trunk/src/c++/Makefile
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/Makefile?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/Makefile (original)
+++ hadoop/avro/trunk/src/c++/Makefile Tue Aug 18 21:07:08 2009
@@ -61,7 +61,7 @@
 precompile: test/precompile.cc $(OBJDIR)/avrolib.a 
 	$(CXX) $(CXXFLAGS) $(INCFLAGS) -o $@ $^ $(LIBS)
 
-test/code.hh: scripts/gen.py precompile
+test/code.hh: scripts/gen.py precompile jsonschemas/bigrecord
 	./precompile < jsonschemas/bigrecord > obj/bigrecord.flat
 	python scripts/gen.py < obj/bigrecord.flat > test/code.hh
 

Modified: hadoop/avro/trunk/src/c++/api/AvroParse.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/AvroParse.hh?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/AvroParse.hh (original)
+++ hadoop/avro/trunk/src/c++/api/AvroParse.hh Tue Aug 18 21:07:08 2009
@@ -56,7 +56,7 @@
 
 template <typename Reader>
 void parse(Reader &p, std::vector<uint8_t> &val, const boost::true_type &) {
-    p.getBytes(&val);
+    p.getBytes(val);
 }
 
 // @}

Modified: hadoop/avro/trunk/src/c++/api/Exception.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/Exception.hh?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/Exception.hh (original)
+++ hadoop/avro/trunk/src/c++/api/Exception.hh Tue Aug 18 21:07:08 2009
@@ -20,10 +20,25 @@
 #define avro_Exception_hh__
 
 #include <stdexcept>
+#include <boost/format.hpp>
 
 namespace avro {
 
-typedef std::runtime_error Exception;
+/// Wrapper for std::runtime_error that provides convenience constructor
+/// for boost::format objects
+
+class Exception : public std::runtime_error
+{
+  public:
+
+    Exception(const std::string msg) :
+        std::runtime_error(msg)
+    { }
+
+    Exception(const boost::format &msg) :
+        std::runtime_error( boost::str(msg))
+    { }  
+};
 
 } // namespace avro
 

Modified: hadoop/avro/trunk/src/c++/api/Reader.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/Reader.hh?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/Reader.hh (original)
+++ hadoop/avro/trunk/src/c++/api/Reader.hh Tue Aug 18 21:07:08 2009
@@ -148,7 +148,7 @@
         uint64_t encoded = 0;
         uint8_t val = 0;
         do {
-            encoded <<= 8;
+            encoded <<= 7;
             in_.getByte(val);
             encoded |= (val & 0x7F);
 

Modified: hadoop/avro/trunk/src/c++/impl/ValidSchema.cc
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/impl/ValidSchema.cc?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/impl/ValidSchema.cc (original)
+++ hadoop/avro/trunk/src/c++/impl/ValidSchema.cc Tue Aug 18 21:07:08 2009
@@ -16,6 +16,8 @@
  * limitations under the License.
  */
 
+#include <boost/format.hpp>
+
 #include "ValidSchema.hh"
 #include "Schema.hh"
 #include "Node.hh"
@@ -53,7 +55,7 @@
     if(node->hasName()) {
         if(node->type() == AVRO_SYMBOLIC) {
             if(!symbolMap_.hasSymbol(node->name())) {
-                throw Exception("Symbolic name not found");
+                throw Exception( boost::format("Symbolic name \"%1%\" is unknown") % node->name());
             }
             return true;
         }

Modified: hadoop/avro/trunk/src/c++/jsonschemas/bigrecord
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/jsonschemas/bigrecord?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/jsonschemas/bigrecord (original)
+++ hadoop/avro/trunk/src/c++/jsonschemas/bigrecord Tue Aug 18 21:07:08 2009
@@ -45,6 +45,13 @@
             ]
         },
         {
+            "name": "anotherunion",
+            "type": [
+                "bytes",
+                "null"
+            ]
+        },
+        {
             "name": "mybool",
             "type": "boolean"
         },
@@ -59,6 +66,10 @@
         {
             "name": "anotherint",
             "type": "int"
+        },
+        {
+            "name": "bytes",
+            "type": "bytes"
         }
     ]
 }

Modified: hadoop/avro/trunk/src/c++/parser/avro.y
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/parser/avro.y?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/parser/avro.y (original)
+++ hadoop/avro/trunk/src/c++/parser/avro.y Tue Aug 18 21:07:08 2009
@@ -18,14 +18,16 @@
 */
 
 #include <stdio.h>
+#include <boost/format.hpp>
 #include "Compiler.hh"
+#include "Exception.hh"
+
 #define YYLEX_PARAM ctx
 #define YYPARSE_PARAM ctx
 
 void yyerror(const char *str)
 {
-    // fixme, do something better than this
-    fprintf(stderr,"error: %s\n",str);
+    throw avro::Exception(boost::format("Parser error: %1%") % str);
 }
  
 extern void *lexer; 
@@ -38,6 +40,7 @@
 %}
 
 %pure-parser
+%error-verbose
 
 %token AVRO_LEX_INT AVRO_LEX_LONG AVRO_LEX_FLOAT AVRO_LEX_DOUBLE
 %token AVRO_LEX_BOOL AVRO_LEX_NULL AVRO_LEX_BYTES AVRO_LEX_STRING

Modified: hadoop/avro/trunk/src/c++/scripts/gen.py
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/scripts/gen.py?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/scripts/gen.py (original)
+++ hadoop/avro/trunk/src/c++/scripts/gen.py Tue Aug 18 21:07:08 2009
@@ -30,7 +30,7 @@
 '''
 
 typeToC= { 'int' : 'int32_t', 'long' :'int64_t', 'float' : 'float', 'double' : 'double', 
-'boolean' : 'bool', 'null': 'avro::Null', 'string' : 'std::string', 'bytes' : 'std::vector<int8_t>'} 
+'boolean' : 'bool', 'null': 'avro::Null', 'string' : 'std::string', 'bytes' : 'std::vector<uint8_t>'} 
 
 structList = []
 structNames = {} 
@@ -133,7 +133,7 @@
 }
 '''
 
-unionser = '    case $choice$:\n      serialize(s, val.getValue<$type$>());\n      break;\n'
+unionser = '    case $choice$:\n      serialize(s, val.getValue< $type$ >());\n      break;\n'
 unionpar = '    case $choice$:\n      { $type$ chosenVal; parse(p, chosenVal); val.value = chosenVal; }\n      break;\n'
 
 setfunc =  '''    void set_$name$(const $type$ &val) {

Modified: hadoop/avro/trunk/src/c++/test/testgen.cc
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/test/testgen.cc?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/test/testgen.cc (original)
+++ hadoop/avro/trunk/src/c++/test/testgen.cc Tue Aug 18 21:07:08 2009
@@ -49,7 +49,7 @@
 
 void checkArray(const avrouser::Array_of_double &a1, const avrouser::Array_of_double &a2) 
 {
-    assert(a1.value.size() == a2.value.size());
+    assert((a1.value.size() == a2.value.size()) && (a1.value.size() == 3));
     for(size_t i = 0; i < a1.value.size(); ++i) {
         assert(a1.value[i] == a2.value[i]);
     }
@@ -70,6 +70,14 @@
     }
 }
 
+void checkBytes(const std::vector<uint8_t> &v1, const std::vector<uint8_t> &v2)
+{
+    assert((v1.size() == v2.size()) && (v1.size() == 2));
+    for(size_t i = 0; i < v1.size(); ++i) {
+        assert(v1[i] == v2[i]);
+    }
+}
+
 void checkOk(const avrouser::RootRecord &rec1, const avrouser::RootRecord &rec2)
 {
     assert(rec1.mylong == rec1.mylong);
@@ -85,6 +93,14 @@
         checkMap(rec1.myunion.getValue<avrouser::Map_of_int>(), rec2.myunion.getValue<avrouser::Map_of_int>());
     }
 
+    assert(rec1.anotherunion.choice == rec2.anotherunion.choice);
+    // in this test I know choice was 0
+    {
+        assert(rec1.anotherunion.choice == 0);
+        typedef std::vector<uint8_t> mytype;
+        checkBytes(rec1.anotherunion.getValue<mytype>(), rec2.anotherunion.getValue<avrouser::Union_of_bytes_null::T0>());
+    }
+
     assert(rec1.mybool == rec2.mybool);
     for(int i = 0; i < static_cast<int>(avrouser::md5::fixedSize); ++i) {
         assert(rec1.myfixed.value[i] == rec2.myfixed.value[i]);
@@ -154,14 +170,21 @@
     myRecord.mymap.value.clear();
     myRecord.myarray.addValue(3434.9);
     myRecord.myarray.addValue(7343.9);
+    myRecord.myarray.addValue(-63445.9);
     myRecord.myenum.value = avrouser::ExampleEnum::one;
     avrouser::Map_of_int map;
     map.addValue("one", 1);
     map.addValue("two", 2);
     myRecord.myunion.set_Map_of_int(map);
+    std::vector<uint8_t> vec;
+    vec.push_back(1);
+    vec.push_back(2);
+    myRecord.anotherunion.set_bytes(vec);
     myRecord.mybool = true;
     memcpy(myRecord.myfixed.value, fixed, avrouser::md5::fixedSize);
     myRecord.anotherint = 4534;
+    myRecord.bytes.push_back(10);
+    myRecord.bytes.push_back(20);
 
     runTests(myRecord);
 

Modified: hadoop/avro/trunk/src/c++/test/unittest.cc
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/test/unittest.cc?rev=805580&r1=805579&r2=805580&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/test/unittest.cc (original)
+++ hadoop/avro/trunk/src/c++/test/unittest.cc Tue Aug 18 21:07:08 2009
@@ -108,10 +108,9 @@
     template<typename Serializer>
     void writeEncoding(Serializer &s, int path)
     {
-
         std::cout << "Record\n";
         s.beginRecord();
-        s.putInt(212);
+        s.putInt(1000);
 
         std::cout << "Map\n";
         s.beginMapBlock(2);
@@ -141,7 +140,7 @@
         s.putFixed(fixeddata, 16);
 
         std::cout << "Int\n";
-        s.putInt(-1);
+        s.putInt(-3456);
     }
 
     void printEncoding() {
@@ -168,6 +167,7 @@
     }
 
     void printNext(Parser<Reader> &p) {
+        // no-op printer
     }
 
     void printNext(Parser<ValidatingReader> &p)
@@ -208,16 +208,18 @@
     void readArray(Parser &p)
     {
         int64_t size = 0;
+        double d = 0.0;
         do {
             printNext(p);
             size = p.getArrayBlockSize();
             std::cout << "Size " << size << '\n';
             for(int32_t i=0; i < size; ++i) {
                 printNext(p);
-                double d = p.getDouble();
+                d = p.getDouble();
                 std::cout << i << ":" << d << '\n';
             }
         } while(size != 0);
+        assert(d = 1000.0);
     }
 
     template <typename Parser>
@@ -241,6 +243,7 @@
         printNext(p);
         int64_t longval = p.getLong();
         std::cout << longval << '\n';
+        assert(longval == 1000);
 
         readMap(p);
         readArray(p);
@@ -257,6 +260,7 @@
         printNext(p);
         bool boolval = p.getBool();
         std::cout << boolval << '\n';
+        assert(boolval == true);
 
         printNext(p);
         readFixed(p);
@@ -264,6 +268,7 @@
         printNext(p);
         int32_t intval = p.getInt();
         std::cout << intval << '\n';
+        assert(intval == -3456);
     }
 
     void readRawData() {