private void ProcessInsertMessage(RequestMessage message, Queue <RequestMessage> messageQueue, ConnectionId connectionId, InsertMessageBinaryEncoder <RawBsonDocument> encoder, Stopwatch stopwatch) { var commandName = "insert"; var operationId = EventContext.OperationId; var requestId = message.RequestId; var expectedResponseType = ExpectedResponseType.None; int numberOfDocuments = 0; int gleRequestId; WriteConcern writeConcern; if (TryGetWriteConcernFromGLE(messageQueue, out gleRequestId, out writeConcern)) { requestId = gleRequestId; expectedResponseType = ExpectedResponseType.GLE; } if (_startedEvent != null) { // InsertMessage is generic, and we don't know the generic type... // Plus, for this we really want BsonDocuments, not whatever the generic type is. var decodedMessage = encoder.ReadMessage(); var documents = decodedMessage.DocumentSource.GetBatchItems(); numberOfDocuments = documents.Count; try { var command = new BsonDocument { { commandName, decodedMessage.CollectionNamespace.CollectionName }, { "documents", new BsonArray(documents) }, { "ordered", !decodedMessage.ContinueOnError } }; if (writeConcern == null) { command["writeConcern"] = WriteConcern.Unacknowledged.ToBsonDocument(); } else if (!writeConcern.IsServerDefault) { command["writeConcern"] = writeConcern.ToBsonDocument(); } var @event = new CommandStartedEvent( commandName, command, decodedMessage.CollectionNamespace.DatabaseNamespace, operationId, requestId, connectionId); _startedEvent(@event); } finally { foreach (var document in documents) { document.Dispose(); } } } if (_shouldTrackState) { _state.TryAdd(requestId, new CommandState { CommandName = commandName, OperationId = operationId, Stopwatch = stopwatch, ExpectedResponseType = expectedResponseType, NumberOfInsertedDocuments = numberOfDocuments }); } }
private void ProcessInsertMessage(RequestMessage message, Queue<RequestMessage> messageQueue, ConnectionId connectionId, InsertMessageBinaryEncoder<RawBsonDocument> encoder, Stopwatch stopwatch) { var commandName = "insert"; var operationId = EventContext.OperationId; var requestId = message.RequestId; var expectedResponseType = ExpectedResponseType.None; int numberOfDocuments = 0; int gleRequestId; WriteConcern writeConcern; if (TryGetWriteConcernFromGLE(messageQueue, out gleRequestId, out writeConcern)) { requestId = gleRequestId; expectedResponseType = ExpectedResponseType.GLE; } if (_startedEvent != null) { // InsertMessage is generic, and we don't know the generic type... // Plus, for this we really want BsonDocuments, not whatever the generic type is. var decodedMessage = encoder.ReadMessage(); var documents = decodedMessage.DocumentSource.GetRemainingItems().ToList(); numberOfDocuments = documents.Count; try { var command = new BsonDocument { { commandName, decodedMessage.CollectionNamespace.CollectionName }, { "documents", new BsonArray(documents) }, { "ordered", !decodedMessage.ContinueOnError } }; if (writeConcern == null) { command["writeConcern"] = WriteConcern.Unacknowledged.ToBsonDocument(); } else if (!writeConcern.IsServerDefault) { command["writeConcern"] = writeConcern.ToBsonDocument(); } var @event = new CommandStartedEvent( commandName, command, decodedMessage.CollectionNamespace.DatabaseNamespace, operationId, requestId, connectionId); _startedEvent(@event); } finally { documents.ForEach(d => d.Dispose()); } } if (_shouldTrackState) { _state.TryAdd(requestId, new CommandState { CommandName = commandName, OperationId = operationId, Stopwatch = stopwatch, ExpectedResponseType = expectedResponseType, NumberOfInsertedDocuments = numberOfDocuments }); } }
public void ReadMessage_should_decode_flags_correctly(int flags, bool continueOnError) { var bytes = (byte[])__testMessageBytes.Clone(); bytes[__flagsOffset] = (byte)flags; using (var stream = new MemoryStream(bytes)) { var subject = new InsertMessageBinaryEncoder<BsonDocument>(stream, __messageEncoderSettings, __serializer); var message = subject.ReadMessage(); message.ContinueOnError.Should().Be(continueOnError); } }
public void ReadMessage_should_read_a_message() { using (var stream = new MemoryStream(__testMessageBytes)) { var subject = new InsertMessageBinaryEncoder<BsonDocument>(stream, __messageEncoderSettings, __serializer); var message = subject.ReadMessage(); message.CollectionNamespace.Should().Be(__collectionNamespace); message.ContinueOnError.Should().Be(__continueOnError); message.DocumentSource.Batch.Should().Equal(__documentSource.Batch); message.MaxBatchCount.Should().Be(0); message.MaxMessageSize.Should().Be(0); message.RequestId.Should().Be(__requestId); message.Serializer.Should().BeSameAs(__serializer); } }
public void ReadMessage_should_throw_if_binaryReader_was_not_provided() { using (var stream = new MemoryStream()) using (var binaryWriter = new BsonBinaryWriter(stream)) { var subject = new InsertMessageBinaryEncoder<BsonDocument>(null, binaryWriter, __serializer); Action action = () => subject.ReadMessage(); action.ShouldThrow<InvalidOperationException>(); } }