Skip to content

Commit 4ca7541

Browse files
author
Oleksandr Poliakov
committed
CSHARP-5560: CSOT: Refactor IOperationExecutor to use operation context
1 parent 4ecb179 commit 4ca7541

15 files changed

+1605
-1680
lines changed

.editorconfig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ dotnet_style_qualification_for_property = false:suggestion
4747
dotnet_style_qualification_for_method = false:suggestion
4848
dotnet_style_qualification_for_event = false:suggestion
4949

50-
# Types: use keywords instead of BCL types, and permit var only when the type is clear
51-
csharp_style_var_for_built_in_types = false:suggestion
52-
csharp_style_var_when_type_is_apparent = false:none
53-
csharp_style_var_elsewhere = false:suggestion
50+
# Types: use keywords instead of BCL types, and prefer var instead of the explicit type
51+
csharp_style_var_for_built_in_types = true:suggestion
52+
csharp_style_var_when_type_is_apparent = true:suggestion
53+
csharp_style_var_elsewhere = true:suggestion
5454
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
5555
dotnet_style_predefined_type_for_member_access = true:suggestion
5656

src/MongoDB.Driver/AggregateHelper.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Copyright 2010-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+
using System;
17+
using System.Linq;
18+
using MongoDB.Bson;
19+
20+
namespace MongoDB.Driver
21+
{
22+
internal static class AggregateHelper
23+
{
24+
public static RenderedPipelineDefinition<TResult> RenderAggregatePipeline<TDocument, TResult>(PipelineDefinition<TDocument, TResult> pipeline, RenderArgs<TDocument> renderArgs, out bool isAggregateToCollection)
25+
{
26+
var renderedPipeline = pipeline.Render(renderArgs);
27+
28+
var lastStage = renderedPipeline.Documents.LastOrDefault();
29+
var lastStageName = lastStage?.GetElement(0).Name;
30+
isAggregateToCollection = lastStageName == "$out" || lastStageName == "$merge";
31+
32+
return renderedPipeline;
33+
}
34+
35+
public static CollectionNamespace GetOutCollection(BsonDocument outStage, DatabaseNamespace defaultDatabaseNamespace)
36+
{
37+
var stageName = outStage.GetElement(0).Name;
38+
switch (stageName)
39+
{
40+
case "$out":
41+
{
42+
var outValue = outStage[0];
43+
DatabaseNamespace outputDatabaseNamespace;
44+
string outputCollectionName;
45+
if (outValue.IsString)
46+
{
47+
outputDatabaseNamespace = defaultDatabaseNamespace;
48+
outputCollectionName = outValue.AsString;
49+
}
50+
else
51+
{
52+
outputDatabaseNamespace = new DatabaseNamespace(outValue["db"].AsString);
53+
outputCollectionName = outValue["coll"].AsString;
54+
}
55+
return new CollectionNamespace(outputDatabaseNamespace, outputCollectionName);
56+
}
57+
case "$merge":
58+
{
59+
var mergeArguments = outStage[0];
60+
DatabaseNamespace outputDatabaseNamespace;
61+
string outputCollectionName;
62+
if (mergeArguments.IsString)
63+
{
64+
outputDatabaseNamespace = defaultDatabaseNamespace;
65+
outputCollectionName = mergeArguments.AsString;
66+
}
67+
else
68+
{
69+
var into = mergeArguments.AsBsonDocument["into"];
70+
if (into.IsString)
71+
{
72+
outputDatabaseNamespace = defaultDatabaseNamespace;
73+
outputCollectionName = into.AsString;
74+
}
75+
else
76+
{
77+
if (into.AsBsonDocument.Contains("db"))
78+
{
79+
outputDatabaseNamespace = new DatabaseNamespace(into["db"].AsString);
80+
}
81+
else
82+
{
83+
outputDatabaseNamespace = defaultDatabaseNamespace;
84+
}
85+
outputCollectionName = into["coll"].AsString;
86+
}
87+
}
88+
return new CollectionNamespace(outputDatabaseNamespace, outputCollectionName);
89+
}
90+
default:
91+
throw new ArgumentException($"Unexpected stage name: {stageName}.");
92+
}
93+
}
94+
}
95+
}
96+

src/MongoDB.Driver/IOperationExecutor.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,42 @@
1616
using System;
1717
using System.Threading;
1818
using System.Threading.Tasks;
19-
using MongoDB.Driver.Core.Bindings;
2019
using MongoDB.Driver.Core.Operations;
2120

2221
namespace MongoDB.Driver
2322
{
24-
internal interface IOperationExecutor
23+
internal interface IOperationExecutor : IDisposable
2524
{
26-
TResult ExecuteReadOperation<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken);
27-
Task<TResult> ExecuteReadOperationAsync<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken);
25+
TResult ExecuteReadOperation<TResult>(
26+
IReadOperation<TResult> operation,
27+
ReadOperationOptions options,
28+
IClientSessionHandle session = null,
29+
bool disableChannelPinning = false,
30+
CancellationToken cancellationToken = default);
2831

29-
TResult ExecuteWriteOperation<TResult>(IWriteBinding binding, IWriteOperation<TResult> operation, CancellationToken cancellationToken);
30-
Task<TResult> ExecuteWriteOperationAsync<TResult>(IWriteBinding binding, IWriteOperation<TResult> operation, CancellationToken cancellationToken);
32+
Task<TResult> ExecuteReadOperationAsync<TResult>(
33+
IReadOperation<TResult> operation,
34+
ReadOperationOptions options,
35+
IClientSessionHandle session = null,
36+
bool disableChannelPinning = false,
37+
CancellationToken cancellationToken = default);
38+
39+
TResult ExecuteWriteOperation<TResult>(
40+
IWriteOperation<TResult> operation,
41+
WriteOperationOptions options,
42+
IClientSessionHandle session = null,
43+
bool disableChannelPinning = false,
44+
CancellationToken cancellationToken = default);
45+
46+
Task<TResult> ExecuteWriteOperationAsync<TResult>(
47+
IWriteOperation<TResult> operation,
48+
WriteOperationOptions options,
49+
IClientSessionHandle session = null,
50+
bool disableChannelPinning = false,
51+
CancellationToken cancellationToken = default);
3152

3253
IClientSessionHandle StartImplicitSession(CancellationToken cancellationToken);
54+
3355
Task<IClientSessionHandle> StartImplicitSessionAsync(CancellationToken cancellationToken);
3456
}
3557
}

0 commit comments

Comments
 (0)