private void getMore(final ServerCursor cursor, final SingleResultCallback<List<T>> callback, final boolean tryNext) { connectionSource.getConnection(new SingleResultCallback<AsyncConnection>() { @Override public void onResult(final AsyncConnection connection, final Throwable t) { if (t != null) { callback.onResult(null, t); } else { getMore(connection, cursor, callback, tryNext); } } }); }
private AsyncBatchCursor<T> emptyAsyncCursor(final AsyncConnectionSource source) { return createEmptyAsyncBatchCursor(namespace, source.getServerDescription().getAddress()); }
@Override public void onResult(final AsyncConnection connection, final Throwable t) { if (t != null) { connectionSource.release(); } else { killCursorAsynchronouslyAndReleaseConnectionAndSource(connection, localCursor); } } });
private void killCursorAsynchronouslyAndReleaseConnectionAndSource(final AsyncConnection connection, final ServerCursor localCursor) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.commandAsync(namespace.getDatabaseName(), asKillCursorsCommandDocument(localCursor), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(), connectionSource.getSessionContext(), new SingleResultCallback<BsonDocument>() { @Override public void onResult(final BsonDocument result, final Throwable t) { connection.release(); connectionSource.release(); } }); } else { connection.killCursorAsync(namespace, singletonList(localCursor.getId()), new SingleResultCallback<Void>() { @Override public void onResult(final Void result, final Throwable t) { connection.release(); connectionSource.release(); } }); } }
AsyncQueryBatchCursor(final QueryResult<T> firstBatch, final int limit, final int batchSize, final long maxTimeMS, final Decoder<T> decoder, final AsyncConnectionSource connectionSource, final AsyncConnection connection) { isTrueArgument("maxTimeMS >= 0", maxTimeMS >= 0); this.maxTimeMS = maxTimeMS; this.namespace = firstBatch.getNamespace(); this.firstBatch = firstBatch; this.limit = limit; this.batchSize = batchSize; this.decoder = decoder; this.cursor = new AtomicReference<ServerCursor>(firstBatch.getCursor()); this.connectionSource = notNull("connectionSource", connectionSource); this.count.addAndGet(firstBatch.getResults().size()); if (firstBatch.getCursor() != null) { connectionSource.retain(); if (limitReached()) { killCursor(connection); } } }
private void getMore(final AsyncConnection connection, final ServerCursor cursor, final SingleResultCallback<List<T>> callback, final boolean tryNext) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.commandAsync(namespace.getDatabaseName(), asGetMoreCommandDocument(cursor.getId()), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"), connectionSource.getSessionContext(), new CommandResultSingleResultCallback(connection, cursor, callback, tryNext)); } else { connection.getMoreAsync(namespace, cursor.getId(), getNumberToReturn(limit, batchSize, count.get()), decoder, new QueryResultSingleResultCallback(connection, callback, tryNext)); } }
AsyncQueryBatchCursor(final QueryResult<T> firstBatch, final int limit, final int batchSize, final long maxTimeMS, final Decoder<T> decoder, final AsyncConnectionSource connectionSource, final AsyncConnection connection) { isTrueArgument("maxTimeMS >= 0", maxTimeMS >= 0); this.maxTimeMS = maxTimeMS; this.namespace = firstBatch.getNamespace(); this.firstBatch = firstBatch; this.limit = limit; this.batchSize = batchSize; this.decoder = decoder; this.cursor = new AtomicReference<ServerCursor>(firstBatch.getCursor()); this.connectionSource = notNull("connectionSource", connectionSource); this.count.addAndGet(firstBatch.getResults().size()); if (firstBatch.getCursor() != null) { connectionSource.retain(); if (limitReached()) { killCursor(connection); } } }
@Override public void onResult(final BsonDocument result, final Throwable t) { connection.release(); connectionSource.release(); } });
private static void withConnectionSource(final AsyncConnectionSource source, final AsyncCallableWithConnectionAndSource callable) { source.getConnection(new SingleResultCallback<AsyncConnection>() { @Override public void onResult(final AsyncConnection result, final Throwable t) { callable.call(source, result, t); } }); }
@Override public List<AsyncBatchCursor<T>> apply(final BsonDocument result, final ServerAddress serverAddress) { List<AsyncBatchCursor<T>> cursors = new ArrayList<AsyncBatchCursor<T>>(); for (BsonValue cursorValue : getCursorDocuments(result)) { cursors.add(new AsyncQueryBatchCursor<T>(createQueryResult(getCursorDocument(cursorValue.asDocument()), source.getServerDescription().getAddress()), 0, getBatchSize(), 0, decoder, source, connection)); } return cursors; } };
private void killCursorAsynchronouslyAndReleaseConnectionAndSource(final AsyncConnection connection, final ServerCursor localCursor) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.commandAsync(namespace.getDatabaseName(), asKillCursorsCommandDocument(localCursor), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(), connectionSource.getSessionContext(), new SingleResultCallback<BsonDocument>() { @Override public void onResult(final BsonDocument result, final Throwable t) { connection.release(); connectionSource.release(); } }); } else { connection.killCursorAsync(namespace, singletonList(localCursor.getId()), new SingleResultCallback<Void>() { @Override public void onResult(final Void result, final Throwable t) { connection.release(); connectionSource.release(); } }); } }
public SingleResultCallback<T> releaseConnectionAndGetWrapped() { connection.release(); source.release(); return wrapped; } }
private static void withConnectionSource(final AsyncConnectionSource source, final AsyncCallableWithConnection callable) { source.getConnection(new SingleResultCallback<AsyncConnection>() { @Override public void onResult(final AsyncConnection connection, final Throwable t) { source.release(); if (t != null) { callable.call(null, t); } else { callable.call(connection, null); } } }); }
static <T> AsyncBatchCursor<T> cursorDocumentToAsyncBatchCursor(final BsonDocument cursorDocument, final Decoder<T> decoder, final AsyncConnectionSource source, final AsyncConnection connection, final int batchSize) { return new AsyncQueryBatchCursor<T>(OperationHelper.<T>cursorDocumentToQueryResult(cursorDocument, source.getServerDescription().getAddress()), 0, batchSize, 0, decoder, source, connection); }
private void getMore(final AsyncConnection connection, final ServerCursor cursor, final SingleResultCallback<List<T>> callback, final boolean tryNext) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.commandAsync(namespace.getDatabaseName(), asGetMoreCommandDocument(cursor.getId()), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"), connectionSource.getSessionContext(), new CommandResultSingleResultCallback(connection, cursor, callback, tryNext)); } else { connection.getMoreAsync(namespace, cursor.getId(), getNumberToReturn(limit, batchSize, count.get()), decoder, new QueryResultSingleResultCallback(connection, callback, tryNext)); } }
@Override public void onResult(final Void result, final Throwable t) { connection.release(); connectionSource.release(); } });
private void killCursorOnClose() { final ServerCursor localCursor = getServerCursor(); if (localCursor != null) { connectionSource.getConnection(new SingleResultCallback<AsyncConnection>() { @Override public void onResult(final AsyncConnection connection, final Throwable t) { if (t != null) { connectionSource.release(); } else { killCursorAsynchronouslyAndReleaseConnectionAndSource(connection, localCursor); } } }); } }
private AsyncBatchCursor<T> emptyAsyncCursor(final AsyncConnectionSource source) { return createEmptyAsyncBatchCursor(createNamespace(), source.getServerDescription().getAddress()); }
private void killCursor(final AsyncConnection connection) { ServerCursor localCursor = cursor.getAndSet(null); if (localCursor != null) { killCursorAsynchronouslyAndReleaseConnectionAndSource(connection.retain(), localCursor); } else { connectionSource.release(); } }
@Override public void onResult(final AsyncConnectionSource source, final Throwable t) { if (t != null) { errorHandlingCallback.onResult(null, t); } else { source.getConnection(new SingleResultCallback<AsyncConnection>() { @Override public void onResult(final AsyncConnection connection, final Throwable t) { if (t != null) { releasingCallback(errorHandlingCallback, source).onResult(null, t); } else { try { BsonDocument command = commandCreator.create(source.getServerDescription(), connection.getDescription()); connection.commandAsync(database, command, fieldNameValidator, readPreference, commandResultDecoder, binding.getSessionContext(), createCommandCallback(binding, source, connection, database, readPreference, command, fieldNameValidator, commandResultDecoder, transformer, errorHandlingCallback)); } catch (Throwable t1) { releasingCallback(errorHandlingCallback, source, connection).onResult(null, t1); } } } }); } } });