Skip to content

Clear CommandCursorResult.results after next()/tryNext(). #1780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getMoreCommandDocument;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.logCommandCursorResult;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.translateCommandException;
import static com.mongodb.internal.operation.CommandCursorResult.withEmptyResults;
import static java.util.Collections.emptyList;

class AsyncCommandBatchCursor<T> implements AsyncAggregateResponseBatchCursor<T> {
Expand Down Expand Up @@ -117,6 +118,7 @@ public void next(final SingleResultCallback<List<T>> callback) {
}

if (serverCursorIsNull || !batchResults.isEmpty()) {
commandCursorResult = withEmptyResults(commandCursorResult);
funcCallback.onResult(batchResults, null);
} else {
getMore(localServerCursor, funcCallback);
Expand Down Expand Up @@ -206,6 +208,7 @@ private void getMoreLoop(final AsyncConnection connection, final ServerCursor se
resourceManager.setServerCursor(nextServerCursor);
List<T> nextBatch = commandCursorResult.getResults();
if (nextServerCursor == null || !nextBatch.isEmpty()) {
commandCursorResult = withEmptyResults(commandCursorResult);
callback.onResult(nextBatch, null);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private List<T> doNext() {

List<T> retVal = nextBatch;
nextBatch = null;
commandCursorResult = CommandCursorResult.withEmptyResults(commandCursorResult);
return retVal;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bson.BsonDocument;
import org.bson.BsonTimestamp;

import java.util.Collections;
import java.util.List;

import static com.mongodb.assertions.Assertions.isTrue;
Expand Down Expand Up @@ -60,6 +61,31 @@ public CommandCursorResult(
this.postBatchResumeToken = cursorDocument.getDocument(POST_BATCH_RESUME_TOKEN, null);
}

private CommandCursorResult(
final ServerAddress serverAddress,
final List<T> results,
final MongoNamespace namespace,
final long cursorId,
@Nullable final BsonTimestamp operationTime,
@Nullable final BsonDocument postBatchResumeToken) {
this.serverAddress = serverAddress;
this.results = results;
this.namespace = namespace;
this.cursorId = cursorId;
this.operationTime = operationTime;
this.postBatchResumeToken = postBatchResumeToken;
}

public static <T> CommandCursorResult<T> withEmptyResults(final CommandCursorResult<T> commandCursorResult) {
return new CommandCursorResult<>(
commandCursorResult.getServerAddress(),
Collections.emptyList(),
commandCursorResult.getNamespace(),
commandCursorResult.getCursorId(),
commandCursorResult.getOperationTime(),
commandCursorResult.getPostBatchResumeToken());
}

/**
* Gets the namespace.
*
Expand Down