Exemplo n.º 1
0
        /// <summary>   Reads a map reduce delcaration </summary>
        ///
        /// <remarks>   Vasko, 06-Dec-17. </remarks>
        ///
        /// <returns>   The map reduce expression. </returns>

        public MapReduceExpression ReadMapReduce()
        {
            if (Reader.Current.TokenType != TokenType.Reduce)
            {
                return(null);
            }
            Reader.DiscardToken(TokenType.Reduce);
            var currentCursor        = Reader.Marker.Clone();
            var reduceKeyExpressions = ReadExpressions(Filters.Keyword(currentCursor));

            Reader.DiscardToken(TokenType.ReduceMap);
            currentCursor = Reader.Marker.Clone();
            var valueExpressions = ReadExpressions(Filters.Keyword(currentCursor));
            IEnumerable <IExpression> aggregate = new List <IExpression>();

            if (Reader.Current.TokenType == TokenType.ReduceAggregate)
            {
                Reader.DiscardToken(TokenType.ReduceAggregate);
                currentCursor = Reader.Marker.Clone();
                aggregate     = ReadExpressions(Filters.Keyword(currentCursor));
            }
            var mapReduce = new MapReduceExpression()
            {
                Keys         = reduceKeyExpressions.Cast <AssignmentExpression>(),
                ValueMembers = valueExpressions.Cast <AssignmentExpression>(),
                Aggregate    = new MapAggregateExpression(aggregate.Cast <AssignmentExpression>())
            };

            return(mapReduce);
        }
Exemplo n.º 2
0
        public static MapReduceJsScript Create(MapReduceExpression mapReduce)
        {
            var keyGenerator = mapReduce.GetCodeGenerator();
            var aggGenerator = mapReduce.Aggregate.GetCodeGenerator();
            var keyValue     = keyGenerator.GenerateFromExpression(mapReduce);
            var aggValue     = aggGenerator.GenerateFromExpression(mapReduce.Aggregate);
            var sc           = new MapReduceJsScript()
            {
                Reduce = aggValue,
                Map    = keyValue
            };

            return(sc);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mapReduce"></param>
        /// <param name="keyBuff"></param>
        /// <param name="valueBuff"></param>
        private static void GetMapReduceContent(MapReduceExpression mapReduce, ref StringBuilder keyBuff, ref StringBuilder valueBuff)
        {
            if (keyBuff == null)
            {
                keyBuff = new StringBuilder();
            }
            if (valueBuff == null)
            {
                valueBuff = new StringBuilder();
            }
            var lstKeys    = VisitVariables(mapReduce.Keys, keyBuff, new JsGeneratingExpressionVisitor());
            var lstValues  = VisitVariables(mapReduce.ValueMembers, valueBuff, new JsGeneratingExpressionVisitor());
            var keyParts   = lstKeys.Select(x => $"'{x}' : {x}").ToArray();
            var valueParts = lstValues.Select(x => $"'{x}' : {x}").ToArray();
            var keysPart   = String.Join(",", keyParts) + '\n';
            var valuesPart = String.Join(",", valueParts) + '\n';

            keyBuff.AppendLine("var __key = {  " + keysPart + "};");
            valueBuff.AppendLine("var __value = { " + valuesPart + "};");
        }