|
In order to make cursor iteration blocking for tailable cursors, and in general to support the possibility of empty batches, driver implementations of cursor iteration typically have an inner loop within the method that advances the cursor:
while (serverCursor != null) {
|
_nextBatch = getMore();
|
if (!_nextBatch.isEmpty()) {
|
return true; // has more results
|
}
|
}
|
return false; // no more results
|
Drivers should ensure that there is a check within that loop that the cursor has not been closed by another thread in the application.
while (serverCursor != null) {
|
_nextBatch = getMore();
|
|
// if another thread closed the cursor, throw an exception so that this method terminates
|
if (_closed) {
|
throw ...
|
}
|
|
if (!_nextBatch.isEmpty()) {
|
return true; // has more results
|
}
|
}
|
return false; // no more results
|
This is necessary because a call to killCursors from the close method of the cursor will fail if there is a getMore in progress on that cursor, and thus the repeated calls to getMore in the loop will never encounter a cursorNotFound error and the loop will not exit.
|