Skip to content

Commit 13e6165

Browse files
committed
CSHARP-1688: Support sending WriteConcern for commands that write.
1 parent 1b394b2 commit 13e6165

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1461
-164
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
namespace MongoDB.Driver.Core.Misc
17+
{
18+
/// <summary>
19+
/// Represents the commands that write accept write concern concern feature.
20+
/// </summary>
21+
/// <seealso cref="MongoDB.Driver.Core.Misc.Feature" />
22+
public class CommandsThatWriteAcceptWriteConcernFeature : Feature
23+
{
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="CommandsThatWriteAcceptWriteConcernFeature"/> class.
26+
/// </summary>
27+
/// <param name="name">The name of the feature.</param>
28+
/// <param name="firstSupportedVersion">The first server version that supports the feature.</param>
29+
public CommandsThatWriteAcceptWriteConcernFeature(string name, SemanticVersion firstSupportedVersion)
30+
: base(name, firstSupportedVersion)
31+
{
32+
}
33+
34+
/// <summary>
35+
/// Returns true if the write concern value supplied is one that should be sent to the server and the server version supports the commands that write accept write concern feature.
36+
/// </summary>
37+
/// <param name="serverVersion">The server version.</param>
38+
/// <param name="value">The write concern value.</param>
39+
public bool ShouldSendWriteConcern(SemanticVersion serverVersion, WriteConcern value)
40+
{
41+
return value != null && !value.IsServerDefault && base.IsSupported(serverVersion);
42+
}
43+
}
44+
}

src/MongoDB.Driver.Core/Core/Misc/Feature.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Feature
3030
private static readonly Feature __aggregateOut = new Feature("Aggregate", new SemanticVersion(2, 6, 0));
3131
private static readonly Feature __bypassDocumentValidation = new Feature("BypassDocumentValidation", new SemanticVersion(3, 2, 0));
3232
private static readonly CollationFeature __collation = new CollationFeature("Collation", new SemanticVersion(3, 3, 11));
33-
private static readonly Feature __commandsWriteConcern = new Feature("CommandsWriteConcern", new SemanticVersion(3, 3, 11));
33+
private static readonly CommandsThatWriteAcceptWriteConcernFeature __commandsThatWriteAcceptWriteConcern = new CommandsThatWriteAcceptWriteConcernFeature("CommandsThatWriteAcceptWriteConcern", new SemanticVersion(3, 3, 11));
3434
private static readonly Feature __createIndexesCommand = new Feature("CreateIndexesCommand", new SemanticVersion(3, 0, 0));
3535
private static readonly Feature __currentOpCommand = new Feature("CurrentOpCommand", new SemanticVersion(3, 2, 0));
3636
private static readonly Feature __documentValidation = new Feature("DocumentValidation", new SemanticVersion(3, 2, 0));
@@ -86,9 +86,9 @@ public class Feature
8686
public static CollationFeature Collation => __collation;
8787

8888
/// <summary>
89-
/// Gets the commands write concern feature.
89+
/// Gets the commands that write accept write concern feature.
9090
/// </summary>
91-
public static Feature CommandsWriteConcern => __commandsWriteConcern;
91+
public static CommandsThatWriteAcceptWriteConcernFeature CommandsThatWriteAcceptWriteConcern => __commandsThatWriteAcceptWriteConcern;
9292

9393
/// <summary>
9494
/// Gets the create indexes command feature.

src/MongoDB.Driver.Core/Core/Operations/AggregateToCollectionOperation.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class AggregateToCollectionOperation : IWriteOperation<BsonDocument>
3939
private TimeSpan? _maxTime;
4040
private readonly MessageEncoderSettings _messageEncoderSettings;
4141
private readonly IReadOnlyList<BsonDocument> _pipeline;
42+
private WriteConcern _writeConcern;
4243

4344
// constructors
4445
/// <summary>
@@ -138,6 +139,18 @@ public IReadOnlyList<BsonDocument> Pipeline
138139
get { return _pipeline; }
139140
}
140141

142+
/// <summary>
143+
/// Gets or sets the write concern.
144+
/// </summary>
145+
/// <value>
146+
/// The write concern.
147+
/// </value>
148+
public WriteConcern WriteConcern
149+
{
150+
get { return _writeConcern; }
151+
set { _writeConcern = value; }
152+
}
153+
141154
// methods
142155
/// <inheritdoc/>
143156
public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
@@ -178,7 +191,8 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
178191
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
179192
{ "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue && Feature.BypassDocumentValidation.IsSupported(serverVersion) },
180193
{ "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
181-
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
194+
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
195+
{ "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
182196
};
183197
}
184198

src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class CreateCollectionOperation : IWriteOperation<BsonDocument>
4444
private DocumentValidationAction? _validationAction;
4545
private DocumentValidationLevel? _validationLevel;
4646
private BsonDocument _validator;
47+
private WriteConcern _writeConcern;
4748

4849
// constructors
4950
/// <summary>
@@ -214,6 +215,18 @@ public BsonDocument Validator
214215
set { _validator = value; }
215216
}
216217

218+
/// <summary>
219+
/// Gets or sets the write concern.
220+
/// </summary>
221+
/// <value>
222+
/// The write concern.
223+
/// </value>
224+
public WriteConcern WriteConcern
225+
{
226+
get { return _writeConcern; }
227+
set { _writeConcern = value; }
228+
}
229+
217230
// methods
218231
internal BsonDocument CreateCommand(SemanticVersion serverVersion)
219232
{
@@ -232,7 +245,8 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
232245
{ "validator", _validator, _validator != null },
233246
{ "validationAction", () => _validationAction.Value.ToString().ToLowerInvariant(), _validationAction.HasValue },
234247
{ "validationLevel", () => _validationLevel.Value.ToString().ToLowerInvariant(), _validationLevel.HasValue },
235-
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
248+
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
249+
{ "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
236250
};
237251
}
238252

src/MongoDB.Driver.Core/Core/Operations/CreateIndexesOperation.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -111,7 +111,7 @@ public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellatio
111111
using (var channel = channelSource.GetChannel(cancellationToken))
112112
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
113113
{
114-
var operation = CreateOperation(channel);
114+
var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
115115
return operation.Execute(channelBinding, cancellationToken);
116116
}
117117
}
@@ -124,21 +124,27 @@ public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, Cancellation
124124
using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
125125
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
126126
{
127-
var operation = CreateOperation(channel);
127+
var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
128128
return await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);
129129
}
130130
}
131131

132132
// private methods
133-
private IWriteOperation<BsonDocument> CreateOperation(IChannel channel)
133+
internal IWriteOperation<BsonDocument> CreateOperation(SemanticVersion serverVersion)
134134
{
135-
if (Feature.CreateIndexesCommand.IsSupported(channel.ConnectionDescription.ServerVersion))
135+
if (Feature.CreateIndexesCommand.IsSupported(serverVersion))
136136
{
137-
return new CreateIndexesUsingCommandOperation(_collectionNamespace, _requests, _messageEncoderSettings);
137+
return new CreateIndexesUsingCommandOperation(_collectionNamespace, _requests, _messageEncoderSettings)
138+
{
139+
WriteConcern = _writeConcern
140+
};
138141
}
139142
else
140143
{
141-
return new CreateIndexesUsingInsertOperation(_collectionNamespace, _requests, _messageEncoderSettings);
144+
return new CreateIndexesUsingInsertOperation(_collectionNamespace, _requests, _messageEncoderSettings)
145+
{
146+
WriteConcern = _writeConcern
147+
};
142148
}
143149
}
144150
}

src/MongoDB.Driver.Core/Core/Operations/CreateIndexesUsingCommandOperation.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CreateIndexesUsingCommandOperation : IWriteOperation<BsonDocument>
4141

4242
// constructors
4343
/// <summary>
44-
/// Initializes a new instance of the <see cref="CreateIndexesOperation"/> class.
44+
/// Initializes a new instance of the <see cref="CreateIndexesUsingCommandOperation"/> class.
4545
/// </summary>
4646
/// <param name="collectionNamespace">The collection namespace.</param>
4747
/// <param name="requests">The requests.</param>
@@ -135,7 +135,8 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
135135
return new BsonDocument
136136
{
137137
{ "createIndexes", _collectionNamespace.CollectionName },
138-
{ "indexes", new BsonArray(_requests.Select(request => request.CreateIndexDocument(serverVersion))) }
138+
{ "indexes", new BsonArray(_requests.Select(request => request.CreateIndexDocument(serverVersion))) },
139+
{ "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
139140
};
140141
}
141142

src/MongoDB.Driver.Core/Core/Operations/CreateIndexesUsingInsertOperation.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CreateIndexesUsingInsertOperation : IWriteOperation<BsonDocument>
4141

4242
// constructors
4343
/// <summary>
44-
/// Initializes a new instance of the <see cref="CreateIndexesOperation"/> class.
44+
/// Initializes a new instance of the <see cref="CreateIndexesUsingInsertOperation"/> class.
4545
/// </summary>
4646
/// <param name="collectionNamespace">The collection namespace.</param>
4747
/// <param name="requests">The requests.</param>
@@ -140,7 +140,7 @@ public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, Cancellation
140140
}
141141

142142
// private methods
143-
private InsertOpcodeOperation<BsonDocument> CreateOperation(SemanticVersion serverVersion, CreateIndexRequest createIndexRequest)
143+
internal InsertOpcodeOperation<BsonDocument> CreateOperation(SemanticVersion serverVersion, CreateIndexRequest createIndexRequest)
144144
{
145145
var systemIndexesCollection = _collectionNamespace.DatabaseNamespace.SystemIndexesCollection;
146146
var document = createIndexRequest.CreateIndexDocument(serverVersion);
@@ -150,7 +150,10 @@ private InsertOpcodeOperation<BsonDocument> CreateOperation(SemanticVersion serv
150150
systemIndexesCollection,
151151
documentSource,
152152
BsonDocumentSerializer.Instance,
153-
_messageEncoderSettings);
153+
_messageEncoderSettings)
154+
{
155+
WriteConcern = _writeConcern
156+
};
154157
}
155158
}
156159
}

src/MongoDB.Driver.Core/Core/Operations/CreateViewOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
183183
{ "viewOn", _viewOn },
184184
{ "pipeline", new BsonArray(_pipeline) },
185185
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
186-
{ "writeConcern", () => _writeConcern.ToBsonDocument(), _writeConcern != null && !_writeConcern.IsServerDefault && Feature.CommandsWriteConcern.IsSupported(serverVersion) }
186+
{ "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
187187
};
188188
}
189189

src/MongoDB.Driver.Core/Core/Operations/DropCollectionOperation.cs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ public class DropCollectionOperation : IWriteOperation<BsonDocument>
3232
// fields
3333
private readonly CollectionNamespace _collectionNamespace;
3434
private readonly MessageEncoderSettings _messageEncoderSettings;
35+
private WriteConcern _writeConcern;
3536

3637
// constructors
3738
/// <summary>
@@ -70,54 +71,82 @@ public MessageEncoderSettings MessageEncoderSettings
7071
get { return _messageEncoderSettings; }
7172
}
7273

74+
/// <summary>
75+
/// Gets or sets the write concern.
76+
/// </summary>
77+
/// <value>
78+
/// The write concern.
79+
/// </value>
80+
public WriteConcern WriteConcern
81+
{
82+
get { return _writeConcern; }
83+
set { _writeConcern = value; }
84+
}
85+
7386
// public methods
7487
/// <inheritdoc/>
7588
public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
7689
{
7790
Ensure.IsNotNull(binding, nameof(binding));
78-
var operation = CreateOperation();
79-
try
80-
{
81-
return operation.Execute(binding, cancellationToken);
82-
}
83-
catch (MongoCommandException ex)
91+
92+
using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
93+
using (var channel = channelSource.GetChannel(cancellationToken))
94+
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
8495
{
85-
if (ShouldIgnoreException(ex))
96+
var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
97+
try
98+
{
99+
return operation.Execute(binding, cancellationToken);
100+
}
101+
catch (MongoCommandException ex)
86102
{
87-
return ex.Result;
103+
if (ShouldIgnoreException(ex))
104+
{
105+
return ex.Result;
106+
}
107+
throw;
88108
}
89-
throw;
90109
}
91110
}
92111

93112
/// <inheritdoc/>
94113
public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
95114
{
96115
Ensure.IsNotNull(binding, nameof(binding));
97-
var operation = CreateOperation();
98-
try
99-
{
100-
return await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
101-
}
102-
catch (MongoCommandException ex)
116+
117+
using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
118+
using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
119+
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
103120
{
104-
if (ShouldIgnoreException(ex))
121+
var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
122+
try
123+
{
124+
return await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
125+
}
126+
catch (MongoCommandException ex)
105127
{
106-
return ex.Result;
128+
if (ShouldIgnoreException(ex))
129+
{
130+
return ex.Result;
131+
}
132+
throw;
107133
}
108-
throw;
109134
}
110135
}
111136

112137
// private methods
113-
internal BsonDocument CreateCommand()
138+
internal BsonDocument CreateCommand(SemanticVersion serverVersion)
114139
{
115-
return new BsonDocument { { "drop", _collectionNamespace.CollectionName } };
140+
return new BsonDocument
141+
{
142+
{ "drop", _collectionNamespace.CollectionName },
143+
{ "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
144+
};
116145
}
117146

118-
private WriteCommandOperation<BsonDocument> CreateOperation()
147+
private WriteCommandOperation<BsonDocument> CreateOperation(SemanticVersion serverVersion)
119148
{
120-
var command = CreateCommand();
149+
var command = CreateCommand(serverVersion);
121150
return new WriteCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
122151
}
123152

0 commit comments

Comments
 (0)