Example #1
0
        // public methods
        public WriteConcernResult Execute(MongoConnection connection)
        {
            var serverInstance = connection.ServerInstance;

            if (serverInstance.Supports(FeatureId.WriteCommands) && _args.WriteConcern.Enabled)
            {
                var emulator = new DeleteOpcodeOperationEmulator(_args);
                return(emulator.Execute(connection));
            }

            var requests = _args.Requests.ToArray();

            if (requests.Length != 1)
            {
                throw new NotSupportedException("Delete Opcode only supports a single delete request.");
            }
            var deleteRequest = (DeleteRequest)requests[0];

            RemoveFlags flags;

            switch (deleteRequest.Limit)
            {
            case 0: flags = RemoveFlags.None; break;

            case 1: flags = RemoveFlags.Single; break;

            default: throw new NotSupportedException("Delete Opcode only supports limit values of 0 and 1.");
            }

            SendMessageWithWriteConcernResult sendMessageResult;

            using (var stream = new MemoryStream())
            {
                var maxDocumentSize = connection.ServerInstance.MaxDocumentSize;

                var message = new MongoDeleteMessage(WriterSettings, CollectionFullName, flags, maxDocumentSize, deleteRequest.Query);
                message.WriteTo(stream);

                sendMessageResult = SendMessageWithWriteConcern(connection, stream, message.RequestId, ReaderSettings, WriterSettings, WriteConcern);
            }

            return(WriteConcern.Enabled ? ReadWriteConcernResult(connection, sendMessageResult) : null);
        }
        // public methods
        public WriteConcernResult Execute(MongoConnection connection)
        {
            var serverInstance = connection.ServerInstance;
            if (serverInstance.Supports(FeatureId.WriteCommands) && _args.WriteConcern.Enabled)
            {
                var emulator = new DeleteOpcodeOperationEmulator(_args);
                return emulator.Execute(connection);
            }

            var requests = _args.Requests.ToArray();
            if (requests.Length != 1)
            {
                throw new NotSupportedException("Delete Opcode only supports a single delete request.");
            }
            var deleteRequest = (DeleteRequest)requests[0];

            RemoveFlags flags;
            switch (deleteRequest.Limit)
            {
                case 0: flags = RemoveFlags.None; break;
                case 1: flags = RemoveFlags.Single; break;
                default: throw new NotSupportedException("Delete Opcode only supports limit values of 0 and 1.");
            }

            SendMessageWithWriteConcernResult sendMessageResult;
            using (var stream = new MemoryStream())
            {
                var maxDocumentSize = connection.ServerInstance.MaxDocumentSize;

                var message = new MongoDeleteMessage(WriterSettings, CollectionFullName, flags, maxDocumentSize, deleteRequest.Query);
                message.WriteTo(stream);

                sendMessageResult = SendMessageWithWriteConcern(connection, stream, message.RequestId, ReaderSettings, WriterSettings, WriteConcern);
            }

            return WriteConcern.Enabled ? ReadWriteConcernResult(connection, sendMessageResult) : null;
        }