use of com.mongodb.internal.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class ListIndexesOperation method executeAsync.
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) {
RetryState retryState = initialRetryState(retryReads);
binding.retain();
AsyncCallbackSupplier<AsyncBatchCursor<T>> asyncRead = CommandOperationHelper.<AsyncBatchCursor<T>>decorateReadWithRetries(retryState, funcCallback -> {
logRetryExecute(retryState);
withAsyncSourceAndConnection(binding::getReadConnectionSource, false, funcCallback, (source, connection, releasingCallback) -> {
if (retryState.breakAndCompleteIfRetryAnd(() -> !canRetryRead(source.getServerDescription(), connection.getDescription(), binding.getSessionContext()), releasingCallback)) {
return;
}
if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
createReadCommandAndExecuteAsync(retryState, binding, source, namespace.getDatabaseName(), getCommandCreator(), createCommandDecoder(), asyncTransformer(), connection, (result, t) -> {
if (t != null && !isNamespaceError(t)) {
releasingCallback.onResult(null, t);
} else {
releasingCallback.onResult(result != null ? result : emptyAsyncCursor(source), null);
}
});
} else {
retryState.markAsLastAttempt();
connection.queryAsync(getIndexNamespace(), asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize, binding.getReadPreference().isSecondaryOk(), false, false, false, false, false, decoder, binding.getRequestContext(), new SingleResultCallback<QueryResult<T>>() {
@Override
public void onResult(final QueryResult<T> result, final Throwable t) {
if (t != null) {
releasingCallback.onResult(null, t);
} else {
releasingCallback.onResult(new AsyncQueryBatchCursor<T>(result, 0, batchSize, 0, decoder, source, connection), null);
}
}
});
}
});
}).whenComplete(binding::release);
asyncRead.get(errorHandlingCallback(callback, LOGGER));
}
use of com.mongodb.internal.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class BatchCursorPublisherTest method createVerifiableBatchCursor.
BatchCursorPublisher<Document> createVerifiableBatchCursor(final List<Document> expected, final int batchSize, final boolean errorCreatingCursor, final boolean errorOnEmpty) {
BatchCursorPublisher<Document> publisher = new BatchCursorPublisher<Document>(null, OPERATION_PUBLISHER) {
@Override
AsyncReadOperation<AsyncBatchCursor<Document>> asAsyncReadOperation(final int initialBatchSize) {
return readOperation;
}
};
OperationExecutor executor = OPERATION_EXECUTOR;
if (batchSize > 0) {
publisher.batchSize(batchSize);
}
if (errorCreatingCursor) {
Mockito.doAnswer(invocation -> Mono.fromCallable(() -> {
throw new Exception(ERROR_CREATING_CURSOR);
})).when(executor).execute(eq(readOperation), eq(ReadPreference.primary()), eq(ReadConcern.DEFAULT), eq(null));
} else {
Mockito.doAnswer(invocation -> Mono.fromCallable(() -> batchCursor)).when(executor).execute(eq(readOperation), eq(ReadPreference.primary()), eq(ReadConcern.DEFAULT), eq(null));
Queue<List<Document>> queuedResults = new LinkedList<>(createBatches(expected, batchSize));
AtomicBoolean isClosed = new AtomicBoolean(false);
Mockito.lenient().doAnswer(i -> isClosed.get()).when(batchCursor).isClosed();
Mockito.doAnswer(invocation -> {
List<Document> next = queuedResults.poll();
if (queuedResults.isEmpty()) {
if (!errorOnEmpty) {
isClosed.set(true);
} else if (next == null) {
invocation.getArgument(0, SingleResultCallback.class).onResult(null, new Exception(ERROR_RETURNING_RESULTS));
return null;
}
}
invocation.getArgument(0, SingleResultCallback.class).onResult(next, null);
return null;
}).when(batchCursor).next(any(SingleResultCallback.class));
}
return publisher;
}
use of com.mongodb.internal.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class AggregatePublisherImpl method asAsyncReadOperation.
@Override
AsyncReadOperation<AsyncBatchCursor<T>> asAsyncReadOperation(final int initialBatchSize) {
MongoNamespace outNamespace = getOutNamespace();
if (outNamespace != null) {
AsyncReadOperation<Void> aggregateToCollectionOperation = getAggregateToCollectionOperation();
FindOptions findOptions = new FindOptions().collation(collation).batchSize(initialBatchSize);
AsyncReadOperation<AsyncBatchCursor<T>> findOperation = getOperations().find(outNamespace, new BsonDocument(), getDocumentClass(), findOptions);
return new VoidReadOperationThenCursorReadOperation<>(aggregateToCollectionOperation, findOperation);
} else {
return asAggregateOperation(initialBatchSize);
}
}
use of com.mongodb.internal.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class ListCollectionsOperation method executeAsync.
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) {
RetryState retryState = initialRetryState(retryReads);
binding.retain();
AsyncCallbackSupplier<AsyncBatchCursor<T>> asyncRead = CommandOperationHelper.<AsyncBatchCursor<T>>decorateReadWithRetries(retryState, funcCallback -> {
logRetryExecute(retryState);
withAsyncSourceAndConnection(binding::getReadConnectionSource, false, funcCallback, (source, connection, releasingCallback) -> {
if (retryState.breakAndCompleteIfRetryAnd(() -> !canRetryRead(source.getServerDescription(), connection.getDescription(), binding.getSessionContext()), releasingCallback)) {
return;
}
if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
createReadCommandAndExecuteAsync(retryState, binding, source, databaseName, getCommandCreator(), createCommandDecoder(), asyncTransformer(), connection, (result, t) -> {
if (t != null && !isNamespaceError(t)) {
releasingCallback.onResult(null, t);
} else {
releasingCallback.onResult(result != null ? result : emptyAsyncCursor(source), null);
}
});
} else {
retryState.markAsLastAttempt();
connection.queryAsync(getNamespace(), asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize, binding.getReadPreference().isSecondaryOk(), false, false, false, false, false, new BsonDocumentCodec(), binding.getRequestContext(), new SingleResultCallback<QueryResult<BsonDocument>>() {
@Override
public void onResult(final QueryResult<BsonDocument> result, final Throwable t) {
if (t != null) {
releasingCallback.onResult(null, t);
} else {
releasingCallback.onResult(new ProjectingAsyncBatchCursor(new AsyncQueryBatchCursor<BsonDocument>(result, 0, batchSize, 0, new BsonDocumentCodec(), source, connection)), null);
}
}
});
}
});
}).whenComplete(binding::release);
asyncRead.get(errorHandlingCallback(callback, LOGGER));
}
use of com.mongodb.internal.async.AsyncBatchCursor in project mongo-java-driver by mongodb.
the class FindOperation method executeAsync.
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) {
RetryState retryState = initialRetryState(retryReads);
binding.retain();
AsyncCallbackSupplier<AsyncBatchCursor<T>> asyncRead = CommandOperationHelper.<AsyncBatchCursor<T>>decorateReadWithRetries(retryState, funcCallback -> {
logRetryExecute(retryState);
withAsyncSourceAndConnection(binding::getReadConnectionSource, false, funcCallback, (source, connection, releasingCallback) -> {
if (retryState.breakAndCompleteIfRetryAnd(() -> !canRetryRead(source.getServerDescription(), connection.getDescription(), binding.getSessionContext()), releasingCallback)) {
return;
}
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
final SingleResultCallback<AsyncBatchCursor<T>> wrappedCallback = exceptionTransformingCallback(releasingCallback);
createReadCommandAndExecuteAsync(retryState, binding, source, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()), CommandResultDocumentCodec.create(decoder, FIRST_BATCH), asyncTransformer(), connection, wrappedCallback);
} else {
retryState.markAsLastAttempt();
validateFindOptions(source, connection, binding.getSessionContext().getReadConcern(), collation, allowDiskUse, new AsyncCallableWithConnectionAndSource() {
@Override
public void call(final AsyncConnectionSource source, final AsyncConnection connection, final Throwable t) {
if (t != null) {
releasingCallback.onResult(null, t);
} else {
connection.queryAsync(namespace, asDocument(connection.getDescription(), binding.getReadPreference()), projection, skip, limit, batchSize, isSecondaryOk() || binding.getReadPreference().isSecondaryOk(), isTailableCursor(), isAwaitData(), isNoCursorTimeout(), isPartial(), isOplogReplay(), decoder, binding.getRequestContext(), new SingleResultCallback<QueryResult<T>>() {
@Override
public void onResult(final QueryResult<T> result, final Throwable t) {
if (t != null) {
releasingCallback.onResult(null, t);
} else {
releasingCallback.onResult(new AsyncQueryBatchCursor<T>(result, limit, batchSize, getMaxTimeForCursor(), decoder, source, connection), null);
}
}
});
}
}
});
}
});
}).whenComplete(binding::release);
asyncRead.get(errorHandlingCallback(callback, LOGGER));
}
Aggregations