@Nullable @Override public TResult first() { BatchCursor<TResult> batchCursor = getExecutor().execute(operations.findFirst(filter, resultClass, findOptions), getReadPreference(), getReadConcern(), getClientSession()); try { return batchCursor.hasNext() ? batchCursor.next().iterator().next() : null; } finally { batchCursor.close(); } }
@Override public ServerAddress getServerAddress() { return wrapped.getServerAddress(); }
@Override public ServerCursor getServerCursor() { return wrapped.getServerCursor(); }
@Override public void close() { batchCursor.close(); }
@Override public List<T> apply(final BatchCursor<RawBsonDocument> queryBatchCursor) { return convertResults(queryBatchCursor.next()); } });
@Override public List<T> apply(final BatchCursor<RawBsonDocument> queryBatchCursor) { return convertResults(queryBatchCursor.tryNext()); } });
@Override public Boolean apply(final BatchCursor<RawBsonDocument> queryBatchCursor) { return queryBatchCursor.hasNext(); } });
@Override public int getBatchSize() { return wrapped.getBatchSize(); }
@Override public void setBatchSize(final int batchSize) { wrapped.setBatchSize(batchSize); }
@Override public Long execute(final ReadBinding binding) { if (countStrategy.equals(CountStrategy.COMMAND)) { return withConnection(binding, new CallableWithConnection<Long>() { @Override public Long call(final Connection connection) { validateReadConcernAndCollation(connection, binding.getSessionContext().getReadConcern(), collation); return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), getCommand(binding.getSessionContext()), DECODER, connection, transformer()); } }); } else { BatchCursor<BsonDocument> cursor = getAggregateOperation().execute(binding); return cursor.hasNext() ? getCountFromAggregateResults(cursor.next()) : 0; } }
@Override public void close() { wrapped.close(); binding.release(); }
@Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } if (curBatch == null) { curBatch = batchCursor.next(); } return getNextInBatch(); }
@Nullable @Override public T tryNext() { if (curBatch == null) { curBatch = batchCursor.tryNext(); } return curBatch == null ? null : getNextInBatch(); }
@Override public boolean hasNext() { return curBatch != null || batchCursor.hasNext(); }
@Override public int getBatchSize() { return wrapped.getBatchSize(); }
@Override public void setBatchSize(final int batchSize) { wrapped.setBatchSize(batchSize); }
@Nullable @Override public TResult first() { BatchCursor<TResult> batchCursor = getExecutor().execute(operations.findFirst(filter, resultClass, findOptions), getReadPreference(), getReadConcern(), getClientSession()); try { return batchCursor.hasNext() ? batchCursor.next().iterator().next() : null; } finally { batchCursor.close(); } }
<R> R resumeableOperation(final Function<BatchCursor<RawBsonDocument>, R> function) { while (true) { try { return function.apply(wrapped); } catch (Throwable t) { if (!isRetryableError(t)) { throw MongoException.fromThrowableNonNull(t); } } wrapped.close(); if (resumeToken != null) { changeStreamOperation.startOperationTimeForResume(null); changeStreamOperation.resumeAfter(resumeToken); } wrapped = ((ChangeStreamBatchCursor<T>) changeStreamOperation.execute(binding)).getWrapped(); binding.release(); // release the new change stream batch cursor's reference to the binding } } }