// constructor
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageJsonEncoderBase"/> class.
 /// </summary>
 /// <param name="textReader">The text reader.</param>
 /// <param name="textWriter">The text writer.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 protected MessageJsonEncoderBase(TextReader textReader, TextWriter textWriter, MessageEncoderSettings encoderSettings)
 {
     Ensure.That(textReader != null || textWriter != null, "textReader and textWriter cannot both be null.");
     _textReader = textReader;
     _textWriter = textWriter;
     _encoderSettings = encoderSettings;
 }
 // constructors
 public BulkDeleteOperationEmulator(
     CollectionNamespace collectionNamespace,
     IEnumerable<DeleteRequest> requests,
     MessageEncoderSettings messageEncoderSettings)
     : base(collectionNamespace, requests, messageEncoderSettings)
 {
 }
 public void Setup()
 {
     _collectionNamespace = new CollectionNamespace("foo", "bar");
     _fieldName = "a.b";
     _messageEncoderSettings = new MessageEncoderSettings();
     _valueSerializer = new Int32Serializer();
 }
 // constructors
 public BulkUpdateOperation(
     CollectionNamespace collectionNamespace,
     IEnumerable<UpdateRequest> requests,
     MessageEncoderSettings messageEncoderSettings)
     : base(collectionNamespace, requests, messageEncoderSettings)
 {
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MapReduceOperationBase"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="mapFunction">The map function.</param>
 /// <param name="reduceFunction">The reduce function.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 protected MapReduceOperationBase(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _mapFunction = Ensure.IsNotNull(mapFunction, nameof(mapFunction));
     _reduceFunction = Ensure.IsNotNull(reduceFunction, nameof(reduceFunction));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
 // constructors
 protected MapReduceOperationBase(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, BsonDocument query, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _mapFunction = Ensure.IsNotNull(mapFunction, "mapFunction");
     _reduceFunction = Ensure.IsNotNull(reduceFunction, "reduceFunction");
     _query = query;
     _messageEncoderSettings = messageEncoderSettings;
 }
 // constructors
 protected AggregateOperationBase(
     CollectionNamespace collectionNamespace,
     IEnumerable<BsonDocument> pipeline,
     MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _pipeline = Ensure.IsNotNull(pipeline, "pipeline").ToList();
     _messageEncoderSettings = messageEncoderSettings;
 }
 // constructors
 public UpdateOpcodeOperationEmulator(
     CollectionNamespace collectionNamespace,
     UpdateRequest request,
     MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _request = Ensure.IsNotNull(request, nameof(request));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
 // constructors
 public DeleteOpcodeOperationEmulator(
     CollectionNamespace collectionNamespace,
     BsonDocument criteria,
     MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _criteria = Ensure.IsNotNull(criteria, "criteria");
     _messageEncoderSettings = messageEncoderSettings;
 }
 // constructors
 public UpdateOpcodeOperationEmulator(
     CollectionNamespace collectionNamespace,
     UpdateRequest request,
     MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _request = Ensure.IsNotNull(request, "request");
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, "messageEncoderSettings");
 }
 // constructors
 protected BulkUnmixedWriteOperationEmulatorBase(
     CollectionNamespace collectionNamespace,
     IEnumerable<WriteRequest> requests,
     MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _requests = Ensure.IsNotNull(requests, nameof(requests));
     _messageEncoderSettings = messageEncoderSettings;
 }
 // constructors
 public UpdateOpcodeOperationEmulator(
     CollectionNamespace collectionNamespace,
     BsonDocument query,
     BsonDocument update,
     MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _criteria = Ensure.IsNotNull(query, "criteria");
     _update = Ensure.IsNotNull(update, "update");
     _messageEncoderSettings = messageEncoderSettings;
 }
 // constructors
 public DeleteWireProtocol(
     CollectionNamespace collectionNamespace,
     BsonDocument query,
     bool isMulti,
     MessageEncoderSettings messageEncoderSettings,
     WriteConcern writeConcern)
     : base(collectionNamespace, messageEncoderSettings, writeConcern)
 {
     _query = Ensure.IsNotNull(query, "query");
     _isMulti = isMulti;
 }
Esempio n. 14
0
        public void constructor_with_key_should_throw_when_initial_is_null()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key = new BsonDocument("key", 1);
            var reduceFunction = new BsonJavaScript("reduceFunction");
            var filter = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();

            Action action = () => new GroupOperation<BsonDocument>(collectionNamespace, key, null, reduceFunction, filter, messageEncoderSettings);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void DeserializeBatch_should_return_expected_result_when_batch_is_empty()
        {
            var document = BsonDocument.Parse("{ batch : [ ] }");
            var bson = document.ToBson();
            var rawDocument = new RawBsonDocument(bson);
            var batch = (RawBsonArray)rawDocument["batch"];
            var documentSerializer = BsonDocumentSerializer.Instance;
            var messageEncoderSettings = new MessageEncoderSettings();

            var result = CursorBatchDeserializationHelper.DeserializeBatch<BsonDocument>(batch, documentSerializer, messageEncoderSettings);

            result.Count.Should().Be(0);
        }
 // constructors
 public UpdateWireProtocol(
     CollectionNamespace collectionNamespace,
     MessageEncoderSettings messageEncoderSettings,
     WriteConcern writeConcern,
     BsonDocument query,
     BsonDocument update,
     IElementNameValidator updateValidator,
     bool isMulti,
     bool isUpsert)
     : base(collectionNamespace, messageEncoderSettings, writeConcern)
 {
     _updateValidator = Ensure.IsNotNull(updateValidator, nameof(updateValidator));
     _query = Ensure.IsNotNull(query, nameof(query));
     _update = Ensure.IsNotNull(update, nameof(update));
     _isMulti = isMulti;
     _isUpsert = isUpsert;
 }
        public void constructor_should_initialize_instance()
        {
            var channelSource = Substitute.For<IChannelSource>();
            var databaseNamespace = new DatabaseNamespace("test");
            var collectionNamespace = new CollectionNamespace(databaseNamespace, "test");
            var query = new BsonDocument("x", 1);
            var firstBatch = new BsonDocument[] { new BsonDocument("y", 2) };
            var cursorId = 1L;
            var batchSize = 2;
            var limit = 3;
            var serializer = BsonDocumentSerializer.Instance;
            var messageEncoderSettings = new MessageEncoderSettings();
            var maxTime = TimeSpan.FromSeconds(1);

            var result = new AsyncCursor<BsonDocument>(
                channelSource,
                collectionNamespace,
                query,
                firstBatch,
                cursorId,
                batchSize,
                limit,
                serializer,
                messageEncoderSettings,
                maxTime);

            var reflector = new Reflector(result);
            reflector.BatchSize.Should().Be(batchSize);
            reflector.ChannelSource.Should().Be(channelSource);
            reflector.CollectionNamespace.Should().Be(collectionNamespace);
            reflector.Count.Should().Be(firstBatch.Length);
            reflector.CurrentBatch.Should().BeNull();
            reflector.CursorId.Should().Be(cursorId);
            reflector.Disposed.Should().BeFalse();
            reflector.FirstBatch.Should().Equal(firstBatch);
            reflector.Limit.Should().Be(limit);
            reflector.MaxTime.Should().Be(maxTime);
            reflector.MessageEncoderSettings.Should().BeEquivalentTo(messageEncoderSettings);
            reflector.Query.Should().Be(query);
            reflector.Serializer.Should().Be(serializer);
        }
Esempio n. 18
0
        public void constructor_with_key_should_initialize_subject()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key = new BsonDocument("key", 1);
            var initial = new BsonDocument("x", 1);
            var reduceFunction = new BsonJavaScript("reduceFunction");
            var filter = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();

            var subject = new GroupOperation<BsonDocument>(collectionNamespace, key, initial, reduceFunction, filter, messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(collectionNamespace);
            subject.Filter.Should().Be(filter);
            subject.FinalizeFunction.Should().BeNull();
            subject.Initial.Should().Be(initial);
            subject.Key.Should().Be(key);
            subject.KeyFunction.Should().BeNull();
            subject.MaxTime.Should().Be(default(TimeSpan?));
            Assert.Equal(messageEncoderSettings, subject.MessageEncoderSettings);
            subject.ReduceFunction.Should().Be(reduceFunction);
            subject.ResultSerializer.Should().BeNull();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonMessageEncoderFactory"/> class.
 /// </summary>
 /// <param name="textWriter">The text writer.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 public JsonMessageEncoderFactory(TextWriter textWriter, MessageEncoderSettings encoderSettings)
     : this(null, Ensure.IsNotNull(textWriter, nameof(textWriter)), encoderSettings)
 {
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="FindOneAndDeleteOperation{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="filter">The filter.</param>
 /// <param name="resultSerializer">The result serializer.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public FindOneAndDeleteOperation(CollectionNamespace collectionNamespace, BsonDocument filter, IBsonSerializer <TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
     : base(collectionNamespace, resultSerializer, messageEncoderSettings)
 {
     _filter = Ensure.IsNotNull(filter, nameof(filter));
 }
 public EvalOperationTests()
 {
     _adminDatabaseNamespace = DatabaseNamespace.Admin;
     _messageEncoderSettings = CoreTestConfiguration.MessageEncoderSettings;
 }
 public Task SendMessagesAsync(IEnumerable <RequestMessage> messages, MessageEncoderSettings messageEncoderSettings, TimeSpan timeout, CancellationToken cancellationToken)
 {
     _sentMessages.AddRange(messages);
     return(Task.FromResult <object>(null));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonMessageEncoderFactory"/> class.
 /// </summary>
 /// <param name="textReader">The text reader.</param>
 /// <param name="textWriter">The text writer.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 public JsonMessageEncoderFactory(TextReader textReader, TextWriter textWriter, MessageEncoderSettings encoderSettings)
 {
     Ensure.That(textReader != null || textWriter != null, "textReader and textWriter cannot both be null.");
     _textReader      = textReader;
     _textWriter      = textWriter;
     _encoderSettings = encoderSettings;
 }
Esempio n. 24
0
        public void BeforeSending(IEnumerable <RequestMessage> messages, ConnectionId connectionId, IByteBuffer buffer, MessageEncoderSettings encoderSettings, Stopwatch stopwatch)
        {
            using (var stream = new ByteBufferStream(buffer, ownsBuffer: false))
            {
                var messageQueue = new Queue <RequestMessage>(messages);

                while (messageQueue.Count > 0)
                {
                    ProcessRequestMessages(messageQueue, connectionId, stream, encoderSettings, stopwatch);
                }
            }
        }
Esempio n. 25
0
        private void ProcessReplyMessage(CommandState state, ResponseMessage message, IByteBuffer buffer, ConnectionId connectionId, MessageEncoderSettings encoderSettings)
        {
            state.Stopwatch.Stop();
            bool disposeOfDocuments = false;
            var  replyMessage       = message as ReplyMessage <RawBsonDocument>;

            if (replyMessage == null)
            {
                // ReplyMessage is generic, which means that we can't use it here, so, we need to use a different one...
                using (var stream = new ByteBufferStream(buffer, ownsBuffer: false))
                {
                    var encoderFactory = new BinaryMessageEncoderFactory(stream, encoderSettings);
                    replyMessage = (ReplyMessage <RawBsonDocument>)encoderFactory
                                   .GetReplyMessageEncoder(RawBsonDocumentSerializer.Instance)
                                   .ReadMessage();
                    disposeOfDocuments = true;
                }
            }

            try
            {
                if (replyMessage.CursorNotFound ||
                    replyMessage.QueryFailure ||
                    (state.ExpectedResponseType != ExpectedResponseType.Query && replyMessage.Documents.Count == 0))
                {
                    var queryFailureDocument = replyMessage.QueryFailureDocument;
                    if (__securitySensitiveCommands.Contains(state.CommandName))
                    {
                        queryFailureDocument = new BsonDocument();
                    }
                    if (_failedEvent != null)
                    {
                        _failedEvent(new CommandFailedEvent(
                                         state.CommandName,
                                         new MongoCommandException(
                                             connectionId,
                                             string.Format("{0} command failed", state.CommandName),
                                             null,
                                             queryFailureDocument),
                                         state.OperationId,
                                         replyMessage.ResponseTo,
                                         connectionId,
                                         state.Stopwatch.Elapsed));
                    }
                }
                else
                {
                    switch (state.ExpectedResponseType)
                    {
                    case ExpectedResponseType.Command:
                        ProcessCommandReplyMessage(state, replyMessage, connectionId);
                        break;

                    case ExpectedResponseType.GLE:
                        ProcessGLEReplyMessage(state, replyMessage, connectionId);
                        break;

                    case ExpectedResponseType.Query:
                        ProcessQueryReplyMessage(state, replyMessage, connectionId);
                        break;
                    }
                }
            }
            finally
            {
                if (disposeOfDocuments && replyMessage.Documents != null)
                {
                    replyMessage.Documents.ForEach(d => d.Dispose());
                }
            }
        }
 // constructors
 public DistinctOperation(CollectionNamespace collectionNamespace, IBsonSerializer <TValue> valueSerializer, string fieldName, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace    = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _valueSerializer        = Ensure.IsNotNull(valueSerializer, "valueSerializer");
     _fieldName              = Ensure.IsNotNullOrEmpty(fieldName, "fieldName");
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, "messageEncoderSettings");
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GeoNearOperation{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="near">The point for which to find the closest documents.</param>
 /// <param name="resultSerializer">The result serializer.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public GeoNearOperation(CollectionNamespace collectionNamespace, BsonValue near, IBsonSerializer <TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _near                   = Ensure.IsNotNull(near, nameof(near));
     _resultSerializer       = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="GroupOperation{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="key">The key.</param>
 /// <param name="initial">The initial aggregation result for each group.</param>
 /// <param name="reduceFunction">The reduce function.</param>
 /// <param name="filter">The filter.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public GroupOperation(CollectionNamespace collectionNamespace, BsonDocument key, BsonDocument initial, BsonJavaScript reduceFunction, BsonDocument filter, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _key                    = Ensure.IsNotNull(key, nameof(key));
     _initial                = Ensure.IsNotNull(initial, nameof(initial));
     _reduceFunction         = Ensure.IsNotNull(reduceFunction, nameof(reduceFunction));
     _filter                 = filter; // can be null
     _messageEncoderSettings = messageEncoderSettings;
 }
Esempio n. 29
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="AggregateExplainOperation"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="pipeline">The pipeline.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public AggregateExplainOperation(CollectionNamespace collectionNamespace, IEnumerable <BsonDocument> pipeline, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace    = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _pipeline               = Ensure.IsNotNull(pipeline, nameof(pipeline)).ToList();
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
Esempio n. 30
0
 // constructor
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageBinaryEncoderBase"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 protected MessageBinaryEncoderBase(Stream stream, MessageEncoderSettings encoderSettings)
 {
     _stream          = Ensure.IsNotNull(stream, "stream");
     _encoderSettings = encoderSettings;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="CountOperation"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public CountOperation(CollectionNamespace collectionNamespace, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace    = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
Esempio n. 32
0
 // constructors
 public KillCursorsMessageBinaryEncoder(Stream stream, MessageEncoderSettings encoderSettings)
     : base(stream, encoderSettings)
 {
 }
 // constructor
 protected MessageJsonEncoderBase(TextReader textReader, TextWriter textWriter, MessageEncoderSettings encoderSettings)
 {
     Ensure.That(textReader != null || textWriter != null, "textReader and textWriter cannot both be null.");
     _textReader      = textReader;
     _textWriter      = textWriter;
     _encoderSettings = encoderSettings;
 }
Esempio n. 34
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="FindAndModifyOperationBase{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="resultSerializer">The result serializer.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public FindAndModifyOperationBase(CollectionNamespace collectionNamespace, IBsonSerializer <TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace    = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _resultSerializer       = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
Esempio n. 35
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ExplainOperation"/> class.
 /// </summary>
 /// <param name="databaseNamespace">The database namespace.</param>
 /// <param name="command">The command.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public ExplainOperation(DatabaseNamespace databaseNamespace, BsonDocument command, MessageEncoderSettings messageEncoderSettings)
 {
     _databaseNamespace      = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
     _command                = Ensure.IsNotNull(command, nameof(command));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
     _verbosity              = ExplainVerbosity.QueryPlanner;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="PingOperation"/> class.
 /// </summary>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public PingOperation(MessageEncoderSettings messageEncoderSettings)
 {
     _messageEncoderSettings = messageEncoderSettings;
 }
 public Task <ReplyMessage <TDocument> > ReceiveMessageAsync <TDocument>(int responseTo, Bson.Serialization.IBsonSerializer <TDocument> serializer, MessageEncoderSettings messageEncoderSettings, TimeSpan timeout, CancellationToken cancellationToken)
 {
     return(Task.FromResult <ReplyMessage <TDocument> >((ReplyMessage <TDocument>)_replyMessages.Dequeue()));
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="KillCursorsMessageJsonEncoder"/> class.
 /// </summary>
 /// <param name="textReader">The text reader.</param>
 /// <param name="textWriter">The text writer.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 public KillCursorsMessageJsonEncoder(TextReader textReader, TextWriter textWriter, MessageEncoderSettings encoderSettings)
     : base(textReader, textWriter, encoderSettings)
 {
 }
Esempio n. 39
0
        private void ProcessCommandResponseMessage(CommandState state, CommandResponseMessage message, IByteBuffer buffer, ConnectionId connectionId, MessageEncoderSettings encoderSettings)
        {
            var wrappedMessage = message.WrappedMessage;
            var type0Section   = wrappedMessage.Sections.OfType <Type0CommandMessageSection <RawBsonDocument> >().Single();
            var reply          = (BsonDocument)type0Section.Document;

            BsonValue ok;

            if (!reply.TryGetValue("ok", out ok))
            {
                // this is a degenerate case with the server and
                // we don't really know what to do here...
                return;
            }

            if (__securitySensitiveCommands.Contains(state.CommandName))
            {
                reply = new BsonDocument();
            }

            if (ok.ToBoolean())
            {
                if (_succeededEvent != null)
                {
                    _succeededEvent(new CommandSucceededEvent(
                                        state.CommandName,
                                        reply,
                                        state.OperationId,
                                        message.ResponseTo,
                                        connectionId,
                                        state.Stopwatch.Elapsed));
                }
            }
            else
            {
                if (_failedEvent != null)
                {
                    _failedEvent(new CommandFailedEvent(
                                     state.CommandName,
                                     new MongoCommandException(
                                         connectionId,
                                         string.Format("{0} command failed", state.CommandName),
                                         null,
                                         reply),
                                     state.OperationId,
                                     message.ResponseTo,
                                     connectionId,
                                     state.Stopwatch.Elapsed));
                }
            }
        }
        public async Task<ResponseMessage> ReceiveMessageAsync(
            int responseTo,
            IMessageEncoderSelector encoderSelector,
            MessageEncoderSettings messageEncoderSettings,
            CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(encoderSelector, nameof(encoderSelector));
            ThrowIfDisposedOrNotOpen();

            var helper = new ReceiveMessageHelper(this, responseTo, messageEncoderSettings);
            try
            {
                helper.ReceivingMessage();
                using (var buffer = await ReceiveBufferAsync(responseTo, cancellationToken).ConfigureAwait(false))
                {
                    var message = helper.DecodeMessage(buffer, encoderSelector, cancellationToken);
                    helper.ReceivedMessage(buffer, message);
                    return message;
                }
            }
            catch (Exception ex)
            {
                helper.FailedReceivingMessage(ex);
                throw;
            }
        }
Esempio n. 41
0
 public CreateCollectionOperationTests()
 {
     _collectionNamespace    = CoreTestConfiguration.GetCollectionNamespaceForTestClass(typeof(CreateCollectionOperationTests));
     _messageEncoderSettings = CoreTestConfiguration.MessageEncoderSettings;
 }
 public ReceiveMessageHelper(BinaryConnection connection, int responseTo, MessageEncoderSettings messageEncoderSettings)
 {
     _connection = connection;
     _responseTo = responseTo;
     _messageEncoderSettings = messageEncoderSettings;
 }
Esempio n. 43
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="InsertMessageJsonEncoder{TDocument}"/> class.
 /// </summary>
 /// <param name="textReader">The text reader.</param>
 /// <param name="textWriter">The text writer.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 /// <param name="serializer">The serializer.</param>
 public InsertMessageJsonEncoder(TextReader textReader, TextWriter textWriter, MessageEncoderSettings encoderSettings, IBsonSerializer <TDocument> serializer)
     : base(textReader, textWriter, encoderSettings)
 {
     _serializer = Ensure.IsNotNull(serializer, "serializer");
 }
Esempio n. 44
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="FindOneAndReplaceOperation{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="filter">The filter.</param>
 /// <param name="replacement">The replacement.</param>
 /// <param name="resultSerializer">The result serializer.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public FindOneAndReplaceOperation(CollectionNamespace collectionNamespace, BsonDocument filter, BsonDocument replacement, IBsonSerializer <TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
     : base(collectionNamespace, resultSerializer, messageEncoderSettings)
 {
     _filter         = Ensure.IsNotNull(filter, nameof(filter));
     _replacement    = Ensure.IsNotNull(replacement, nameof(replacement));
     _returnDocument = ReturnDocument.Before;
 }
        public void MessageEncoderSettings_get_should_return_expected_result(
            [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.Standard)]
            GuidRepresentation guidRepresentation)
        {
            var messageEncoderSettings = new MessageEncoderSettings { { "GuidRepresentation", guidRepresentation } };
            var subject = new FindOpcodeOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, messageEncoderSettings);

            var result = subject.MessageEncoderSettings;

            result.Should().BeEquivalentTo(messageEncoderSettings);
        }
Esempio n. 46
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="UpdateMessageBinaryEncoder"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 public UpdateMessageBinaryEncoder(Stream stream, MessageEncoderSettings encoderSettings)
     : base(stream, encoderSettings)
 {
 }
 // constructors
 public KillCursorsWireProtocol(IEnumerable<long> cursorIds, MessageEncoderSettings messageEncoderSettings)
 {
     Ensure.IsNotNull(cursorIds, "cursorIds");
     _cursorIds = (cursorIds as IReadOnlyList<long>) ?? cursorIds.ToList();
     _messageEncoderSettings = messageEncoderSettings;
 }
 public void TestFixtureSetUp()
 {
     _collectionNamespace = CoreTestConfiguration.GetCollectionNamespaceForTestFixture();
     _messageEncoderSettings = CoreTestConfiguration.MessageEncoderSettings;
 }
        public async Task SendMessagesAsync(IEnumerable<RequestMessage> messages, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(messages, nameof(messages));
            ThrowIfDisposedOrNotOpen();

            var helper = new SendMessagesHelper(this, messages, messageEncoderSettings);
            try
            {
                helper.EncodingMessages();
                using (var buffer = helper.EncodeMessages(cancellationToken))
                {
                    helper.SendingMessages(buffer);
                    await SendBufferAsync(buffer, cancellationToken).ConfigureAwait(false);
                    helper.SentMessages(buffer.Length);
                }
            }
            catch (Exception ex)
            {
                helper.FailedSendingMessages(ex);
                throw;
            }
        }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="DeleteMessageJsonEncoder"/> class.
 /// </summary>
 /// <param name="textReader">The text reader.</param>
 /// <param name="textWriter">The text writer.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 public DeleteMessageJsonEncoder(TextReader textReader, TextWriter textWriter, MessageEncoderSettings encoderSettings)
     : base(textReader, textWriter, encoderSettings)
 {
 }
            public SendMessagesHelper(BinaryConnection connection, IEnumerable<RequestMessage> messages, MessageEncoderSettings messageEncoderSettings)
            {
                _connection = connection;
                _messages = messages.ToList();
                _messageEncoderSettings = messageEncoderSettings;

                _commandStopwatch = Stopwatch.StartNew();
                _requestIds = new Lazy<List<int>>(() => _messages.Select(m => m.RequestId).ToList());
            }
Esempio n. 52
0
        private void ProcessRequestMessages(Queue <RequestMessage> messageQueue, ConnectionId connectionId, Stream stream, MessageEncoderSettings encoderSettings, Stopwatch stopwatch)
        {
            var message = messageQueue.Dequeue();

            switch (message.MessageType)
            {
            case MongoDBMessageType.Command:
                ProcessCommandRequestMessage((CommandRequestMessage)message, messageQueue, connectionId, new CommandMessageBinaryEncoder(stream, encoderSettings), stopwatch);
                break;

            case MongoDBMessageType.Delete:
                ProcessDeleteMessage((DeleteMessage)message, messageQueue, connectionId, new DeleteMessageBinaryEncoder(stream, encoderSettings), stopwatch);
                break;

            case MongoDBMessageType.GetMore:
                ProcessGetMoreMessage((GetMoreMessage)message, connectionId, stopwatch);
                break;

            case MongoDBMessageType.Insert:
                ProcessInsertMessage(message, messageQueue, connectionId, new InsertMessageBinaryEncoder <RawBsonDocument>(stream, encoderSettings, RawBsonDocumentSerializer.Instance), stopwatch);
                break;

            case MongoDBMessageType.KillCursors:
                ProcessKillCursorsMessages((KillCursorsMessage)message, connectionId, stopwatch);
                break;

            case MongoDBMessageType.Query:
                ProcessQueryMessage((QueryMessage)message, connectionId, new QueryMessageBinaryEncoder(stream, encoderSettings), stopwatch);
                break;

            case MongoDBMessageType.Update:
                ProcessUpdateMessage((UpdateMessage)message, messageQueue, connectionId, new UpdateMessageBinaryEncoder(stream, encoderSettings), stopwatch);
                break;

            default:
                throw new MongoInternalException("Invalid message type.");
            }
        }
 public virtual void TestFixtureSetUp()
 {
     _adminDatabaseNamespace = new DatabaseNamespace("admin");
     _messageEncoderSettings = CoreTestConfiguration.MessageEncoderSettings;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MapReduceOperationBase"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="mapFunction">The map function.</param>
 /// <param name="reduceFunction">The reduce function.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 protected MapReduceOperationBase(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace    = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _mapFunction            = Ensure.IsNotNull(mapFunction, "mapFunction");
     _reduceFunction         = Ensure.IsNotNull(reduceFunction, "reduceFunction");
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, "messageEncoderSettings");
 }
Esempio n. 55
0
 public GroupOperation(CollectionNamespace collectionNamespace, BsonJavaScript keyFunction, BsonDocument initial, BsonJavaScript reduceFunction, BsonDocument filter, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _keyFunction         = Ensure.IsNotNull(keyFunction, "keyFunction");
     _initial             = Ensure.IsNotNull(initial, "initial");
     _reduceFunction      = Ensure.IsNotNull(reduceFunction, "reduceFunction");
     _filter = filter;
     _messageEncoderSettings = messageEncoderSettings;
 }
 public void TestFixtureSetUp()
 {
     _collectionNamespace    = CoreTestConfiguration.GetCollectionNamespaceForTestFixture();
     _messageEncoderSettings = CoreTestConfiguration.MessageEncoderSettings;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="UpdateMessageBinaryEncoder"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="encoderSettings">The encoder settings.</param>
 public UpdateMessageBinaryEncoder(Stream stream, MessageEncoderSettings encoderSettings)
     : base(stream, encoderSettings)
 {
 }
 // constructors
 public CurrentOpOperation(DatabaseNamespace databaseNamespace, MessageEncoderSettings messageEncoderSettings)
 {
     _databaseNamespace      = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
     _messageEncoderSettings = messageEncoderSettings;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="AggregateOperation{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="pipeline">The pipeline.</param>
 /// <param name="resultSerializer">The result value serializer.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public AggregateOperation(CollectionNamespace collectionNamespace, IEnumerable <BsonDocument> pipeline, IBsonSerializer <TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace    = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _pipeline               = Ensure.IsNotNull(pipeline, nameof(pipeline)).ToList();
     _resultSerializer       = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
Esempio n. 60
0
        public void AfterReceiving(ResponseMessage message, IByteBuffer buffer, ConnectionId connectionId, MessageEncoderSettings encoderSettings)
        {
            CommandState state;

            if (!_state.TryRemove(message.ResponseTo, out state))
            {
                // this indicates a bug in the sending portion...
                return;
            }

            if (message is CommandResponseMessage)
            {
                ProcessCommandResponseMessage(state, (CommandResponseMessage)message, buffer, connectionId, encoderSettings);
            }
            else
            {
                ProcessReplyMessage(state, message, buffer, connectionId, encoderSettings);
            }
        }