private CursorBatch <TDocument> GetNextBatch(CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation(_operationId))
         using (var channel = _channelSource.GetChannel(cancellationToken))
         {
             if (Feature.FindCommand.IsSupported(channel.ConnectionDescription.ServerVersion))
             {
                 return(ExecuteGetMoreCommand(channel, cancellationToken));
             }
             else
             {
                 return(ExecuteGetMoreProtocol(channel, cancellationToken));
             }
         }
 }
 /// <inheritdoc/>
 public async Task <BulkWriteOperationResult> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
         using (var context = await RetryableWriteContext.CreateAsync(binding, _retryRequested, cancellationToken).ConfigureAwait(false))
         {
             EnsureHintIsSupportedIfAnyRequestHasHint();
             context.DisableRetriesIfAnyWriteRequestIsNotRetryable(_requests);
             var helper = new BatchHelper(_requests, _isOrdered, _writeConcern);
             foreach (var batch in helper.GetBatches())
             {
                 batch.Result = await ExecuteBatchAsync(context, batch, cancellationToken).ConfigureAwait(false);
             }
             return(helper.GetFinalResultOrThrow(context.Channel.ConnectionDescription.ConnectionId));
         }
 }
 private async Task <CursorBatch <TDocument> > GetNextBatchAsync(CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation(_operationId))
         using (var channel = await _channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
         {
             if (SupportedFeatures.IsFindCommandSupported(channel.ConnectionDescription.ServerVersion))
             {
                 return(await ExecuteGetMoreCommandAsync(channel, cancellationToken).ConfigureAwait(false));
             }
             else
             {
                 return(await ExecuteGetMoreProtocolAsync(channel, cancellationToken).ConfigureAwait(false));
             }
         }
 }
Beispiel #4
0
 /// <inheritdoc/>
 public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         if (_writeConcern.IsAcknowledged)
         {
             var emulator = CreateEmulator();
             return(emulator.Execute(context, cancellationToken));
         }
         else
         {
             return(ExecuteProtocol(context.Channel, cancellationToken));
         }
     }
 }
Beispiel #5
0
 /// <inheritdoc/>
 public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
         using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
         {
             if (channelSource.ServerDescription.Version >= __serverVersionSupportingCreateIndexesCommand)
             {
                 return(await ExecuteUsingCommandAsync(channelSource, cancellationToken).ConfigureAwait(false));
             }
             else
             {
                 return(await ExecuteUsingInsertAsync(channelSource, cancellationToken).ConfigureAwait(false));
             }
         }
 }
Beispiel #6
0
        /// <inheritdoc/>
        public async Task <IReadOnlyList <IAsyncCursor <TDocument> > > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                    using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                        using (var channelBinding = new ChannelReadBinding(channelSource.Server, channel, binding.ReadPreference))
                        {
                            var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                            var result    = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);

                            return(CreateCursors(channelSource, operation.Command, result));
                        }
        }
Beispiel #7
0
 /// <inheritdoc/>
 public async Task <WriteConcernResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         if (_writeConcern.IsAcknowledged)
         {
             var emulator = CreateEmulator();
             return(await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false));
         }
         else
         {
             return(await ExecuteProtocolAsync(context.Channel, cancellationToken).ConfigureAwait(false));
         }
     }
 }
 public async Task <BulkWriteOperationResult> ExecuteAsync(IChannelHandle channel, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         if (SupportedFeatures.AreWriteCommandsSupported(channel.ConnectionDescription.ServerVersion))
         {
             return(await ExecuteBatchesAsync(channel, cancellationToken).ConfigureAwait(false));
         }
         else
         {
             var emulator = CreateEmulator();
             return(await emulator.ExecuteAsync(channel, cancellationToken).ConfigureAwait(false));
         }
     }
 }
 // public methods
 public BulkWriteOperationResult Execute(IChannelHandle channel, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         if (SupportedFeatures.AreWriteCommandsSupported(channel.ConnectionDescription.ServerVersion))
         {
             return(ExecuteBatches(channel, cancellationToken));
         }
         else
         {
             var emulator = CreateEmulator();
             return(emulator.Execute(channel, cancellationToken));
         }
     }
 }
 /// <inheritdoc/>
 public async Task <WriteConcernResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
         {
             var emulator = CreateEmulator();
             return(await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false));
         }
         else
         {
             return(await ExecuteProtocolAsync(context.Channel, cancellationToken).ConfigureAwait(false));
         }
     }
 }
Beispiel #11
0
        /// <inheritdoc/>
        public IAsyncCursor <TDocument> Execute(RetryableReadContext context, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(context, nameof(context));

            using (EventContext.BeginOperation())
                using (EventContext.BeginFind(_batchSize, _limit))
                {
                    var readPreference    = context.Binding.ReadPreference;
                    var serverDescription = context.ChannelSource.ServerDescription;
                    var wrappedQuery      = CreateWrappedQuery(serverDescription.Type, readPreference, out var secondaryOk);

                    var batch = ExecuteProtocol(context.Channel, wrappedQuery, secondaryOk, cancellationToken);
                    return(CreateCursor(context.ChannelSource, context.Channel, wrappedQuery, batch));
                }
        }
        // methods
        /// <inheritdoc/>
        public IAsyncCursor <TResult> Execute(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));
            EnsureIsReadOnlyPipeline();

            using (EventContext.BeginOperation())
                using (var channelSource = binding.GetReadChannelSource(cancellationToken))
                    using (var channel = channelSource.GetChannel(cancellationToken))
                        using (var channelBinding = new ChannelReadBinding(channelSource.Server, channel, binding.ReadPreference))
                        {
                            var operation = CreateOperation(channel);
                            var result    = operation.Execute(channelBinding, cancellationToken);
                            return(CreateCursor(channelSource, channel, operation.Command, result));
                        }
        }
        /// <inheritdoc/>
        public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                    using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                        {
                            foreach (var createIndexRequest in _requests)
                            {
                                var operation = CreateOperation(channel.ConnectionDescription.ServerVersion, createIndexRequest);
                                await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);
                            }

                            return(new BsonDocument("ok", 1));
                        }
        }
Beispiel #14
0
        /// <inheritdoc/>
        public async Task <IAsyncCursor <TResult> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));
            EnsureIsReadOnlyPipeline();

            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                    using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                        using (var channelBinding = new ChannelReadBinding(channelSource.Server, channel, binding.ReadPreference, binding.Session.Fork()))
                        {
                            var operation = CreateOperation(channel, channelBinding);
                            var result    = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);

                            return(CreateCursor(channelSource, channel, operation.Command, result));
                        }
        }
        // public methods
        /// <inheritdoc/>
        public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (EventContext.BeginOperation())
                using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                    using (var channel = channelSource.GetChannel(cancellationToken))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                        {
                            foreach (var createIndexRequest in _requests)
                            {
                                var operation = CreateOperation(channel.ConnectionDescription.ServerVersion, createIndexRequest);
                                operation.Execute(channelBinding, cancellationToken);
                            }

                            return(new BsonDocument("ok", 1));
                        }
        }
        /// <inheritdoc/>
        public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(context, nameof(context));

            using (EventContext.BeginOperation())
            {
                if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
                {
                    var emulator = CreateEmulator();
                    return(emulator.Execute(context, cancellationToken));
                }
                else
                {
                    return(ExecuteProtocol(context.Channel, cancellationToken));
                }
            }
        }
Beispiel #17
0
        // methods
        /// <inheritdoc/>
        public async Task <IAsyncCursor <BsonDocument> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                {
                    if (channelSource.ServerDescription.Version >= __versionSupportingListCollectionsCommand)
                    {
                        return(await ExecuteUsingCommandAsync(channelSource, binding.ReadPreference, cancellationToken).ConfigureAwait(false));
                    }
                    else
                    {
                        return(await ExecuteUsingQueryAsync(channelSource, binding.ReadPreference, cancellationToken).ConfigureAwait(false));
                    }
                }
        }
 private void KillCursor(long cursorId, CancellationToken cancellationToken)
 {
     try
     {
         using (EventContext.BeginOperation(_operationId))
             using (EventContext.BeginKillCursors(_collectionNamespace))
                 using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                     using (var channel = _channelSource.GetChannel(cancellationTokenSource.Token))
                     {
                         ExecuteKillCursorsProtocol(channel, cancellationToken);
                     }
     }
     catch
     {
         // ignore exceptions
     }
 }
 private void KillCursors(CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation(_operationId))
         using (EventContext.BeginKillCursors(_collectionNamespace))
             using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                 using (var channel = _channelSource.GetChannel(cancellationTokenSource.Token))
                 {
                     if (Feature.KillCursorsCommand.IsSupported(channel.ConnectionDescription.ServerVersion))
                     {
                         ExecuteKillCursorsCommand(channel, cancellationToken);
                     }
                     else
                     {
                         ExecuteKillCursorsProtocol(channel, cancellationToken);
                     }
                 }
 }
 private async Task KillCursorsAsync(CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation(_operationId))
         using (EventContext.BeginKillCursors(_collectionNamespace))
             using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                 using (var channel = await _channelSource.GetChannelAsync(cancellationTokenSource.Token).ConfigureAwait(false))
                 {
                     if (Feature.KillCursorsCommand.IsSupported(channel.ConnectionDescription.ServerVersion))
                     {
                         await ExecuteKillCursorsCommandAsync(channel, cancellationToken).ConfigureAwait(false);
                     }
                     else
                     {
                         await ExecuteKillCursorsProtocolAsync(channel, cancellationToken).ConfigureAwait(false);
                     }
                 }
 }
Beispiel #21
0
        /// <inheritdoc/>
        public async Task <IAsyncCursor <TDocument> > ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(context, nameof(context));

            using (EventContext.BeginOperation())
                using (EventContext.BeginFind(_batchSize, _limit))
                {
                    var readPreference    = context.Binding.ReadPreference;
                    var serverDescription = context.ChannelSource.ServerDescription;
                    var wrappedQuery      = CreateWrappedQuery(serverDescription.Type, readPreference);
                    var slaveOk           = readPreference != null && readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary;

                    var batch = await ExecuteProtocolAsync(context.Channel, wrappedQuery, slaveOk, cancellationToken).ConfigureAwait(false);

                    return(CreateCursor(context.ChannelSource, wrappedQuery, batch));
                }
        }
Beispiel #22
0
 private async Task KillCursorAsync(long cursorId, CancellationToken cancellationToken)
 {
     try
     {
         using (EventContext.BeginOperation(_operationId))
             using (EventContext.BeginKillCursors(_collectionNamespace))
                 using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                     using (var channel = await _channelSource.GetChannelAsync(cancellationTokenSource.Token).ConfigureAwait(false))
                     {
                         await ExecuteKillCursorsProtocolAsync(channel, cancellationToken).ConfigureAwait(false);
                     }
     }
     catch
     {
         // ignore exceptions
     }
 }
Beispiel #23
0
        /// <inheritdoc/>
        public async Task <IReadOnlyList <IAsyncCursor <TDocument> > > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                {
                    var command   = CreateCommand();
                    var operation = new ReadCommandOperation <BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
                    var result    = await operation.ExecuteAsync(channelSource, binding.ReadPreference, cancellationToken).ConfigureAwait(false);

                    var cursors = new List <AsyncCursor <TDocument> >();

                    foreach (var cursorDocument in result["cursors"].AsBsonArray.Select(v => v["cursor"].AsBsonDocument))
                    {
                        var cursorId   = cursorDocument["id"].ToInt64();
                        var firstBatch = cursorDocument["firstBatch"].AsBsonArray.Select(v =>
                        {
                            var bsonDocument = (BsonDocument)v;
                            using (var reader = new BsonDocumentReader(bsonDocument))
                            {
                                var context  = BsonDeserializationContext.CreateRoot(reader);
                                var document = _serializer.Deserialize(context);
                                return(document);
                            }
                        })
                                         .ToList();

                        var cursor = new AsyncCursor <TDocument>(
                            channelSource.Fork(),
                            _collectionNamespace,
                            command,
                            firstBatch,
                            cursorId,
                            _batchSize ?? 0,
                            0, // limit
                            _serializer,
                            _messageEncoderSettings);

                        cursors.Add(cursor);
                    }

                    return(cursors);
                }
        }
Beispiel #24
0
        // methods
        /// <inheritdoc/>
        public async Task <IAsyncCursor <TResult> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));
            EnsureIsReadOnlyPipeline();

            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                {
                    var command = CreateCommand(channelSource.ServerDescription.Version);

                    var serializer = new AggregateResultDeserializer(_resultSerializer);
                    var operation  = new ReadCommandOperation <AggregateResult>(CollectionNamespace.DatabaseNamespace, command, serializer, MessageEncoderSettings);

                    var result = await operation.ExecuteAsync(channelSource, binding.ReadPreference, cancellationToken).ConfigureAwait(false);

                    return(CreateCursor(channelSource, command, result));
                }
        }
Beispiel #25
0
        /// <inheritdoc/>
        public async Task <WriteConcernResult> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                    using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                    {
                        if (SupportedFeatures.AreWriteCommandsSupported(channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
                        {
                            var emulator = CreateEmulator();
                            return(await emulator.ExecuteAsync(channel, cancellationToken).ConfigureAwait(false));
                        }
                        else
                        {
                            return(await ExecuteProtocolAsync(channel, cancellationToken).ConfigureAwait(false));
                        }
                    }
        }
Beispiel #26
0
        // public methods
        /// <inheritdoc/>
        public WriteConcernResult Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                    using (var channel = channelSource.GetChannel(cancellationToken))
                    {
                        if (SupportedFeatures.AreWriteCommandsSupported(channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
                        {
                            var emulator = CreateEmulator();
                            return(emulator.Execute(channel, cancellationToken));
                        }
                        else
                        {
                            return(ExecuteProtocol(channel, cancellationToken));
                        }
                    }
        }
        /// <inheritdoc/>
        public IAsyncCursor <TDocument> Execute(IReadBinding binding, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = binding.GetReadChannelSource(cancellationToken))
                    using (var channel = channelSource.GetChannel(cancellationToken))
                        using (var channelBinding = new ChannelReadBinding(channelSource.Server, channel, binding.ReadPreference, binding.Session.Fork()))
                        {
                            var readPreference = binding.ReadPreference;

                            using (EventContext.BeginFind(_batchSize, _limit))
                            {
                                var operation     = CreateOperation(channel, channelBinding);
                                var commandResult = operation.Execute(channelBinding, cancellationToken);
                                return(CreateCursor(channelSource, commandResult));
                            }
                        }
        }
Beispiel #28
0
        // public methods
        /// <inheritdoc/>
        public IEnumerable <WriteConcernResult> Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var context = RetryableWriteContext.Create(binding, false, cancellationToken))
                {
                    if (_writeConcern.IsAcknowledged)
                    {
                        var emulator = CreateEmulator();
                        var result   = emulator.Execute(context, cancellationToken);
                        return(new[] { result });
                    }
                    else
                    {
                        return(InsertBatches(context.Channel, cancellationToken));
                    }
                }
        }
        public BinaryConnection_CommandEventTests()
        {
            _capturedEvents = new EventCapturer()
                              .Capture <CommandStartedEvent>()
                              .Capture <CommandSucceededEvent>()
                              .Capture <CommandFailedEvent>();

            _mockStreamFactory = new Mock <IStreamFactory>();

            _endPoint = new DnsEndPoint("localhost", 27017);
            var serverId = new ServerId(new ClusterId(), _endPoint);

            _mockConnectionInitializer = new Mock <IConnectionInitializer>();
            _mockConnectionInitializer.Setup(i => i.SendHelloAsync(It.IsAny <IConnection>(), CancellationToken.None))
            .Returns(() => Task.FromResult(new ConnectionDescription(
                                               new ConnectionId(serverId),
                                               new HelloResult(new BsonDocument {
                { "maxWireVersion", WireVersion.Server36 }
            }))));
            _mockConnectionInitializer.Setup(i => i.AuthenticateAsync(It.IsAny <IConnection>(), It.IsAny <ConnectionDescription>(), CancellationToken.None))
            .Returns(() => Task.FromResult(new ConnectionDescription(
                                               new ConnectionId(serverId),
                                               new HelloResult(new BsonDocument {
                { "maxWireVersion", WireVersion.Server36 }
            }))));

            _subject = new BinaryConnection(
                serverId: serverId,
                endPoint: _endPoint,
                settings: new ConnectionSettings(),
                streamFactory: _mockStreamFactory.Object,
                connectionInitializer: _mockConnectionInitializer.Object,
                eventSubscriber: _capturedEvents);

            _stream = new BlockingMemoryStream();
            _mockStreamFactory.Setup(f => f.CreateStreamAsync(_endPoint, CancellationToken.None))
            .Returns(Task.FromResult <Stream>(_stream));
            _subject.OpenAsync(CancellationToken.None).Wait();
            _capturedEvents.Clear();

            _operationIdDisposer = EventContext.BeginOperation();
        }
Beispiel #30
0
        /// <inheritdoc/>
        public IAsyncCursor <TDocument> Execute(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var channelSource = binding.GetReadChannelSource(cancellationToken))
                    using (var channel = channelSource.GetChannel(cancellationToken))
                    {
                        var readPreference    = binding.ReadPreference;
                        var serverDescription = channelSource.ServerDescription;
                        var wrappedQuery      = CreateWrappedQuery(serverDescription.Type, readPreference);
                        var slaveOk           = readPreference != null && readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary;

                        using (EventContext.BeginFind(_batchSize, _limit))
                        {
                            var batch = ExecuteProtocol(channel, wrappedQuery, slaveOk, cancellationToken);
                            return(CreateCursor(channelSource, wrappedQuery, batch));
                        }
                    }
        }