throw new IOException("Attempting sorted split on unsorted table");
}
}
if (numSplits == 1) {
BlockDistribution bd = null;
for (Iterator<BasicTable.Reader> it = readers.iterator(); it.hasNext();) {
BasicTable.Reader reader = it.next();
bd = BlockDistribution.sum(bd, reader.getBlockDistribution((RangeSplit)null));
}
SortedTableSplit split = new SortedTableSplit(null, null, bd, conf);
return new InputSplit[] { split };
}
// TODO: Does it make sense to interleave keys for all leaf tables if
// numSplits <= 0 ?
int nLeaves = readers.size();
KeyDistribution keyDistri = null;
for (int i = 0; i < nLeaves; ++i) {
KeyDistribution btKeyDistri =
readers.get(i).getKeyDistribution(
(numSplits <= 0) ? -1 :
Math.max(numSplits * 5 / nLeaves, numSplits));
keyDistri = KeyDistribution.sum(keyDistri, btKeyDistri);
}
if (keyDistri == null) {
// should never happen.
SortedTableSplit split = new SortedTableSplit(null, null, null, conf);
return new InputSplit[] { split };
}
if (numSplits > 0) {
keyDistri.resize(numSplits);
}
RawComparable[] rawKeys = keyDistri.getKeys();
BytesWritable[] keys = new BytesWritable[rawKeys.length];
for (int i=0; i<keys.length; ++i) {
RawComparable rawKey = rawKeys[i];
keys[i] = new BytesWritable();
keys[i].setSize(rawKey.size());
System.arraycopy(rawKey.buffer(), rawKey.offset(), keys[i].get(), 0,
rawKey.size());
}
// TODO: Should we change to RawComparable to avoid the creation of
// BytesWritables?
for (int i = 0; i < keys.length; ++i) {
BytesWritable begin = (i == 0) ? null : keys[i - 1];
BytesWritable end = (i == keys.length - 1) ? null : keys[i];
BlockDistribution bd = keyDistri.getBlockDistribution(keys[i]);
SortedTableSplit split = new SortedTableSplit(begin, end, bd, conf);
splits.add(split);
}
return splits.toArray(new InputSplit[splits.size()]);
}