private object InternalExecuteQuery(Expression expression, Delegate efn, CancellationToken cancellationToken = default) { var attrs = GetMethodAttributes(efn.Method); object result; if (attrs.Item1) { if (attrs.Item2) { var segment = SegmentFinder.Find(expression); result = efn.DynamicInvoke(_database, segment, cancellationToken); } else { result = efn.DynamicInvoke(_database, cancellationToken); } } else { if (attrs.Item2) { var segment = SegmentFinder.Find(expression); result = efn.DynamicInvoke(_database, segment); } else { result = efn.DynamicInvoke(_database); } } return(result); }
void Update() { //Always draw gyro and line var sample = new GyroSample(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z); transform.rotation = gyro.attitude; lineRenderer.SetPosition(0, transform.position); lineRenderer.SetPosition(1, transform.position + transform.forward * 20); //We're either recording, music playing or neither if (recording) { samples.Add(sample); //fileSystem.WriteToFile(sample.ToCsv()); } else if (musicPlaying) { var gyroWrapped = musicModeWrapper.WrapAttitudeValue(sample); var gyroWrappedAsVector = new Vector3(gyroWrapped.x, gyroWrapped.y, gyroWrapped.z); var closestSegmentIndex = SegmentFinder.FindIndexOfClosestSegment(segments, gyroWrappedAsVector); var insideRange = SegmentFinder.IsWithinEffectiveRange(segments[closestSegmentIndex], gyroWrappedAsVector); var currentSegment = segments[closestSegmentIndex]; segmentText.text = "Segment: " + closestSegmentIndex + ". Inside range: " + insideRange.ToString() + ". Note is: " + currentSegment.description; if (lastSegmentIndex == -1 || closestSegmentIndex != lastSegmentIndex) { currentSegment.Trigger(); lastSegmentIndex = closestSegmentIndex; } } }
/// <summary> /// 执行 <see cref="Expression"/> 的查询,返回查询结果。 /// </summary> /// <param name="expression">表示 LINQ 查询的表达式树。</param> /// <returns>单值对象。</returns> /// <exception cref="TranslateException">对 LINQ 表达式解析失败时抛出此异常。</exception> public object Execute(Expression expression) { var queryCache = ContextService.ServiceProvider.TryGetService(() => DefaultQueryCache.Instance); var efn = queryCache.TryGetDelegate(expression, GetCacheContext(), () => (LambdaExpression)GetExecutionPlan(expression)); var attrs = GetMethodAttributes(efn.Method); if (!attrs.Item2) { return(efn.DynamicInvoke(_database)); } var segment = SegmentFinder.Find(expression); return(efn.DynamicInvoke(_database, segment)); }
/// <summary> /// 异步的,尝试通过表达式获取执行后的结果缓存。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <param name="context"></param> /// <param name="creator"></param> /// <param name="cancellationToken"></param> /// <returns></returns> async Task <T> IExecuteCache.TryGetAsync <T>(Expression expression, ExecuteCacheContext context, Func <CancellationToken, Task <T> > creator, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>(); var option = section == null ? TranslateOptions.Default : section.Options; if (context.Enabled == true || (context.Enabled == null && option.CacheExecution)) { TryExpire(expression); } var evaluator = _serviceProvider.TryGetService <IExecuteCacheEvaluator>(); var result = CacheableChecker.Check(evaluator, expression); //没有开启数据缓存 if ((result.Enabled == null && (context.Enabled == false || (context.Enabled == null && !option.CacheExecution))) || result.Enabled == false || _cacheMgr == null) { return(await creator(cancellationToken)); } var generator = _serviceProvider.TryGetService <IExecuteCacheKeyGenerator>(() => ExpressionKeyGenerator.Instance); var cacheKey = _serviceProvider.GetCacheKey(generator.Generate(expression, CACHE_KEY)); Tracer.Debug($"DefaultExecuteCache access to '{cacheKey}'"); var segment = SegmentFinder.Find(expression); var pager = segment as DataPager; using var edps = new EntityDeserializeProcessorScope(); var cacheItem = _cacheMgr.TryGet(cacheKey, () => HandleCacheItem(cacheKey, expression, creator(cancellationToken).AsSync(), pager), () => new RelativeTime(GetExpire(result, option, context))); if (pager != null) { pager.RecordCount = cacheItem.Total; } TryStartRunner(); return(cacheItem.Data); }
/// <summary> /// 尝试获取指定表达式的缓存。 /// </summary> /// <param name="expression"></param> /// <param name="context"></param> /// <param name="creator">当缓存不存在时,创建缓存数据的函数。</param> /// <returns></returns> Delegate IQueryCache.TryGetDelegate(Expression expression, QueryCacheContext context, Func <LambdaExpression> creator) { var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>(); var option = section == null ? TranslateOptions.Default : section.Options; var result = CacheableChecker.Check(expression); if (!result.Required || (result.Enabled == null && (context.Enabled == false || (context.Enabled == null && !option.CacheParsing))) || result.Enabled == false) { return(creator().Compile()); } var generator = _serviceProvider.TryGetService <IQueryCacheKeyGenerator>(() => ExpressionKeyGenerator.Instance); var cacheKey = _serviceProvider.GetCacheKey(generator.Generate(expression, "Trans")); Tracer.Debug($"QueryCache access to '{cacheKey}'"); var cacheMgr = _serviceProvider.TryGetService <IMemoryCacheManager>(() => MemoryCacheManager.Instance); return(cacheMgr.TryGet(cacheKey, () => { var lambdaExp = creator() as LambdaExpression; var segment = SegmentFinder.Find(expression); if (segment != null) { //将表达式内的 Segment 替换成参数 var segParExp = Expression.Parameter(typeof(IDataSegment), "g"); var newExp = SegmentReplacer.Repalce(lambdaExp.Body, segParExp); var parameters = new List <ParameterExpression>(lambdaExp.Parameters); parameters.Insert(1, segParExp); lambdaExp = Expression.Lambda(newExp, parameters.ToArray()); } return lambdaExp.Compile(); }, () => new RelativeTime(GetExpire(result, option, context)))); }
/// <summary> /// Represents a message parser to a bundle /// </summary> internal static Bundle Parse(IGroup message) { Bundle retVal = new Bundle(); var finder = new SegmentFinder(message); while (finder.HasNextChild()) { finder.NextChild(); foreach (var current in finder.CurrentChildReps) { if (current is AbstractGroupItem) { foreach (var s in (current as AbstractGroupItem)?.Structures.OfType <IGroup>()) { var parsed = Parse(s); retVal.Item.AddRange(parsed.Item.Select(i => { var ret = i.Clone(); (ret as ITaggable)?.AddTag("$v2.group", current.GetStructureName()); return(ret); })); retVal.FocalObjects.AddRange(parsed.FocalObjects); } } else if (current is AbstractSegment) { // Empty, don't parse if (PipeParser.Encode(current as AbstractSegment, new EncodingCharacters('|', "^~\\&")).Length == 3) { continue; } var handler = SegmentHandlers.GetSegmentHandler(current.GetStructureName()); if (handler != null) { var parsed = handler.Parse(current as AbstractSegment, retVal.Item); if (parsed.Any()) { retVal.FocalObjects.AddRange(parsed .OfType <ITaggable>() .Where(o => o.GetTag(Hl7Constants.FocalObjectTag) == "true") .OfType <IIdentifiedEntity>() .Select(o => o.Key.GetValueOrDefault()) .Where(o => Guid.Empty != o) ); retVal.Item.AddRange(parsed.Select(i => { var ret = i.Clone(); (ret as ITaggable)?.AddTag("$v2.segment", current.GetStructureName()); return(ret); })); } } } else if (current is AbstractGroup) { var subObject = Parse(current as AbstractGroup); retVal.Item.Add(subObject); retVal.FocalObjects.AddRange(subObject.FocalObjects); } // Tag the objects } } return(retVal); }
/// <summary> /// Represents a message parser to a bundle /// </summary> internal static Bundle Parse(IGroup message) { Bundle retVal = new Bundle(); var finder = new SegmentFinder(message); while (finder.hasNextChild()) { finder.nextChild(); foreach (var current in finder.CurrentChildReps) { if (current is AbstractGroupItem) { foreach (var s in (current as AbstractGroupItem)?.Structures.OfType <IGroup>()) { var parsed = Parse(s); retVal.Item.AddRange(parsed.Item.Select(i => { var ret = i.Clone(); (ret as ITaggable)?.AddTag(".v2.group", current.GetStructureName()); return(ret); })); retVal.ExpansionKeys.AddRange(parsed.ExpansionKeys); } } else if (current is AbstractSegment) { // Empty, don't parse if (PipeParser.Encode(current as AbstractSegment, new EncodingCharacters('|', "^~\\&")).Length == 3) { continue; } var handler = SegmentHandlers.GetSegmentHandler(current.GetStructureName()); if (handler != null) { var parsed = handler.Parse(current as AbstractSegment, retVal.Item); if (parsed.Any()) { retVal.ExpansionKeys.Add(parsed.First().Key.GetValueOrDefault()); retVal.Item.AddRange(parsed.Select(i => { var ret = i.Clone(); (ret as ITaggable)?.AddTag(".v2.segment", current.GetStructureName()); return(ret); })); } } } else if (current is AbstractGroup) { var subObject = Parse(current as AbstractGroup); retVal.Item.AddRange(subObject.Item.Select(i => { var ret = i.Clone(); (ret as ITaggable)?.AddTag(".v2.group", current.GetStructureName()); return(ret); })); retVal.ExpansionKeys.AddRange(subObject.ExpansionKeys); } // Tag the objects } } return(retVal); }