Browse Source

HADOOP-4046. Made WritableComparable's constructor protected instead of
private to re-enable class derivation. (cdouglas via omalley)


git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@691306 13f79535-47bb-0310-9956-ffa450edef68

Owen O'Malley 17 years ago
parent
commit
894c1dc174

+ 3 - 0
CHANGES.txt

@@ -457,6 +457,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<? extends WritableComparable> keyClass,
+  protected WritableComparator(Class<? extends WritableComparable> keyClass,
       boolean createInstances) {
       boolean createInstances) {
     this.keyClass = keyClass;
     this.keyClass = keyClass;
     if (createInstances) {
     if (createInstances) {

+ 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;
+    }
+  }
+
 }
 }