Esempio n. 1
0
        private TResult ProcessCommandResult(ConnectionId connectionId, RawBsonDocument rawBsonDocument)
        {
            var binaryReaderSettings = new BsonBinaryReaderSettings
            {
                Encoding           = _messageEncoderSettings.GetOrDefault <UTF8Encoding>(MessageEncoderSettingsName.ReadEncoding, Utf8Encodings.Strict),
                GuidRepresentation = _messageEncoderSettings.GetOrDefault <GuidRepresentation>(MessageEncoderSettingsName.GuidRepresentation, GuidRepresentation.CSharpLegacy)
            };

            BsonValue writeConcernError;

            if (rawBsonDocument.TryGetValue("writeConcernError", out writeConcernError))
            {
                var message            = writeConcernError["errmsg"].AsString;
                var response           = rawBsonDocument.Materialize(binaryReaderSettings);
                var writeConcernResult = new WriteConcernResult(response);
                throw new MongoWriteConcernException(connectionId, message, writeConcernResult);
            }

            using (var stream = new ByteBufferStream(rawBsonDocument.Slice, ownsBuffer: false))
                using (var reader = new BsonBinaryReader(stream, binaryReaderSettings))
                {
                    var context = BsonDeserializationContext.CreateRoot(reader);
                    return(_resultSerializer.Deserialize(context));
                }
        }
        protected virtual async Task <BulkWriteBatchResult> EmulateSingleRequestAsync(IConnectionHandle connection, WriteRequest request, int originalIndex, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var protocol = CreateProtocol(connection, request);

            WriteConcernResult    writeConcernResult    = null;
            WriteConcernException writeConcernException = null;

            try
            {
                writeConcernResult = await protocol.ExecuteAsync(connection, timeout, cancellationToken);
            }
            catch (WriteConcernException ex)
            {
                writeConcernResult    = ex.WriteConcernResult;
                writeConcernException = ex;
            }

            var indexMap = new IndexMap.RangeBased(0, originalIndex, 1);

            return(BulkWriteBatchResult.Create(
                       request,
                       writeConcernResult,
                       writeConcernException,
                       indexMap));
        }
 public void AddResult(WriteConcernResult result)
 {
     if (_results != null)
     {
         _results.Add(result);
     }
 }
Esempio n. 4
0
        public static void ThrowIfHasWriteConcernError(ConnectionId connectionId, BsonDocument result)
        {
            BsonValue value;

            if (result.TryGetValue("writeConcernError", out value))
            {
                var message            = (string)value.AsBsonDocument.GetValue("errmsg", null);
                var writeConcernResult = new WriteConcernResult(result);
                throw new MongoWriteConcernException(connectionId, message, writeConcernResult);
            }
        }
Esempio n. 5
0
        private async Task <BulkWriteBatchResult> EmulateSingleRequestAsync(IChannelHandle channel, TWriteRequest request, int originalIndex, CancellationToken cancellationToken)
        {
            WriteConcernResult         writeConcernResult    = null;
            MongoWriteConcernException writeConcernException = null;

            try
            {
                writeConcernResult = await ExecuteProtocolAsync(channel, request, cancellationToken).ConfigureAwait(false);
            }
            catch (MongoWriteConcernException ex)
            {
                writeConcernResult    = ex.WriteConcernResult;
                writeConcernException = ex;
            }

            return(CreateSingleRequestResult(request, originalIndex, writeConcernResult, writeConcernException));
        }
Esempio n. 6
0
        // private methods
        private BulkWriteBatchResult EmulateSingleRequest(IChannelHandle channel, TWriteRequest request, int originalIndex, CancellationToken cancellationToken)
        {
            WriteConcernResult         writeConcernResult    = null;
            MongoWriteConcernException writeConcernException = null;

            try
            {
                writeConcernResult = ExecuteProtocol(channel, request, cancellationToken);
            }
            catch (MongoWriteConcernException ex)
            {
                writeConcernResult    = ex.WriteConcernResult;
                writeConcernException = ex;
            }

            return(CreateSingleRequestResult(request, originalIndex, writeConcernResult, writeConcernException));
        }
Esempio n. 7
0
        // methods
        protected virtual async Task <BulkWriteBatchResult> EmulateSingleRequestAsync(IChannelHandle channel, WriteRequest request, int originalIndex, CancellationToken cancellationToken)
        {
            WriteConcernResult         writeConcernResult    = null;
            MongoWriteConcernException writeConcernException = null;

            try
            {
                writeConcernResult = await ExecuteProtocolAsync(channel, request, cancellationToken).ConfigureAwait(false);
            }
            catch (MongoWriteConcernException ex)
            {
                writeConcernResult    = ex.WriteConcernResult;
                writeConcernException = ex;
            }

            var indexMap = new IndexMap.RangeBased(0, originalIndex, 1);

            return(BulkWriteBatchResult.Create(
                       request,
                       writeConcernResult,
                       writeConcernException,
                       indexMap));
        }
        public static BulkWriteBatchResult Create(
            WriteRequest request,
            WriteConcernResult writeConcernResult,
            MongoWriteConcernException writeConcernException,
            IndexMap indexMap)
        {
            var       processedRequests   = new[] { request };
            var       unprocessedRequests = __noWriteRequests;
            BsonValue upsertId            = null;
            var       documentsAffected   = 0L;

            if (writeConcernResult != null)
            {
                upsertId          = writeConcernResult.Upserted;
                documentsAffected = writeConcernResult.DocumentsAffected;
                var updateRequest = request as UpdateRequest;

                if (upsertId == null &&
                    documentsAffected == 1 &&
                    updateRequest != null &&
                    updateRequest.IsUpsert &&
                    !writeConcernResult.UpdatedExisting)
                {
                    // Get the _id field first from the Update document
                    // and then from the Query document.
                    upsertId =
                        updateRequest.Update.ToBsonDocument().GetValue("_id", null) ??
                        updateRequest.Filter.ToBsonDocument().GetValue("_id", null);
                }
            }

            var upserts     = (upsertId == null) ? __noUpserts : new[] { new BulkWriteOperationUpsert(0, upsertId) };
            var writeErrors = __noWriteErrors;
            BulkWriteConcernError writeConcernError = null;

            if (writeConcernException != null)
            {
                var getLastErrorResponse = writeConcernResult.Response;
                if (IsGetLasterrorResponseAWriteConcernError(getLastErrorResponse))
                {
                    writeConcernError = CreateWriteConcernErrorFromGetLastErrorResponse(getLastErrorResponse);
                }
                else
                {
                    writeErrors = new[] { CreateWriteErrorFromGetLastErrorResponse(getLastErrorResponse) };
                }
            }

            if (request.RequestType == WriteRequestType.Insert && writeErrors.Count == 0)
            {
                documentsAffected = 1; // note: DocumentsAffected is 0 for inserts
            }

            var  matchedCount  = 0L;
            var  deletedCount  = 0L;
            var  insertedCount = 0L;
            long?modifiedCount = 0L;

            switch (request.RequestType)
            {
            case WriteRequestType.Delete:
                deletedCount = documentsAffected;
                break;

            case WriteRequestType.Insert:
                insertedCount = documentsAffected;
                break;

            case WriteRequestType.Update:
                matchedCount  = documentsAffected - upserts.Count();
                modifiedCount = null;     // getLasterror does not report this value
                break;
            }

            return(new BulkWriteBatchResult(
                       1, // batchCount
                       processedRequests,
                       unprocessedRequests,
                       matchedCount,
                       deletedCount,
                       insertedCount,
                       modifiedCount,
                       upserts,
                       writeErrors,
                       writeConcernError,
                       indexMap));
        }
Esempio n. 9
0
        private BulkWriteBatchResult CreateSingleRequestResult(TWriteRequest request, int originalIndex, WriteConcernResult writeConcernResult, MongoWriteConcernException writeConcernException)
        {
            var indexMap = new IndexMap.RangeBased(0, originalIndex, 1);

            return(BulkWriteBatchResult.Create(
                       request,
                       writeConcernResult,
                       writeConcernException,
                       indexMap));
        }