|
| 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.Linq; |
| 17 | +using System.Linq.Expressions; |
| 18 | +using System.Reflection; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; |
| 22 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 23 | +using MongoDB.Driver.Linq.Linq3Implementation.Reflection; |
| 24 | +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators |
| 27 | +{ |
| 28 | + internal static class SkipWhileOrTakeWhileMethodToAggregationExpressionTranslator |
| 29 | + { |
| 30 | + private static MethodInfo[] __skipWhileOrTakeWhileMethods = |
| 31 | + { |
| 32 | + EnumerableMethod.SkipWhile, |
| 33 | + EnumerableMethod.TakeWhile, |
| 34 | + QueryableMethod.SkipWhile, |
| 35 | + QueryableMethod.TakeWhile |
| 36 | + }; |
| 37 | + |
| 38 | + private static MethodInfo[] __skipWhileMethods = |
| 39 | + { |
| 40 | + EnumerableMethod.SkipWhile, |
| 41 | + QueryableMethod.SkipWhile |
| 42 | + }; |
| 43 | + |
| 44 | + private static MethodInfo[] __takeWhileMethods = |
| 45 | + { |
| 46 | + EnumerableMethod.TakeWhile, |
| 47 | + QueryableMethod.TakeWhile |
| 48 | + }; |
| 49 | + |
| 50 | + public static TranslatedExpression Translate(TranslationContext context, MethodCallExpression expression) |
| 51 | + { |
| 52 | + var method = expression.Method; |
| 53 | + var arguments = expression.Arguments; |
| 54 | + |
| 55 | + if (method.IsOneOf(__skipWhileOrTakeWhileMethods)) |
| 56 | + { |
| 57 | + var sourceExpression = arguments[0]; |
| 58 | + var sourceTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, sourceExpression); |
| 59 | + NestedAsQueryableHelper.EnsureQueryableMethodHasNestedAsQueryableSource(expression, sourceTranslation); |
| 60 | + var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer); |
| 61 | + |
| 62 | + var predicateExpression = arguments[1]; |
| 63 | + var predicateLambda = ExpressionHelper.UnquoteLambdaIfQueryableMethod(method, predicateExpression); |
| 64 | + var predicateParameter = predicateLambda.Parameters.Single(); |
| 65 | + var thisSymbol = context.CreateSymbol(predicateParameter, "this", itemSerializer); |
| 66 | + var predicateTranslation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, predicateLambda, thisSymbol); |
| 67 | + |
| 68 | + var (sourceBinding, sourceAst) = AstExpression.UseVarIfNotSimple("source", sourceTranslation.Ast); |
| 69 | + |
| 70 | + var valueVar = AstExpression.Var("value"); |
| 71 | + var valuePredicateField = AstExpression.GetField(valueVar, "predicate"); |
| 72 | + var valueCountField = AstExpression.GetField(valueVar, "count"); |
| 73 | + |
| 74 | + var reduceAst = AstExpression.Reduce( |
| 75 | + input: sourceAst, |
| 76 | + initialValue: new BsonDocument { { "predicate", true }, { "count", 0 } }, |
| 77 | + @in: AstExpression.Switch( |
| 78 | + branches: |
| 79 | + [ |
| 80 | + (AstExpression.Not(valuePredicateField), valueVar), |
| 81 | + (predicateTranslation.Ast, AstExpression.ComputedDocument([new AstComputedField("predicate", true), new AstComputedField("count", AstExpression.Add(valueCountField, 1))])) |
| 82 | + ], |
| 83 | + @default: AstExpression.ComputedDocument([new AstComputedField("predicate", false), new AstComputedField("count", valueCountField)]))); |
| 84 | + |
| 85 | + var whileVar = AstExpression.Var("while"); |
| 86 | + var whileBinding = AstExpression.VarBinding(whileVar, reduceAst); |
| 87 | + var whileCountField = AstExpression.GetField(whileVar, "count"); |
| 88 | + |
| 89 | + var sliceAst = method switch |
| 90 | + { |
| 91 | + _ when method.IsOneOf(__skipWhileMethods) => AstExpression.Slice(sourceAst, whileCountField, int.MaxValue), |
| 92 | + _ when method.IsOneOf(__takeWhileMethods) => AstExpression.Slice(sourceAst, whileCountField), |
| 93 | + _ => throw new ExpressionNotSupportedException(expression) |
| 94 | + }; |
| 95 | + |
| 96 | + var ast = AstExpression.Let( |
| 97 | + sourceBinding, |
| 98 | + whileBinding, |
| 99 | + sliceAst); |
| 100 | + |
| 101 | + var resultSerializer = NestedAsQueryableSerializer.CreateIEnumerableOrNestedAsQueryableSerializer(expression.Type, itemSerializer); |
| 102 | + return new TranslatedExpression(expression, ast, resultSerializer); |
| 103 | + } |
| 104 | + |
| 105 | + throw new ExpressionNotSupportedException(expression); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments