// methods public void Add(WriteRequest request, int originalIndex) { var index = _requests.Count; _indexMap = _indexMap.Add(index, originalIndex); _requests.Add(request); }
public async Task ExecuteAsync_with_mixed_requests_and_ordered_is_true() { var requests = new WriteRequest[] { new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 3}"), BsonDocument.Parse("{$set: {y: 1}}")), new DeleteRequest(new BsonDocument("_id", 2)), new InsertRequest(new BsonDocument("_id", 7)), new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 4}"), BsonDocument.Parse("{$set: {y: 2}}")) }; var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings) { IsOrdered = true }; var result = await ExecuteOperationAsync(subject); result.DeletedCount.Should().Be(1); result.InsertedCount.Should().Be(1); if (result.IsModifiedCountAvailable) { result.ModifiedCount.Should().Be(1); } result.MatchedCount.Should().Be(1); result.ProcessedRequests.Should().HaveCount(4); result.RequestCount.Should().Be(4); result.Upserts.Should().HaveCount(0); var list = await ReadAllFromCollectionAsync(); list.Should().HaveCount(6); }
// methods protected override IWireProtocol<WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var deleteRequest = (DeleteRequest)request; return new DeleteWireProtocol( CollectionNamespace, deleteRequest.Criteria, false, // isMulti MessageEncoderSettings, WriteConcern); }
private BulkWriteBatchResult CreateSingleRequestResult(WriteRequest request, int originalIndex, WriteConcernResult writeConcernResult, MongoWriteConcernException writeConcernException) { var indexMap = new IndexMap.RangeBased(0, originalIndex, 1); return(BulkWriteBatchResult.Create( request, writeConcernResult, writeConcernException, indexMap)); }
// methods protected override IWireProtocol<BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var deleteRequest = (DeleteRequest)request; return new DeleteWireProtocol( DatabaseName, CollectionName, WriteConcern, deleteRequest.Query, isMulti: false); }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var deleteRequest = (DeleteRequest)request; var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(MaxDocumentSize); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); BsonSerializer.Serialize(bsonWriter, deleteRequest.Query); bsonWriter.WriteInt32("limit", deleteRequest.Limit); bsonWriter.WriteEndDocument(); bsonWriter.PopMaxDocumentSize(); }
// methods protected override IWireProtocol<BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var updateRequest = (UpdateRequest)request; return new UpdateWireProtocol( DatabaseName, CollectionName, WriteConcern, updateRequest.Query, updateRequest.Update, updateRequest.IsMultiUpdate ?? false, updateRequest.IsUpsert ?? false); }
protected override Task<WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var deleteRequest = (DeleteRequest)request; var isMulti = deleteRequest.Limit == 0; return channel.DeleteAsync( CollectionNamespace, deleteRequest.Filter, isMulti, MessageEncoderSettings, WriteConcern, cancellationToken); }
private bool RequestHasHint(WriteRequest request) { if (request is DeleteRequest deleteRequest) { return(deleteRequest.Hint != null); } if (request is UpdateRequest updateRequest) { return(updateRequest.Hint != null); } return(false); }
private bool IsHintSupportedForRequestWithHint(WriteRequest request, SemanticVersion serverVersion) { if (request is DeleteRequest && (Feature.HintForDeleteOperations.DriverMustThrowIfNotSupported(serverVersion) || !_writeConcern.IsAcknowledged)) { return(false); } if (request is UpdateRequest && (Feature.HintForUpdateAndReplaceOperations.DriverMustThrowIfNotSupported(serverVersion) || !_writeConcern.IsAcknowledged)) { return(false); } return(true); }
// methods protected override IWireProtocol<WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var updateRequest = (UpdateRequest)request; return new UpdateWireProtocol( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Criteria, updateRequest.Update, ElementNameValidatorFactory.ForUpdateType(updateRequest.UpdateType), updateRequest.IsMulti, updateRequest.IsUpsert); }
// methods protected override IWireProtocol<WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var insertRequest = (InsertRequest)request; var documentSource = new BatchableSource<BsonDocument>(new[] { insertRequest.Document }); return new InsertWireProtocol<BsonDocument>( CollectionNamespace, WriteConcern, BsonDocumentSerializer.Instance, MessageEncoderSettings, documentSource, connection.Description.MaxBatchCount, connection.Description.MaxMessageSize, continueOnError: false); }
// methods protected override IWireProtocol<WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var updateRequest = (UpdateRequest)request; var updateValidator = new UpdateOrReplacementElementNameValidator(); return new UpdateWireProtocol( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Criteria, updateRequest.Update, updateValidator, updateRequest.IsMultiUpdate ?? false, updateRequest.IsUpsert ?? false); }
// methods protected override IWireProtocol<BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var insertRequest = (InsertRequest)request; var wrapper = new BsonDocumentWrapper(insertRequest.Document, insertRequest.Serializer); var documentSource = new BatchableSource<BsonDocument>(new[] { wrapper }); return new InsertWireProtocol<BsonDocument>( DatabaseName, CollectionName, WriteConcern, BsonDocumentSerializer.Instance, documentSource, connection.Description.MaxBatchCount, connection.Description.MaxMessageSize, continueOnError: false); }
protected override Task<WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var updateRequest = (UpdateRequest)request; return channel.UpdateAsync( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Filter, updateRequest.Update, ElementNameValidatorFactory.ForUpdateType(updateRequest.UpdateType), updateRequest.IsMulti, updateRequest.IsUpsert, cancellationToken); }
public void Constructor_should_work() { var processedRequests = new WriteRequest[0]; var upserts = new BulkWriteUpsert[0]; var result = new AcknowledgedBulkWriteOperationResult(1, 2, 3, 4, 5, processedRequests, upserts); var writeErrors = new BulkWriteError[0]; var writeConcernError = new WriteConcernError(1, "message", new BsonDocument("x", 1)); var unprocessedRequests = new WriteRequest[0]; var subject = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests); subject.Result.Should().BeSameAs(result); subject.UnprocessedRequests.Should().BeSameAs(unprocessedRequests); subject.WriteConcernError.Should().BeSameAs(writeConcernError); subject.WriteErrors.Should().BeSameAs(writeErrors); }
public void Constructor_should_work() { var processedRequests = new WriteRequest[0]; var upserts = new BulkWriteOperationUpsert[0]; var result = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, processedRequests, upserts); var writeErrors = new BulkWriteOperationError[0]; var writeConcernError = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1)); var unprocessedRequests = new WriteRequest[0]; var subject = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests); subject.Result.Should().BeSameAs(result); subject.UnprocessedRequests.Should().BeSameAs(unprocessedRequests); subject.WriteConcernError.Should().BeSameAs(writeConcernError); subject.WriteErrors.Should().BeSameAs(writeErrors); }
// private methods private BulkWriteBatchResult EmulateSingleRequest(IChannelHandle channel, WriteRequest 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)); }
// methods protected override Task<WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var insertRequest = (InsertRequest)request; var documentSource = new BatchableSource<BsonDocument>(new[] { insertRequest.Document }); return channel.InsertAsync( CollectionNamespace, WriteConcern, BsonDocumentSerializer.Instance, MessageEncoderSettings, documentSource, MaxBatchCount, MaxBatchLength, !IsOrdered, // continueOnError null, // shouldSendGetLastError cancellationToken); }
protected override Task<WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var deleteRequest = (DeleteRequest)request; var isMulti = deleteRequest.Limit == 0; if (deleteRequest.Collation != null) { throw new NotSupportedException("OP_DELETE does not support collations."); } return channel.DeleteAsync( CollectionNamespace, deleteRequest.Filter, isMulti, MessageEncoderSettings, WriteConcern, cancellationToken); }
private bool RequestHasCollation(WriteRequest request) { DeleteRequest deleteRequest; if ((deleteRequest = request as DeleteRequest) != null) { return(deleteRequest.Collation != null); } UpdateRequest updateRequest; if ((updateRequest = request as UpdateRequest) != null) { return(updateRequest.Collation != null); } return(false); }
protected override Task<WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var updateRequest = (UpdateRequest)request; if (updateRequest.Collation != null) { throw new NotSupportedException("BulkUpdateOperationEmulator does not support collations."); } return channel.UpdateAsync( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Filter, updateRequest.Update, ElementNameValidatorFactory.ForUpdateType(updateRequest.UpdateType), updateRequest.IsMulti, updateRequest.IsUpsert, cancellationToken); }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var deleteRequest = (DeleteRequest)request; Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, deleteRequest.Collation); var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(ConnectionDescription.MaxDocumentSize); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); BsonSerializer.Serialize(bsonWriter, deleteRequest.Filter); bsonWriter.WriteInt32("limit", deleteRequest.Limit); if (deleteRequest.Collation != null) { bsonWriter.WriteName("collation"); BsonDocumentSerializer.Instance.Serialize(context, deleteRequest.Collation.ToBsonDocument()); } bsonWriter.WriteEndDocument(); bsonWriter.PopMaxDocumentSize(); }
public void Execute_with_mixed_upserts_and_ordered_is_true( [Values(false, true)] bool async) { var requests = new WriteRequest[] { new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 12}"), BsonDocument.Parse("{$set: {y: 1}}")) { IsUpsert = true }, new DeleteRequest(new BsonDocument("_id", 2)), new InsertRequest(new BsonDocument("_id", 7)), new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 4}"), BsonDocument.Parse("{$set: {y: 2}}")) { IsUpsert = true } }; var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings) { IsOrdered = true }; var result = ExecuteOperation(subject, async); result.DeletedCount.Should().Be(1); result.InsertedCount.Should().Be(1); if (result.IsModifiedCountAvailable) { result.ModifiedCount.Should().Be(0); } result.MatchedCount.Should().Be(0); result.ProcessedRequests.Should().HaveCount(4); result.RequestCount.Should().Be(4); result.Upserts.Should().HaveCount(2); var list = ReadAllFromCollection(async); list.Should().HaveCount(8); }
public void Serialization_should_drop_custom_fields() { var processedRequests = new WriteRequest[0]; var upserts = new BulkWriteOperationUpsert[0]; var result = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, processedRequests, upserts); var writeErrors = new BulkWriteOperationError[0]; var writeConcernError = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1)); var unprocessedRequests = new WriteRequest[0]; var subject = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests); var formatter = new BinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, subject); stream.Position = 0; var rehydrated = (BulkWriteOperationException)formatter.Deserialize(stream); rehydrated.Result.Should().BeNull(); rehydrated.UnprocessedRequests.Should().BeNull(); rehydrated.WriteConcernError.Should().BeNull(); rehydrated.WriteErrors.Should().BeNull(); } }
public void Serialization_should_drop_custom_fields() { var processedRequests = new WriteRequest[0]; var upserts = new BulkWriteUpsert[0]; var result = new AcknowledgedBulkWriteOperationResult(1, 2, 3, 4, 5, processedRequests, upserts); var writeErrors = new BulkWriteError[0]; var writeConcernError = new WriteConcernError(1, "message", new BsonDocument("x", 1)); var unprocessedRequests = new WriteRequest[0]; var subject = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests); var formatter = new BinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, subject); stream.Position = 0; var rehydrated = (BulkWriteOperationException)formatter.Deserialize(stream); rehydrated.Result.Should().BeNull(); rehydrated.UnprocessedRequests.Should().BeNull(); rehydrated.WriteConcernError.Should().BeNull(); rehydrated.WriteErrors.Should().BeNull(); } }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var deleteRequest = (DeleteRequest)request; Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, deleteRequest.Collation); var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(ConnectionDescription.MaxDocumentSize); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); BsonSerializer.Serialize(bsonWriter, deleteRequest.Filter); bsonWriter.WriteInt32("limit", deleteRequest.Limit); if (deleteRequest.Collation != null) { bsonWriter.WriteName("collation"); BsonDocumentSerializer.Instance.Serialize(context, deleteRequest.Collation.ToBsonDocument()); } bsonWriter.WriteEndDocument(); bsonWriter.PopMaxDocumentSize(); }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var insertRequest = (InsertRequest)request; var document = insertRequest.Document; var actualType = document.GetType(); if (_cachedSerializerType != actualType) { _cachedSerializer = BsonSerializer.LookupSerializer(actualType); _cachedSerializerType = actualType; } var serializer = _cachedSerializer; var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(MaxDocumentSize); bsonWriter.PushElementNameValidator(_elementNameValidator); try { serializer.Serialize(context, document); } finally { bsonWriter.PopMaxDocumentSize(); bsonWriter.PopElementNameValidator(); } }
private BulkWriteBatchResult CreateSingleRequestResult(WriteRequest request, int originalIndex, WriteConcernResult writeConcernResult, MongoWriteConcernException writeConcernException) { var indexMap = new IndexMap.RangeBased(0, originalIndex, 1); return BulkWriteBatchResult.Create( request, writeConcernResult, writeConcernException, indexMap); }
// private methods private BulkWriteBatchResult EmulateSingleRequest(IChannelHandle channel, WriteRequest 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); }
// methods protected override Task <WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var deleteRequest = (DeleteRequest)request; var isMulti = deleteRequest.Limit == 0; return(channel.DeleteAsync( CollectionNamespace, deleteRequest.Filter, isMulti, MessageEncoderSettings, WriteConcern, cancellationToken)); }
// methods protected abstract IWireProtocol<BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request);
public void Execute_with_mixed_upserts_and_ordered_is_true( [Values(false, true)] bool async) { var requests = new WriteRequest[] { new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 12}"), BsonDocument.Parse("{$set: {y: 1}}")) { IsUpsert = true }, new DeleteRequest(new BsonDocument("_id", 2)), new InsertRequest(new BsonDocument("_id", 7)), new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 4}"), BsonDocument.Parse("{$set: {y: 2}}")) { IsUpsert = true } }; var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings) { IsOrdered = true }; var result = ExecuteOperation(subject, async); result.DeletedCount.Should().Be(1); result.InsertedCount.Should().Be(1); if (result.IsModifiedCountAvailable) { result.ModifiedCount.Should().Be(0); } result.MatchedCount.Should().Be(0); result.ProcessedRequests.Should().HaveCount(4); result.RequestCount.Should().Be(4); result.Upserts.Should().HaveCount(2); var list = ReadAllFromCollection(async); list.Should().HaveCount(8); }
// 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)); }
protected abstract Task <WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken);
protected override Task <WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var deleteRequest = (DeleteRequest)request; var isMulti = deleteRequest.Limit == 0; if (deleteRequest.Collation != null) { throw new NotSupportedException("OP_DELETE does not support collations."); } return(channel.DeleteAsync( CollectionNamespace, deleteRequest.Filter, isMulti, MessageEncoderSettings, WriteConcern, cancellationToken)); }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var deleteRequest = (DeleteRequest)request; Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, deleteRequest.Collation); var writer = context.Writer; writer.PushSettings(s => { var bs = s as BsonBinaryWriterSettings; if (bs != null) { bs.MaxDocumentSize = ConnectionDescription.MaxDocumentSize; } }); try { writer.WriteStartDocument(); writer.WriteName("q"); BsonSerializer.Serialize(writer, deleteRequest.Filter); writer.WriteInt32("limit", deleteRequest.Limit); if (deleteRequest.Collation != null) { writer.WriteName("collation"); BsonDocumentSerializer.Instance.Serialize(context, deleteRequest.Collation.ToBsonDocument()); } writer.WriteEndDocument(); } finally { writer.PopSettings(); } }
public async Task ExecuteAsync_with_mixed_requests_and_ordered_is_true() { var requests = new WriteRequest[] { new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 3}"), BsonDocument.Parse("{$set: {y: 1}}")), new DeleteRequest(new BsonDocument("_id", 2)), new InsertRequest(new BsonDocument("_id", 7)), new UpdateRequest(UpdateType.Update, BsonDocument.Parse("{x: 4}"), BsonDocument.Parse("{$set: {y: 2}}")) }; var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings) { IsOrdered = true }; var result = await ExecuteOperationAsync(subject); result.DeletedCount.Should().Be(1); result.InsertedCount.Should().Be(1); if (result.IsModifiedCountAvailable) { result.ModifiedCount.Should().Be(1); } result.MatchedCount.Should().Be(1); result.ProcessedRequests.Should().HaveCount(4); result.RequestCount.Should().Be(4); result.Upserts.Should().HaveCount(0); var list = await ReadAllFromCollectionAsync(); list.Should().HaveCount(6); }
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); }
// methods protected override IWireProtocol <WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var insertRequest = (InsertRequest)request; var documentSource = new BatchableSource <BsonDocument>(new[] { insertRequest.Document }); return(new InsertWireProtocol <BsonDocument>( CollectionNamespace, WriteConcern, BsonDocumentSerializer.Instance, MessageEncoderSettings, documentSource, connection.Description.MaxBatchCount, connection.Description.MaxMessageSize, continueOnError: false)); }
// methods protected abstract IWireProtocol <WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request);
// methods public override async Task AbortAsync(CancellationToken cancellationToken = default(CancellationToken)) { if (_aborted) { return; } ThrowIfClosedOrDisposed(); _aborted = true; var chunksCollectionNamespace = _bucket.GetChunksCollectionNamespace(); var filter = new BsonDocument("files_id", _id); var deleteRequest = new DeleteRequest(filter) { Limit = 0 }; var requests = new WriteRequest[] { deleteRequest }; var messageEncoderSettings = _bucket.GetMessageEncoderSettings(); var operation = new BulkMixedWriteOperation(chunksCollectionNamespace, requests, messageEncoderSettings) { WriteConcern = _bucket.Options.WriteConcern }; await operation.ExecuteAsync(_binding, cancellationToken).ConfigureAwait(false); }
// 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); }
protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var updateRequest = (UpdateRequest)request; var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(MaxWireDocumentSize); try { bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); SerializeFilter(bsonWriter, updateRequest.Filter); bsonWriter.WriteName("u"); SerializeUpdate(bsonWriter, updateRequest.Update, updateRequest.UpdateType); if (updateRequest.IsMulti) { bsonWriter.WriteBoolean("multi", updateRequest.IsMulti); } if (updateRequest.IsUpsert) { bsonWriter.WriteBoolean("upsert", updateRequest.IsUpsert); } bsonWriter.WriteEndDocument(); } finally { bsonWriter.PopMaxDocumentSize(); } }
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 { var protocolResult = await protocol.ExecuteAsync(connection, timeout, cancellationToken); if (protocolResult != null) { writeConcernResult = new WriteConcernResult(protocolResult); } } catch (WriteException ex) { writeConcernResult = new WriteConcernResult(ex.Result); writeConcernException = new WriteConcernException(ex.Message, writeConcernResult); } var indexMap = new IndexMap.RangeBased(0, originalIndex, 1); return BulkWriteBatchResult.Create( request, writeConcernResult, writeConcernException, indexMap); }
protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var updateRequest = (UpdateRequest)request; Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, updateRequest.Collation); var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(ConnectionDescription.MaxWireDocumentSize); try { bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); SerializeFilter(bsonWriter, updateRequest.Filter); bsonWriter.WriteName("u"); SerializeUpdate(bsonWriter, updateRequest.Update, updateRequest.UpdateType); if (updateRequest.IsMulti) { bsonWriter.WriteBoolean("multi", updateRequest.IsMulti); } if (updateRequest.IsUpsert) { bsonWriter.WriteBoolean("upsert", updateRequest.IsUpsert); } if (updateRequest.Collation != null) { bsonWriter.WriteName("collation"); BsonDocumentSerializer.Instance.Serialize(context, updateRequest.Collation.ToBsonDocument()); } bsonWriter.WriteEndDocument(); } finally { bsonWriter.PopMaxDocumentSize(); } }
protected abstract Task<WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken);
protected override Task <WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var updateRequest = (UpdateRequest)request; if (updateRequest.Collation != null) { throw new NotSupportedException("BulkUpdateOperationEmulator does not support collations."); } if (updateRequest.ArrayFilters != null) { throw new NotSupportedException("BulkUpdateOperationEmulator does not support arrayFilters."); } return(channel.UpdateAsync( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Filter, updateRequest.Update, ElementNameValidatorFactory.ForUpdateType(updateRequest.UpdateType), updateRequest.IsMulti, updateRequest.IsUpsert, cancellationToken)); }
private 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; } return CreateSingleRequestResult(request, originalIndex, writeConcernResult, writeConcernException); }
// private methods private BulkMixedWriteOperation CreateAbortOperation() { var chunksCollectionNamespace = _bucket.GetChunksCollectionNamespace(); var filter = new BsonDocument("files_id", _id); var deleteRequest = new DeleteRequest(filter) { Limit = 0 }; var requests = new WriteRequest[] { deleteRequest }; var messageEncoderSettings = _bucket.GetMessageEncoderSettings(); return new BulkMixedWriteOperation(chunksCollectionNamespace, requests, messageEncoderSettings) { WriteConcern = _bucket.Options.WriteConcern }; }
// methods protected override IWireProtocol <BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var updateRequest = (UpdateRequest)request; return(new UpdateWireProtocol( DatabaseName, CollectionName, WriteConcern, updateRequest.Query, updateRequest.Update, updateRequest.IsMultiUpdate ?? false, updateRequest.IsUpsert ?? false)); }
// methods protected override IWireProtocol <WriteConcernResult> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var updateRequest = (UpdateRequest)request; var updateValidator = new UpdateOrReplacementElementNameValidator(); return(new UpdateWireProtocol( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Criteria, updateRequest.Update, updateValidator, updateRequest.IsMultiUpdate ?? false, updateRequest.IsUpsert ?? false)); }
// methods protected override Task <WriteConcernResult> ExecuteProtocolAsync(IChannelHandle channel, WriteRequest request, CancellationToken cancellationToken) { var updateRequest = (UpdateRequest)request; return(channel.UpdateAsync( CollectionNamespace, MessageEncoderSettings, WriteConcern, updateRequest.Filter, updateRequest.Update, ElementNameValidatorFactory.ForUpdateType(updateRequest.UpdateType), updateRequest.IsMulti, updateRequest.IsUpsert, cancellationToken)); }
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)); }
protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var updateRequest = (UpdateRequest)request; Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, updateRequest.Collation); Feature.ArrayFilters.ThrowIfNotSupported(ConnectionDescription.ServerVersion, updateRequest.ArrayFilters); var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(ConnectionDescription.MaxWireDocumentSize); try { bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); SerializeFilter(bsonWriter, updateRequest.Filter); bsonWriter.WriteName("u"); SerializeUpdate(bsonWriter, updateRequest.Update, updateRequest.UpdateType); if (updateRequest.IsMulti) { bsonWriter.WriteBoolean("multi", updateRequest.IsMulti); } if (updateRequest.IsUpsert) { bsonWriter.WriteBoolean("upsert", updateRequest.IsUpsert); } if (updateRequest.Collation != null) { bsonWriter.WriteName("collation"); BsonDocumentSerializer.Instance.Serialize(context, updateRequest.Collation.ToBsonDocument()); } if (updateRequest.ArrayFilters != null) { bsonWriter.WriteName("arrayFilters"); BsonArraySerializer.Instance.Serialize(context, new BsonArray(updateRequest.ArrayFilters)); } bsonWriter.WriteEndDocument(); } finally { bsonWriter.PopMaxDocumentSize(); } }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var insertRequest = (InsertRequest)request; var document = insertRequest.Document; if (document == null) { throw new ArgumentException("Batch contains one or more null documents."); } var serializer = insertRequest.Serializer; if (serializer == null) { var actualType = document.GetType(); if (_cachedSerializerType != actualType) { _cachedSerializer = BsonSerializer.LookupSerializer(actualType); _cachedSerializerType = actualType; } serializer = _cachedSerializer; } var bsonWriter = (BsonBinaryWriter)context.Writer; var savedCheckElementNames = bsonWriter.CheckElementNames; try { bsonWriter.PushMaxDocumentSize(MaxDocumentSize); bsonWriter.CheckElementNames = _checkElementNames; var documentNominalType = serializer.ValueType; var documentContext = context.CreateChild(documentNominalType); serializer.Serialize(documentContext, document); } finally { bsonWriter.PopMaxDocumentSize(); bsonWriter.CheckElementNames = savedCheckElementNames; } }
protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var updateRequest = (UpdateRequest)request; var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(MaxWireDocumentSize); try { bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); SerializeCriteria(bsonWriter, updateRequest.Criteria); bsonWriter.WriteName("u"); SerializeUpdate(bsonWriter, updateRequest.Update); if (updateRequest.IsMultiUpdate.HasValue) { bsonWriter.WriteBoolean("multi", updateRequest.IsMultiUpdate.Value); } if (updateRequest.IsUpsert.HasValue) { bsonWriter.WriteBoolean("upsert", updateRequest.IsUpsert.Value); } bsonWriter.WriteEndDocument(); } finally { bsonWriter.PopMaxDocumentSize(); } }
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)); }
// methods protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request) { var deleteRequest = (DeleteRequest)request; var bsonWriter = (BsonBinaryWriter)context.Writer; bsonWriter.PushMaxDocumentSize(MaxDocumentSize); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("q"); BsonSerializer.Serialize(bsonWriter, deleteRequest.Query); bsonWriter.WriteInt32("limit", deleteRequest.Limit); bsonWriter.WriteEndDocument(); bsonWriter.PopMaxDocumentSize(); }
// methods protected override IWireProtocol <BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request) { var deleteRequest = (DeleteRequest)request; return(new DeleteWireProtocol( DatabaseName, CollectionName, WriteConcern, deleteRequest.Query, isMulti: false)); }