Exemplo n.º 1
0
        public static void ValueOfAny(object funcOrValue, Generator.Ctx ctx, int nMaxParallelism, Action <object> onComplete, CancellationToken cancellation)
        {
            if (nMaxParallelism < 1)
            {
                nMaxParallelism = 1024;
            }
            var sema = W.Common.Utils.NewAsyncSemaphore(nMaxParallelism);
            var ae   = new AsyncExprRootCtx(ctx.name2ndx, ctx.values, sema);

            ae.cancellationToken = cancellation;
            ThreadPool.QueueUserWorkItem(state =>
            {
                var task = ConstValueOf(ae, funcOrValue);
                //task.Start();
                task.ContinueWith(t =>
                {
                    try
                    {
                        onComplete(t.Result);
                    }
                    catch (Exception ex)
                    {
                        onComplete(ex);
                        Console.WriteLine(string.Join("; ", ctx.name2ndx.Keys));
                    }
                });
            });
        }
Exemplo n.º 2
0
        public static Task <object> GlobalValueOfAny(object funcOrValue, Generator.Ctx ctx, CancellationToken cancellation)
        {
            //if (nMaxParallelism < 1)
            //	nMaxParallelism = 128;
            //var sema = Cached<IAsyncSemaphore>("WExpr:GlobalMaxParallelismSemaphore",
            //	() => W.Common.Utils.NewAsyncSemaphore(nMaxParallelism),
            //	null, TimeSpan.FromMinutes(5), System.Web.Caching.Cache.NoAbsoluteExpiration);
            var sema = GlobalMaxParallelismSemaphore;
            var ae   = new AsyncExprRootCtx(ctx.name2ndx, ctx.values, sema);

            ae.cancellationToken = cancellation;
            return(ConstValueOf(ae, funcOrValue));
        }
Exemplo n.º 3
0
        public static Task <object> GlobalValueOfAny(object funcOrValue, Generator.Ctx ctx, int nMaxParallelism, CancellationToken cancellation)
        {
            if (nMaxParallelism < 1)
            {
                nMaxParallelism = 1024;
            }
            var sema = W.Common.Utils.NewAsyncSemaphore(nMaxParallelism);
            var ae   = new AsyncExprRootCtx(ctx.name2ndx, ctx.values, sema);

            ae.cancellationToken = cancellation;

            return(ConstValueOf(ae, funcOrValue));
        }
Exemplo n.º 4
0
        public static object ValueOfAny(object funcOrValue, int nMaxParallelism, Generator.Ctx ctx, CancellationToken cancellation)
        {
            if (nMaxParallelism < 1)
            {
                nMaxParallelism = 128;
            }
            var ae = new AsyncExprRootCtx(ctx.name2ndx, ctx.values, W.Common.Utils.NewAsyncSemaphore(nMaxParallelism));

            ae.cancellationToken = cancellation;
            var task = ConstValueOf(ae, funcOrValue);

            //task.Start();
            task.Wait();
            return(task.Exception ?? task.Result);
        }
Exemplo n.º 5
0
        public static async Task Load(IDictionary <string, object> dataExprsAndCookies, DateTime StartDate, DateTime EndDate,
                                      long[] ids, string idParamName, string contextInitializationScript,
                                      OnDataLoading onDataLoading,
                                      Action <float> onProgress,
                                      int maxPartSize,
                                      System.Threading.CancellationToken ct)
        {
            bool timeRange         = StartDate < EndDate;
            bool withAggr          = ids.Length > 1;
            int  nTotalPartsToLoad = 0;
            int  nPartsLoaded      = 0;

            AsyncFn fProgress = async(aec, args) =>
            {
                if (!ct.IsCancellationRequested)
                {
                    var info = (LoadingInfo)await OPs.ConstValueOf(aec, args[0]);

                    var value = await OPs.ConstValueOf(aec, args[1]);

                    var data = value as IList;
                    if (data != null)
                    {
                        var aggData = await info.Aggregate(aec, data);

                        onDataLoading(info.dataExpr, info.cookie, (IList)aggData);
                        Interlocked.Increment(ref nPartsLoaded);
                    }
                    else // calc n
                    {
                        int n = Convert.ToInt32(value);
                        Interlocked.Add(ref nTotalPartsToLoad, n);
                    }
                    if (onProgress != null)
                    {
                        onProgress(100f * nPartsLoaded / nTotalPartsToLoad);
                    }
                }
                return(string.Empty);
            };

            // initialize code generator context
            var defs = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            defs.Add(idParamName, ids);
            defs.Add("PROGRESS", new FuncDef(fProgress, 2, "PROGRESS"));
            if (timeRange)
            {
                defs.Add(nameof(ValueInfo.A_TIME__XT), StartDate.ToOADate());
                defs.Add(nameof(ValueInfo.B_TIME__XT), EndDate.ToOADate());
            }
            else
            {
                defs.Add(nameof(ValueInfo.At_TIME__XT), StartDate.ToOADate());
            }
            var rootCtx = new Generator.Ctx(defs, CoreFuncDefs().GetFuncs);

            Generator.Generate(Parser.ParseToExpr(contextInitializationScript), rootCtx);
            rootCtx.UseFuncs(new FuncDefs().AddFrom(withAggr ? typeof(FuncDefs_Aggr) : typeof(FuncDefs_NoAggr)).GetFuncs);

            var infos = new List <LoadingInfo>(dataExprsAndCookies.Count);
            var tasks = new List <Task>();

            foreach (var pair in dataExprsAndCookies)
            {
                infos.Add(new LoadingInfo(pair.Key, pair.Value, rootCtx));
            }

            var loadingExpr = Parser.ParseToExpr(@"(
	PartsOfLimitedSize("     + idParamName + " ," + maxPartSize.ToString() + @")
	..let(parts)
	,let(n,COLUMNS(parts)) .. PROGRESS("     + sLoadingInfo + @"),
	,_ParFor(
		let(i,0),
		i<n,
		(
			let("             + idParamName + @", parts[i])
			,FindSolutionExpr({}, "             + sUsedParamsDict + @".Keys()) . ExprToExecutable() . AtNdx(0) .. PROGRESS(" + sLoadingInfo + @")
			,let(i, i+1)
		)
	)
)");
            var rootAec     = new AsyncExprRootCtx(rootCtx.name2ndx, rootCtx.values, OPs.GlobalMaxParallelismSemaphore);

            foreach (var info in infos)
            {
                tasks.Add(Task.Factory.StartNew(() => info.LoadData(rootAec, loadingExpr, ct)).Unwrap());
            }
            await TaskEx.WhenAll(tasks);
        }