Search in sources :

Example 1 with AsyncBatchCursor

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);
                }
            }
        });
    }
}
Also used : AsyncBatchCursor(com.mongodb.async.AsyncBatchCursor) List(java.util.List) Document(org.bson.Document)

Example 2 with AsyncBatchCursor

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."));
            }
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) AsyncBatchCursor(com.mongodb.async.AsyncBatchCursor)

Aggregations

AsyncBatchCursor (com.mongodb.async.AsyncBatchCursor)2 MongoException (com.mongodb.MongoException)1 List (java.util.List)1 Document (org.bson.Document)1