convertedKey = (WritableComparable) ReflectionUtils.newInstance(
convertedKeyClass, conf);
value = new MapWritable();
readers.get(i).next(convertedKey, value);
candidates.put(i, new KeyValuePair(convertedKey, value));
}
while (readers.size() > 0) {
convertedKey = (WritableComparable) ReflectionUtils.newInstance(
convertedKeyClass, conf);
value = new MapWritable();
int readerIndex = 0;
WritableComparable firstKey = null;
MapWritable rawRecord = null;
for (Map.Entry<Integer, KeyValuePair<WritableComparable, MapWritable>> keys : candidates
.entrySet()) {
if (firstKey == null) {
readerIndex = keys.getKey();
firstKey = keys.getValue().getKey();
rawRecord = (MapWritable) keys.getValue().getValue();
} else {
WritableComparable currentKey = keys.getValue().getKey();
if (firstKey.compareTo(currentKey) > 0) {
readerIndex = keys.getKey();
firstKey = currentKey;
rawRecord = (MapWritable) keys.getValue().getValue();
}
}
}
for (Map.Entry<Writable, Writable> e : rawRecord.entrySet()) {
writer.append(e.getKey(), e.getValue());
}
candidates.remove(readerIndex);
if (readers.get(readerIndex).next(convertedKey, value)) {
candidates.put(readerIndex, new KeyValuePair(convertedKey, value));
} else {
readers.get(readerIndex).close();
readers.remove(readerIndex);
}
}