avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r989263 - in /avro/trunk: ./ lang/java/src/java/org/apache/avro/generic/ lang/java/src/java/org/apache/avro/reflect/ lang/java/src/test/java/org/apache/avro/
Date Wed, 25 Aug 2010 17:51:13 GMT
Author: cutting
Date: Wed Aug 25 17:51:13 2010
New Revision: 989263

URL: http://svn.apache.org/viewvc?rev=989263&view=rev
Log:
AVRO-598. Use generic data structures when reading with reflect API and classes are not defined.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java
    avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumReader.java
    avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=989263&r1=989262&r2=989263&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Aug 25 17:51:13 2010
@@ -136,6 +136,9 @@ Avro 1.4.0 (unreleased)
 
     AVRO-617. Java: Detect erroneous default field values. (cutting)
 
+    AVRO-598. Java: Use generic data structures when reading with
+    reflect API and classes are not defined. (cutting)
+
   BUG FIXES
 
     AVRO-622. python avro.ipc doesn't work with python2.4 (philz)

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java?rev=989263&r1=989262&r2=989263&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java Wed Aug 25 17:51:13
2010
@@ -479,6 +479,8 @@ public class GenericData {
     if (o1 == o2) return 0;
     switch (s.getType()) {
     case RECORD:
+      if (!(o1 instanceof IndexedRecord))
+        return ((Comparable)o1).compareTo(o2);
       IndexedRecord r1 = (IndexedRecord)o1;
       IndexedRecord r2 = (IndexedRecord)o2;
       for (Field f : s.getFields()) {

Modified: avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java?rev=989263&r1=989262&r2=989263&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java Wed Aug 25 17:51:13
2010
@@ -38,6 +38,7 @@ import org.apache.avro.AvroTypeException
 import org.apache.avro.Protocol;
 import org.apache.avro.Schema;
 import org.apache.avro.Protocol.Message;
+import org.apache.avro.generic.IndexedRecord;
 import org.apache.avro.generic.GenericFixed;
 import org.apache.avro.specific.SpecificData;
 import org.apache.avro.specific.FixedSize;
@@ -264,6 +265,8 @@ public class ReflectData extends Specifi
         } else if (GenericFixed.class.isAssignableFrom(c)) { // fixed
           int size = c.getAnnotation(FixedSize.class).value();
           schema = Schema.createFixed(name, null /* doc */, space, size);
+        } else if (IndexedRecord.class.isAssignableFrom(c)) { // specific
+          return super.createSchema(type, names);
         } else {                                             // record
           List<Schema.Field> fields = new ArrayList<Schema.Field>();
           boolean error = Throwable.class.isAssignableFrom(c);

Modified: avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumReader.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumReader.java?rev=989263&r1=989262&r2=989263&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumReader.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumReader.java Wed Aug
25 17:51:13 2010
@@ -26,6 +26,7 @@ import java.nio.ByteBuffer;
 
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
 import org.apache.avro.specific.SpecificDatumReader;
 import org.apache.avro.io.Decoder;
 
@@ -46,7 +47,11 @@ public class ReflectDatumReader<T> exten
 
   @Override
   protected void setField(Object record, String name, int position, Object o) {
-    try {
+    if (record instanceof IndexedRecord) {
+      super.setField(record, name, position, o);
+      return;
+    }
+      try {
       ReflectData.getField(record.getClass(), name).set(record, o);
     } catch (IllegalAccessException e) {
       throw new AvroRuntimeException(e);
@@ -55,6 +60,8 @@ public class ReflectDatumReader<T> exten
 
   @Override
   protected Object getField(Object record, String name, int position) {
+    if (record instanceof IndexedRecord)
+      return super.getField(record, name, position);
     try {
       return ReflectData.getField(record.getClass(), name).get(record);
     } catch (IllegalAccessException e) {

Modified: avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumWriter.java?rev=989263&r1=989262&r2=989263&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumWriter.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectDatumWriter.java Wed Aug
25 17:51:13 2010
@@ -24,6 +24,7 @@ import java.util.Collection;
 
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
 import org.apache.avro.specific.SpecificDatumWriter;
 import org.apache.avro.io.Encoder;
 
@@ -58,6 +59,8 @@ public class ReflectDatumWriter<T> exten
   
   @Override
   protected Object getField(Object record, String name, int position) {
+    if (record instanceof IndexedRecord)
+      return super.getField(record, name, position);
     try {
       return ReflectData.getField(record.getClass(), name).get(record);
     } catch (IllegalAccessException e) {

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java?rev=989263&r1=989262&r2=989263&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java Wed Aug 25 17:51:13
2010
@@ -37,6 +37,7 @@ import org.apache.avro.TestReflect.Sampl
 import org.apache.avro.io.DecoderFactory;
 import org.apache.avro.io.BinaryEncoder;
 import org.apache.avro.io.Decoder;
+import org.apache.avro.generic.GenericData;
 import org.apache.avro.reflect.ReflectData;
 import org.apache.avro.reflect.ReflectDatumReader;
 import org.apache.avro.reflect.ReflectDatumWriter;
@@ -522,4 +523,32 @@ public class TestReflect {
     ReflectData.get().getProtocol(Class.forName("NoPackage"));
   }
 
+  public static class Y implements Comparable {
+    int i;
+    @Override public boolean equals(Object o) { return ((Y)o).i == this.i; }
+    @Override public int compareTo(Object o) { return ((Y)o).i - this.i; }
+  }
+
+  @Test
+  /** Test nesting of reflect data within generic. */
+  public void testReflectWithinGeneric() throws Exception {
+    ReflectData data = ReflectData.get();
+    // define a record with a field that's a specific Y
+    Schema schema = Schema.createRecord("Foo", "", "x.y.z", false);
+    List<Schema.Field> fields = new ArrayList<Schema.Field>();
+    fields.add(new Schema.Field("f", data.getSchema(Y.class), "", null));
+    schema.setFields(fields);
+
+    // create a generic instance of this record
+    Y y = new Y();
+    y.i = 1;
+    GenericData.Record record = new GenericData.Record(schema);
+    record.put("f", y);
+
+    // test that this instance can be written & re-read
+    TestSchema.checkBinary(schema, record,
+                           new ReflectDatumWriter<Object>(),
+                           new ReflectDatumReader<Object>());
+  }
+
 }



Mime
View raw message