avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1086727 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/generic/ lang/java/avro/src/main/java/org/apache/avro/reflect/ lang/java/avro/src/main/java/org/apache/avro/specific/ lang/java/ipc/src/test/java/org/apache/avro/
Date Tue, 29 Mar 2011 20:34:13 GMT
Author: cutting
Date: Tue Mar 29 20:34:13 2011
New Revision: 1086727

URL: http://svn.apache.org/viewvc?rev=1086727&view=rev
Log:
AVRO-786. Java: Fix equals() to work on objects containing maps.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1086727&r1=1086726&r2=1086727&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Mar 29 20:34:13 2011
@@ -14,6 +14,8 @@ Avro 1.5.1 (unreleased)
 
   BUG FIXES
 
+    AVRO-786. Java: Fix equals() to work on objects containing maps. (cutting)
+
 Avro 1.5.0 (10 March 2011)
 
   INCOMPATIBLE CHANGES

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?rev=1086727&r1=1086726&r2=1086727&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java Tue Mar
29 20:34:13 2011
@@ -70,7 +70,7 @@ public class GenericData {
       Record that = (Record)o;
       if (!schema.getFullName().equals(that.schema.getFullName()))
         return false;                             // not the same schema
-      return this.compareTo(that) == 0;
+      return GenericData.get().compare(this, that, schema, true) == 0;
     }
     @Override public int hashCode() {
       return GenericData.get().hashCode(this, schema);
@@ -609,8 +609,14 @@ public class GenericData {
    * greater-than, return 1, if less than return -1.  Order is consistent with
    * that of {@link BinaryData#compare(byte[], int, byte[], int, Schema)}.
    */
-  @SuppressWarnings(value="unchecked")
   public int compare(Object o1, Object o2, Schema s) {
+    return compare(o1, o2, s, false);
+  }
+
+  /** Comparison implementation.  When equals is true, only checks for equality,
+   * not for order. */
+  @SuppressWarnings(value="unchecked")
+  protected int compare(Object o1, Object o2, Schema s, boolean equals) {
     if (o1 == o2) return 0;
     switch (s.getType()) {
     case RECORD:
@@ -620,7 +626,8 @@ public class GenericData {
         int pos = f.pos();
         String name = f.name();
         int compare =
-          compare(getField(o1, name, pos), getField(o2, name, pos), f.schema());
+          compare(getField(o1, name, pos), getField(o2, name, pos),
+                  f.schema(), equals);
         if (compare != 0)                         // not equal
           return f.order() == Field.Order.DESCENDING ? -compare : compare;
       }
@@ -634,17 +641,19 @@ public class GenericData {
       Iterator e2 = a2.iterator();
       Schema elementType = s.getElementType();
       while(e1.hasNext() && e2.hasNext()) {
-        int compare = compare(e1.next(), e2.next(), elementType);
+        int compare = compare(e1.next(), e2.next(), elementType, equals);
         if (compare != 0) return compare;
       }
       return e1.hasNext() ? 1 : (e2.hasNext() ? -1 : 0);
     case MAP:
+      if (equals)
+        return ((Map)o1).equals(o2) ? 0 : 1;
       throw new AvroRuntimeException("Can't compare maps!");
     case UNION:
       int i1 = resolveUnion(s, o1);
       int i2 = resolveUnion(s, o2);
       return (i1 == i2)
-        ? compare(o1, o2, s.getTypes().get(i1))
+        ? compare(o1, o2, s.getTypes().get(i1), equals)
         : i1 - i2;
     case NULL:
       return 0;

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java?rev=1086727&r1=1086726&r2=1086727&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java Tue Mar
29 20:34:13 2011
@@ -449,7 +449,7 @@ public class ReflectData extends Specifi
   }
 
   @Override
-  public int compare(Object o1, Object o2, Schema s) {
+  protected int compare(Object o1, Object o2, Schema s, boolean equals) {
     switch (s.getType()) {
     case ARRAY:
       if (!o1.getClass().isArray())
@@ -461,7 +461,7 @@ public class ReflectData extends Specifi
       for (int i = 0; i < l; i++) {
         int compare = compare(java.lang.reflect.Array.get(o1, i),
                               java.lang.reflect.Array.get(o2, i),
-                              elementType);
+                              elementType, equals);
         if (compare != 0) return compare;
       }
       return l1 - l2;
@@ -472,7 +472,7 @@ public class ReflectData extends Specifi
       byte[] b2 = (byte[])o2; 
       return BinaryData.compareBytes(b1, 0, b1.length, b2, 0, b2.length);
     }
-    return super.compare(o1, o2, s);
+    return super.compare(o1, o2, s, equals);
   }
 
 }

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java?rev=1086727&r1=1086726&r2=1086727&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java Tue
Mar 29 20:34:13 2011
@@ -189,13 +189,13 @@ public class SpecificData extends Generi
   }
 
   @Override
-  public int compare(Object o1, Object o2, Schema s) {
+  protected int compare(Object o1, Object o2, Schema s, boolean eq) {
     switch (s.getType()) {
     case ENUM:
       if (o1 instanceof Enum)
         return ((Enum)o1).ordinal() - ((Enum)o2).ordinal();
     default:
-      return super.compare(o1, o2, s);
+      return super.compare(o1, o2, s, eq);
     }
   }
 

Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java?rev=1086727&r1=1086726&r2=1086727&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java (original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java Tue Mar 29 20:34:13
2011
@@ -200,6 +200,20 @@ public class TestSchema {
   }
 
   @Test
+  public void testMapInRecord() throws Exception {
+    String json = "{\"type\":\"record\", \"name\":\"Test\", \"fields\":"
+      +"[{\"name\":\"f\", \"type\": {\"type\":\"map\", \"values\":\"long\"}}]}";
+    Schema schema = Schema.parse(json);
+
+    HashMap<Utf8,Long> map = new HashMap<Utf8,Long>();
+    map.put(new Utf8("a"), 1L);
+    GenericData.Record record = new GenericData.Record(schema);
+    record.put("f", map);
+    check(json, "{\"f\":{\"a\":1}}", record, false);
+  }
+
+
+  @Test
   public void testEnum() throws Exception {
     check(BASIC_ENUM_SCHEMA, "\"B\"",
           new GenericData.EnumSymbol(Schema.parse(BASIC_ENUM_SCHEMA), "B"),



Mime
View raw message