Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ private[spark] class PartitionedAppendOnlyMap[K, V]

def partitionedDestructiveSortedIterator(keyComparator: Option[Comparator[K]])
: Iterator[((Int, K), V)] = {
val comparator = keyComparator.map(partitionKeyComparator).getOrElse(partitionComparator)
destructiveSortedIterator(comparator)
destructiveSortedIterator(getComparator(keyComparator))
}

def insert(partition: Int, key: K, value: V): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private[spark] class PartitionedPairBuffer[K, V](initialCapacity: Int = 64)
/** Iterate through the data in a given order. For this class this is not really destructive. */
override def partitionedDestructiveSortedIterator(keyComparator: Option[Comparator[K]])
: Iterator[((Int, K), V)] = {
val comparator = keyComparator.map(partitionKeyComparator).getOrElse(partitionComparator)
val comparator = getComparator(keyComparator)
new Sorter(new KVArraySortDataFormat[(Int, K), AnyRef]).sort(data, 0, curSize, comparator)
iterator
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,22 @@ private[spark] object WritablePartitionedPairCollection {
}

/**
* A comparator for (Int, K) pairs that orders them both by their partition ID and a key ordering.
* Takes an optional parameter (keyComparator), use if provided
* and returns a comparator for the partitions
*/
def partitionKeyComparator[K](keyComparator: Comparator[K]): Comparator[(Int, K)] = {
new Comparator[(Int, K)] {
override def compare(a: (Int, K), b: (Int, K)): Int = {
val partitionDiff = a._1 - b._1
if (partitionDiff != 0) {
partitionDiff
} else {
keyComparator.compare(a._2, b._2)
def getComparator[K](keyComparator: Option[Comparator[K]]): Comparator[(Int, K)] = {
if (!keyComparator.isDefined) return partitionComparator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline and remove partitionComparator, as I think it's not used

else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style is off here -- you need braces in both clauses, return is redundant, and there's no point in inverting the condition as opposed to just flipping the clauses.

val theKeyComp = keyComparator.get
new Comparator[(Int, K)] {
// We know we have a non-empty comparator here
override def compare(a: (Int, K), b: (Int, K)): Int = {
val partitionDiff = a._1 - b._1
if (partitionDiff != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably very very slightly better to say

if (a._1 == b._1) {
  theKeyComp.compare(a._2, b._2)
} else {
  a._1 - b._1
}

partitionDiff
} else {
theKeyComp.compare(a._2, b._2)
}
}
}
}
Expand Down