avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sbana...@apache.org
Subject svn commit: r835480 - in /hadoop/avro/trunk: ./ src/c++/api/ src/c++/impl/ src/c++/scripts/ src/c++/test/
Date Thu, 12 Nov 2009 18:16:44 GMT
Author: sbanacho
Date: Thu Nov 12 18:16:44 2009
New Revision: 835480

URL: http://svn.apache.org/viewvc?rev=835480&view=rev
Log:
AVRO-190. Use fixed size C++ types for Avro fixed types.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/c++/api/Parser.hh
    hadoop/avro/trunk/src/c++/api/Reader.hh
    hadoop/avro/trunk/src/c++/api/Serializer.hh
    hadoop/avro/trunk/src/c++/api/ValidSchema.hh
    hadoop/avro/trunk/src/c++/api/ValidatingReader.hh
    hadoop/avro/trunk/src/c++/api/ValidatingWriter.hh
    hadoop/avro/trunk/src/c++/api/Writer.hh
    hadoop/avro/trunk/src/c++/impl/ValidSchema.cc
    hadoop/avro/trunk/src/c++/scripts/gen-cppcode.py
    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=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Thu Nov 12 18:16:44 2009
@@ -16,6 +16,8 @@
 
   IMPROVEMENTS
 
+    AVRO-190. Use fixed size C++ types for Avro fixed types. (sbanacho)
+
     AVRO-180. Enhance code generator script and unit tests. (sbanacho)
 
     AVRO-157. Changes from code review comments for C++. (sbanacho)

Modified: hadoop/avro/trunk/src/c++/api/Parser.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/Parser.hh?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/Parser.hh (original)
+++ hadoop/avro/trunk/src/c++/api/Parser.hh Thu Nov 12 18:16:44 2009
@@ -88,12 +88,14 @@
         reader_.readBytes(val);
     }
 
-    void readFixed(std::vector<uint8_t> &val, size_t size) {
-        reader_.readFixed(val, size);
+    template <size_t N>
+    void readFixed(uint8_t (&val)[N]) {
+        reader_.readFixed(val);
     }
 
-    void readFixed(uint8_t *val, size_t size) {
-        reader_.readFixed(val, size);
+    template<size_t N>
+    void readFixed(boost::array<uint8_t, N> &val) {
+        reader_.readFixed(val);
     }
 
     void readRecord() { 

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=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/Reader.hh (original)
+++ hadoop/avro/trunk/src/c++/api/Reader.hh Thu Nov 12 18:16:44 2009
@@ -100,21 +100,17 @@
         }
     }
 
-
-    void readFixed(std::vector<uint8_t> &val, size_t size) {
-        val.reserve(size);
-        uint8_t bval;
-        for(size_t bytes = 0; bytes < size; bytes++) {
-            in_.readByte(bval);
-            val.push_back(bval);
+    template <size_t N>
+    void readFixed(uint8_t (&val)[N]) {
+        for(size_t bytes = 0; bytes < N; bytes++) {
+            in_.readByte(val[bytes]);
         }
     }
 
-    void readFixed(uint8_t *val, size_t size) {
-        uint8_t bval;
-        for(size_t bytes = 0; bytes < size; bytes++) {
-            in_.readByte(bval);
-            *val++ = bval;
+    template <size_t N>
+    void readFixed(boost::array<uint8_t, N> &val) {
+        for(size_t bytes = 0; bytes < val.size(); bytes++) {
+            in_.readByte(val[bytes]);
         }
     }
 

Modified: hadoop/avro/trunk/src/c++/api/Serializer.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/Serializer.hh?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/Serializer.hh (original)
+++ hadoop/avro/trunk/src/c++/api/Serializer.hh Thu Nov 12 18:16:44 2009
@@ -73,8 +73,14 @@
         writer_.writeBytes(val);
     }
 
-    void writeFixed(const uint8_t *val, size_t size) {
-        writer_.writeFixed(val, size);
+    template <size_t N>
+    void writeFixed(const uint8_t (&val)[N]) {
+        writer_.writeFixed(val);
+    }
+
+    template <size_t N>
+    void writeFixed(const boost::array<uint8_t, N> &val) {
+        writer_.writeFixed(val);
     }
 
     void writeString(const std::string &val) {

Modified: hadoop/avro/trunk/src/c++/api/ValidSchema.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/ValidSchema.hh?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/ValidSchema.hh (original)
+++ hadoop/avro/trunk/src/c++/api/ValidSchema.hh Thu Nov 12 18:16:44 2009
@@ -49,11 +49,7 @@
     void setSchema(const Schema &schema);
 
     const NodePtr &root() const {
-        return node_;
-    }
-
-    const Type rootType() const {
-        return node_->type();
+        return root_;
     }
 
     void toJson(std::ostream &os) const;
@@ -69,7 +65,7 @@
     bool validate(const NodePtr &node);
 
     SymbolMap symbolMap_;
-    NodePtr node_;
+    NodePtr root_;
 };
 
 } // namespace avro

Modified: hadoop/avro/trunk/src/c++/api/ValidatingReader.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/ValidatingReader.hh?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/ValidatingReader.hh (original)
+++ hadoop/avro/trunk/src/c++/api/ValidatingReader.hh Thu Nov 12 18:16:44 2009
@@ -62,21 +62,22 @@
         reader_.readBytes(val);
     }
 
-    void readFixed(uint8_t *val, size_t size) {
+    template <size_t N>
+    void readFixed(uint8_t (&val)[N]) {
         checkSafeToGet(AVRO_FIXED);
-        checkSizeExpected(size);
+        checkSizeExpected(N);
         validator_.advance();
-        reader_.readFixed(val, size);
+        reader_.readFixed(val);
     }
 
-    void readFixed(std::vector<uint8_t> &val, size_t size) {
+    template<size_t N>
+    void readFixed(boost::array<uint8_t, N> &val) {
         checkSafeToGet(AVRO_FIXED);
-        checkSizeExpected(size);
+        checkSizeExpected(val.size());
         validator_.advance();
-        reader_.readFixed(val, size);
+        reader_.readFixed(val);
     }
 
-
     void readRecord();
 
     int64_t readArrayBlockSize();

Modified: hadoop/avro/trunk/src/c++/api/ValidatingWriter.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/ValidatingWriter.hh?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/ValidatingWriter.hh (original)
+++ hadoop/avro/trunk/src/c++/api/ValidatingWriter.hh Thu Nov 12 18:16:44 2009
@@ -56,10 +56,20 @@
 
     void writeBytes(const uint8_t *val, size_t size);
 
-    void writeFixed(const uint8_t *val, size_t size) {
+
+    template <size_t N>
+    void writeFixed(const uint8_t (&val)[N]) {
+        checkSafeToPut(AVRO_FIXED);
+        checkSizeExpected(N);
+        writer_.writeFixed(val);
+        validator_.advance();
+    }
+
+    template <size_t N>
+    void writeFixed(const boost::array<uint8_t, N> &val) {
         checkSafeToPut(AVRO_FIXED);
-        checkSizeExpected(size);
-        writer_.writeFixed(val, size);
+        checkSizeExpected(val.size());
+        writer_.writeFixed(val);
         validator_.advance();
     }
 

Modified: hadoop/avro/trunk/src/c++/api/Writer.hh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/api/Writer.hh?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/api/Writer.hh (original)
+++ hadoop/avro/trunk/src/c++/api/Writer.hh Thu Nov 12 18:16:44 2009
@@ -86,8 +86,14 @@
         out_.writeBytes(val, size);
     }
 
-    void writeFixed(const uint8_t *val, size_t size) {
-        out_.writeBytes(val, size);
+    template <size_t N>
+    void writeFixed(const uint8_t (&val)[N]) {
+        out_.writeBytes(val, N);
+    }
+
+    template <size_t N>
+    void writeFixed(const boost::array<uint8_t, N> &val) {
+        out_.writeBytes(val.data(), val.size());
     }
 
     void writeRecord() {}

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=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/impl/ValidSchema.cc (original)
+++ hadoop/avro/trunk/src/c++/impl/ValidSchema.cc Thu Nov 12 18:16:44 2009
@@ -25,13 +25,13 @@
 namespace avro {
 
     ValidSchema::ValidSchema(const Schema &schema) :
-    node_(schema.root())
+    root_(schema.root())
 {
-    validate(node_);
+    validate(root_);
 }
 
 ValidSchema::ValidSchema() :
-   node_(NullSchema().root()) 
+   root_(NullSchema().root()) 
 { }
 
 void
@@ -39,14 +39,14 @@
 {
     const NodePtr &node(schema.root());
     validate(schema.root());
-    node_ = node;
+    root_ = node;
 }
 
 bool
 ValidSchema::validate(const NodePtr &node) 
 {
     if(!node) {
-        node_.reset(new NodePrimitive(AVRO_NULL));
+        root_.reset(new NodePrimitive(AVRO_NULL));
     }
 
     if(!node->isValid()) {
@@ -80,14 +80,14 @@
 void 
 ValidSchema::toJson(std::ostream &os) const
 { 
-    node_->printJson(os, 0);
+    root_->printJson(os, 0);
     os << '\n';
 }
 
 void 
 ValidSchema::toFlatList(std::ostream &os) const
 { 
-    node_->printBasicInfo(os);
+    root_->printBasicInfo(os);
 }
 
 } // namespace avro

Modified: hadoop/avro/trunk/src/c++/scripts/gen-cppcode.py
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c%2B%2B/scripts/gen-cppcode.py?rev=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/scripts/gen-cppcode.py (original)
+++ hadoop/avro/trunk/src/c++/scripts/gen-cppcode.py Thu Nov 12 18:16:44 2009
@@ -56,6 +56,11 @@
 
 recordfieldTemplate = '$type$ $name$\n'
 recordTemplate = '''struct $name$ {
+
+    $name$ () :
+$initializers$
+    { }
+
 $recordfields$};
 
 template <typename Serializer>
@@ -78,17 +83,22 @@
     fields = ''
     serializefields = ''
     parsefields = ''
+    initlist = ''
     end = False
     while not end:
         line = getNextLine()
-        if line[0] == 'end': end = True
+        if line[0] == 'end': 
+            end = True
+            initlist = initlist.rstrip(',\n')
         elif line[0] == 'name':
             fieldname = line[1]
             fieldline = getNextLine()
             fieldtypename, fieldtype = processType(fieldline)
             fields += '    ' +  fieldtypename + ' ' + fieldname + ';\n'
             serializefields += '    serialize(s, val.' + fieldname + ');\n'
+            initlist += '        ' + fieldname + '(),\n'
             parsefields += '    parse(p, val.' + fieldname + ');\n'
+    structDef = structDef.replace('$initializers$', initlist)
     structDef = structDef.replace('$recordfields$', fields)
     structDef = structDef.replace('$serializefields$', serializefields)
     structDef = structDef.replace('$parsefields$', parsefields)
@@ -100,7 +110,10 @@
 
 $typedeflist$
 
-    $name$() : choice(0), value(T0()) {}
+    $name$() : 
+        choice(0), 
+        value(T0()) 
+    { }
 
 $setfuncs$
 #ifdef AVRO_BOOST_NO_ANYREF
@@ -188,9 +201,15 @@
     return (typename,typename)
 
 enumTemplate = '''struct $name$ {
+
     enum EnumSymbols {
         $enumsymbols$
     };
+
+    $name$() : 
+        value($firstsymbol$) 
+    { }
+
     EnumSymbols value;
 };
 
@@ -211,14 +230,19 @@
     structDef = structDef.replace('$name$', typename)
     end = False
     symbols = '';
+    firstsymbol = '';
     while not end:
         line = getNextLine()
         if line[0] == 'end': end = True
         elif line[0] == 'name':
-            if not symbols=='' : symbols += ', '
+            if symbols== '' :
+                firstsymbol = line[1]
+            else :
+                symbols += ', '
             symbols += line[1]
         else: print "error"
     structDef = structDef.replace('$enumsymbols$', symbols);
+    structDef = structDef.replace('$firstsymbol$', firstsymbol);
     addStruct(typename, structDef)
     return (typename,typename)
 
@@ -226,6 +250,10 @@
     typedef $valuetype$ ValueType;
     typedef std::vector<ValueType> ArrayType;
     
+    $name$() :
+        value()
+    { }
+
     void addValue(const ValueType &val) {
         value.push_back(val);
     }
@@ -283,6 +311,10 @@
     typedef $valuetype$ ValueType;
     typedef std::map<std::string, ValueType> MapType;
     
+    $name$() :
+        value()
+    { }
+
     void addValue(const std::string &key, const ValueType &val) {
         value.insert(MapType::value_type(key, val));
     }
@@ -345,17 +377,22 @@
     enum {
         fixedSize = $N$
     };
+
+    $name$() {
+        memset(value, 0, sizeof(value));
+    }
+    
     uint8_t value[fixedSize];
 };
 
 template <typename Serializer>
 inline void serialize(Serializer &s, const $name$ &val, const boost::true_type &)
{
-    s.writeFixed(val.value, $name$::fixedSize);
+    s.writeFixed(val.value);
 }
 
 template <typename Parser>
 inline void parse(Parser &p, $name$ &val, const boost::true_type &) {
-    p.readFixed(val.value, $name$::fixedSize);
+    p.readFixed(val.value);
 }
 '''
 
@@ -436,6 +473,7 @@
         print "%s\n" % x
 
     for x in structList:
+        print "/*----------------------------------------------------------------------------------*/\n"
         print "%s\n" % x
 
     print "\n} // namespace %s\n" % namespace

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=835480&r1=835479&r2=835480&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c++/test/unittest.cc (original)
+++ hadoop/avro/trunk/src/c++/test/unittest.cc Thu Nov 12 18:16:44 2009
@@ -147,7 +147,7 @@
 
         std::cout << "Fixed16\n";
         
-        s.writeFixed(fixeddata, 16);
+        s.writeFixed(fixeddata);
 
         std::cout << "Int\n";
         s.writeInt(-3456);
@@ -246,8 +246,8 @@
     template <typename Parser>
     void readFixed(Parser &p) {
 
-        std::vector<uint8_t> input;
-        p.readFixed(input, 16);
+        boost::array<uint8_t, 16> input;
+        p.readFixed<16>(input);
         BOOST_CHECK_EQUAL(input.size(), 16U);
 
         for(int i=0; i< 16; ++i) {



Mime
View raw message