private long createInMemorySegments(List<MapOutput<K,V>> inMemoryMapOutputs,
List<Segment<K, V>> inMemorySegments,
long leaveBytes
) throws IOException {
long totalSize = 0L;
long fullSize = 0L;
for (MapOutput<K,V> mo : inMemoryMapOutputs) {
fullSize += mo.getMemory().length;
}
while(fullSize > leaveBytes) {
MapOutput<K,V> mo = inMemoryMapOutputs.remove(0);
byte[] data = mo.getMemory();
long size = data.length;
totalSize += size;
fullSize -= size;
Reader<K,V> reader = new InMemoryReader<K,V>(MergeManager.this,
mo.getMapId(),
data, 0, (int)size);
inMemorySegments.add(new Segment<K,V>(reader, true,
(mo.isPrimaryMapOutput() ?
mergedMapOutputsCounter : null)));
}
return totalSize;
}