Return-Path: X-Original-To: apmail-avro-commits-archive@www.apache.org Delivered-To: apmail-avro-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0D23110E80 for ; Sun, 15 Dec 2013 16:50:24 +0000 (UTC) Received: (qmail 79502 invoked by uid 500); 15 Dec 2013 16:50:24 -0000 Delivered-To: apmail-avro-commits-archive@avro.apache.org Received: (qmail 79431 invoked by uid 500); 15 Dec 2013 16:50:23 -0000 Mailing-List: contact commits-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list commits@avro.apache.org Received: (qmail 79424 invoked by uid 99); 15 Dec 2013 16:50:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 15 Dec 2013 16:50:23 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 15 Dec 2013 16:50:21 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 9671E2388868; Sun, 15 Dec 2013 16:50:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1551029 - in /avro/trunk: CHANGES.txt lang/c++/api/Generic.hh lang/c++/impl/FileStream.cc lang/c++/impl/parsing/ResolvingDecoder.cc lang/c++/test/DataFileTests.cc Date: Sun, 15 Dec 2013 16:50:01 -0000 To: commits@avro.apache.org From: thiru@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131215165001.9671E2388868@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: thiru Date: Sun Dec 15 16:50:00 2013 New Revision: 1551029 URL: http://svn.apache.org/r1551029 Log: AVRO-1406 Avro C++ GenericRecord (GenericDatum, etc.) doesn't support getters and setters with field name argument Modified: avro/trunk/CHANGES.txt avro/trunk/lang/c++/api/Generic.hh avro/trunk/lang/c++/impl/FileStream.cc avro/trunk/lang/c++/impl/parsing/ResolvingDecoder.cc avro/trunk/lang/c++/test/DataFileTests.cc Modified: avro/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1551029&r1=1551028&r2=1551029&view=diff ============================================================================== --- avro/trunk/CHANGES.txt (original) +++ avro/trunk/CHANGES.txt Sun Dec 15 16:50:00 2013 @@ -44,6 +44,8 @@ Trunk (not yet released) AVRO-1344. Java: Expose sync interval configuration in mapreduce API. (Rob Turner via cutting) + AVRO-1406. C++. GenericRecord (GenericDatum, etc.) doesn't support getters and setters with field name argument. (Iaroslav Zeigerman via thiru) + BUG FIXES AVRO-1368. Fix SpecificDatumWriter to, when writing a string Modified: avro/trunk/lang/c++/api/Generic.hh URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/api/Generic.hh?rev=1551029&r1=1551028&r2=1551029&view=diff ============================================================================== --- avro/trunk/lang/c++/api/Generic.hh (original) +++ avro/trunk/lang/c++/api/Generic.hh Sun Dec 15 16:50:00 2013 @@ -249,6 +249,41 @@ public: } /** + * Returns index of the field with the given name \p name + */ + size_t fieldIndex(const std::string& name) const { + size_t index = 0; + if (!schema()->nameIndex(name, index)) { + throw Exception("Invalid field name: " + name); + } + return index; + } + + /** + * Returns true if a field with the given name \p name is located in this record, + * false otherwise + */ + bool hasField(const std::string& name) const { + size_t index = 0; + return schema()->nameIndex(name, index); + } + + /** + * Returns the field with the given name \p name. + */ + const GenericDatum& field(const std::string& name) const { + return fieldAt(fieldIndex(name)); + } + + /** + * Returns the reference to the field with the given name \p name, + * which can be used to change the contents. + */ + GenericDatum& field(const std::string& name) { + return fieldAt(fieldIndex(name)); + } + + /** * Returns the field at the given position \p pos. */ const GenericDatum& fieldAt(size_t pos) const { @@ -264,6 +299,14 @@ public: } /** + * Replaces the field with the given name \p name with \p v. + */ + void setField(const std::string& name, const GenericDatum& v) { + // assertSameType(v, schema()->leafAt(pos)); + return setFieldAt(fieldIndex(name), v); + } + + /** * Replaces the field at the given position \p pos with \p v. */ void setFieldAt(size_t pos, const GenericDatum& v) { Modified: avro/trunk/lang/c++/impl/FileStream.cc URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/impl/FileStream.cc?rev=1551029&r1=1551028&r2=1551029&view=diff ============================================================================== --- avro/trunk/lang/c++/impl/FileStream.cc (original) +++ avro/trunk/lang/c++/impl/FileStream.cc Sun Dec 15 16:50:00 2013 @@ -16,6 +16,7 @@ * limitations under the License. */ +#include #include "Stream.hh" #ifndef _WIN32 #include "unistd.h" Modified: avro/trunk/lang/c++/impl/parsing/ResolvingDecoder.cc URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/impl/parsing/ResolvingDecoder.cc?rev=1551029&r1=1551028&r2=1551029&view=diff ============================================================================== --- avro/trunk/lang/c++/impl/parsing/ResolvingDecoder.cc (original) +++ avro/trunk/lang/c++/impl/parsing/ResolvingDecoder.cc Sun Dec 15 16:50:00 2013 @@ -60,7 +60,7 @@ using std::make_pair; typedef pair NodePair; class ResolvingGrammarGenerator : public ValidatingGrammarGenerator { - Production doGenerate(const NodePtr& writer, const NodePtr& reader, + Production doGenerate2(const NodePtr& writer, const NodePtr& reader, map > &m, const map > &m2); Production resolveRecords(const NodePtr& writer, const NodePtr& reader, @@ -99,7 +99,7 @@ Symbol ResolvingGrammarGenerator::genera fixup(backup, m2); map > m; - Production main = doGenerate(rr, rw, m, m2); + Production main = doGenerate2(rr, rw, m, m2); fixup(main, m); return Symbol::rootSymbol(main, backup); } @@ -183,7 +183,7 @@ Production ResolvingGrammarGenerator::re find_if(rf.begin(), rf.end(), equalsFirst(it->first)); if (it2 != rf.end()) { - Production p = doGenerate(writer->leafAt(it->second), + Production p = doGenerate2(writer->leafAt(it->second), reader->leafAt(it2->second), m, m2); copy(p.rbegin(), p.rend(), back_inserter(result)); fieldOrder.push_back(it2->second); @@ -219,7 +219,7 @@ Production ResolvingGrammarGenerator::re size_t c = writer->leaves(); v.reserve(c); for (size_t i = 0; i < c; ++i) { - Production p = doGenerate(writer->leafAt(i), reader, m, m2); + Production p = doGenerate2(writer->leafAt(i), reader, m, m2); v.push_back(p); } Symbol r[] = { @@ -229,7 +229,7 @@ Production ResolvingGrammarGenerator::re return Production(r, r + 2); } -Production ResolvingGrammarGenerator::doGenerate( +Production ResolvingGrammarGenerator::doGenerate2( const NodePtr& writer, const NodePtr& reader, map > &m, const map > &m2) @@ -298,14 +298,14 @@ Production ResolvingGrammarGenerator::do Symbol r[] = { Symbol::arrayEndSymbol(), Symbol::repeater( - doGenerate(writer->leafAt(0), reader->leafAt(0), m, m2), + doGenerate2(writer->leafAt(0), reader->leafAt(0), m, m2), p, true), Symbol::arrayStartSymbol() }; return Production(r, r + 3); } case AVRO_MAP: { - Production v = doGenerate(writer->leafAt(1), + Production v = doGenerate2(writer->leafAt(1), reader->leafAt(1), m, m2); v.push_back(Symbol::stringSymbol()); @@ -369,7 +369,7 @@ Production ResolvingGrammarGenerator::do { int j = bestBranch(writer, reader); if (j >= 0) { - Production p = doGenerate(writer, reader->leafAt(j), m, m2); + Production p = doGenerate2(writer, reader->leafAt(j), m, m2); Symbol r[] = { Symbol::unionAdjustSymbol(j, p), Symbol::unionSymbol() Modified: avro/trunk/lang/c++/test/DataFileTests.cc URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/test/DataFileTests.cc?rev=1551029&r1=1551028&r2=1551029&view=diff ============================================================================== --- avro/trunk/lang/c++/test/DataFileTests.cc (original) +++ avro/trunk/lang/c++/test/DataFileTests.cc Sun Dec 15 16:50:00 2013 @@ -177,6 +177,24 @@ public: df.close(); } + void testWriteGenericByName() { + avro::DataFileWriter df(filename, writerSchema, 100); + int64_t re = 3; + int64_t im = 5; + Pair p(writerSchema, GenericDatum()); + + GenericDatum& c = p.second; + c = GenericDatum(writerSchema.root()); + GenericRecord& r = c.value(); + + for (int i = 0; i < count; ++i, re *= im, im += 3) { + r.field("re") = re; + r.field("im") = im; + df.write(p); + } + df.close(); + } + void testWriteDouble() { avro::DataFileWriter df(filename, writerSchema, 100); double re = 3.0; @@ -257,6 +275,33 @@ public: BOOST_CHECK_EQUAL(i, count); } + void testReaderGenericByName() { + avro::DataFileReader df(filename, writerSchema); + int i = 0; + Pair p(writerSchema, GenericDatum()); + int64_t re = 3; + int64_t im = 5; + + const GenericDatum& ci = p.second; + while (df.read(p)) { + BOOST_REQUIRE_EQUAL(ci.type(), avro::AVRO_RECORD); + const GenericRecord& r = ci.value(); + const size_t n = 2; + BOOST_REQUIRE_EQUAL(r.fieldCount(), n); + const GenericDatum& f0 = r.field("re"); + BOOST_REQUIRE_EQUAL(f0.type(), avro::AVRO_LONG); + BOOST_CHECK_EQUAL(f0.value(), re); + + const GenericDatum& f1 = r.field("im"); + BOOST_REQUIRE_EQUAL(f1.type(), avro::AVRO_LONG); + BOOST_CHECK_EQUAL(f1.value(), im); + re *= im; + im += 3; + ++i; + } + BOOST_CHECK_EQUAL(i, count); + } + void testReaderGenericProjection() { avro::DataFileReader df(filename, readerSchema); int i = 0; @@ -350,6 +395,7 @@ void addReaderTests(test_suite* ts, cons ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testReadFull, t)); ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testReadProjection, t)); ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testReaderGeneric, t)); + ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testReaderGenericByName, t)); ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testReaderGenericProjection, t)); ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t)); @@ -380,5 +426,9 @@ init_unit_test_suite( int argc, char* ar ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testTruncate, t4)); ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t4)); + shared_ptr t5(new DataFileTest("test5.df", sch, isch)); + ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testWriteGenericByName, t5)); + addReaderTests(ts, t5); + return ts; }