Skip to content

CSHARP-5560: CSOT: Refactor IOperationExecutor to use operation context #1676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Types: use keywords instead of BCL types, and permit var only when the type is clear
csharp_style_var_for_built_in_types = false:suggestion
csharp_style_var_when_type_is_apparent = false:none
csharp_style_var_elsewhere = false:suggestion
# Types: use keywords instead of BCL types, and prefer var instead of the explicit type
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

Expand Down
96 changes: 96 additions & 0 deletions src/MongoDB.Driver/AggregateHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Linq;
using MongoDB.Bson;

namespace MongoDB.Driver
{
internal static class AggregateHelper
{
public static RenderedPipelineDefinition<TResult> RenderAggregatePipeline<TDocument, TResult>(PipelineDefinition<TDocument, TResult> pipeline, RenderArgs<TDocument> renderArgs, out bool isAggregateToCollection)
{
var renderedPipeline = pipeline.Render(renderArgs);

var lastStage = renderedPipeline.Documents.LastOrDefault();
var lastStageName = lastStage?.GetElement(0).Name;
isAggregateToCollection = lastStageName == "$out" || lastStageName == "$merge";

return renderedPipeline;
}

public static CollectionNamespace GetOutCollection(BsonDocument outStage, DatabaseNamespace defaultDatabaseNamespace)
{
var stageName = outStage.GetElement(0).Name;
switch (stageName)
{
case "$out":
{
var outValue = outStage[0];
DatabaseNamespace outputDatabaseNamespace;
string outputCollectionName;
if (outValue.IsString)
{
outputDatabaseNamespace = defaultDatabaseNamespace;
outputCollectionName = outValue.AsString;
}
else
{
outputDatabaseNamespace = new DatabaseNamespace(outValue["db"].AsString);
outputCollectionName = outValue["coll"].AsString;
}
return new CollectionNamespace(outputDatabaseNamespace, outputCollectionName);
}
case "$merge":
{
var mergeArguments = outStage[0];
DatabaseNamespace outputDatabaseNamespace;
string outputCollectionName;
if (mergeArguments.IsString)
{
outputDatabaseNamespace = defaultDatabaseNamespace;
outputCollectionName = mergeArguments.AsString;
}
else
{
var into = mergeArguments.AsBsonDocument["into"];
if (into.IsString)
{
outputDatabaseNamespace = defaultDatabaseNamespace;
outputCollectionName = into.AsString;
}
else
{
if (into.AsBsonDocument.Contains("db"))
{
outputDatabaseNamespace = new DatabaseNamespace(into["db"].AsString);
}
else
{
outputDatabaseNamespace = defaultDatabaseNamespace;
}
outputCollectionName = into["coll"].AsString;
}
}
return new CollectionNamespace(outputDatabaseNamespace, outputCollectionName);
}
default:
throw new ArgumentException($"Unexpected stage name: {stageName}.");
}
}
}
}

34 changes: 28 additions & 6 deletions src/MongoDB.Driver/IOperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,42 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Bindings;
using MongoDB.Driver.Core.Operations;

namespace MongoDB.Driver
{
internal interface IOperationExecutor
internal interface IOperationExecutor : IDisposable
{
TResult ExecuteReadOperation<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken);
Task<TResult> ExecuteReadOperationAsync<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken);
TResult ExecuteReadOperation<TResult>(
IReadOperation<TResult> operation,
ReadOperationOptions options,
IClientSessionHandle session = null,
bool disableChannelPinning = false,
CancellationToken cancellationToken = default);

TResult ExecuteWriteOperation<TResult>(IWriteBinding binding, IWriteOperation<TResult> operation, CancellationToken cancellationToken);
Task<TResult> ExecuteWriteOperationAsync<TResult>(IWriteBinding binding, IWriteOperation<TResult> operation, CancellationToken cancellationToken);
Task<TResult> ExecuteReadOperationAsync<TResult>(
IReadOperation<TResult> operation,
ReadOperationOptions options,
IClientSessionHandle session = null,
bool disableChannelPinning = false,
CancellationToken cancellationToken = default);

TResult ExecuteWriteOperation<TResult>(
IWriteOperation<TResult> operation,
WriteOperationOptions options,
IClientSessionHandle session = null,
bool disableChannelPinning = false,
CancellationToken cancellationToken = default);

Task<TResult> ExecuteWriteOperationAsync<TResult>(
IWriteOperation<TResult> operation,
WriteOperationOptions options,
IClientSessionHandle session = null,
bool disableChannelPinning = false,
CancellationToken cancellationToken = default);

IClientSessionHandle StartImplicitSession(CancellationToken cancellationToken);

Task<IClientSessionHandle> StartImplicitSessionAsync(CancellationToken cancellationToken);
}
}
Loading