|
| 1 | +/* Copyright 2016 MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Bson.Serialization.Serializers; |
| 22 | +using MongoDB.Driver.Core.Bindings; |
| 23 | +using MongoDB.Driver.Core.Misc; |
| 24 | +using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Core.Operations |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Represents a create view operation. |
| 30 | + /// </summary> |
| 31 | + public class CreateViewOperation : IWriteOperation<BsonDocument> |
| 32 | + { |
| 33 | + // private fields |
| 34 | + private Collation _collation; |
| 35 | + private readonly DatabaseNamespace _databaseNamespace; |
| 36 | + private readonly MessageEncoderSettings _messageEncoderSettings; |
| 37 | + private readonly IReadOnlyList<BsonDocument> _pipeline; |
| 38 | + private readonly string _viewName; |
| 39 | + private readonly string _viewOn; |
| 40 | + private WriteConcern _writeConcern; |
| 41 | + |
| 42 | + // constructors |
| 43 | + /// <summary> |
| 44 | + /// Initializes a new instance of the <see cref="CreateCollectionOperation" /> class. |
| 45 | + /// </summary> |
| 46 | + /// <param name="databaseNamespace">Name of the database.</param> |
| 47 | + /// <param name="viewName">Name of the view.</param> |
| 48 | + /// <param name="viewOn">The view on.</param> |
| 49 | + /// <param name="pipeline">The pipeline.</param> |
| 50 | + /// <param name="messageEncoderSettings">The message encoder settings.</param> |
| 51 | + public CreateViewOperation( |
| 52 | + DatabaseNamespace databaseNamespace, |
| 53 | + string viewName, |
| 54 | + string viewOn, |
| 55 | + IEnumerable<BsonDocument> pipeline, |
| 56 | + MessageEncoderSettings messageEncoderSettings) |
| 57 | + { |
| 58 | + _databaseNamespace = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace)); |
| 59 | + _viewName = Ensure.IsNotNull(viewName, nameof(viewName)); |
| 60 | + _viewOn = Ensure.IsNotNull(viewOn, nameof(viewOn)); |
| 61 | + _pipeline = Ensure.IsNotNull(pipeline, nameof(pipeline)).ToList(); |
| 62 | + _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings)); |
| 63 | + } |
| 64 | + |
| 65 | + // public properties |
| 66 | + /// <summary> |
| 67 | + /// Gets or sets the collation. |
| 68 | + /// </summary> |
| 69 | + /// <value> |
| 70 | + /// The collation. |
| 71 | + /// </value> |
| 72 | + public Collation Collation |
| 73 | + { |
| 74 | + get { return _collation; } |
| 75 | + set { _collation = value; } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Gets the namespace of the database. |
| 80 | + /// </summary> |
| 81 | + /// <value> |
| 82 | + /// The namespace of the database. |
| 83 | + /// </value> |
| 84 | + public DatabaseNamespace DatabaseNamespace |
| 85 | + { |
| 86 | + get { return _databaseNamespace; } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Gets the message encoder settings. |
| 91 | + /// </summary> |
| 92 | + /// <value> |
| 93 | + /// The message encoder settings. |
| 94 | + /// </value> |
| 95 | + public MessageEncoderSettings MessageEncoderSettings |
| 96 | + { |
| 97 | + get { return _messageEncoderSettings; } |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Gets the pipeline. |
| 102 | + /// </summary> |
| 103 | + /// <value> |
| 104 | + /// The pipeline. |
| 105 | + /// </value> |
| 106 | + public IReadOnlyList<BsonDocument> Pipeline |
| 107 | + { |
| 108 | + get { return _pipeline; } |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Gets the name of the view. |
| 113 | + /// </summary> |
| 114 | + /// <value> |
| 115 | + /// The name of the view. |
| 116 | + /// </value> |
| 117 | + public string ViewName |
| 118 | + { |
| 119 | + get { return _viewName; } |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Gets the collection namespace. |
| 124 | + /// </summary> |
| 125 | + /// <value> |
| 126 | + /// The collection namespace. |
| 127 | + /// </value> |
| 128 | + public string ViewOn |
| 129 | + { |
| 130 | + get { return _viewOn; } |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Gets or sets the write concern. |
| 135 | + /// </summary> |
| 136 | + /// <value> |
| 137 | + /// The write concern. |
| 138 | + /// </value> |
| 139 | + public WriteConcern WriteConcern |
| 140 | + { |
| 141 | + get { return _writeConcern; } |
| 142 | + set { _writeConcern = value; } |
| 143 | + } |
| 144 | + |
| 145 | + // public methods |
| 146 | + /// <inheritdoc/> |
| 147 | + public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken) |
| 148 | + { |
| 149 | + Ensure.IsNotNull(binding, nameof(binding)); |
| 150 | + |
| 151 | + using (var channelSource = binding.GetWriteChannelSource(cancellationToken)) |
| 152 | + using (var channel = channelSource.GetChannel(cancellationToken)) |
| 153 | + using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel)) |
| 154 | + { |
| 155 | + var operation = CreateOperation(channel.ConnectionDescription.ServerVersion); |
| 156 | + return operation.Execute(channelBinding, cancellationToken); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// <inheritdoc/> |
| 161 | + public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken) |
| 162 | + { |
| 163 | + Ensure.IsNotNull(binding, nameof(binding)); |
| 164 | + |
| 165 | + using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false)) |
| 166 | + using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false)) |
| 167 | + using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel)) |
| 168 | + { |
| 169 | + var operation = CreateOperation(channel.ConnectionDescription.ServerVersion); |
| 170 | + return await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // private methods |
| 175 | + internal BsonDocument CreateCommand(SemanticVersion serverVersion) |
| 176 | + { |
| 177 | + Feature.Views.ThrowIfNotSupported(serverVersion); |
| 178 | + Feature.Collation.ThrowIfNotSupported(serverVersion, _collation); |
| 179 | + |
| 180 | + return new BsonDocument |
| 181 | + { |
| 182 | + { "create", _viewName }, |
| 183 | + { "viewOn", _viewOn }, |
| 184 | + { "pipeline", new BsonArray(_pipeline) }, |
| 185 | + { "collation", () => _collation.ToBsonDocument(), _collation != null }, |
| 186 | + { "writeConcern", () => _writeConcern.ToBsonDocument(), _writeConcern != null && !_writeConcern.IsServerDefault && Feature.CommandsWriteConcern.IsSupported(serverVersion) } |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + private WriteCommandOperation<BsonDocument> CreateOperation(SemanticVersion serverVersion) |
| 191 | + { |
| 192 | + var command = CreateCommand(serverVersion); |
| 193 | + return new WriteCommandOperation<BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments