DatastorePb.QueryResult res = peekQueryResultAndIfFirstRecordIndexList();
queryResultFuture = null;
processQueryResult(res, buffer);
if (res.isMoreResults()) {
NextRequest req = new NextRequest();
req.getMutableCursor().copyFrom(res.getCursor());
if (res.hasCompiledCursor()) {
req.setCompile(true);
}
boolean setCount = true;
if (numberToLoad <= 0) {
setCount = false;
if (chunkSize != AT_LEAST_ONE) {
req.setCount(chunkSize);
}
if (numberToLoad == AT_LEAST_ONE) {
numberToLoad = 1;
}
}
while (
(skippedResults < offset ||
buffer.size() - previousSize < numberToLoad) &&
res.isMoreResults()) {
if (skippedResults < offset) {
req.setOffset(offset - skippedResults);
} else {
req.clearOffset();
}
if (setCount) {
req.setCount(Math.max(chunkSize, numberToLoad - buffer.size() + previousSize));
}
queryResultFuture = makeAsyncCall(apiConfig, "Next", req,
new DatastorePb.QueryResult());
res = peekQueryResultAndIfFirstRecordIndexList();
queryResultFuture = null;
processQueryResult(res, buffer);
}
if (res.isMoreResults()) {
if (chunkSize != AT_LEAST_ONE) {
req.setCount(chunkSize);
} else {
req.clearCount();
}
req.clearOffset();
queryResultFuture = makeAsyncCall(apiConfig, "Next", req, new DatastorePb.QueryResult());
}
}
return res.hasCompiledCursor() ? new Cursor(res.getCompiledCursor()) : null;
}