-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
663 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
core/src/main/java/org/apache/spark/unsafe/sort/UnsafeExternalSortSpillMerger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.unsafe.sort; | ||
|
||
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.PriorityQueue; | ||
|
||
import static org.apache.spark.unsafe.sort.UnsafeSorter.*; | ||
|
||
public final class UnsafeExternalSortSpillMerger { | ||
|
||
private final PriorityQueue<MergeableIterator> priorityQueue; | ||
|
||
public static abstract class MergeableIterator { | ||
public abstract boolean hasNext(); | ||
|
||
public abstract void advanceRecord(); | ||
|
||
public abstract long getPrefix(); | ||
|
||
public abstract Object getBaseObject(); | ||
|
||
public abstract long getBaseOffset(); | ||
} | ||
|
||
public static final class RecordAddressAndKeyPrefix { | ||
public Object baseObject; | ||
public long baseOffset; | ||
public int recordLength; | ||
public long keyPrefix; | ||
} | ||
|
||
public UnsafeExternalSortSpillMerger( | ||
final RecordComparator recordComparator, | ||
final UnsafeSorter.PrefixComparator prefixComparator) { | ||
final Comparator<MergeableIterator> comparator = new Comparator<MergeableIterator>() { | ||
|
||
@Override | ||
public int compare(MergeableIterator left, MergeableIterator right) { | ||
final int prefixComparisonResult = | ||
prefixComparator.compare(left.getPrefix(), right.getPrefix()); | ||
if (prefixComparisonResult == 0) { | ||
return recordComparator.compare( | ||
left.getBaseObject(), left.getBaseOffset(), | ||
right.getBaseObject(), right.getBaseOffset()); | ||
} else { | ||
return prefixComparisonResult; | ||
} | ||
} | ||
}; | ||
priorityQueue = new PriorityQueue<MergeableIterator>(10, comparator); | ||
} | ||
|
||
public void addSpill(MergeableIterator spillReader) { | ||
priorityQueue.add(spillReader); | ||
} | ||
|
||
public Iterator<RecordAddressAndKeyPrefix> getSortedIterator() { | ||
return new Iterator<RecordAddressAndKeyPrefix>() { | ||
|
||
private MergeableIterator spillReader; | ||
private final RecordAddressAndKeyPrefix record = new RecordAddressAndKeyPrefix(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return spillReader.hasNext() || !priorityQueue.isEmpty(); | ||
} | ||
|
||
@Override | ||
public RecordAddressAndKeyPrefix next() { | ||
if (spillReader != null) { | ||
if (spillReader.hasNext()) { | ||
priorityQueue.add(spillReader); | ||
} | ||
} | ||
spillReader = priorityQueue.poll(); | ||
record.baseObject = spillReader.getBaseObject(); | ||
record.baseOffset = spillReader.getBaseOffset(); | ||
record.keyPrefix = spillReader.getPrefix(); | ||
return record; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.