public IndexedResultEntry getNextResult() throws IOException {
if (isClosed)
return null;
if (!flushBuffer) {
ResultEntry re = results.poll();
if (re == null) {
reachedEnd();
return null;
}
return new IndexedResultEntry(index, re);
}
if (readBuffer == null) {
readBuffer = this.fc.map(MapMode.READ_ONLY, readIndex, mappingSize);
}
int length = readBuffer.getInt();
if (length < 0) {
reachedEnd();
return null;
}
byte[] rb = new byte[length];
readBuffer.get(rb);
Result result = ResultUtil.toResult(new ImmutableBytesWritable(rb));
ResultTuple rt = new ResultTuple(result);
int sortKeySize = readBuffer.getInt();
ImmutableBytesWritable[] sortKeys = new ImmutableBytesWritable[sortKeySize];
for (int i = 0; i < sortKeySize; i++) {
int contentLength = readBuffer.getInt();
if (contentLength > 0) {
byte[] sortKeyContent = new byte[contentLength];
readBuffer.get(sortKeyContent);
sortKeys[i] = new ImmutableBytesWritable(sortKeyContent);
} else {
sortKeys[i] = null;
}
}
// buffer close to exhausted, re-map.
if (mappingSize - readBuffer.position() < maxResultSize) {
readIndex += readBuffer.position();
readBuffer = fc.map(MapMode.READ_ONLY, readIndex, mappingSize);
}
return new IndexedResultEntry(index, new ResultEntry(sortKeys, rt));
}