Skip to content

Commit

Permalink
CSHARP-5321: Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jan 22, 2025
1 parent cc78201 commit 782ad86
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
{
internal static class ClientSideProjectionExpressionRewriter
internal static class ClientSideProjectionRewriter
{
public static (AstProjectStage, IBsonSerializer) CreateClientSideProjection(
public static (AstProjectStage, IBsonSerializer) CreateProjectSnippetsStage(
TranslationContext context,
LambdaExpression projectionLambda,
IBsonSerializer sourceSerializer)
{
var (snippetsExpression, snippetsProjectionDeserializer) = ClientSideProjectionExpressionRewriter.TranslateLambdaBodyUsingSnippets(context, sourceSerializer, projectionLambda);
if (snippetsExpression == null)
var (snippetsAst, snippetsProjectionDeserializer) = RewriteProjectionUsingSnippets(context, projectionLambda, sourceSerializer);
if (snippetsAst == null)
{
return (null, snippetsProjectionDeserializer);
}
else
{
var snippetsTranslation = new AggregationExpression(projectionLambda, snippetsExpression, snippetsProjectionDeserializer);
var snippetsTranslation = new AggregationExpression(projectionLambda, snippetsAst, snippetsProjectionDeserializer);
return ProjectionHelper.CreateProjectStage(snippetsTranslation);
}
}

private static (AstComputedDocumentExpression, IBsonSerializer) TranslateLambdaBodyUsingSnippets(
private static (AstComputedDocumentExpression, IBsonSerializer) RewriteProjectionUsingSnippets(
TranslationContext context,
IBsonSerializer sourceSerializer,
LambdaExpression projectionLambda)
LambdaExpression projectionLambda,
IBsonSerializer sourceSerializer)
{
var wireVersion = context.TranslationOptions.CompatibilityLevel.ToWireVersion();
if (!Feature.FindProjectionExpressions.IsSupported(wireVersion))
Expand All @@ -65,9 +65,9 @@ private static (AstComputedDocumentExpression, IBsonSerializer) TranslateLambdaB
{
var snippetsComputedDocument = CreateSnippetsComputedDocument(snippets);
var snippetDeserializers = snippets.Select(s => s.Serializer).ToArray();
var rewrittenSelectorLamdba = RewriteSelector(projectionLambda, snippets);
var rewrittenSelectorDelegate = rewrittenSelectorLamdba.Compile();
var clientSideProjectionSnippetsDeserializer = ClientSideProjectionSnippetsDeserializer.Create(projectionLambda.ReturnType, snippetDeserializers, rewrittenSelectorDelegate);
var rewrittenProjectionLamdba = RewriteProjection(projectionLambda, snippets);
var rewrittenProjectionDelegate = rewrittenProjectionLamdba.Compile();
var clientSideProjectionSnippetsDeserializer = ClientSideProjectionSnippetsDeserializer.Create(projectionLambda.ReturnType, snippetDeserializers, rewrittenProjectionDelegate);
return (snippetsComputedDocument, clientSideProjectionSnippetsDeserializer);
}

Expand All @@ -77,14 +77,13 @@ private static (AstComputedDocumentExpression, IBsonSerializer) TranslateLambdaB
private static AstComputedDocumentExpression CreateSnippetsComputedDocument(AggregationExpression[] snippets)
{
var snippetsArray = AstExpression.ComputedArray(snippets.Select(s => s.Ast));
var snippetsdField = AstExpression.ComputedField("_snippets", snippetsArray);
return (AstComputedDocumentExpression)AstExpression.ComputedDocument([snippetsdField]);

var snippetsField = AstExpression.ComputedField("_snippets", snippetsArray);
return (AstComputedDocumentExpression)AstExpression.ComputedDocument([snippetsField]);
}

private static LambdaExpression RewriteSelector(LambdaExpression selectorLambda, AggregationExpression[] snippets)
private static LambdaExpression RewriteProjection(LambdaExpression projectionLambda, AggregationExpression[] snippets)
{
var rewrittenBody = selectorLambda.Body;
var rewrittenBody = projectionLambda.Body;
var snippetsParameter = Expression.Parameter(typeof(object[]), "snippets");

for (var i = 0; i < snippets.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override Expression Visit(Expression node)
{
var snippet = ExpressionToAggregationExpressionTranslator.Translate(_context, node);
_snippets.Add(snippet);
return node;
return node; // suppress any further visiting below this node
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static TranslatedPipeline Translate(TranslationContext context, MethodCal
}
catch (ExpressionNotSupportedException) when (context.TranslationOptions?.EnableClientSideProjections ?? false)
{
(projectStage, projectionSerializer) = ClientSideProjectionExpressionRewriter.CreateClientSideProjection(context, selectorLambda, sourceSerializer);
(projectStage, projectionSerializer) = ClientSideProjectionRewriter.CreateProjectSnippetsStage(context, selectorLambda, sourceSerializer);
}

return projectStage == null ?
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver/Linq/LinqProviderAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private static RenderedProjectionDefinition<TOutput> TranslateExpressionToProjec
}
catch (ExpressionNotSupportedException) when (translationOptions?.EnableClientSideProjections ?? false)
{
var (projectStage, projectionSerializer) = ClientSideProjectionExpressionRewriter.CreateClientSideProjection(context, expression, inputSerializer);
var projectionDocument = projectStage == null ? null : AstSimplifier.SimplifyAndConvert(projectStage).Render()["$project"].AsBsonDocument;
var (projectStage, projectionSerializer) = ClientSideProjectionRewriter.CreateProjectSnippetsStage(context, expression, inputSerializer);
var projectionDocument = projectStage == null ? null : simplifier.VisitAndConvert(projectStage).Render()["$project"].AsBsonDocument;
return new RenderedProjectionDefinition<TOutput>(projectionDocument, (IBsonSerializer<TOutput>)projectionSerializer);
}
}
Expand Down

0 comments on commit 782ad86

Please sign in to comment.