2011-02-27 22:15:52 -05:00
|
|
|
package spark
|
|
|
|
|
|
|
|
@serializable
|
2011-03-07 02:38:16 -05:00
|
|
|
abstract class Partitioner {
|
2011-02-27 22:15:52 -05:00
|
|
|
def numPartitions: Int
|
2011-03-07 02:38:16 -05:00
|
|
|
def getPartition(key: Any): Int
|
2011-02-27 22:15:52 -05:00
|
|
|
}
|
|
|
|
|
2011-03-07 02:38:16 -05:00
|
|
|
class HashPartitioner(partitions: Int) extends Partitioner {
|
2011-02-27 22:15:52 -05:00
|
|
|
def numPartitions = partitions
|
|
|
|
|
2011-03-07 02:38:16 -05:00
|
|
|
def getPartition(key: Any) = {
|
2011-02-27 22:15:52 -05:00
|
|
|
val mod = key.hashCode % partitions
|
|
|
|
if (mod < 0) mod + partitions else mod // Guard against negative hash codes
|
|
|
|
}
|
|
|
|
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
2011-03-07 02:38:16 -05:00
|
|
|
case h: HashPartitioner =>
|
2011-02-27 22:15:52 -05:00
|
|
|
h.numPartitions == numPartitions
|
|
|
|
case _ => false
|
|
|
|
}
|
|
|
|
}
|