Skip to content

Commit 76bf15e

Browse files
committed
convertAndProduceLastId now is fully non null
1 parent 7db464c commit 76bf15e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

driver-core/src/main/com/mongodb/internal/operation/ChangeStreamBatchCursor.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.function.Consumer;
3434
import java.util.function.Function;
3535

36+
import static com.mongodb.assertions.Assertions.assertNotNull;
3637
import static com.mongodb.internal.operation.ChangeStreamBatchCursorHelper.isResumableError;
3738
import static com.mongodb.internal.operation.SyncOperationHelper.withReadConnectionSource;
3839

@@ -94,8 +95,9 @@ public int available() {
9495
public List<T> tryNext() {
9596
return resumeableOperation(commandBatchCursor -> {
9697
try {
97-
return convertAndProduceLastId(commandBatchCursor.tryNext(), changeStreamOperation.getDecoder(),
98-
lastId -> resumeToken = lastId);
98+
List<RawBsonDocument> tryNext = commandBatchCursor.tryNext();
99+
return tryNext == null ? null :
100+
convertAndProduceLastId(tryNext, changeStreamOperation.getDecoder(), lastId -> resumeToken = lastId);
99101
} finally {
100102
cachePostBatchResumeToken(commandBatchCursor);
101103
}
@@ -165,20 +167,18 @@ private void cachePostBatchResumeToken(final AggregateResponseBatchCursor<RawBso
165167
* @param lastIdConsumer Is {@linkplain Consumer#accept(Object) called} iff {@code rawDocuments} is successfully converted
166168
* and the returned {@link List} is neither {@code null} nor {@linkplain List#isEmpty() empty}.
167169
*/
168-
static <T> List<T> convertAndProduceLastId(@Nullable final List<RawBsonDocument> rawDocuments,
170+
static <T> List<T> convertAndProduceLastId(final List<RawBsonDocument> rawDocuments,
169171
final Decoder<T> decoder,
170172
final Consumer<BsonDocument> lastIdConsumer) {
171173
List<T> results = new ArrayList<>();
172-
if (rawDocuments != null) {
173-
for (RawBsonDocument rawDocument : rawDocuments) {
174-
if (!rawDocument.containsKey("_id")) {
175-
throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
176-
}
177-
results.add(rawDocument.decode(decoder));
178-
}
179-
if (!rawDocuments.isEmpty()) {
180-
lastIdConsumer.accept(rawDocuments.get(rawDocuments.size() - 1).getDocument("_id"));
174+
for (RawBsonDocument rawDocument : assertNotNull(rawDocuments)) {
175+
if (!rawDocument.containsKey("_id")) {
176+
throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
181177
}
178+
results.add(rawDocument.decode(decoder));
179+
}
180+
if (!rawDocuments.isEmpty()) {
181+
lastIdConsumer.accept(rawDocuments.get(rawDocuments.size() - 1).getDocument("_id"));
182182
}
183183
return results;
184184
}

0 commit comments

Comments
 (0)