Browse Source

HADOOP-4046. Merge -r 691305:691306 from trunk to branch 0.18

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.18@691308 13f79535-47bb-0310-9956-ffa450edef68
Owen O'Malley 17 years ago
parent
commit
89da6a8d6c

+ 3 - 0
CHANGES.txt

@@ -14,6 +14,9 @@ Release 0.18.1 - Unreleased
     from the TaskTracker, which was causing HDFS client connections to not be 
     from the TaskTracker, which was causing HDFS client connections to not be 
     collected. (ddas via omalley)
     collected. (ddas via omalley)
 
 
+    HADOOP-4046. Made WritableComparable's constructor protected instead of 
+    private to re-enable class derivation. (cdouglas via omalley)
+
 Release 0.18.0 - 2008-08-19
 Release 0.18.0 - 2008-08-19
 
 
   INCOMPATIBLE CHANGES
   INCOMPATIBLE CHANGES

+ 1 - 1
src/core/org/apache/hadoop/io/WritableComparator.java

@@ -63,7 +63,7 @@ public class WritableComparator implements RawComparator {
     this(keyClass, false);
     this(keyClass, false);
   }
   }
 
 
-  private WritableComparator(Class keyClass, boolean createInstances) {
+  protected WritableComparator(Class keyClass, boolean createInstances) {
     this.keyClass = keyClass;
     this.keyClass = keyClass;
     if (createInstances) {
     if (createInstances) {
       key1 = newKey();
       key1 = newKey();

+ 44 - 1
src/test/org/apache/hadoop/mapred/TestComparators.java

@@ -420,5 +420,48 @@ public class TestComparators extends TestCase
       fail("Oops! The job broke due to an unexpected error");
       fail("Oops! The job broke due to an unexpected error");
     }
     }
   }
   }
-  
+
+  /**
+   * Test a user comparator that relies on deserializing both arguments
+   * for each compare.
+   */
+  public void testBakedUserComparator() throws Exception {
+    MyWritable a = new MyWritable(8, 8);
+    MyWritable b = new MyWritable(7, 9);
+    assertTrue(a.compareTo(b) > 0);
+    assertTrue(WritableComparator.get(MyWritable.class).compare(a, b) < 0);
+  }
+
+  public static class MyWritable implements WritableComparable<MyWritable> {
+    int i, j;
+    public MyWritable() { }
+    public MyWritable(int i, int j) {
+      this.i = i;
+      this.j = j;
+    }
+    public void readFields(DataInput in) throws IOException {
+      i = in.readInt();
+      j = in.readInt();
+    }
+    public void write(DataOutput out) throws IOException {
+      out.writeInt(i);
+      out.writeInt(j);
+    }
+    public int compareTo(MyWritable b) {
+      return this.i - b.i;
+    }
+    static {
+      WritableComparator.define(MyWritable.class, new MyCmp());
+    }
+  }
+
+  public static class MyCmp extends WritableComparator {
+    public MyCmp() { super(MyWritable.class, true); }
+    public int compare(WritableComparable a, WritableComparable b) {
+      MyWritable aa = (MyWritable)a;
+      MyWritable bb = (MyWritable)b;
+      return aa.j - bb.j;
+    }
+  }
+
 }
 }