From 782ad86ba6abc999b2c100b214ce6776465e15a2 Mon Sep 17 00:00:00 2001 From: rstam Date: Tue, 21 Jan 2025 16:32:33 -0800 Subject: [PATCH] CSHARP-5321: Minor refactoring. --- ...lientSideProjectionSnippetsDeserializer.cs | 1 - ...ter.cs => ClientSideProjectionRewriter.cs} | 31 +++++++++---------- .../ClientSideProjectionSnippetsTranslator.cs | 2 +- .../SelectMethodToPipelineTranslator.cs | 2 +- .../Linq/LinqProviderAdapter.cs | 4 +-- 5 files changed, 19 insertions(+), 21 deletions(-) rename src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/{ClientSideProjectionExpressionRewriter.cs => ClientSideProjectionRewriter.cs} (77%) diff --git a/src/MongoDB.Driver/ClientSideProjectionSnippetsDeserializer.cs b/src/MongoDB.Driver/ClientSideProjectionSnippetsDeserializer.cs index 195fa3ac2cf..0b49a540c4b 100644 --- a/src/MongoDB.Driver/ClientSideProjectionSnippetsDeserializer.cs +++ b/src/MongoDB.Driver/ClientSideProjectionSnippetsDeserializer.cs @@ -14,7 +14,6 @@ */ using System; -using System.Linq; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionExpressionRewriter.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionRewriter.cs similarity index 77% rename from src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionExpressionRewriter.cs rename to src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionRewriter.cs index 3f73ed99a44..cb5a71c23b5 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionExpressionRewriter.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionRewriter.cs @@ -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)) @@ -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); } @@ -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++) diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionSnippetsTranslator.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionSnippetsTranslator.cs index ff74a684324..edab477917b 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionSnippetsTranslator.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ClientSideProjectionSnippetsTranslator.cs @@ -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 { diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/SelectMethodToPipelineTranslator.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/SelectMethodToPipelineTranslator.cs index 6362cba2eef..4d94063aec0 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/SelectMethodToPipelineTranslator.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/SelectMethodToPipelineTranslator.cs @@ -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 ? diff --git a/src/MongoDB.Driver/Linq/LinqProviderAdapter.cs b/src/MongoDB.Driver/Linq/LinqProviderAdapter.cs index 4f57d68b3dc..cbd1219711d 100644 --- a/src/MongoDB.Driver/Linq/LinqProviderAdapter.cs +++ b/src/MongoDB.Driver/Linq/LinqProviderAdapter.cs @@ -188,8 +188,8 @@ private static RenderedProjectionDefinition 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(projectionDocument, (IBsonSerializer)projectionSerializer); } }