Skip to content

Commit eeb85dc

Browse files
committed
CSHARP-1706: Code review changes.
1 parent bc1f602 commit eeb85dc

27 files changed

+130
-119
lines changed

src/MongoDB.Driver.Core/Collation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public enum CollationStrength
9999
/// <summary>
100100
/// Represents a MongoDB collation.
101101
/// </summary>
102-
public class Collation : IEquatable<Collation>, IConvertibleToBsonDocument
102+
public sealed class Collation : IEquatable<Collation>, IConvertibleToBsonDocument
103103
{
104104
#region static
105105
// private static fields
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
17+
18+
namespace MongoDB.Driver.Core.Misc
19+
{
20+
/// <summary>
21+
/// Represents the collation feature.
22+
/// </summary>
23+
/// <seealso cref="MongoDB.Driver.Core.Misc.Feature" />
24+
public class CollationFeature : Feature
25+
{
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="CollationFeature"/> class.
28+
/// </summary>
29+
/// <param name="name">The name of the feature.</param>
30+
/// <param name="firstSupportedVersion">The first server version that supports the feature.</param>
31+
public CollationFeature(string name, SemanticVersion firstSupportedVersion)
32+
: base(name, firstSupportedVersion)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Throws if collation value is not null and collations are not supported.
38+
/// </summary>
39+
/// <param name="serverVersion">The server version.</param>
40+
/// <param name="value">The value.</param>
41+
public void ThrowIfNotSupported(SemanticVersion serverVersion, Collation value)
42+
{
43+
if (value != null && !base.IsSupported(serverVersion))
44+
{
45+
throw new NotSupportedException($"Server version {serverVersion} does not support collations.");
46+
}
47+
}
48+
}
49+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Feature
2929
private static readonly Feature __aggregateExplain = new Feature("AggregateExplain", new SemanticVersion(2, 6, 0));
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));
32-
private static readonly Feature __collation = new Feature("Collation", new SemanticVersion(3, 3, 11));
32+
private static readonly CollationFeature __collation = new CollationFeature("Collation", new SemanticVersion(3, 3, 11));
3333
private static readonly Feature __createIndexesCommand = new Feature("CreateIndexesCommand", new SemanticVersion(3, 0, 0));
3434
private static readonly Feature __currentOpCommand = new Feature("CurrentOpCommand", new SemanticVersion(3, 2, 0));
3535
private static readonly Feature __documentValidation = new Feature("DocumentValidation", new SemanticVersion(3, 2, 0));
@@ -42,7 +42,7 @@ public class Feature
4242
private static readonly Feature __indexOptionsDefaults = new Feature("IndexOptionsDefaults", new SemanticVersion(3, 2, 0));
4343
private static readonly Feature __maxTime = new Feature("MaxTime", new SemanticVersion(2, 6, 0));
4444
private static readonly Feature __partialIndexes = new Feature("PartialIndexes", new SemanticVersion(3, 2, 0));
45-
private static readonly Feature __readConcern = new Feature("ReadConcern", new SemanticVersion(3, 2, 0));
45+
private static readonly ReadConcernFeature __readConcern = new ReadConcernFeature("ReadConcern", new SemanticVersion(3, 2, 0));
4646
private static readonly Feature __scramSha1Authentication = new Feature("ScramSha1Authentication", new SemanticVersion(3, 0, 0));
4747
private static readonly Feature __userManagementCommands = new Feature("UserManagementCommands", new SemanticVersion(2, 6, 0));
4848
private static readonly Feature __writeCommands = new Feature("WriteCommands", new SemanticVersion(2, 6, 0));
@@ -80,7 +80,7 @@ public class Feature
8080
/// <summary>
8181
/// Gets the collation feature.
8282
/// </summary>
83-
public static Feature Collation => __collation;
83+
public static CollationFeature Collation => __collation;
8484

8585
/// <summary>
8686
/// Gets the create indexes command feature.
@@ -145,7 +145,7 @@ public class Feature
145145
/// <summary>
146146
/// Gets the read concern feature.
147147
/// </summary>
148-
public static Feature ReadConcern => __readConcern;
148+
public static ReadConcernFeature ReadConcern => __readConcern;
149149

150150
/// <summary>
151151
/// Gets the scram sha1 authentication feature.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
17+
18+
namespace MongoDB.Driver.Core.Misc
19+
{
20+
/// <summary>
21+
/// Represents the read concern feature.
22+
/// </summary>
23+
/// <seealso cref="MongoDB.Driver.Core.Misc.Feature" />
24+
public class ReadConcernFeature : Feature
25+
{
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="ReadConcernFeature"/> class.
28+
/// </summary>
29+
/// <param name="name">The name of the feature.</param>
30+
/// <param name="firstSupportedVersion">The first server version that supports the feature.</param>
31+
public ReadConcernFeature(string name, SemanticVersion firstSupportedVersion)
32+
: base(name, firstSupportedVersion)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Throws if the read concern value is not the server default and read concern is not supported.
38+
/// </summary>
39+
/// <param name="serverVersion">The server version.</param>
40+
/// <param name="value">The value.</param>
41+
public void ThrowIfNotSupported(SemanticVersion serverVersion, ReadConcern value)
42+
{
43+
if (!value.IsServerDefault && !base.IsSupported(serverVersion))
44+
{
45+
throw new MongoClientException($"Server version {serverVersion} does not support read concern.");
46+
}
47+
}
48+
}
49+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ public IReadOnlyList<BsonDocument> Pipeline
126126
// methods
127127
internal BsonDocument CreateCommand(SemanticVersion serverVersion)
128128
{
129-
if (_collation != null && !Feature.Collation.IsSupported(serverVersion))
130-
{
131-
throw new NotSupportedException($"Server version {serverVersion} does not support collations.");
132-
}
129+
Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);
133130

134131
return new BsonDocument
135132
{

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,8 @@ public IReadOperation<BsonDocument> ToExplainOperation(ExplainVerbosity verbosit
229229

230230
internal BsonDocument CreateCommand(SemanticVersion serverVersion)
231231
{
232-
_readConcern.ThrowIfNotServerDefaultAndNotSupported(serverVersion);
233-
if (_collation != null && !Feature.Collation.IsSupported(serverVersion))
234-
{
235-
throw new NotSupportedException($"Server version {serverVersion} does not support collations.");
236-
}
232+
Feature.ReadConcern.ThrowIfNotSupported(serverVersion, _readConcern);
233+
Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);
237234

238235
var command = new BsonDocument
239236
{

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, Cancellation
169169

170170
internal BsonDocument CreateCommand(SemanticVersion serverVersion)
171171
{
172-
if (_collation != null && !Feature.Collation.IsSupported(serverVersion))
173-
{
174-
throw new NotSupportedException($"Server version {serverVersion} does not support collations.");
175-
}
172+
Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);
176173

177174
return new BsonDocument
178175
{

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ public DeleteBatchSerializer(ConnectionDescription connectionDescription, int ma
8282
protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request)
8383
{
8484
var deleteRequest = (DeleteRequest)request;
85-
if (deleteRequest.Collation != null && !Feature.Collation.IsSupported(ConnectionDescription.ServerVersion))
86-
{
87-
throw new NotSupportedException($"Server version {ConnectionDescription.ServerVersion} does not support collations.");
88-
}
85+
Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, deleteRequest.Collation);
8986

9087
var bsonWriter = (BsonBinaryWriter)context.Writer;
9188
bsonWriter.PushMaxDocumentSize(ConnectionDescription.MaxDocumentSize);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ private void SerializeFilter(BsonBinaryWriter bsonWriter, BsonDocument filter)
9292
protected override void SerializeRequest(BsonSerializationContext context, WriteRequest request)
9393
{
9494
var updateRequest = (UpdateRequest)request;
95-
if (updateRequest.Collation != null && !Feature.Collation.IsSupported(ConnectionDescription.ServerVersion))
96-
{
97-
throw new NotSupportedException($"Server version {ConnectionDescription.ServerVersion} does not support collations.");
98-
}
95+
Feature.Collation.ThrowIfNotSupported(ConnectionDescription.ServerVersion, updateRequest.Collation);
9996

10097
var bsonWriter = (BsonBinaryWriter)context.Writer;
10198
bsonWriter.PushMaxDocumentSize(ConnectionDescription.MaxWireDocumentSize);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,8 @@ public long? Skip
161161
// methods
162162
internal BsonDocument CreateCommand(SemanticVersion serverVersion)
163163
{
164-
_readConcern.ThrowIfNotServerDefaultAndNotSupported(serverVersion);
165-
if (_collation != null && !Feature.Collation.IsSupported(serverVersion))
166-
{
167-
throw new NotSupportedException($"Server version {serverVersion} does not support collations.");
168-
}
164+
Feature.ReadConcern.ThrowIfNotSupported(serverVersion, _readConcern);
165+
Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);
169166

170167
return new BsonDocument
171168
{

0 commit comments

Comments
 (0)