diff --git a/src/MongoDB.Driver/Core/Bindings/IChannel.cs b/src/MongoDB.Driver/Core/Bindings/IChannel.cs index f58ba48103e..275d6cdebbc 100644 --- a/src/MongoDB.Driver/Core/Bindings/IChannel.cs +++ b/src/MongoDB.Driver/Core/Bindings/IChannel.cs @@ -59,74 +59,6 @@ Task CommandAsync( IBsonSerializer resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken); - - CursorBatch Query( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken); - - [Obsolete("Use an overload that does not have an oplogReplay parameter instead.")] - CursorBatch Query( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool oplogReplay, // obsolete: OplogReplay is ignored by server versions 4.4.0 and newer - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken); - - Task> QueryAsync( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken); - - [Obsolete("Use an overload that does not have an oplogReplay parameter instead.")] - Task> QueryAsync( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool oplogReplay, // obsolete: OplogReplay is ignored by server versions 4.4.0 and newer - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken); } internal interface IChannelHandle : IChannel diff --git a/src/MongoDB.Driver/Core/Servers/Server.cs b/src/MongoDB.Driver/Core/Servers/Server.cs index fa93175f1c0..c7fe1a94b28 100644 --- a/src/MongoDB.Driver/Core/Servers/Server.cs +++ b/src/MongoDB.Driver/Core/Servers/Server.cs @@ -379,176 +379,11 @@ public void Dispose() } } - public CursorBatch Query( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken) - { -#pragma warning disable 618 - return Query( - collectionNamespace, - query, - fields, - queryValidator, - skip, - batchSize, - secondaryOk, - partialOk, - noCursorTimeout, - oplogReplay: false, - tailableCursor, - awaitData, - serializer, - messageEncoderSettings, - cancellationToken); -#pragma warning restore 618 - } - - [Obsolete("Use the newest overload instead.")] - public CursorBatch Query( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool oplogReplay, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken) - { - secondaryOk = GetEffectiveSecondaryOk(secondaryOk); -#pragma warning disable 618 - var protocol = new QueryWireProtocol( - collectionNamespace, - query, - fields, - queryValidator, - skip, - batchSize, - secondaryOk, - partialOk, - noCursorTimeout, - oplogReplay, - tailableCursor, - awaitData, - serializer, - messageEncoderSettings); -#pragma warning restore 618 - - return ExecuteProtocol(protocol, cancellationToken); - } - - public Task> QueryAsync( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken) - { -#pragma warning disable 618 - return QueryAsync( - collectionNamespace, - query, - fields, - queryValidator, - skip, - batchSize, - secondaryOk, - partialOk, - noCursorTimeout, - oplogReplay: false, - tailableCursor, - awaitData, - serializer, - messageEncoderSettings, - cancellationToken); -#pragma warning restore 618 - } - - [Obsolete("Use the newest overload instead.")] - public Task> QueryAsync( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool oplogReplay, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings, - CancellationToken cancellationToken) - { - secondaryOk = GetEffectiveSecondaryOk(secondaryOk); -#pragma warning disable 618 - var protocol = new QueryWireProtocol( - collectionNamespace, - query, - fields, - queryValidator, - skip, - batchSize, - secondaryOk, - partialOk, - noCursorTimeout, - oplogReplay, - tailableCursor, - awaitData, - serializer, - messageEncoderSettings); -#pragma warning restore 618 - - return ExecuteProtocolAsync(protocol, cancellationToken); - } - private ICoreSession CreateClusterClockAdvancingCoreSession(ICoreSession session) { return new ClusterClockAdvancingCoreSession(session, _server.ClusterClock); } - private TResult ExecuteProtocol(IWireProtocol protocol, CancellationToken cancellationToken) - { - try - { - return protocol.Execute(_connection, cancellationToken); - } - catch (Exception ex) - { - _server.HandleChannelException(_connection, ex); - throw; - } - } - private TResult ExecuteProtocol(IWireProtocol protocol, ICoreSession session, CancellationToken cancellationToken) { try @@ -563,19 +398,6 @@ private TResult ExecuteProtocol(IWireProtocol protocol, ICoreS } } - private async Task ExecuteProtocolAsync(IWireProtocol protocol, CancellationToken cancellationToken) - { - try - { - return await protocol.ExecuteAsync(_connection, cancellationToken).ConfigureAwait(false); - } - catch (Exception ex) - { - _server.HandleChannelException(_connection, ex); - throw; - } - } - private async Task ExecuteProtocolAsync(IWireProtocol protocol, ICoreSession session, CancellationToken cancellationToken) { try @@ -597,16 +419,6 @@ public IChannelHandle Fork() return new ServerChannel(_server, _connection.Fork(), false); } - private bool GetEffectiveSecondaryOk(bool secondaryOk) - { - if (_server.DirectConnection && _server.Description.Type != ServerType.ShardRouter) - { - return true; - } - - return secondaryOk; - } - private void MarkSessionDirtyIfNeeded(ICoreSession session, Exception ex) { if (ex is MongoConnectionException) diff --git a/src/MongoDB.Driver/Core/WireProtocol/QueryWireProtocol.cs b/src/MongoDB.Driver/Core/WireProtocol/QueryWireProtocol.cs deleted file mode 100644 index 405fb21608f..00000000000 --- a/src/MongoDB.Driver/Core/WireProtocol/QueryWireProtocol.cs +++ /dev/null @@ -1,182 +0,0 @@ -/* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Bson; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization; -using MongoDB.Driver.Core.Connections; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.WireProtocol -{ - internal sealed class QueryWireProtocol : IWireProtocol> - { - // fields - private readonly bool _awaitData; - private readonly int _batchSize; - private readonly CollectionNamespace _collectionNamespace; - private readonly MessageEncoderSettings _messageEncoderSettings; - private readonly BsonDocument _fields; - private readonly bool _noCursorTimeout; - private readonly bool _oplogReplay; - private readonly bool _partialOk; - private readonly BsonDocument _query; - private readonly IElementNameValidator _queryValidator; - private readonly IBsonSerializer _serializer; - private readonly int _skip; - private readonly bool _secondaryOk; - private readonly bool _tailableCursor; - - // constructors - public QueryWireProtocol( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings) -#pragma warning disable 618 - : this( - collectionNamespace, - query, - fields, - queryValidator, - skip, - batchSize, - secondaryOk, - partialOk, - noCursorTimeout, - oplogReplay: false, - tailableCursor, - awaitData, - serializer, - messageEncoderSettings) -#pragma warning restore 618 - { - } - - [Obsolete("Use a constructor that does not have an oplogReplay parameter instead.")] - public QueryWireProtocol( - CollectionNamespace collectionNamespace, - BsonDocument query, - BsonDocument fields, - IElementNameValidator queryValidator, - int skip, - int batchSize, - bool secondaryOk, - bool partialOk, - bool noCursorTimeout, - bool oplogReplay, // obsolete: OplogReplay is ignored by server versions 4.4.0 and newer - bool tailableCursor, - bool awaitData, - IBsonSerializer serializer, - MessageEncoderSettings messageEncoderSettings) - { - _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); - _query = Ensure.IsNotNull(query, nameof(query)); - _fields = fields; // can be null - _queryValidator = Ensure.IsNotNull(queryValidator, nameof(queryValidator)); - _skip = Ensure.IsGreaterThanOrEqualToZero(skip, nameof(skip)); - _batchSize = batchSize; // can be negative - _secondaryOk = secondaryOk; - _partialOk = partialOk; - _noCursorTimeout = noCursorTimeout; - _oplogReplay = oplogReplay; - _tailableCursor = tailableCursor; - _awaitData = awaitData; - _serializer = Ensure.IsNotNull(serializer, nameof(serializer)); - _messageEncoderSettings = messageEncoderSettings; - } - - // public properties - public bool MoreToCome => false; - - // methods - private QueryMessage CreateMessage() - { -#pragma warning disable 618 - return new QueryMessage( - RequestMessage.GetNextRequestId(), - _collectionNamespace, - _query, - _fields, - _queryValidator, - _skip, - _batchSize, - _secondaryOk, - _partialOk, - _noCursorTimeout, - _oplogReplay, - _tailableCursor, - _awaitData); -#pragma warning restore 618 - } - - public CursorBatch Execute(IConnection connection, CancellationToken cancellationToken) - { - var message = CreateMessage(); - connection.SendMessage(message, _messageEncoderSettings, cancellationToken); - var encoderSelector = new ReplyMessageEncoderSelector(_serializer); - var reply = connection.ReceiveMessage(message.RequestId, encoderSelector, _messageEncoderSettings, cancellationToken); - return ProcessReply(connection.ConnectionId, (ReplyMessage)reply); - } - - public async Task> ExecuteAsync(IConnection connection, CancellationToken cancellationToken) - { - var message = CreateMessage(); - await connection.SendMessageAsync(message, _messageEncoderSettings, cancellationToken).ConfigureAwait(false); - var encoderSelector = new ReplyMessageEncoderSelector(_serializer); - var reply = await connection.ReceiveMessageAsync(message.RequestId, encoderSelector, _messageEncoderSettings, cancellationToken).ConfigureAwait(false); - return ProcessReply(connection.ConnectionId, (ReplyMessage)reply); - } - - private CursorBatch ProcessReply(ConnectionId connectionId, ReplyMessage reply) - { - if (reply.QueryFailure) - { - var response = reply.QueryFailureDocument; - - var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, _query, response, "$err"); - if (notPrimaryOrNodeIsRecoveringException != null) - { - throw notPrimaryOrNodeIsRecoveringException; - } - - var mappedException = ExceptionMapper.Map(connectionId, response); - if (mappedException != null) - { - throw mappedException; - } - - var message = string.Format("QueryFailure flag was true (response was {0}).", response.ToJson()); - throw new MongoQueryException(connectionId, message, _query, response); - } - - return new CursorBatch(reply.CursorId, reply.Documents); - } - } -}