use of com.mongodb.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class GridFSDownloadStreamImpl method checkAndFetchResults.
private void checkAndFetchResults(final int amountRead, final ByteBuffer dst, final SingleResultCallback<Integer> callback) {
if (currentPosition == fileInfo.getLength() || dst.remaining() == 0) {
callback.onResult(amountRead, null);
} else if (hasResultsToProcess()) {
processResults(amountRead, dst, callback);
} else if (cursor == null) {
chunksCollection.find(new Document("files_id", fileInfo.getId()).append("n", new Document("$gte", chunkIndex))).batchSize(batchSize).sort(new Document("n", 1)).batchCursor(new SingleResultCallback<AsyncBatchCursor<Document>>() {
@Override
public void onResult(final AsyncBatchCursor<Document> result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
cursor = result;
checkAndFetchResults(amountRead, dst, callback);
}
}
});
} else {
cursor.next(new SingleResultCallback<List<Document>>() {
@Override
public void onResult(final List<Document> result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else if (result == null || result.isEmpty()) {
callback.onResult(null, chunkNotFound(chunkIndex));
} else {
resultsQueue.addAll(result);
if (batchSize == 1) {
discardCursor();
}
processResults(amountRead, dst, callback);
}
}
});
}
}
use of com.mongodb.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class MongoIterableSubscription method requestInitialData.
@Override
void requestInitialData() {
mongoIterable.batchSize(getBatchSize());
mongoIterable.batchCursor(new SingleResultCallback<AsyncBatchCursor<TResult>>() {
@Override
public void onResult(final AsyncBatchCursor<TResult> result, final Throwable t) {
if (t != null) {
onError(t);
} else if (result != null) {
batchCursor = result;
requestMoreData();
} else {
onError(new MongoException("Unexpected error, no AsyncBatchCursor returned from the MongoIterable."));
}
}
});
}
Aggregations