diff --git a/src/MongoDB.Driver/Core/Operations/DeleteOpcodeOperation.cs b/src/MongoDB.Driver/Core/Operations/DeleteOpcodeOperation.cs deleted file mode 100644 index d21c46885b9..00000000000 --- a/src/MongoDB.Driver/Core/Operations/DeleteOpcodeOperation.cs +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright 2013-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Driver.Core.Bindings; -using MongoDB.Driver.Core.Events; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.Operations -{ - internal sealed class DeleteOpcodeOperation : IWriteOperation, IExecutableInRetryableWriteContext - { - private readonly CollectionNamespace _collectionNamespace; - private readonly MessageEncoderSettings _messageEncoderSettings; - private readonly DeleteRequest _request; - private bool _retryRequested; - private WriteConcern _writeConcern = WriteConcern.Acknowledged; - - public DeleteOpcodeOperation( - CollectionNamespace collectionNamespace, - DeleteRequest request, - MessageEncoderSettings messageEncoderSettings) - { - _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); - _request = Ensure.IsNotNull(request, nameof(request)); - _messageEncoderSettings = messageEncoderSettings; - } - - public CollectionNamespace CollectionNamespace - { - get { return _collectionNamespace; } - } - - public DeleteRequest Request - { - get { return _request; } - } - - public MessageEncoderSettings MessageEncoderSettings - { - get { return _messageEncoderSettings; } - } - - public bool RetryRequested - { - get { return _retryRequested; } - set { _retryRequested = value; } - } - - public WriteConcern WriteConcern - { - get { return _writeConcern; } - set { _writeConcern = Ensure.IsNotNull(value, nameof(value)); } - } - - public WriteConcernResult Execute(IWriteBinding binding, CancellationToken cancellationToken) - { - Ensure.IsNotNull(binding, nameof(binding)); - - using (var context = RetryableWriteContext.Create(binding, false, cancellationToken)) - { - return Execute(context, cancellationToken); - } - } - - public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - using (EventContext.BeginOperation()) - { - var emulator = CreateEmulator(); - return emulator.Execute(context, cancellationToken); - } - } - - public async Task ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken) - { - Ensure.IsNotNull(binding, nameof(binding)); - - using (var context = await RetryableWriteContext.CreateAsync(binding, false, cancellationToken).ConfigureAwait(false)) - { - return await ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - } - - public async Task ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - using (EventContext.BeginOperation()) - { - var emulator = CreateEmulator(); - return await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - } - - private IExecutableInRetryableWriteContext CreateEmulator() - { - return new DeleteOpcodeOperationEmulator(_collectionNamespace, _request, _messageEncoderSettings) - { - RetryRequested = _retryRequested, - WriteConcern = _writeConcern - }; - } - } -} diff --git a/src/MongoDB.Driver/Core/Operations/DeleteOpcodeOperationEmulator.cs b/src/MongoDB.Driver/Core/Operations/DeleteOpcodeOperationEmulator.cs deleted file mode 100644 index e325eee4b95..00000000000 --- a/src/MongoDB.Driver/Core/Operations/DeleteOpcodeOperationEmulator.cs +++ /dev/null @@ -1,144 +0,0 @@ -/* Copyright 2013-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Driver.Core.Bindings; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.Operations -{ - internal class DeleteOpcodeOperationEmulator : IExecutableInRetryableWriteContext - { - // fields - private readonly CollectionNamespace _collectionNamespace; - private readonly DeleteRequest _request; - private readonly MessageEncoderSettings _messageEncoderSettings; - private bool _retryRequested; - private WriteConcern _writeConcern = WriteConcern.Acknowledged; - - // constructors - public DeleteOpcodeOperationEmulator( - CollectionNamespace collectionNamespace, - DeleteRequest request, - MessageEncoderSettings messageEncoderSettings) - { - _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); - _request = Ensure.IsNotNull(request, nameof(request)); - _messageEncoderSettings = messageEncoderSettings; - } - - // properties - public CollectionNamespace CollectionNamespace - { - get { return _collectionNamespace; } - } - - public DeleteRequest Request - { - get { return _request; } - } - - public MessageEncoderSettings MessageEncoderSettings - { - get { return _messageEncoderSettings; } - } - - public bool RetryRequested - { - get { return _retryRequested; } - set { _retryRequested = value; } - } - - public WriteConcern WriteConcern - { - get { return _writeConcern; } - set { _writeConcern = Ensure.IsNotNull(value, nameof(value)); } - } - - // public methods - public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - var operation = CreateOperation(); - BulkWriteOperationResult result; - MongoBulkWriteOperationException exception = null; - try - { - result = operation.Execute(context, cancellationToken); - } - catch (MongoBulkWriteOperationException ex) - { - result = ex.Result; - exception = ex; - } - - return CreateResultOrThrow(context.Channel, result, exception); - } - - public async Task ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - var operation = CreateOperation(); - BulkWriteOperationResult result; - MongoBulkWriteOperationException exception = null; - try - { - result = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - catch (MongoBulkWriteOperationException ex) - { - result = ex.Result; - exception = ex; - } - - return CreateResultOrThrow(context.Channel, result, exception); - } - - // private methods - private BulkDeleteOperation CreateOperation() - { - var requests = new[] { _request }; - return new BulkDeleteOperation(_collectionNamespace, requests, _messageEncoderSettings) - { - RetryRequested = _retryRequested, - WriteConcern = _writeConcern - }; - } - - private WriteConcernResult CreateResultOrThrow(IChannelHandle channel, BulkWriteOperationResult result, MongoBulkWriteOperationException exception) - { - var converter = new BulkWriteOperationResultConverter(); - if (exception != null) - { - throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, exception); - } - else - { - if (_writeConcern.IsAcknowledged) - { - return converter.ToWriteConcernResult(result); - } - else - { - return null; - } - } - } - } -} diff --git a/src/MongoDB.Driver/Core/Operations/InsertOpcodeOperation.cs b/src/MongoDB.Driver/Core/Operations/InsertOpcodeOperation.cs deleted file mode 100644 index 71316bccbff..00000000000 --- a/src/MongoDB.Driver/Core/Operations/InsertOpcodeOperation.cs +++ /dev/null @@ -1,161 +0,0 @@ -/* Copyright 2013-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Bson.Serialization; -using MongoDB.Driver.Core.Bindings; -using MongoDB.Driver.Core.Events; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.Operations -{ - internal sealed class InsertOpcodeOperation : IWriteOperation> - { - private bool? _bypassDocumentValidation; - private readonly CollectionNamespace _collectionNamespace; - private bool _continueOnError; - private readonly IReadOnlyList _documents; - private readonly BatchableSource _documentSource; - private int? _maxBatchCount; - private int? _maxDocumentSize; - private int? _maxMessageSize; - private readonly MessageEncoderSettings _messageEncoderSettings; - private bool _retryRequested; - private readonly IBsonSerializer _serializer; - private WriteConcern _writeConcern; - - public InsertOpcodeOperation(CollectionNamespace collectionNamespace, IEnumerable documents, IBsonSerializer serializer, MessageEncoderSettings messageEncoderSettings) - { - _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); - _documents = Ensure.IsNotNull(documents, nameof(documents)).ToList(); - _serializer = Ensure.IsNotNull(serializer, nameof(serializer)); - _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings)); - _writeConcern = WriteConcern.Acknowledged; - - _documentSource = new BatchableSource(_documents, canBeSplit: true); - } - - public bool? BypassDocumentValidation - { - get { return _bypassDocumentValidation; } - set { _bypassDocumentValidation = value; } - } - - public CollectionNamespace CollectionNamespace - { - get { return _collectionNamespace; } - } - - public bool ContinueOnError - { - get { return _continueOnError; } - set { _continueOnError = value; } - } - - public IReadOnlyList Documents - { - get { return _documents; } - } - - public BatchableSource DocumentSource - { - get { return _documentSource; } - } - - public int? MaxBatchCount - { - get { return _maxBatchCount; } - set { _maxBatchCount = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public int? MaxDocumentSize - { - get { return _maxDocumentSize; } - set { _maxDocumentSize = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public int? MaxMessageSize - { - get { return _maxMessageSize; } - set { _maxMessageSize = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public MessageEncoderSettings MessageEncoderSettings - { - get { return _messageEncoderSettings; } - } - - public bool RetryRequested - { - get { return _retryRequested; } - set { _retryRequested = value; } - } - - public IBsonSerializer Serializer - { - get { return _serializer; } - } - - public WriteConcern WriteConcern - { - get { return _writeConcern; } - set { _writeConcern = Ensure.IsNotNull(value, nameof(value)); } - } - - public IEnumerable Execute(IWriteBinding binding, CancellationToken cancellationToken) - { - Ensure.IsNotNull(binding, nameof(binding)); - - using (EventContext.BeginOperation()) - using (var context = RetryableWriteContext.Create(binding, false, cancellationToken)) - { - var emulator = CreateEmulator(); - var result = emulator.Execute(context, cancellationToken); - return result != null ? new[] { result } : null; - } - } - - public async Task> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken) - { - Ensure.IsNotNull(binding, nameof(binding)); - - using (EventContext.BeginOperation()) - using (var context = await RetryableWriteContext.CreateAsync(binding, false, cancellationToken).ConfigureAwait(false)) - { - var emulator = CreateEmulator(); - var result = await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - return result != null ? new[] { result } : null; - } - } - - private InsertOpcodeOperationEmulator CreateEmulator() - { - return new InsertOpcodeOperationEmulator(_collectionNamespace, _serializer, _documentSource, _messageEncoderSettings) - { - BypassDocumentValidation = _bypassDocumentValidation, - ContinueOnError = _continueOnError, - MaxBatchCount = _maxBatchCount, - MaxDocumentSize = _maxDocumentSize, - MaxMessageSize = _maxMessageSize, - RetryRequested = _retryRequested, - WriteConcern = _writeConcern - }; - } - } -} diff --git a/src/MongoDB.Driver/Core/Operations/InsertOpcodeOperationEmulator.cs b/src/MongoDB.Driver/Core/Operations/InsertOpcodeOperationEmulator.cs deleted file mode 100644 index 7b22092b54e..00000000000 --- a/src/MongoDB.Driver/Core/Operations/InsertOpcodeOperationEmulator.cs +++ /dev/null @@ -1,203 +0,0 @@ -/* Copyright 2013-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using MongoDB.Driver.Core.Bindings; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.Operations -{ - internal class InsertOpcodeOperationEmulator : IExecutableInRetryableWriteContext - { - // fields - private bool? _bypassDocumentValidation; - private readonly CollectionNamespace _collectionNamespace; - private bool _continueOnError; - private readonly BatchableSource _documentSource; - private int? _maxBatchCount; - private int? _maxDocumentSize; - private int? _maxMessageSize; - private readonly MessageEncoderSettings _messageEncoderSettings; - private bool _retryRequested; - private readonly IBsonSerializer _serializer; - private WriteConcern _writeConcern = WriteConcern.Acknowledged; - - // constructors - public InsertOpcodeOperationEmulator( - CollectionNamespace collectionNamespace, - IBsonSerializer serializer, - BatchableSource documentSource, - MessageEncoderSettings messageEncoderSettings) - { - _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); - _serializer = Ensure.IsNotNull(serializer, nameof(serializer)); - _documentSource = Ensure.IsNotNull(documentSource, nameof(documentSource)); - _messageEncoderSettings = messageEncoderSettings; - - if (documentSource.Items.Skip(documentSource.Offset).Take(documentSource.Count).Any(d => d == null)) - { - throw new ArgumentException("Batch contains one or more null documents."); - } - } - - // properties - public bool? BypassDocumentValidation - { - get { return _bypassDocumentValidation; } - set { _bypassDocumentValidation = value; } - } - - public CollectionNamespace CollectionNamespace - { - get { return _collectionNamespace; } - } - - public bool ContinueOnError - { - get { return _continueOnError; } - set { _continueOnError = value; } - } - - public BatchableSource DocumentSource - { - get { return _documentSource; } - } - - public int? MaxBatchCount - { - get { return _maxBatchCount; } - set { _maxBatchCount = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public int? MaxDocumentSize - { - get { return _maxDocumentSize; } - set { _maxDocumentSize = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public int? MaxMessageSize - { - get { return _maxMessageSize; } - set { _maxMessageSize = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public MessageEncoderSettings MessageEncoderSettings - { - get { return _messageEncoderSettings; } - } - - public bool RetryRequested - { - get { return _retryRequested; } - set { _retryRequested = value; } - } - - public IBsonSerializer Serializer - { - get { return _serializer; } - } - - public WriteConcern WriteConcern - { - get { return _writeConcern; } - set { _writeConcern = Ensure.IsNotNull(value, nameof(value)); } - } - - // public methods - public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - var operation = CreateOperation(); - BulkWriteOperationResult result; - MongoBulkWriteOperationException exception = null; - try - { - result = operation.Execute(context, cancellationToken); - } - catch (MongoBulkWriteOperationException ex) - { - result = ex.Result; - exception = ex; - } - - return CreateResultOrThrow(context.Channel, result, exception); - } - - public async Task ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - var operation = CreateOperation(); - BulkWriteOperationResult result; - MongoBulkWriteOperationException exception = null; - try - { - result = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - catch (MongoBulkWriteOperationException ex) - { - result = ex.Result; - exception = ex; - } - - return CreateResultOrThrow(context.Channel, result, exception); - } - - // private methods - private BulkInsertOperation CreateOperation() - { - var requests = _documentSource.GetBatchItems().Select(d => new InsertRequest(new BsonDocumentWrapper(d, _serializer))); - - return new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings) - { - BypassDocumentValidation = _bypassDocumentValidation, - IsOrdered = !_continueOnError, - MaxBatchCount = _maxBatchCount, - MaxBatchLength = _maxMessageSize, - // ReaderSettings = ? - RetryRequested = _retryRequested, - WriteConcern = _writeConcern, - // WriteSettings = ? - }; - } - - private WriteConcernResult CreateResultOrThrow(IChannel channel, BulkWriteOperationResult result, MongoBulkWriteOperationException exception) - { - var converter = new BulkWriteOperationResultConverter(); - if (exception != null) - { - throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, exception); - } - else - { - if (_writeConcern.IsAcknowledged) - { - return converter.ToWriteConcernResult(result); - } - else - { - return null; - } - } - } - } -} diff --git a/src/MongoDB.Driver/Core/Operations/UpdateOpcodeOperation.cs b/src/MongoDB.Driver/Core/Operations/UpdateOpcodeOperation.cs deleted file mode 100644 index 72862d1079f..00000000000 --- a/src/MongoDB.Driver/Core/Operations/UpdateOpcodeOperation.cs +++ /dev/null @@ -1,133 +0,0 @@ -/* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Driver.Core.Bindings; -using MongoDB.Driver.Core.Events; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.Operations -{ - internal sealed class UpdateOpcodeOperation : IWriteOperation, IExecutableInRetryableWriteContext - { - private bool? _bypassDocumentValidation; - private readonly CollectionNamespace _collectionNamespace; - private int? _maxDocumentSize; - private readonly MessageEncoderSettings _messageEncoderSettings; - private readonly UpdateRequest _request; - private bool _retryRequested; - private WriteConcern _writeConcern = WriteConcern.Acknowledged; - - public UpdateOpcodeOperation( - CollectionNamespace collectionNamespace, - UpdateRequest request, - MessageEncoderSettings messageEncoderSettings) - { - _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); - _request = Ensure.IsNotNull(request, nameof(request)); - _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings)); - } - - public bool? BypassDocumentValidation - { - get { return _bypassDocumentValidation; } - set { _bypassDocumentValidation = value; } - } - - public CollectionNamespace CollectionNamespace - { - get { return _collectionNamespace; } - } - - public int? MaxDocumentSize - { - get { return _maxDocumentSize; } - set { _maxDocumentSize = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public MessageEncoderSettings MessageEncoderSettings - { - get { return _messageEncoderSettings; } - } - - public UpdateRequest Request - { - get { return _request; } - } - - public bool RetryRequested - { - get { return _retryRequested; } - set { _retryRequested = value; } - } - - public WriteConcern WriteConcern - { - get { return _writeConcern; } - set { _writeConcern = Ensure.IsNotNull(value, nameof(value)); } - } - - public WriteConcernResult Execute(IWriteBinding binding, CancellationToken cancellationToken) - { - Ensure.IsNotNull(binding, nameof(binding)); - - using (var context = RetryableWriteContext.Create(binding, false, cancellationToken)) - { - return Execute(context, cancellationToken); - } - } - - public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken) - { - using (EventContext.BeginOperation()) - { - var emulator = CreateEmulator(); - return emulator.Execute(context, cancellationToken); - } - } - - public async Task ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken) - { - Ensure.IsNotNull(binding, nameof(binding)); - - using (var context = await RetryableWriteContext.CreateAsync(binding, false, cancellationToken).ConfigureAwait(false)) - { - return await ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - } - - public async Task ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken) - { - using (EventContext.BeginOperation()) - { - var emulator = CreateEmulator(); - return await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - } - - private UpdateOpcodeOperationEmulator CreateEmulator() - { - return new UpdateOpcodeOperationEmulator(_collectionNamespace, _request, _messageEncoderSettings) - { - BypassDocumentValidation = _bypassDocumentValidation, - MaxDocumentSize = _maxDocumentSize, - RetryRequested = _retryRequested, - WriteConcern = _writeConcern - }; - } - } -} diff --git a/src/MongoDB.Driver/Core/Operations/UpdateOpcodeOperationEmulator.cs b/src/MongoDB.Driver/Core/Operations/UpdateOpcodeOperationEmulator.cs deleted file mode 100644 index 77947fdf372..00000000000 --- a/src/MongoDB.Driver/Core/Operations/UpdateOpcodeOperationEmulator.cs +++ /dev/null @@ -1,168 +0,0 @@ -/* Copyright 2013-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Threading; -using System.Threading.Tasks; -using MongoDB.Driver.Core.Bindings; -using MongoDB.Driver.Core.Connections; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; - -namespace MongoDB.Driver.Core.Operations -{ - internal class UpdateOpcodeOperationEmulator : IExecutableInRetryableWriteContext - { - // fields - private bool? _bypassDocumentValidation; - private readonly CollectionNamespace _collectionNamespace; - private int? _maxDocumentSize; - private readonly MessageEncoderSettings _messageEncoderSettings; - private readonly UpdateRequest _request; - private bool _retryRequested; - private WriteConcern _writeConcern = WriteConcern.Acknowledged; - - // 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)); - } - - // properties - public bool? BypassDocumentValidation - { - get { return _bypassDocumentValidation; } - set { _bypassDocumentValidation = value; } - } - - public CollectionNamespace CollectionNamespace - { - get { return _collectionNamespace; } - } - - /// - /// Gets or sets the maximum size of a document. - /// - /// - /// The maximum size of a document. - /// - public int? MaxDocumentSize - { - get { return _maxDocumentSize; } - set { _maxDocumentSize = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); } - } - - public MessageEncoderSettings MessageEncoderSettings - { - get { return _messageEncoderSettings; } - } - - public UpdateRequest Request - { - get { return _request; } - } - - public bool RetryRequested - { - get { return _retryRequested; } - set { _retryRequested = value; } - } - - public WriteConcern WriteConcern - { - get { return _writeConcern; } - set { _writeConcern = Ensure.IsNotNull(value, nameof(value)); } - } - - // public methods - public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - var operation = CreateOperation(); - BulkWriteOperationResult result; - MongoBulkWriteOperationException exception = null; - try - { - result = operation.Execute(context, cancellationToken); - } - catch (MongoBulkWriteOperationException ex) - { - result = ex.Result; - exception = ex; - } - - return CreateResultOrThrow(context.Channel, result, exception); - } - - public async Task ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken) - { - Ensure.IsNotNull(context, nameof(context)); - - var operation = CreateOperation(); - BulkWriteOperationResult result; - MongoBulkWriteOperationException exception = null; - try - { - result = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); - } - catch (MongoBulkWriteOperationException ex) - { - result = ex.Result; - exception = ex; - } - - return CreateResultOrThrow(context.Channel, result, exception); - } - - // private methods - private BulkUpdateOperation CreateOperation() - { - var requests = new[] { _request }; - return new BulkUpdateOperation(_collectionNamespace, requests, _messageEncoderSettings) - { - BypassDocumentValidation = _bypassDocumentValidation, - IsOrdered = true, - RetryRequested = _retryRequested, - WriteConcern = _writeConcern - }; - } - - private WriteConcernResult CreateResultOrThrow(IChannelHandle channel, BulkWriteOperationResult result, MongoBulkWriteOperationException exception) - { - var converter = new BulkWriteOperationResultConverter(); - if (exception != null) - { - throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, exception); - } - else - { - if (_writeConcern.IsAcknowledged) - { - return converter.ToWriteConcernResult(result); - } - else - { - return null; - } - } - } - } -} diff --git a/tests/MongoDB.Driver.Tests/Core/Operations/InsertOpcodeOperationTests.cs b/tests/MongoDB.Driver.Tests/Core/Operations/InsertOpcodeOperationTests.cs deleted file mode 100644 index bd8ff43b2f5..00000000000 --- a/tests/MongoDB.Driver.Tests/Core/Operations/InsertOpcodeOperationTests.cs +++ /dev/null @@ -1,176 +0,0 @@ -/* Copyright 2013-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Collections.Generic; -using FluentAssertions; -using MongoDB.Bson; -using MongoDB.Bson.Serialization.Serializers; -using MongoDB.TestHelpers.XunitExtensions; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Core.TestHelpers.XunitExtensions; -using Xunit; - -namespace MongoDB.Driver.Core.Operations -{ - public class InsertOpcodeOperationTests : OperationTestBase - { - private BsonDocument[] _documents; - - public InsertOpcodeOperationTests() - { - _documents = new[] - { - BsonDocument.Parse("{_id: 1, x: 1}") - }; - } - - [Fact] - public void Constructor_should_throw_when_collection_namespace_is_null() - { - Action act = () => new InsertOpcodeOperation(null, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - act.ShouldThrow(); - } - - [Fact] - public void Constructor_should_throw_when_serializer_is_null() - { - Action act = () => new InsertOpcodeOperation(_collectionNamespace, _documents, null, _messageEncoderSettings); - - act.ShouldThrow(); - } - - [Fact] - public void Constructor_should_throw_when_message_encoder_settings_is_null() - { - Action act = () => new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, null); - - act.ShouldThrow(); - } - - [Fact] - public void Constructor_should_initialize_object() - { - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - subject.CollectionNamespace.FullName.Should().Be(_collectionNamespace.FullName); - subject.Documents.Count.Should().Be(_documents.Length); - subject.Serializer.Should().BeSameAs(BsonDocumentSerializer.Instance); - subject.MessageEncoderSettings.Should().BeEquivalentTo(_messageEncoderSettings); - } - - [Fact] - public void ContinueOnError_should_work() - { - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - subject.ContinueOnError.Should().Be(false); - - subject.ContinueOnError = true; - - subject.ContinueOnError.Should().Be(true); - } - - [Fact] - public void MaxBatchCount_should_work() - { - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - subject.MaxBatchCount.Should().Be(null); - - subject.MaxBatchCount = 20; - - subject.MaxBatchCount.Should().Be(20); - } - - [Fact] - public void MaxDocumentSize_should_work() - { - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - subject.MaxDocumentSize.Should().Be(null); - - subject.MaxDocumentSize = 20; - - subject.MaxDocumentSize.Should().Be(20); - } - - [Fact] - public void MaxMessageSize_should_work() - { - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - subject.MaxMessageSize.Should().Be(null); - - subject.MaxMessageSize = 20; - - subject.MaxMessageSize.Should().Be(20); - } - - [Fact] - public void WriteConcern_should_work() - { - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - subject.WriteConcern.Should().Be(WriteConcern.Acknowledged); - - subject.WriteConcern = WriteConcern.W2; - - subject.WriteConcern.Should().Be(WriteConcern.W2); - } - - [Theory] - [ParameterAttributeData] - public void Execute_should_insert_a_single_document( - [Values(false, true)] - bool async) - { - RequireServer.Check(); - DropCollection(); - var subject = new InsertOpcodeOperation(_collectionNamespace, _documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - var result = ExecuteOperation(subject, async); - result.Should().HaveCount(1); - - var list = ReadAllFromCollection(async); - list.Should().HaveCount(1); - } - - [Theory] - [ParameterAttributeData] - public void Execute_should_insert_multiple_documents( - [Values(false, true)] - bool async) - { - RequireServer.Check(); - DropCollection(); - var documents = new[] - { - BsonDocument.Parse("{_id: 1, x: 1}"), - BsonDocument.Parse("{_id: 2, x: 2}"), - BsonDocument.Parse("{_id: 3, x: 3}"), - BsonDocument.Parse("{_id: 4, x: 4}"), - }; - var subject = new InsertOpcodeOperation(_collectionNamespace, documents, BsonDocumentSerializer.Instance, _messageEncoderSettings); - - var result = ExecuteOperation(subject, async); - result.Should().HaveCount(1); - - var list = ReadAllFromCollection(async); - list.Should().HaveCount(4); - } - } -} diff --git a/tests/MongoDB.Driver.Tests/Core/Operations/UpdateOpcodeOperationTests.cs b/tests/MongoDB.Driver.Tests/Core/Operations/UpdateOpcodeOperationTests.cs deleted file mode 100644 index 150eaa56d90..00000000000 --- a/tests/MongoDB.Driver.Tests/Core/Operations/UpdateOpcodeOperationTests.cs +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2020-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using FluentAssertions; -using MongoDB.Bson; -using MongoDB.TestHelpers.XunitExtensions; -using MongoDB.Driver.Core.Misc; -using Xunit; - -namespace MongoDB.Driver.Core.Operations -{ - public class UpdateOpcodeOperationTests : OperationTestBase - { - [Theory] - [ParameterAttributeData] - public void Execute_with_hint_should_throw_when_hint_is_not_supported( - [Values(0, 1)] int w, - [Values(false, true)] bool async) - { - var writeConcern = new WriteConcern(w); - var request = new UpdateRequest( - UpdateType.Update, - new BsonDocument("x", 1), - new BsonDocument("$set", new BsonDocument("x", 2))) - { - Hint = new BsonDocument("_id", 1) - }; - var subject = new UpdateOpcodeOperation(_collectionNamespace, request, _messageEncoderSettings) - { - WriteConcern = writeConcern - }; - - var exception = Record.Exception(() => ExecuteOperation(subject, async)); - - if (!writeConcern.IsAcknowledged) - { - exception.Should().BeOfType(); - } -#pragma warning disable CS0618 // Type or member is obsolete - else if (Feature.HintForUpdateAndReplaceOperations.IsSupported(CoreTestConfiguration.MaxWireVersion)) -#pragma warning restore CS0618 // Type or member is obsolete - { - exception.Should().BeNull(); - } - else - { - exception.Should().BeOfType(); - } - } - } -}