HADOOP-7151. Document need for stable hashCode() in WritableComparable. Contributed by Dmitriy V. Ryaboy.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1074241 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2011-02-24 18:05:46 +00:00
parent 27df75e6bb
commit 4d53649f49
2 changed files with 22 additions and 7 deletions

View File

@ -57,6 +57,9 @@ Trunk (unreleased changes)
HADOOP-6376. Add a comment header to conf/slaves that specifies the file
format. (Kay Kay via todd)
HADOOP-7151. Document need for stable hashCode() in WritableComparable.
(Dmitriy V. Ryaboy via todd)
OPTIMIZATIONS
BUG FIXES

View File

@ -28,12 +28,16 @@ import org.apache.hadoop.classification.InterfaceStability;
* via <code>Comparator</code>s. Any type which is to be used as a
* <code>key</code> in the Hadoop Map-Reduce framework should implement this
* interface.</p>
*
* <p>Note that <code>hashCode()</code> is frequently used in Hadoop to partition
* keys. It's important that your implementation of hashCode() returns the same
* result across different instances of the JVM. Note also that the default
* <code>hashCode()</code> implementation in <code>Object</code> does <b>not</b>
* satisfy this property.</p>
*
* <p>Example:</p>
* <p><blockquote><pre>
* public class MyWritableComparable implements
* WritableComparable&lt;MyWritableComparable&gt; {
*
* public class MyWritableComparable implements WritableComparable {
* // Some data
* private int counter;
* private long timestamp;
@ -48,10 +52,18 @@ import org.apache.hadoop.classification.InterfaceStability;
* timestamp = in.readLong();
* }
*
* public int compareTo(MyWritableComparable other) {
* int thisValue = this.counter;
* int thatValue = other.counter;
* return (thisValue &lt; thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
* public int compareTo(MyWritableComparable w) {
* int thisValue = this.value;
* int thatValue = ((IntWritable)o).value;
* return (thisValue &lt; thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
* }
*
* public int hashCode() {
* final int prime = 31;
* int result = 1;
* result = prime * result + counter;
* result = prime * result + (int) (timestamp ^ (timestamp &gt;&gt;&gt; 32));
* return result
* }
* }
* </pre></blockquote></p>