Skip to content

Commit 7517619

Browse files
CSHARP-3109: allowDiskUse option for find should be documented as only being supported in 4.4+
1 parent 138939c commit 7517619

File tree

8 files changed

+274
-1
lines changed

8 files changed

+274
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class Feature
6060
private static readonly Feature __failPoints = new Feature("FailPoints", new SemanticVersion(2, 4, 0));
6161
private static readonly Feature __failPointsFailCommand = new Feature("FailPointsFailCommand", new SemanticVersion(4, 0, 0));
6262
private static readonly Feature __failPointsFailCommandForSharded = new Feature("FailPointsFailCommandForSharded", new SemanticVersion(4, 1, 5));
63+
private static readonly FindAllowDiskUseFeature __findAllowDiskUse = new FindAllowDiskUseFeature("FindAllowDiskUse", new SemanticVersion(4, 4, 0, ""));
6364
private static readonly Feature __findAndModifyWriteConcern = new Feature("FindAndModifyWriteConcern", new SemanticVersion(3, 2, 0));
6465
private static readonly Feature __findCommand = new Feature("FindCommand", new SemanticVersion(3, 2, 0));
6566
private static readonly Feature __geoNearCommand = new Feature("GeoNearCommand", new SemanticVersion(1, 0, 0), new SemanticVersion(4, 1, 0, ""));
@@ -283,6 +284,11 @@ public class Feature
283284
/// </summary>
284285
public static Feature FailPointsFailCommandForSharded => __failPointsFailCommandForSharded;
285286

287+
/// <summary>
288+
/// Gets the find allowDiskUse feature.
289+
/// </summary>
290+
public static FindAllowDiskUseFeature FindAllowDiskUse => __findAllowDiskUse;
291+
286292
/// <summary>
287293
/// Gets the find and modify write concern feature.
288294
/// </summary>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright 2020-present 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 find allowDiskUse feature.
20+
/// </summary>
21+
/// <seealso cref="MongoDB.Driver.Core.Misc.Feature" />
22+
public class FindAllowDiskUseFeature : Feature
23+
{
24+
private readonly SemanticVersion _firstServerVersionWhereWeRelyOnServerToReturnError = new SemanticVersion(3, 2, 0);
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="FindAllowDiskUseFeature"/> 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 FindAllowDiskUseFeature(string name, SemanticVersion firstSupportedVersion)
32+
: base(name, firstSupportedVersion)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Determines whether the driver must throw an exception if the feature is not supported by the server.
38+
/// </summary>
39+
/// <param name="serverVersion">The server version.</param>
40+
/// <returns>Whether the driver must throw if feature is not supported.</returns>
41+
public bool DriverMustThrowIfNotSupported(SemanticVersion serverVersion)
42+
{
43+
return serverVersion < _firstServerVersionWhereWeRelyOnServerToReturnError;
44+
}
45+
}
46+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,17 @@ internal FindOpcodeOperation<TDocument> CreateFindOpcodeOperation()
567567
{
568568
throw new NotSupportedException($"OP_QUERY does not support collations.");
569569
}
570+
if (_allowDiskUse.HasValue)
571+
{
572+
throw new NotSupportedException($"OP_QUERY does not support allowDiskUse.");
573+
}
570574

571575
#pragma warning disable 618
572576
var operation = new FindOpcodeOperation<TDocument>(
573577
_collectionNamespace,
574578
_resultSerializer,
575579
_messageEncoderSettings)
576580
{
577-
// note: FindOpcodeOperation does not support AllowDiskUse
578581
AllowPartialResults = _allowPartialResults,
579582
BatchSize = _batchSize,
580583
Comment = _comment,

tests/MongoDB.Driver.Core.Tests/Core/Operations/FindOperationTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,19 @@ public void CreateFindOpcodeOperation_should_return_expected_result_when_singleB
443443
result.Limit.Should().Be(-subject.Limit);
444444
}
445445

446+
[Fact]
447+
public void CreateFindOpcodeOperation_should_throw_when_AllowDiskUse_is_set()
448+
{
449+
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
450+
{
451+
AllowDiskUse = true
452+
};
453+
454+
var exception = Record.Exception(() => { subject.CreateFindOpcodeOperation(); });
455+
456+
exception.Should().BeOfType<NotSupportedException>();
457+
}
458+
446459
[Fact]
447460
public void CreateFindOpcodeOperation_should_throw_when_Collation_is_set()
448461
{
@@ -632,6 +645,38 @@ public void Execute_should_return_expected_result_when_Collation_is_set(
632645
result.Should().HaveCount(2);
633646
}
634647

648+
[SkippableTheory]
649+
[ParameterAttributeData]
650+
public void Execute_should_throw_when_AllowDiskUse_is_not_supported(
651+
[Values(false, true)] bool async,
652+
[Values(null, false, true)] bool? allowDiskUse)
653+
{
654+
var serverVersion = CoreTestConfiguration.ServerVersion;
655+
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
656+
{
657+
AllowDiskUse = allowDiskUse
658+
};
659+
660+
var exception = Record.Exception(() => { ExecuteOperation(subject, async); });
661+
662+
if (!allowDiskUse.HasValue)
663+
{
664+
exception.Should().BeNull();
665+
}
666+
else if (Feature.FindAllowDiskUse.DriverMustThrowIfNotSupported(serverVersion))
667+
{
668+
exception.Should().BeOfType<NotSupportedException>();
669+
}
670+
else if (Feature.FindAllowDiskUse.IsSupported(serverVersion))
671+
{
672+
exception.Should().BeNull();
673+
}
674+
else
675+
{
676+
exception.Should().BeOfType<MongoCommandException>();
677+
}
678+
}
679+
635680
[SkippableTheory]
636681
[ParameterAttributeData]
637682
public void Execute_should_throw_when_Collation_is_set_but_not_supported(
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"runOn": [
3+
{
4+
"maxServerVersion": "3.0.99"
5+
}
6+
],
7+
"collection_name": "test_find_allowdiskuse_clienterror",
8+
"tests": [
9+
{
10+
"description": "Find fails when allowDiskUse true is specified against pre 3.2 server",
11+
"operations": [
12+
{
13+
"object": "collection",
14+
"name": "find",
15+
"arguments": {
16+
"filter": {},
17+
"allowDiskUse": true
18+
},
19+
"error": true
20+
}
21+
],
22+
"expectations": []
23+
},
24+
{
25+
"description": "Find fails when allowDiskUse false is specified against pre 3.2 server",
26+
"operations": [
27+
{
28+
"object": "collection",
29+
"name": "find",
30+
"arguments": {
31+
"filter": {},
32+
"allowDiskUse": false
33+
},
34+
"error": true
35+
}
36+
],
37+
"expectations": []
38+
}
39+
]
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
runOn:
2+
- { maxServerVersion: "3.0.99" }
3+
4+
collection_name: &collection_name 'test_find_allowdiskuse_clienterror'
5+
6+
tests:
7+
-
8+
description: "Find fails when allowDiskUse true is specified against pre 3.2 server"
9+
operations:
10+
-
11+
object: collection
12+
name: find
13+
arguments:
14+
filter: { }
15+
allowDiskUse: true
16+
error: true
17+
expectations: []
18+
-
19+
description: "Find fails when allowDiskUse false is specified against pre 3.2 server"
20+
operations:
21+
-
22+
object: collection
23+
name: find
24+
arguments:
25+
filter: { }
26+
allowDiskUse: false
27+
error: true
28+
expectations: []
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"runOn": [
3+
{
4+
"minServerVersion": "3.2",
5+
"maxServerVersion": "4.3.0"
6+
}
7+
],
8+
"collection_name": "test_find_allowdiskuse_servererror",
9+
"tests": [
10+
{
11+
"description": "Find fails when allowDiskUse true is specified against pre 4.4 server (server-side error)",
12+
"operations": [
13+
{
14+
"object": "collection",
15+
"name": "find",
16+
"arguments": {
17+
"filter": {},
18+
"allowDiskUse": true
19+
},
20+
"error": true
21+
}
22+
],
23+
"expectations": [
24+
{
25+
"command_started_event": {
26+
"command": {
27+
"find": "test_find_allowdiskuse_servererror",
28+
"filter": {},
29+
"allowDiskUse": true
30+
}
31+
}
32+
}
33+
]
34+
},
35+
{
36+
"description": "Find fails when allowDiskUse false is specified against pre 4.4 server (server-side error)",
37+
"operations": [
38+
{
39+
"object": "collection",
40+
"name": "find",
41+
"arguments": {
42+
"filter": {},
43+
"allowDiskUse": false
44+
},
45+
"error": true
46+
}
47+
],
48+
"expectations": [
49+
{
50+
"command_started_event": {
51+
"command": {
52+
"find": "test_find_allowdiskuse_servererror",
53+
"filter": {},
54+
"allowDiskUse": false
55+
}
56+
}
57+
}
58+
]
59+
}
60+
]
61+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
runOn:
2+
# These tests assert that the driver does not raise client-side errors and
3+
# instead relies on the server to raise an error. Server versions >= 3.2.0
4+
# raise errors for unknown find options, and server versions >= 4.3.1
5+
# support the allowDiskUse option in find.
6+
- { minServerVersion: "3.2", maxServerVersion: "4.3.0" }
7+
8+
collection_name: &collection_name 'test_find_allowdiskuse_servererror'
9+
10+
tests:
11+
-
12+
description: "Find fails when allowDiskUse true is specified against pre 4.4 server (server-side error)"
13+
operations:
14+
-
15+
object: collection
16+
name: find
17+
arguments:
18+
filter: &filter { }
19+
allowDiskUse: true
20+
error: true
21+
expectations:
22+
-
23+
command_started_event:
24+
command:
25+
find: *collection_name
26+
filter: *filter
27+
allowDiskUse: true
28+
-
29+
description: "Find fails when allowDiskUse false is specified against pre 4.4 server (server-side error)"
30+
operations:
31+
-
32+
object: collection
33+
name: find
34+
arguments:
35+
filter: *filter
36+
allowDiskUse: false
37+
error: true
38+
expectations:
39+
-
40+
command_started_event:
41+
command:
42+
find: *collection_name
43+
filter: *filter
44+
allowDiskUse: false

0 commit comments

Comments
 (0)