@Override public <T> T execute(final ReadOperation<T> operation, final ReadPreference readPreference, final ReadConcern readConcern, @Nullable final ClientSession session) { ClientSession actualClientSession = getClientSession(session); ReadBinding binding = getReadBinding(readPreference, readConcern, actualClientSession, session == null && actualClientSession != null); try { if (session != null && session.hasActiveTransaction() && !binding.getReadPreference().equals(primary())) { throw new MongoClientException("Read preference in a transaction must be primary"); } return operation.execute(binding); } catch (MongoException e) { labelException(session, e); throw e; } finally { binding.release(); } }
static <D, T> T executeWrappedCommandProtocol(final ReadBinding binding, final String database, final BsonDocument command, final Decoder<D> decoder, final CommandTransformer<D, T> transformer) { ConnectionSource source = binding.getReadConnectionSource(); try { return transformer.apply(executeWrappedCommandProtocol(database, command, decoder, source, binding.getReadPreference()), source.getServerDescription().getAddress()); } finally { source.release(); } }
static <T> T executeWrappedCommandProtocol(final ReadBinding binding, final String database, final BsonDocument command, final Decoder<BsonDocument> decoder, final Connection connection, final CommandTransformer<BsonDocument, T> transformer) { return executeWrappedCommandProtocol(database, command, decoder, connection, binding.getReadPreference(), transformer, binding.getSessionContext()); }
ChangeStreamBatchCursor(final ChangeStreamOperation<T> changeStreamOperation, final BatchCursor<RawBsonDocument> wrapped, final ReadBinding binding) { changeStreamOperation.startOperationTimeForResume(binding.getSessionContext().getOperationTime()); this.changeStreamOperation = changeStreamOperation; this.resumeToken = changeStreamOperation.getResumeToken(); this.wrapped = wrapped; this.binding = binding.retain(); }
@Override public void close() { wrapped.close(); binding.release(); }
@Override public BsonDocument call(final Connection connection) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { return executeWrappedCommandProtocol(binding, "admin", new BsonDocument("currentOp", new BsonInt32(1)), connection); } else { return connection.query(new MongoNamespace("admin", "$cmd.sys.inprog"), new BsonDocument(), null, 0, 1, 0, binding.getReadPreference().isSlaveOk(), false, false, false, false, false, new BsonDocumentCodec()) .getResults().get(0); } } });
@Override public Long call(final Connection connection) { validateReadConcernAndCollation(connection, binding.getSessionContext().getReadConcern(), collation); return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), getCommand(binding.getSessionContext()), DECODER, connection, transformer()); } });
static <T> T withConnection(final ReadBinding binding, final CallableWithConnection<T> callable) { ConnectionSource source = binding.getReadConnectionSource(); try { return withConnectionSource(source, callable); } finally { source.release(); } }
<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 } } }
@Override public BatchCursor<T> call(final ConnectionSource source, final Connection connection) { if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) { try { return executeWrappedCommandProtocol(binding, databaseName, getCommand(), createCommandDecoder(), connection, commandTransformer(source)); } catch (MongoCommandException e) { return rethrowIfNotNamespaceError(e, createEmptyBatchCursor(createNamespace(), decoder, source.getServerDescription().getAddress(), batchSize)); } } else { return new ProjectingBatchCursor(new QueryBatchCursor<BsonDocument>(connection.query(getNamespace(), asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize, binding.getReadPreference().isSlaveOk(), false, false, false, false, false, new BsonDocumentCodec()), 0, batchSize, new BsonDocumentCodec(), source)); } } });
@Override public BatchCursor<T> call(final ConnectionSource source, final Connection connection) { validateReadConcernAndCollation(connection, binding.getSessionContext().getReadConcern(), collation); return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), getCommand(binding.getSessionContext()), createCommandDecoder(), connection, transformer(source, connection)); } });
ChangeStreamBatchCursor(final ChangeStreamOperation<T> changeStreamOperation, final BatchCursor<RawBsonDocument> wrapped, final ReadBinding binding) { changeStreamOperation.startOperationTimeForResume(binding.getSessionContext().getOperationTime()); this.changeStreamOperation = changeStreamOperation; this.resumeToken = changeStreamOperation.getResumeToken(); this.wrapped = wrapped; this.binding = binding.retain(); }
static <T> T withConnection(final ReadBinding binding, final CallableWithConnectionAndSource<T> callable) { ConnectionSource source = binding.getReadConnectionSource(); try { return withConnectionSource(source, callable); } finally { source.release(); } }
@Override public <T> T execute(final ReadOperation<T> operation, final ReadPreference readPreference, final ReadConcern readConcern, @Nullable final ClientSession session) { ClientSession actualClientSession = getClientSession(session); ReadBinding binding = getReadBinding(readPreference, readConcern, actualClientSession, session == null && actualClientSession != null); try { if (session != null && session.hasActiveTransaction() && !binding.getReadPreference().equals(primary())) { throw new MongoClientException("Read preference in a transaction must be primary"); } return operation.execute(binding); } catch (MongoException e) { labelException(session, e); throw e; } finally { binding.release(); } }
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { try { validateReadConcernAndCollation(connection, binding.getSessionContext().getReadConcern(), collation); return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), wrapInExplainIfNecessary(getCommand(binding.getSessionContext())), CommandResultDocumentCodec.create(decoder, FIRST_BATCH), connection, transformer(source, connection)); validateReadConcernAndCollation(connection, binding.getSessionContext().getReadConcern(), collation); QueryResult<T> queryResult = connection.query(namespace, asDocument(connection.getDescription(), binding.getReadPreference()), projection, skip, limit, batchSize, isSlaveOk() || binding.getReadPreference().isSlaveOk(), isTailableCursor(), isAwaitData(),
static <D, T> T executeWrappedCommandProtocol(final ReadBinding binding, final String database, final BsonDocument command, final Decoder<D> decoder, final CommandTransformer<D, T> transformer) { ConnectionSource source = binding.getReadConnectionSource(); try { return transformer.apply(executeWrappedCommandProtocol(database, command, decoder, source, binding.getReadPreference()), source.getServerDescription().getAddress()); } finally { source.release(); } }
@Override public void close() { wrapped.close(); binding.release(); }
@Override public BsonDocument call(final Connection connection) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { return executeWrappedCommandProtocol(binding, "admin", new BsonDocument("currentOp", new BsonInt32(1)), connection); } else { return connection.query(new MongoNamespace("admin", "$cmd.sys.inprog"), new BsonDocument(), null, 0, 1, 0, binding.getReadPreference().isSlaveOk(), false, false, false, false, false, new BsonDocumentCodec()) .getResults().get(0); } } });
@Override public MapReduceBatchCursor<T> call(final ConnectionSource source, final Connection connection) { validateReadConcernAndCollation(connection, binding.getSessionContext().getReadConcern(), collation); return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), getCommand(binding.getSessionContext()), CommandResultDocumentCodec.create(decoder, "results"), connection, transformer(source, connection)); } });
static <T> T withConnection(final ReadBinding binding, final CallableWithConnectionAndSource<T> callable) { ConnectionSource source = binding.getReadConnectionSource(); try { return withConnectionSource(source, callable); } finally { source.release(); } }