public void TestReduceAll()
        {
            RunTestVariants(() => {
                CreateIndex();

                var options      = C4QueryOptions.Default;
                var context      = new CountContext();
                var reduce       = new C4ManagedReduceFunction(CountAccumulate, CountReduce, context);
                var reduceNative = reduce.Native;
                options.reduce   = &reduceNative;

                var e = (C4QueryEnumerator *)LiteCoreBridge.Check(err => {
                    var localOpts = options;
                    return(Native.c4view_query(_view, &localOpts, err));
                });

                // First row:
                C4Error error;
                Native.c4queryenum_next(e, &error).Should().BeTrue("because otherwise the query failed");
                var valueStr = e->value.CreateString();
                Console.WriteLine($"Key: {Native.c4key_toJSON(&e->key)} Value: {valueStr}");
                Native.c4key_toJSON(&e->key).Should().BeNull("because the reduce has no key");
                valueStr.Should().Be("200", "because the reduce function should return the proper value");

                // No more rows:
                Native.c4queryenum_next(e, &error).Should().BeFalse("because the reduce function only contains one row");
                Native.c4queryenum_free(e);
                error.Code.Should().Be(0, "because otherwise an error occurred somewhere");
                ;
            });
        }
Beispiel #2
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var returnType = methodCall.Method.ReturnType;
            var sequence   = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

            if (sequence.SqlQuery != buildInfo.SqlQuery)
            {
                if (sequence is JoinBuilder.GroupJoinSubQueryContext)
                {
                    var ctx = new CountContext(buildInfo.Parent, sequence, returnType)
                    {
                        SqlQuery = ((JoinBuilder.GroupJoinSubQueryContext)sequence).GetCounter(methodCall)
                    };

                    ctx.Sql        = ctx.SqlQuery;
                    ctx.FieldIndex = ctx.SqlQuery.Select.Add(SqlFunction.CreateCount(returnType, ctx.SqlQuery), "cnt");

                    return(ctx);
                }

                if (sequence is GroupByBuilder.GroupByContext)
                {
                    return(new CountContext(buildInfo.Parent, sequence, returnType)
                    {
                        Sql = SqlFunction.CreateCount(returnType, sequence.SqlQuery),
                        FieldIndex = -1
                    });
                }
            }

            if (sequence.SqlQuery.Select.IsDistinct ||
                sequence.SqlQuery.Select.TakeValue != null ||
                sequence.SqlQuery.Select.SkipValue != null ||
                !sequence.SqlQuery.GroupBy.IsEmpty)
            {
                sequence.ConvertToIndex(null, 0, ConvertFlags.Key);
                sequence = new SubQueryContext(sequence);
            }

            if (sequence.SqlQuery.OrderBy.Items.Count > 0)
            {
                if (sequence.SqlQuery.Select.TakeValue == null && sequence.SqlQuery.Select.SkipValue == null)
                {
                    sequence.SqlQuery.OrderBy.Items.Clear();
                }
                else
                {
                    sequence = new SubQueryContext(sequence);
                }
            }

            var context = new CountContext(buildInfo.Parent, sequence, returnType);

            context.Sql        = context.SqlQuery;
            context.FieldIndex = context.SqlQuery.Select.Add(SqlFunction.CreateCount(returnType, context.SqlQuery), "cnt");

            return(context);
        }
Beispiel #3
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var returnType = methodCall.Method.ReturnType;
            var sequence   = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0])
            {
                CreateSubQuery = true
            });

            if (sequence.SelectQuery != buildInfo.SelectQuery)
            {
                if (sequence is JoinBuilder.GroupJoinSubQueryContext)
                {
                    var ctx = new CountContext(buildInfo.Parent, sequence, returnType)
                    {
                        SelectQuery =
                            sequence.SelectQuery
                            //((JoinBuilder.GroupJoinSubQueryContext)sequence).GetCounter(methodCall)
                    };

                    ctx.Sql        = ctx.SelectQuery;
                    ctx.FieldIndex = ctx.SelectQuery.Select.Add(SqlFunction.CreateCount(returnType, ctx.SelectQuery), "cnt");

                    return(ctx);
                }

                if (sequence is GroupByBuilder.GroupByContext)
                {
//					var ctx = new CountContext(buildInfo.Parent, sequence, returnType);
//
//					ctx.Sql        = ctx.SelectQuery;
//					ctx.FieldIndex = ctx.SelectQuery.Select.Add(SqlFunction.CreateCount(returnType, ctx.SelectQuery), "cnt");
//
//					return ctx;

//					return new CountContext(buildInfo.Parent, sequence, returnType)
//					{
//						Sql        = SqlFunction.CreateCount(returnType, sequence.SelectQuery),
//						FieldIndex = -1
//					};
                }
            }

            if (sequence.SelectQuery.Select.IsDistinct ||
                sequence.SelectQuery.Select.TakeValue != null ||
                sequence.SelectQuery.Select.SkipValue != null)
            {
                sequence.ConvertToIndex(null, 0, ConvertFlags.Key);
                sequence = new SubQueryContext(sequence);
            }
            else if (!sequence.SelectQuery.GroupBy.IsEmpty)
            {
                if (!builder.DataContext.SqlProviderFlags.IsSybaseBuggyGroupBy)
                {
                    sequence.SelectQuery.Select.Add(new SqlValue(0));
                }
                else
                {
                    foreach (var item in sequence.SelectQuery.GroupBy.Items)
                    {
                        sequence.SelectQuery.Select.Add(item);
                    }
                }

                sequence = new SubQueryContext(sequence);
            }

            if (sequence.SelectQuery.OrderBy.Items.Count > 0)
            {
                if (sequence.SelectQuery.Select.TakeValue == null && sequence.SelectQuery.Select.SkipValue == null)
                {
                    sequence.SelectQuery.OrderBy.Items.Clear();
                }
                else
                {
                    sequence = new SubQueryContext(sequence);
                }
            }

            var context = new CountContext(buildInfo.Parent, sequence, returnType);

            context.Sql        = context.SelectQuery;
            context.FieldIndex = context.SelectQuery.Select.Add(SqlFunction.CreateCount(returnType, context.SelectQuery), "cnt");

            return(context);
        }
        public void TestImportNames()
        {
            // Docs look like:
            // {"name":{"first":"Travis","last":"Mutchler"},"gender":"female","birthday":"1990-12-21","contact":{"address":{"street":"22 Kansas Cir","zip":"45384","city":"Wilberforce","state":"OH"},"email":["*****@*****.**","*****@*****.**"],"region":"937","phone":["937-3512486"]},"likes":["travelling"],"memberSince":"2010-01-01"}

            RunTestVariants(() => {
                var numDocs = ImportJSONLines("/Couchbase/example-datasets-master/RandomUsers/names_300000.json",
                                              TimeSpan.FromSeconds(15), true);
                var complete = numDocs == 300000;
                #if !DEBUG
                numDocs.Should().Be(300000, "because otherwise the operation was too slow");
                #endif
                {
                    var st         = Stopwatch.StartNew();
                    var totalLikes = IndexLikesView();
                    Console.WriteLine($"Total of {totalLikes} likes");
                    st.PrintReport("Indexing Likes view", numDocs, "doc");
                    if (complete)
                    {
                        totalLikes.Should().Be(345986, "because otherwise the index missed data set objects");
                    }
                }
                {
                    var st      = Stopwatch.StartNew();
                    var context = new CountContext();
                    using (var reduce = new C4ManagedReduceFunction(CountAccumulate, CountReduce, context)) {
                        var numLikes = QueryGrouped(_likesView, reduce.Native, true);
                        st.PrintReport("Querying all likes", numLikes, "like");
                        if (complete)
                        {
                            numLikes.Should().Be(15, "because that is the number of likes in the data set");
                        }
                    }
                }
                {
                    var st    = Stopwatch.StartNew();
                    var total = IndexStatesView();
                    st.PrintReport("Indexing States view", numDocs, "doc");
                    if (complete)
                    {
                        total.Should().Be(300000, "because otherwise the index missed some dataset objects");
                    }
                }
                {
                    var options = C4QueryOptions.Default;
                    var key     = Native.c4key_new();
                    NativeRaw.c4key_addString(key, C4Slice.Constant("WA"));
                    options.startKey = options.endKey = key;
                    var st           = Stopwatch.StartNew();
                    var total        = RunQuery(_statesView, options);
                    Native.c4key_free(key);
                    st.PrintReport("Querying States view", total, "row");
                    if (complete)
                    {
                        total.Should().Be(5053, "because that is the number of states in the data set");
                    }
                }
                {
                    if (Storage == C4StorageEngine.SQLite && !IsRevTrees())
                    {
                        for (int pass = 0; pass < 2; ++pass)
                        {
                            var st = Stopwatch.StartNew();
                            var n  = QueryWhere("{\"contact.address.state\": \"WA\"}");
                            st.PrintReport("SQL query of state", n, "doc");
                            if (complete)
                            {
                                n.Should().Be(5053, "because that is the number of states in the data set");
                            }
                            if (pass == 0)
                            {
                                var st2 = Stopwatch.StartNew();
                                LiteCoreBridge.Check(err => Native.c4db_createIndex(Db, "contact.address.state", err));
                                st2.PrintReport("Creating SQL index of state", 1, "index");
                            }
                        }
                    }
                }
            });
        }
Beispiel #5
0
        /// <summary>
        /// Returns the total number of expression items which are matching the given.
        /// <paramref name="func"/>.
        /// </summary>
        /// <param name="expr">Expression-Tree which gets counted.</param>
        /// <param name="context">Expression-Tree visitor context.</param>
        /// <param name="func">Predicate which is used to test if the given expression should be counted.</param>
        public static int GetCount <TContext>(this Expression expr, TContext context, Func <TContext, Expression, bool> func)
        {
            var ctx = new CountContext <TContext>(context, func);

            expr.Visit(ctx, static (context, e) =>
	public CountContext count() {
		CountContext _localctx = new CountContext(Context, State);
		EnterRule(_localctx, 366, RULE_count);
		try {
			EnterOuterAlt(_localctx, 1);
			{
			State = 2644; digits();
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			ErrorHandler.ReportError(this, re);
			ErrorHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}