diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 3a381061..644aa515 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -6,6 +6,7 @@ using System.Data; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Sample.MySql.DbContexts; using Sample.MySql.Domain.Entities; diff --git a/samples/Sample.SqlServer/Controllers/ValuesController.cs b/samples/Sample.SqlServer/Controllers/ValuesController.cs index f2b8ee27..1f128edf 100644 --- a/samples/Sample.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.SqlServer/Controllers/ValuesController.cs @@ -177,7 +177,6 @@ namespace Sample.SqlServer.Controllers // var compiler = provider.GetFieldValue("_queryCompiler") as ShardingQueryCompiler; // var shardingDbContext = compiler.GetFieldValue("_shardingDbContext") as IShardingDbContext; - Stopwatch sp = new Stopwatch(); sp.Start(); var shardingPageResultAsync = await _defaultTableDbContext.Set() diff --git a/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs b/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs index d3d6efba..96173542 100644 --- a/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs +++ b/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs @@ -123,6 +123,30 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions new[] { source.Expression, Expression.Quote(selector) }); return callExpression; } + + private static MethodCallExpression CreateSumByConstant(this IQueryable source) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + var valueType = typeof(TSelect); + if (!valueType.IsNumericType()) + throw new ShardingCoreInvalidOperationException( + $"method sum cant calc type :[{valueType}]"); + ParameterExpression parameter = Expression.Parameter(source.ElementType, "s"); + // MemberExpression getter = Expression.MakeMemberAccess(parameter, property); + Expression selector = Expression.Lambda(parameter); + MethodInfo sumMethod = typeof(Queryable).GetMethods().First( + m => m.Name == nameof(Queryable.Sum) + && m.ReturnType == valueType + && m.IsGenericMethod); + + var genericSumMethod = sumMethod.MakeGenericMethod(new[] { source.ElementType }); + + var callExpression = Expression.Call( + null, + genericSumMethod, + new[] { source.Expression, Expression.Quote(selector) }); + return callExpression; + } /// /// 根据属性求和 /// @@ -140,6 +164,11 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions var callExpression = CreateSumByProperty(source, property); return source.Provider.Execute(callExpression); } + public static TSelect SumByConstant(this IQueryable source) + { + var callExpression = CreateSumByConstant(source); + return source.Provider.Execute(callExpression); + } /// /// 根据属性求和 /// diff --git a/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryGroupByOrderStreamMergeAsyncEnumerator.cs b/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryGroupByOrderStreamMergeAsyncEnumerator.cs new file mode 100644 index 00000000..e6bacbbc --- /dev/null +++ b/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryGroupByOrderStreamMergeAsyncEnumerator.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; + +namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync +{ + internal class InMemoryGroupByOrderStreamMergeAsyncEnumerator : IInMemoryStreamMergeAsyncEnumerator + { + private readonly StreamMergeContext _streamMergeContext; + private readonly bool _async; + private readonly IEnumerator _inMemoryEnumerator; + private bool skip; + private int _inMemoryReallyCount; + + public InMemoryGroupByOrderStreamMergeAsyncEnumerator(StreamMergeContext streamMergeContext,IStreamMergeAsyncEnumerator asyncSource, bool async) + { + if (_inMemoryEnumerator != null) + throw new ArgumentNullException(nameof(_inMemoryEnumerator)); + _streamMergeContext = streamMergeContext; + _async = async; + + if (_async) + _inMemoryEnumerator = GetAllRowsAsync(asyncSource).WaitAndUnwrapException(); + else + _inMemoryEnumerator = GetAllRows(asyncSource); + _inMemoryEnumerator.MoveNext(); + skip = true; + } + + private async Task> GetAllRowsAsync(IStreamMergeAsyncEnumerator streamMergeAsyncEnumerator) + { + var list = new List(); +#if !EFCORE2 + while (await streamMergeAsyncEnumerator.MoveNextAsync()) +#endif +#if EFCORE2 + while (await streamMergeAsyncEnumerator.MoveNext(new CancellationToken())) +#endif + { + list.Add(streamMergeAsyncEnumerator.GetCurrent()); + _inMemoryReallyCount++; + } + return list.AsQueryable().OrderWithExpression(_streamMergeContext.Orders).GetEnumerator(); + } + private IEnumerator GetAllRows(IStreamMergeAsyncEnumerator streamMergeAsyncEnumerator) + { + var list = new List(); +#if !EFCORE2 + while ( streamMergeAsyncEnumerator.MoveNext()) +#endif +#if EFCORE2 + while (streamMergeAsyncEnumerator.MoveNext()) +#endif + { + list.Add(streamMergeAsyncEnumerator.GetCurrent()); + _inMemoryReallyCount++; + } + + return list.AsQueryable().OrderWithExpression(_streamMergeContext.Orders).GetEnumerator(); + } + + public bool SkipFirst() + { + if (skip) + { + skip = false; + return true; + } + return false; + } + public int GetReallyCount() + { + return _inMemoryReallyCount; + } +#if !EFCORE2 + + public ValueTask DisposeAsync() + { + _inMemoryEnumerator.Dispose(); + return new ValueTask(); + } + + public ValueTask MoveNextAsync() + { + if (skip) + { + skip = false; + return new ValueTask(null != _inMemoryEnumerator.Current); + } + return new ValueTask(_inMemoryEnumerator.MoveNext()); + } + + public void Dispose() + { + _inMemoryEnumerator?.Dispose(); + } + +#endif + public bool MoveNext() + { + if (skip) + { + skip = false; + return null != _inMemoryEnumerator.Current; + } + return _inMemoryEnumerator.MoveNext(); + } + + public bool HasElement() + { + return null != _inMemoryEnumerator.Current; + } + + + public void Reset() + { + _inMemoryEnumerator.Reset(); + } + + object IEnumerator.Current => Current; + public T Current => GetCurrent(); + public T ReallyCurrent => GetReallyCurrent(); + public T GetCurrent() + { + if (skip) + return default; + return _inMemoryEnumerator.Current; + } + public T GetReallyCurrent() + { + return _inMemoryEnumerator.Current; + } +#if EFCORE2 + public void Dispose() + { + _inMemoryEnumerator?.Dispose(); + } + + public Task MoveNext(CancellationToken cancellationToken = new CancellationToken()) + { + cancellationToken.ThrowIfCancellationRequested(); + if (skip) + { + skip = false; + return Task.FromResult(null != _inMemoryEnumerator.Current); + } + return Task.FromResult(_inMemoryEnumerator.MoveNext()); + } + + +#endif + } +} diff --git a/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryStreamMergeAsyncEnumerator.cs b/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryStreamMergeAsyncEnumerator.cs index 513ef4a3..8aa7120e 100644 --- a/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryStreamMergeAsyncEnumerator.cs +++ b/src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/InMemoryStreamMergeAsyncEnumerator.cs @@ -43,7 +43,6 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync list.Add(streamMergeAsyncEnumerator.GetCurrent()); _inMemoryReallyCount++; } - return list.GetEnumerator(); } private IEnumerator GetAllRows(IStreamMergeAsyncEnumerator streamMergeAsyncEnumerator) diff --git a/src/ShardingCore/Sharding/MergeContexts/GroupByContext.cs b/src/ShardingCore/Sharding/MergeContexts/GroupByContext.cs index 552bca14..7c2f691a 100644 --- a/src/ShardingCore/Sharding/MergeContexts/GroupByContext.cs +++ b/src/ShardingCore/Sharding/MergeContexts/GroupByContext.cs @@ -13,7 +13,11 @@ namespace ShardingCore.Sharding.MergeContexts /// /// group by 表达式 /// - public LambdaExpression GroupExpression { get; set; } + public LambdaExpression GroupExpression { get; set; } + /// + /// 是否内存聚合 + /// + public bool GroupMemoryMerge { get; set; } } } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeContexts/QueryableRewriteEngine.cs b/src/ShardingCore/Sharding/MergeContexts/QueryableRewriteEngine.cs index f5ed8d4d..565d7c9d 100644 --- a/src/ShardingCore/Sharding/MergeContexts/QueryableRewriteEngine.cs +++ b/src/ShardingCore/Sharding/MergeContexts/QueryableRewriteEngine.cs @@ -27,9 +27,11 @@ namespace ShardingCore.Sharding.MergeContexts public QueryableRewriteEngine(ILogger logger) { _logger = logger; - _enableLogDebug=logger.IsEnabled(LogLevel.Debug); + _enableLogDebug = logger.IsEnabled(LogLevel.Debug); } - public IRewriteResult GetRewriteQueryable(IMergeQueryCompilerContext mergeQueryCompilerContext, IParseResult parseResult) + + public IRewriteResult GetRewriteQueryable(IMergeQueryCompilerContext mergeQueryCompilerContext, + IParseResult parseResult) { var paginationContext = parseResult.GetPaginationContext(); _logger.LogDebug($"rewrite queryable pagination context:[{paginationContext}]"); @@ -38,16 +40,20 @@ namespace ShardingCore.Sharding.MergeContexts { _logger.LogDebug($"rewrite queryable order by context:[{orderByContext}]"); } + var groupByContext = parseResult.GetGroupByContext(); if (_enableLogDebug) { - _logger.LogDebug($"rewrite queryable group by context:[{groupByContext.GroupExpression?.ShardingPrint()}]"); + _logger.LogDebug( + $"rewrite queryable group by context:[{groupByContext.GroupExpression?.ShardingPrint()}]"); } + var selectContext = parseResult.GetSelectContext(); if (_enableLogDebug) { _logger.LogDebug($"rewrite queryable select context:[{selectContext}]"); } + var skip = paginationContext.Skip; var take = paginationContext.Take; var orders = orderByContext.PropertyOrders; @@ -60,7 +66,7 @@ namespace ShardingCore.Sharding.MergeContexts reWriteQueryable = reWriteQueryable.RemoveSkipAndTake(); } - + if (take.HasValue) { if (skip.HasValue) @@ -71,52 +77,90 @@ namespace ShardingCore.Sharding.MergeContexts { reWriteQueryable = reWriteQueryable.ReTake(take.Value + skip.GetValueOrDefault()); } - } - //包含group by + } + + //包含group by select必须包含group by字段其余的order字段需要在内存中实现 if (groupByContext.GroupExpression != null) { + //group字段不可以为空 + var selectGroupKeyProperties = + selectContext.SelectProperties.Where(o => !(o is SelectAggregateProperty)).ToArray(); + if (selectGroupKeyProperties.IsEmpty()) + { + throw new ShardingCoreInvalidOperationException( + "group by select object must contains group by key value"); + } + + if (orders.IsEmpty()) { - //将查询的属性转换成order by - var selectProperties = selectContext.SelectProperties.Where(o => !(o is SelectAggregateProperty)).ToArray(); - if (selectProperties.IsNotEmpty()) + groupByContext.GroupMemoryMerge = false; + var sort = string.Join(",", selectGroupKeyProperties.Select(o => $"{o.PropertyName} asc")); + reWriteQueryable = reWriteQueryable.RemoveAnyOrderBy().OrderWithExpression(sort, null); + foreach (var orderProperty in selectGroupKeyProperties) { - var sort = string.Join(",", selectProperties.Select(o => $"{o.PropertyName} asc")); - reWriteQueryable = reWriteQueryable.OrderWithExpression(sort, null); - foreach (var orderProperty in selectProperties) - { - orders.AddLast(new PropertyOrder(orderProperty.PropertyName, true, orderProperty.OwnerType)); - } + orders.AddLast(new PropertyOrder(orderProperty.PropertyName, true, orderProperty.OwnerType)); } } - else if (!mergeQueryCompilerContext.UseUnionAllMerge()) + else { - //将查询的属性转换成order by 并且order和select的未聚合查询必须一致 - var selectProperties = selectContext.SelectProperties.Where(o => !(o is SelectAggregateProperty)); - - if (orders.Count() != selectProperties.Count()) - throw new ShardingCoreInvalidOperationException("group by query order items not equal select un-aggregate items"); - var os = orders.Select(o => o.PropertyExpression).ToList(); - var ss = selectProperties.Select(o => o.PropertyName).ToList(); - for (int i = 0; i < os.Count(); i++) + var groupKeys = selectGroupKeyProperties.Select(o=>o.PropertyName).ToHashSet(); + bool groupMemoryMerge = false; + foreach (var propertyOrder in orders) { - if (!os[i].Equals(ss[i])) - throw new ShardingCoreInvalidOperationException($"group by query order items not equal select un-aggregate items: order:[{os[i]}],select:[{ss[i]}"); + if (groupKeys.IsEmpty()) + { + break; + } + if (!groupKeys.Contains(propertyOrder.PropertyExpression)) + { + groupMemoryMerge = true; + break; + } + groupKeys.Remove(propertyOrder.PropertyExpression); } - + //判断是否优先group key排序如果不是就是要内存聚合 + groupByContext.GroupMemoryMerge = groupMemoryMerge; + + + var sort = string.Join(",", selectGroupKeyProperties.Select(o => $"{o.PropertyName} asc")); + reWriteQueryable = reWriteQueryable.RemoveAnyOrderBy().OrderWithExpression(sort, null); } + + // else if (!mergeQueryCompilerContext.UseUnionAllMerge()) + // { + // //将查询的属性转换成order by 并且order和select的未聚合查询必须一致 + // // var selectProperties = selectContext.SelectProperties.Where(o => !(o is SelectAggregateProperty)); + // + // // if (orders.Count() != selectProperties.Count()) + // // throw new ShardingCoreInvalidOperationException("group by query order items not equal select un-aggregate items"); + // // var os = orders.Select(o => o.PropertyExpression).ToList(); + // // var ss = selectProperties.Select(o => o.PropertyName).ToList(); + // // for (int i = 0; i < os.Count(); i++) + // // { + // // if (!os[i].Equals(ss[i])) + // // throw new ShardingCoreInvalidOperationException($"group by query order items not equal select un-aggregate items: order:[{os[i]}],select:[{ss[i]}"); + // // } + // + // } if (selectContext.HasAverage()) { - var averageSelectProperties = selectContext.SelectProperties.OfType().ToList(); - var selectAggregateProperties = selectContext.SelectProperties.OfType().Where(o => !(o is SelectAverageProperty)).ToList(); + var averageSelectProperties = + selectContext.SelectProperties.OfType().ToList(); + var selectAggregateProperties = selectContext.SelectProperties.OfType() + .Where(o => !(o is SelectAverageProperty)).ToList(); foreach (var averageSelectProperty in averageSelectProperties) { - var selectCountProperty = selectAggregateProperties.FirstOrDefault(o => o is SelectCountProperty selectCountProperty); + var selectCountProperty = + selectAggregateProperties.FirstOrDefault(o => o is SelectCountProperty selectCountProperty); if (null != selectCountProperty) { averageSelectProperty.BindCountProperty(selectCountProperty.Property); } - var selectSumProperty = selectAggregateProperties.FirstOrDefault(o => o is SelectSumProperty selectSumProperty && selectSumProperty.FromProperty == averageSelectProperty.FromProperty); + + var selectSumProperty = selectAggregateProperties.FirstOrDefault(o => + o is SelectSumProperty selectSumProperty && + selectSumProperty.FromProperty == averageSelectProperty.FromProperty); if (selectSumProperty != null) { averageSelectProperty.BindSumProperty(selectSumProperty.Property); @@ -144,14 +188,14 @@ namespace ShardingCore.Sharding.MergeContexts //} } - if (mergeQueryCompilerContext.UseUnionAllMerge() & !mergeQueryCompilerContext.GetShardingDbContext().SupportUnionAllMerge()) + if (mergeQueryCompilerContext.UseUnionAllMerge() & + !mergeQueryCompilerContext.GetShardingDbContext().SupportUnionAllMerge()) { throw new ShardingCoreException( - $"if use {nameof(EntityFrameworkShardingQueryableExtension.UseUnionAllMerge)} plz rewrite {nameof(IQuerySqlGeneratorFactory)} with {nameof(IUnionAllMergeQuerySqlGeneratorFactory)} and {nameof(IQueryCompiler)} with {nameof(IUnionAllMergeQueryCompiler)}"); - + $"if use {nameof(EntityFrameworkShardingQueryableExtension.UseUnionAllMerge)} plz rewrite {nameof(IQuerySqlGeneratorFactory)} with {nameof(IUnionAllMergeQuerySqlGeneratorFactory)} and {nameof(IQueryCompiler)} with {nameof(IUnionAllMergeQueryCompiler)}"); } - return new RewriteResult(combineQueryable,reWriteQueryable); + return new RewriteResult(combineQueryable, reWriteQueryable); } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/AbstractBaseMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/AbstractBaseMergeEngine.cs deleted file mode 100644 index 1e292f84..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/AbstractBaseMergeEngine.cs +++ /dev/null @@ -1,175 +0,0 @@ -using ShardingCore.Core; -using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Helpers; -using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Common.Abstractions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core.UnionAllMergeShardingProviders.Abstractions; -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Abstractions -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/10/2 17:25:33 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal abstract class AbstractBaseMergeEngine - { - protected abstract StreamMergeContext GetStreamMergeContext(); - protected bool UseUnionAllMerge() - { - return GetStreamMergeContext().UseUnionAllMerge(); - } - /// - /// 将查询分表分库结果按每个数据源进行分组 - /// 每组大小为 启动配置的数目 - /// - /// - /// - /// - /// - /// - public Task>[] GetDataSourceGroupAndExecutorGroup(bool async, IEnumerable sqlRouteUnits, CancellationToken cancellationToken = new CancellationToken()) - { - var executor = CreateExecutor(async) ?? throw new ShardingCoreInvalidOperationException($"cant create executor type:{GetType()}"); - var waitTaskQueue = AggregateQueryByDataSourceName(sqlRouteUnits) - .Select(GetSqlExecutorGroups) - .Select(dataSourceSqlExecutorUnit => - { - return Task.Run(async () => - { - if (UseUnionAllMerge()) - { - var customerDatabaseSqlSupportManager = GetStreamMergeContext().ShardingRuntimeContext.GetUnionAllMergeManager(); - using (customerDatabaseSqlSupportManager.CreateScope( - ((UnSupportSqlRouteUnit)dataSourceSqlExecutorUnit.SqlExecutorGroups[0].Groups[0] - .RouteUnit).TableRouteResults)) - { - return await executor.ExecuteAsync(async, dataSourceSqlExecutorUnit, cancellationToken); - } - } - else - { - return await executor.ExecuteAsync(async, dataSourceSqlExecutorUnit, cancellationToken); - } - }, cancellationToken); - }).ToArray(); - return waitTaskQueue; - } - - //protected abstract IParallelExecuteControl CreateParallelExecuteControl(IParallelExecutor executor); - protected abstract IExecutor CreateExecutor(bool async); - - /// - /// sql执行的路由最小单元 - /// - /// - protected virtual IEnumerable GetDefaultSqlRouteUnits() - { - var useUnionAllMerge = UseUnionAllMerge(); - if (useUnionAllMerge) - { - return GetStreamMergeContext().ShardingRouteResult.RouteUnits.GroupBy(o=>o.DataSourceName).Select(o=>new UnSupportSqlRouteUnit(o.Key,o.Select(g=>g.TableRouteResult).ToList())); - } - return GetStreamMergeContext().ShardingRouteResult.RouteUnits; - - } - protected virtual IEnumerable> AggregateQueryByDataSourceName(IEnumerable sqlRouteUnits) - { - return ReOrderTableTails(sqlRouteUnits).GroupBy(o => o.DataSourceName); - } - /// - /// 顺序查询从重排序 - /// - /// - /// - private IEnumerable ReOrderTableTails(IEnumerable sqlRouteUnits) - { - var streamMergeContext = GetStreamMergeContext(); - if (streamMergeContext.IsSeqQuery()) - { - return sqlRouteUnits.OrderByAscDescIf(o => o.TableRouteResult.ReplaceTables.First().Tail, streamMergeContext.TailComparerNeedReverse, streamMergeContext.ShardingTailComparer); - } - - return sqlRouteUnits; - } - - /// - /// 每个数据源下的分表结果按 maxQueryConnectionsLimit 进行组合分组每组大小 maxQueryConnectionsLimit - /// ConnectionModeEnum为用户配置或者系统自动计算,哪怕是用户指定也是按照maxQueryConnectionsLimit来进行分组。 - /// - /// - /// - protected DataSourceSqlExecutorUnit GetSqlExecutorGroups(IGrouping sqlGroups) - { - var streamMergeContext = GetStreamMergeContext(); - var maxQueryConnectionsLimit = streamMergeContext.GetMaxQueryConnectionsLimit(); - var sqlCount = sqlGroups.Count(); - ////根据用户配置单次查询期望并发数 - //int exceptCount = - // Math.Max( - // 0 == sqlCount % maxQueryConnectionsLimit - // ? sqlCount / maxQueryConnectionsLimit - // : sqlCount / maxQueryConnectionsLimit + 1, 1); - //计算应该使用那种链接模式 - ConnectionModeEnum connectionMode = streamMergeContext.GetConnectionMode(sqlCount); - - //将SqlExecutorUnit进行分区,每个区maxQueryConnectionsLimit个 - //[1,2,3,4,5,6,7],maxQueryConnectionsLimit=3,结果就是[[1,2,3],[4,5,6],[7]] - var sqlExecutorUnitPartitions = sqlGroups.Select(o => new SqlExecutorUnit(connectionMode, o)).Partition(maxQueryConnectionsLimit); - - var sqlExecutorGroups = sqlExecutorUnitPartitions.Select(o => new SqlExecutorGroup(connectionMode, o)).ToList(); - return new DataSourceSqlExecutorUnit(connectionMode, sqlExecutorGroups); - } - ///// - ///// 同库同组下面的并行异步执行,需要归并成一个结果 - ///// - ///// - ///// - ///// - ///// - ///// - //protected async Task>> ExecuteAsync(List sqlExecutorUnits, Func>> sqlExecutorUnitExecuteAsync, CancellationToken cancellationToken = new CancellationToken()) - //{ - // if (sqlExecutorUnits.Count <= 0) - // { - // return new LinkedList>(); - // } - // else - // { - // var result = new LinkedList>(); - // Task>[] tasks = null; - // if (sqlExecutorUnits.Count > 1) - // { - // tasks = sqlExecutorUnits.Skip(1).Select(sqlExecutorUnit => - // { - // return Task.Run(async () => await sqlExecutorUnitExecuteAsync(sqlExecutorUnit), cancellationToken); - // }).ToArray(); - // } - // else - // { - // tasks = Array.Empty>>(); - // } - // var firstResult = await sqlExecutorUnitExecuteAsync(sqlExecutorUnits[0]); - // result.AddLast(firstResult); - // var otherResults = await TaskHelper.WhenAllFastFail(tasks); - // foreach (var otherResult in otherResults) - // { - // result.AddLast(otherResult); - // } - - // return result; - // } - //} - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractEnsureMethodCallInMemoryAsyncMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractEnsureMethodCallInMemoryAsyncMergeEngine.cs deleted file mode 100644 index f1e28732..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractEnsureMethodCallInMemoryAsyncMergeEngine.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.StreamMergeEngines; - -namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 13:44:02 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal abstract class AbstractEnsureMethodCallInMemoryAsyncMergeEngine : AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult - { - protected AbstractEnsureMethodCallInMemoryAsyncMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - public virtual TResult MergeResult() - { - return MergeResultAsync().WaitAndUnwrapException(false); - } - - public virtual async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) - { - var resultList = await base.ExecuteAsync(cancellationToken); - return DoMergeResult(resultList); - } - - protected abstract TResult DoMergeResult(List> resultList); - - protected override IExecutor CreateExecutor(bool async) - { - return CreateExecutor0(async) as IExecutor; - } - - protected abstract IExecutor> CreateExecutor0(bool async); - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine.cs deleted file mode 100644 index 66c24503..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.StreamMergeEngines; - -namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 13:44:02 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal abstract class AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine : AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult - { - protected AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - public virtual TResult MergeResult() - { - return MergeResultAsync().WaitAndUnwrapException(false); - } - - public abstract Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()); - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractInMemoryAsyncMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractInMemoryAsyncMergeEngine.cs deleted file mode 100644 index 91f27fe5..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractInMemoryAsyncMergeEngine.cs +++ /dev/null @@ -1,44 +0,0 @@ -using ShardingCore.Sharding.StreamMergeEngines; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Helpers; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/17 14:22:10 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal abstract class AbstractInMemoryAsyncMergeEngine : AbstractBaseMergeEngine, IInMemoryAsyncMergeEngine - { - private readonly StreamMergeContext _mergeContext; - - protected AbstractInMemoryAsyncMergeEngine(StreamMergeContext streamMergeContext) - { - _mergeContext = streamMergeContext; - } - - public async Task>> ExecuteAsync(CancellationToken cancellationToken = new CancellationToken()) - { - if (!_mergeContext.TryPrepareExecuteContinueQuery(() => new List>(0),out var routeQueryResults)) - return routeQueryResults; - var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); - var waitExecuteQueue = GetDataSourceGroupAndExecutorGroup>(true, defaultSqlRouteUnits, cancellationToken).ToArray(); - - return (await TaskHelper.WhenAllFastFail(waitExecuteQueue)).SelectMany(o => o).ToList(); - } - - - protected override StreamMergeContext GetStreamMergeContext() - { - return _mergeContext; - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine.cs deleted file mode 100644 index eec631ea..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.StreamMergeEngines; - -namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/24 10:16:28 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal abstract class AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine : AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - protected AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - private TEntity ProcessTrackResult(TEntity current) - { - if (current != null) - { - if (GetStreamMergeContext().IsUseShardingTrack(current.GetType())) - { - var c = (object)current; - var genericDbContext = GetStreamMergeContext().GetShardingDbContext().CreateGenericDbContext(c); - var attachedEntity = genericDbContext.GetAttachedEntity(c); - if (attachedEntity == null) - genericDbContext.Attach(current); - else - { - return (TEntity)attachedEntity; - } - } - - } - return current; - } - - protected override TEntity DoMergeResult(List> resultList) - { - var entity = DoMergeResult0(resultList); - return ProcessTrackResult(entity); - } - - protected abstract TEntity DoMergeResult0(List> resultList); - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/IInMemoryAsyncMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/IInMemoryAsyncMergeEngine.cs deleted file mode 100644 index 141dbbea..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/IInMemoryAsyncMergeEngine.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.StreamMergeEngines; - -namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/19 8:58:47 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal interface IInMemoryAsyncMergeEngine - { - Task>> ExecuteAsync(CancellationToken cancellationToken = new CancellationToken()); - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/StreamMerge/AbstractStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Abstractions/StreamMerge/AbstractStreamEnumerable.cs deleted file mode 100644 index 60b63907..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/StreamMerge/AbstractStreamEnumerable.cs +++ /dev/null @@ -1,123 +0,0 @@ -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Enumerators; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Helpers; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -#if EFCORE2 -using Microsoft.EntityFrameworkCore.Extensions.Internal; -#endif - -namespace ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/2 15:35:39 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal abstract class AbstractStreamEnumerable : AbstractBaseMergeEngine, IStreamEnumerable - { - private readonly StreamMergeContext _streamMergeContext; - - protected override StreamMergeContext GetStreamMergeContext() - { - return _streamMergeContext; - } - - protected abstract IStreamMergeCombine GetStreamMergeCombine(); - - protected AbstractStreamEnumerable(StreamMergeContext streamMergeContext) - { - _streamMergeContext = streamMergeContext; - } - - -#if !EFCORE2 - public IAsyncEnumerator GetAsyncEnumerator( - CancellationToken cancellationToken = new CancellationToken()) - { - return GetStreamMergeAsyncEnumerator(true, cancellationToken); - } -#endif - -#if EFCORE2 - IAsyncEnumerator IAsyncEnumerable.GetEnumerator() - { - return GetStreamMergeAsyncEnumerator(true); - } - -#endif - - public IEnumerator GetEnumerator() - { - return GetStreamMergeAsyncEnumerator(false); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public void Dispose() - { - _streamMergeContext.Dispose(); - } - - - /// - /// 获取查询的迭代器 - /// - /// - /// - /// - private IStreamMergeAsyncEnumerator GetStreamMergeAsyncEnumerator(bool async, - CancellationToken cancellationToken = new CancellationToken()) - { - cancellationToken.ThrowIfCancellationRequested(); - var dbStreamMergeAsyncEnumerators = GetRouteQueryStreamMergeAsyncEnumerators(async); - if (dbStreamMergeAsyncEnumerators.IsEmpty()) - throw new ShardingCoreException("GetRouteQueryStreamMergeAsyncEnumerators empty"); - return CombineStreamMergeAsyncEnumerator(dbStreamMergeAsyncEnumerators); - } - - /// - /// 获取路由查询的迭代器 - /// - /// - /// - /// - public virtual IStreamMergeAsyncEnumerator[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, - CancellationToken cancellationToken = new CancellationToken()) - { - cancellationToken.ThrowIfCancellationRequested(); - var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); - var enumeratorTasks = GetDataSourceGroupAndExecutorGroup>(async, defaultSqlRouteUnits, cancellationToken); - var streamEnumerators = TaskHelper.WhenAllFastFail(enumeratorTasks).WaitAndUnwrapException().SelectMany(o => o).ToArray(); - return streamEnumerators; - } - - /// - /// 合并成一个迭代器 - /// - /// - /// - private IStreamMergeAsyncEnumerator CombineStreamMergeAsyncEnumerator( - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - return GetStreamMergeCombine().StreamMergeEnumeratorCombine(GetStreamMergeContext(), streamsAsyncEnumerators); - } - - protected override IExecutor CreateExecutor(bool async) - { - return CreateExecutor0(async) as IExecutor; - } - - protected abstract IExecutor> CreateExecutor0(bool async); - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageAsyncInMemoryMergeEngine.cs deleted file mode 100644 index f7918c08..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Enumerators.AggregateExtensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using ShardingCore.Sharding.MergeEngines.AggregateMergeEngines; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; - -namespace ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 22:15:04 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class AverageAsyncInMemoryMergeEngine : AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine - { - public AverageAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - - public override async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) - { - var resultList = await base.ExecuteAsync>(cancellationToken); - if (resultList.IsEmpty()) - throw new InvalidOperationException("Sequence contains no elements."); - var queryable = resultList.Where(o=>o.HasQueryResult()).Select(o => new - { - Sum = o.QueryResult.Sum, - Count = o.QueryResult.Count - }).AsQueryable(); - var sum = queryable.SumByPropertyName(nameof(AverageResult.Sum)); - var count = queryable.Sum(o => o.Count); - return AggregateExtension.AverageConstant(sum, count); - } - - protected override IExecutor CreateExecutor(bool async) - { - return new AverageMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/MaxAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/MaxAsyncInMemoryMergeEngine.cs deleted file mode 100644 index 7da0fea0..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/MaxAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,126 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; - -namespace ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 14:44:53 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class MaxAsyncInMemoryMergeEngine :AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult - { - public MaxAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IExecutor CreateExecutor(bool async) - { - var resultType = typeof(TEntity); - if (!resultType.IsNullableType()) - { - if (typeof(decimal) == resultType) - { - return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(float) == resultType) - { - return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(int) == resultType) - { - return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(long) == resultType) - { - return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(double) == resultType) - { - return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - - throw new ShardingCoreException($"cant calc max value, type:[{resultType}]"); - } - else - { - return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - } - - public TResult MergeResult() - { - return MergeResultAsync().WaitAndUnwrapException(false); - } - - public async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) - { - var resultType = typeof(TEntity); - if (!resultType.IsNullableType()) - { - if (typeof(decimal) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMaxTResult(result); - } - if (typeof(float) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMaxTResult(result); - } - if (typeof(int) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMaxTResult(result); - } - if (typeof(long) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMaxTResult(result); - } - if (typeof(double) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMaxTResult(result); - } - - throw new ShardingCoreException($"cant calc max value, type:[{resultType}]"); - } - else - { - var result = await base.ExecuteAsync(cancellationToken); - return result.Where(o => o.HasQueryResult()).Max(o => o.QueryResult); - } - } - private TResult GetMaxTResult(List> source) - { - var routeQueryResults = source.Where(o => o.QueryResult != null).ToList(); - if (routeQueryResults.IsEmpty()) - throw new InvalidOperationException("Sequence contains no elements."); - var max = routeQueryResults.Max(o => o.QueryResult); - - return ConvertNumber(max); - } - - private TResult ConvertNumber(TNumber number) - { - if (number == null) - return default; - var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); - return Expression.Lambda>(convertExpr).Compile()(); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/MinAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/MinAsyncInMemoryMergeEngine.cs deleted file mode 100644 index 0b44bdf5..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/MinAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,126 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; - -namespace ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 14:40:06 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class MinAsyncInMemoryMergeEngine : AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult - { - public MinAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IExecutor CreateExecutor(bool async) - { - var resultType = typeof(TEntity); - if (!resultType.IsNullableType()) - { - if (typeof(decimal) == resultType) - { - return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(float) == resultType) - { - return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(int) == resultType) - { - return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(long) == resultType) - { - return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - if (typeof(double) == resultType) - { - return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - - throw new ShardingCoreException($"cant calc min value, type:[{resultType}]"); - } - else - { - return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor; - } - } - - public TResult MergeResult() - { - return MergeResultAsync().WaitAndUnwrapException(false); - } - - public async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) - { - var resultType = typeof(TEntity); - if (!resultType.IsNullableType()) - { - if (typeof(decimal) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMinTResult(result); - } - if (typeof(float) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMinTResult(result); - } - if (typeof(int) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMinTResult(result); - } - if (typeof(long) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMinTResult(result); - } - if (typeof(double) == resultType) - { - var result = await base.ExecuteAsync(cancellationToken); - return GetMinTResult(result); - } - - throw new ShardingCoreException($"cant calc min value, type:[{resultType}]"); - } - else - { - var result = await base.ExecuteAsync(cancellationToken); - return result.Where(o => o.HasQueryResult()).Min(o => o.QueryResult); - } - } - private TResult GetMinTResult(List> source) - { - var routeQueryResults = source.Where(o => o.HasQueryResult()).ToList(); - if (routeQueryResults.IsEmpty()) - throw new InvalidOperationException("Sequence contains no elements."); - var min = routeQueryResults.Min(o => o.QueryResult); - - return ConvertNumber(min); - } - - private TResult ConvertNumber(TNumber number) - { - if (number == null) - return default; - var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); - return Expression.Lambda>(convertExpr).Compile()(); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/SumAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/SumAsyncInMemoryMergeEngine.cs deleted file mode 100644 index 6681ec31..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/SumAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,52 +0,0 @@ -using ShardingCore.Exceptions; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Enumerators.AggregateExtensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; - -namespace ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 14:46:20 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class SumAsyncInMemoryMergeEngine : AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - public SumAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - private TResult GetSumResult(List> source) - { - if (source.IsEmpty()) - return default; - var sum = source.AsQueryable().SumByPropertyName(nameof(RouteQueryResult.QueryResult)); - return ConvertSum(sum); - } - private TResult ConvertSum(TNumber number) - { - if (number == null) - return default; - var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); - return Expression.Lambda>(convertExpr).Compile()(); - } - protected override TResult DoMergeResult(List> resultList) - { - return GetSumResult(resultList); - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new SumMethodExecutor(GetStreamMergeContext()); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/AllAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/AllAsyncInMemoryMergeEngine.cs deleted file mode 100644 index 50af7fb2..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/AllAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,60 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; -using ShardingCore.Sharding.ShardingExecutors.QueryableCombines; - -namespace ShardingCore.Sharding.StreamMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 13:39:51 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class AllAsyncInMemoryMergeEngine : AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - public AllAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - //public override async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) - //{ - // var result = await base.ExecuteAsync(queryable => - // { - // var allQueryCombineResult = (AllQueryCombineResult)GetStreamMergeContext().MergeQueryCompilerContext.GetQueryCombineResult(); - // Expression> allPredicate = x => true; - // var predicate = allQueryCombineResult.GetAllPredicate(); - // if (predicate != null) - // { - // allPredicate = (Expression>)predicate; - // } - // return ((IQueryable)queryable).AllAsync(allPredicate, cancellationToken); - // }, cancellationToken); - - // return result.All(o => o.QueryResult); - //} - - //protected override IParallelExecuteControl CreateParallelExecuteControl(IParallelExecutor executor) - //{ - // return AllParallelExecuteControl.Create(GetStreamMergeContext(),executor); - //} - protected override bool DoMergeResult(List> resultList) - { - return resultList.All(o => o.QueryResult); - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new AllMethodExecutor(GetStreamMergeContext()); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/AnyAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/AnyAsyncInMemoryMergeEngine.cs deleted file mode 100644 index ee1e3cfc..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/AnyAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; - -namespace ShardingCore.Sharding.StreamMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 13:37:00 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class AnyAsyncInMemoryMergeEngine : AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - public AnyAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override bool DoMergeResult(List> resultList) - { - return resultList.Any(o => o.QueryResult); - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new AnyMethodExecutor(GetStreamMergeContext()); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Common/SqlSequenceRouteUnit.cs b/src/ShardingCore/Sharding/MergeEngines/Common/SqlSequenceRouteUnit.cs index d0707b71..85b465b8 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Common/SqlSequenceRouteUnit.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Common/SqlSequenceRouteUnit.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Sharding.MergeEngines.Common.Abstractions; -using ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.Base; +using ShardingCore.Sharding.MergeEngines.Enumerables.Base; namespace ShardingCore.Sharding.MergeEngines.Common { diff --git a/src/ShardingCore/Sharding/MergeEngines/ContainsAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ContainsAsyncInMemoryMergeEngine.cs deleted file mode 100644 index f0dc6998..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/ContainsAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; -using ShardingCore.Sharding.ShardingExecutors.QueryableCombines; - -namespace ShardingCore.Sharding.StreamMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 22:30:07 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class ContainsAsyncInMemoryMergeEngine: AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - public ContainsAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - - protected override bool DoMergeResult(List> resultList) - { - return resultList.Any(o => o.QueryResult); - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new ContainsMethodExecutor(GetStreamMergeContext()); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/CountAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/CountAsyncInMemoryMergeEngine.cs deleted file mode 100644 index da1688fa..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/CountAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,47 +0,0 @@ -using ShardingCore.Core.ShardingPage.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; -using System.Collections.Generic; -using System.Linq; - -namespace ShardingCore.Sharding.StreamMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/17 22:36:14 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class CountAsyncInMemoryMergeEngine : AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - private readonly IShardingPageManager _shardingPageManager; - public CountAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - _shardingPageManager =streamMergeContext.ShardingRuntimeContext.GetShardingPageManager(); - } - - protected override int DoMergeResult(List> resultList) - { - - if (_shardingPageManager.Current != null) - { - int r = 0; - foreach (var routeQueryResult in resultList) - { - _shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult)); - r += routeQueryResult.QueryResult; - } - - return r; - } - return resultList.Sum(o => o.QueryResult); - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new CountMethodExecutor(GetStreamMergeContext()); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/AppendOrderSequenceStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs similarity index 75% rename from src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/AppendOrderSequenceStreamEnumerable.cs rename to src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs index dc6ac36b..a0378941 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/AppendOrderSequenceStreamEnumerable.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs @@ -1,52 +1,34 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using Microsoft.EntityFrameworkCore; using ShardingCore.Exceptions; using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Enumerators; using ShardingCore.Sharding.MergeContexts; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; using ShardingCore.Sharding.MergeEngines.Common; using ShardingCore.Sharding.MergeEngines.Common.Abstractions; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; +using ShardingCore.Sharding.MergeEngines.Enumerables.Base; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; using ShardingCore.Sharding.PaginationConfigurations; using ShardingCore.Sharding.StreamMergeEngines; -using ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.Base; -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables +namespace ShardingCore.Sharding.MergeEngines.Enumerables { - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/3 8:09:18 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class AppendOrderSequenceStreamEnumerable : AbstractStreamEnumerable + + internal class AppendOrderSequenceShardingEnumerable :AbstractStreamEnumerable { private readonly PaginationSequenceConfig _dataSourceSequenceOrderConfig; private readonly PaginationSequenceConfig _tableSequenceOrderConfig; private readonly ICollection> _routeQueryResults; - public AppendOrderSequenceStreamEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceOrderConfig, PaginationSequenceConfig tableSequenceOrderConfig, ICollection> routeQueryResults) - : base(streamMergeContext) + public AppendOrderSequenceShardingEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceOrderConfig, PaginationSequenceConfig tableSequenceOrderConfig, ICollection> routeQueryResults) : base(streamMergeContext) { _dataSourceSequenceOrderConfig = dataSourceSequenceOrderConfig; _tableSequenceOrderConfig = tableSequenceOrderConfig; _routeQueryResults = routeQueryResults; } - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return AppendOrderSequenceStreamMergeCombine.Instance; - } - protected override IEnumerable GetDefaultSqlRouteUnits() { - var skip = GetStreamMergeContext().Skip.GetValueOrDefault(); if (skip < 0) throw new ShardingCoreException("skip must ge 0"); @@ -109,13 +91,11 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer GetStreamMergeContext().ReSetOrders(reSetOrders.ToArray()); return sequenceResults.Select(sequenceResult => new SqlSequenceRouteUnit(sequenceResult)); - } - protected override IExecutor> CreateExecutor0(bool async) + protected override IExecutor> CreateExecutor(bool async) { - return new AppendOrderSequenceEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), - async); + throw new System.NotImplementedException(); } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Base/SequencePaginationList.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/Base/SequencePaginationList.cs similarity index 95% rename from src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Base/SequencePaginationList.cs rename to src/ShardingCore/Sharding/MergeEngines/Enumerables/Base/SequencePaginationList.cs index ea5af38e..d0a3ae0b 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Base/SequencePaginationList.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/Base/SequencePaginationList.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Exceptions; +using ShardingCore.Sharding.StreamMergeEngines; -namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.Base +namespace ShardingCore.Sharding.MergeEngines.Enumerables.Base { /* * @Author: xjm diff --git a/src/ShardingCore/Sharding/MergeEngines/Enumerables/DefaultShardingEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/DefaultShardingEnumerable.cs new file mode 100644 index 00000000..5fc54903 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/DefaultShardingEnumerable.cs @@ -0,0 +1,20 @@ +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; + +namespace ShardingCore.Sharding.MergeEngines.Enumerables +{ + + internal class DefaultShardingEnumerable :AbstractStreamEnumerable + { + public DefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) + { + } + + protected override IExecutor> CreateExecutor(bool async) + { + return new DefaultEnumerableExecutor(GetStreamMergeContext(), async); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/EmptyQueryEnumerator.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/EmptyQueryEnumerator.cs similarity index 91% rename from src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/EmptyQueryEnumerator.cs rename to src/ShardingCore/Sharding/MergeEngines/Enumerables/EmptyQueryEnumerator.cs index f50d4a12..b82dc1e1 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/EmptyQueryEnumerator.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/EmptyQueryEnumerator.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables +namespace ShardingCore.Sharding.MergeEngines.Enumerables { internal class EmptyQueryEnumerator : IAsyncEnumerator,IEnumerator { diff --git a/src/ShardingCore/Sharding/MergeEngines/Enumerables/EmptyShardingEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/EmptyShardingEnumerable.cs new file mode 100644 index 00000000..1d0d9a89 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/EmptyShardingEnumerable.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; + +namespace ShardingCore.Sharding.MergeEngines.Enumerables{ + +internal class EmptyShardingEnumerable :AbstractStreamEnumerable +{ + public EmptyShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) + { + } + + public override IStreamMergeAsyncEnumerator GetStreamMergeAsyncEnumerator(bool async, + CancellationToken cancellationToken = new CancellationToken()) + { + cancellationToken.ThrowIfCancellationRequested(); + var asyncEnumerator = new EmptyQueryEnumerator(); + if (async) + { + return new StreamMergeAsyncEnumerator((IAsyncEnumerator)asyncEnumerator); + } + else + { + return new StreamMergeAsyncEnumerator((IEnumerator)asyncEnumerator); + } + } + + protected override IExecutor> CreateExecutor(bool async) + { + throw new NotImplementedException(); + } +} +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Enumerables/FirstOrDefaultShardingEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/FirstOrDefaultShardingEnumerable.cs new file mode 100644 index 00000000..baeceddf --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/FirstOrDefaultShardingEnumerable.cs @@ -0,0 +1,21 @@ +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; + +namespace ShardingCore.Sharding.MergeEngines.Enumerables +{ + + internal class FirstOrDefaultShardingEnumerable :AbstractStreamEnumerable + { + public FirstOrDefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) + { + } + + protected override IExecutor> CreateExecutor(bool async) + { + GetStreamMergeContext().ReSetTake(1); + return new DefaultEnumerableExecutor(GetStreamMergeContext(), async); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Enumerables/LastOrDefaultShardingEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/LastOrDefaultShardingEnumerable.cs new file mode 100644 index 00000000..22351e79 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/LastOrDefaultShardingEnumerable.cs @@ -0,0 +1,29 @@ +using System.Linq; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; + +namespace ShardingCore.Sharding.MergeEngines.Enumerables +{ + internal class LastOrDefaultShardingEnumerable :AbstractStreamEnumerable + { + public LastOrDefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) + { + } + + protected override IExecutor> CreateExecutor(bool async) + { + var streamMergeContext = GetStreamMergeContext(); + var skip = streamMergeContext.Skip; + streamMergeContext.ReverseOrder(); + streamMergeContext.ReSetSkip(0); + var reTake = skip.GetValueOrDefault() + 1; + streamMergeContext.ReSetTake(reTake); + var newQueryable = (IQueryable)streamMergeContext.GetReWriteQueryable().RemoveSkip().RemoveTake().RemoveAnyOrderBy().OrderWithExpression(streamMergeContext.Orders).ReTake(reTake); + + return new LastOrDefaultEnumerableExecutor(GetStreamMergeContext(),newQueryable, async); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/ReverseShardingStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/ReverseShardingEnumerable.cs similarity index 54% rename from src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/ReverseShardingStreamEnumerable.cs rename to src/ShardingCore/Sharding/MergeEngines/Enumerables/ReverseShardingEnumerable.cs index 4ceea31a..1e66e739 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/ReverseShardingStreamEnumerable.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/ReverseShardingEnumerable.cs @@ -1,41 +1,24 @@ -using System.Linq; +using System.Linq; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeContexts; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables +namespace ShardingCore.Sharding.MergeEngines.Enumerables { - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/3 13:32:12 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class ReverseShardingStreamEnumerable : AbstractStreamEnumerable + internal class ReverseShardingEnumerable :AbstractStreamEnumerable { private readonly long _total; - public ReverseShardingStreamEnumerable(StreamMergeContext streamMergeContext, long total) : base( - streamMergeContext) + public ReverseShardingEnumerable(StreamMergeContext streamMergeContext, long total) : base(streamMergeContext) { _total = total; } - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return ReverseStreamMergeCombine.Instance; - } - - - protected override IExecutor> CreateExecutor0(bool async) + protected override IExecutor> CreateExecutor(bool async) { var noPaginationNoOrderQueryable = GetStreamMergeContext().GetOriginalQueryable().RemoveSkip().RemoveTake() .RemoveAnyOrderBy().As>(); @@ -48,8 +31,8 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer GetStreamMergeContext().ReverseOrder(); var reverseOrderQueryable = noPaginationNoOrderQueryable.Take((int)realSkip + (int)take) .OrderWithExpression(GetStreamMergeContext().Orders); - return new ReverseEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), + return new ReverseEnumerableExecutor(GetStreamMergeContext(), reverseOrderQueryable, async); } } -} \ No newline at end of file +} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SequenceStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/SequenceShardingEnumerable.cs similarity index 71% rename from src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SequenceStreamEnumerable.cs rename to src/ShardingCore/Sharding/MergeEngines/Enumerables/SequenceShardingEnumerable.cs index b0cbbda4..d7d36eaf 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SequenceStreamEnumerable.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/SequenceShardingEnumerable.cs @@ -1,38 +1,26 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using Microsoft.EntityFrameworkCore; using ShardingCore.Exceptions; using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; using ShardingCore.Sharding.MergeEngines.Common; using ShardingCore.Sharding.MergeEngines.Common.Abstractions; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; +using ShardingCore.Sharding.MergeEngines.Enumerables.Base; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; using ShardingCore.Sharding.PaginationConfigurations; using ShardingCore.Sharding.StreamMergeEngines; -using ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.Base; -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables +namespace ShardingCore.Sharding.MergeEngines.Enumerables { - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/2 16:29:06 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class SequenceStreamEnumerable : AbstractStreamEnumerable + internal class SequenceShardingEnumerable :AbstractStreamEnumerable { private readonly PaginationSequenceConfig _dataSourceSequenceMatchOrderConfig; private readonly PaginationSequenceConfig _tableSequenceMatchOrderConfig; private readonly ICollection> _routeQueryResults; private readonly bool _isAsc; - public SequenceStreamEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceMatchOrderConfig, PaginationSequenceConfig tableSequenceMatchOrderConfig, ICollection> routeQueryResults, bool isAsc) - : base(streamMergeContext) + public SequenceShardingEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceMatchOrderConfig, PaginationSequenceConfig tableSequenceMatchOrderConfig, ICollection> routeQueryResults, bool isAsc) : base(streamMergeContext) { _dataSourceSequenceMatchOrderConfig = dataSourceSequenceMatchOrderConfig; _tableSequenceMatchOrderConfig = tableSequenceMatchOrderConfig; @@ -40,11 +28,6 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer _isAsc = isAsc; } - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return SequenceStreamMergeCombine.Instance; - } - protected override IEnumerable GetDefaultSqlRouteUnits() { var skip = GetStreamMergeContext().Skip.GetValueOrDefault(); @@ -96,10 +79,9 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer return sequenceResults.Select(sequenceResult => new SqlSequenceRouteUnit(sequenceResult)); } - - protected override IExecutor> CreateExecutor0(bool async) + protected override IExecutor> CreateExecutor(bool async) { - return new SequenceEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), async); + return new SequenceEnumerableExecutor(GetStreamMergeContext(), async); } } } diff --git a/src/ShardingCore/Sharding/MergeEngines/Enumerables/SingleOrDefaultShardingEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/SingleOrDefaultShardingEnumerable.cs new file mode 100644 index 00000000..69c6a107 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/SingleOrDefaultShardingEnumerable.cs @@ -0,0 +1,20 @@ +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; + +namespace ShardingCore.Sharding.MergeEngines.Enumerables +{ + internal class SingleOrDefaultShardingEnumerable :AbstractStreamEnumerable + { + public SingleOrDefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) + { + } + + protected override IExecutor> CreateExecutor(bool async) + { + GetStreamMergeContext().ReSetTake(2); + return new DefaultEnumerableExecutor(GetStreamMergeContext(), async); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/AsyncEnumeratorStreamMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/AsyncEnumeratorStreamMergeEngine.cs index 82db5ed4..49f7ac1e 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/AsyncEnumeratorStreamMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/AsyncEnumeratorStreamMergeEngine.cs @@ -4,7 +4,7 @@ using System.Threading; using Microsoft.EntityFrameworkCore; using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Enumerators.TrackerEnumerators; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables; +using ShardingCore.Sharding.MergeEngines.Enumerables; using ShardingCore.Sharding.ShardingQueryExecutors; /* diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/DefaultShardingStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/DefaultShardingStreamEnumerable.cs deleted file mode 100644 index 1dcb4c06..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/DefaultShardingStreamEnumerable.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/2 16:16:12 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class DefaultShardingStreamEnumerable :AbstractStreamEnumerable - { - public DefaultShardingStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return DefaultStreamMergeCombine.Instance; - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new DefaultEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), async); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/EmptyQueryStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/EmptyQueryStreamEnumerable.cs deleted file mode 100644 index 94969320..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/EmptyQueryStreamEnumerable.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables -{ - internal class EmptyQueryStreamEnumerable : AbstractStreamEnumerable - { - public EmptyQueryStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return EmptyStreamMergeCombine.Instance; - } - - public override IStreamMergeAsyncEnumerator[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, CancellationToken cancellationToken = new CancellationToken()) - { - cancellationToken.ThrowIfCancellationRequested(); - var asyncEnumerator = new EmptyQueryEnumerator(); - if (async) - { - return new[] { new StreamMergeAsyncEnumerator((IAsyncEnumerator)asyncEnumerator) }; - } - else - { - return new[] { new StreamMergeAsyncEnumerator((IEnumerator)asyncEnumerator) }; - } - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new EmptyQueryEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), async); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/FirstOrDefaultStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/FirstOrDefaultStreamEnumerable.cs deleted file mode 100644 index 5704ac54..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/FirstOrDefaultStreamEnumerable.cs +++ /dev/null @@ -1,27 +0,0 @@ -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables -{ - internal class FirstOrDefaultStreamEnumerable:AbstractStreamEnumerable - { - public FirstOrDefaultStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return DefaultStreamMergeCombine.Instance; - } - - protected override IExecutor> CreateExecutor0(bool async) - { - GetStreamMergeContext().ReSetTake(1); - return new DefaultEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), async); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/LastOrDefaultStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/LastOrDefaultStreamEnumerable.cs deleted file mode 100644 index 645acc80..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/LastOrDefaultStreamEnumerable.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Linq; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables -{ - internal class LastOrDefaultStreamEnumerable : AbstractStreamEnumerable - { - public LastOrDefaultStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return DefaultStreamMergeCombine.Instance; - } - - protected override IExecutor> CreateExecutor0(bool async) - { - var streamMergeContext = GetStreamMergeContext(); - var skip = streamMergeContext.Skip; - streamMergeContext.ReverseOrder(); - streamMergeContext.ReSetSkip(0); - var reTake = skip.GetValueOrDefault() + 1; - streamMergeContext.ReSetTake(reTake); - var newQueryable = (IQueryable)streamMergeContext.GetReWriteQueryable().RemoveSkip().RemoveTake().RemoveAnyOrderBy().OrderWithExpression(streamMergeContext.Orders).ReTake(reTake); - - return new LastOrDefaultEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), newQueryable, async); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SingleOrDefaultStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SingleOrDefaultStreamEnumerable.cs deleted file mode 100644 index 2ee51930..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SingleOrDefaultStreamEnumerable.cs +++ /dev/null @@ -1,27 +0,0 @@ -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables -{ - internal class SingleOrDefaultStreamEnumerable : AbstractStreamEnumerable - { - public SingleOrDefaultStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return DefaultStreamMergeCombine.Instance; - } - - protected override IExecutor> CreateExecutor0(bool async) - { - GetStreamMergeContext().ReSetTake(2); - return new DefaultEnumeratorExecutor(GetStreamMergeContext(), GetStreamMergeCombine(), async); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SingleQueryStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SingleQueryStreamEnumerable.cs deleted file mode 100644 index f54b08c7..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/Enumerables/SingleQueryStreamEnumerable.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System.Linq; -using System.Threading; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables -{ - /* - * @Author: xjm - * @Description: - * @Date: Thursday, 02 September 2021 20:58:10 - * @Email: 326308290@qq.com - */ - internal class SingleQueryStreamEnumerable : AbstractStreamEnumerable - { - public SingleQueryStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return SingleStreamMergeCombine.Instance; - } - - public override IStreamMergeAsyncEnumerator[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, CancellationToken cancellationToken = new CancellationToken()) - { - cancellationToken.ThrowIfCancellationRequested(); - var streamMergeContext = GetStreamMergeContext(); - var shardingRouteResult = streamMergeContext.ShardingRouteResult; - var sqlRouteUnit = shardingRouteResult.RouteUnits.First(); - var shardingDbContext = streamMergeContext.CreateDbContext(sqlRouteUnit); - var newQueryable = (IQueryable)streamMergeContext.GetOriginalQueryable().ReplaceDbContextQueryable(shardingDbContext); - var enumeratorParallelExecutor = new SingleQueryEnumeratorExecutor(streamMergeContext); - if (async) - { - var asyncEnumerator = enumeratorParallelExecutor.GetAsyncEnumerator0(newQueryable).WaitAndUnwrapException(); - return new[] { new StreamMergeAsyncEnumerator(asyncEnumerator) }; - } - else - { - var enumerator = enumeratorParallelExecutor.GetEnumerator0(newQueryable); - return new[] { new StreamMergeAsyncEnumerator(enumerator) }; - } - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new SingleQueryEnumeratorExecutor(GetStreamMergeContext()); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/EnumeratorStreamMergeEngineFactory.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/EnumeratorStreamMergeEngineFactory.cs index d71b453d..2fd65aff 100644 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/EnumeratorStreamMergeEngineFactory.cs +++ b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/EnumeratorStreamMergeEngineFactory.cs @@ -14,8 +14,8 @@ using ShardingCore.Extensions; using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.MergeContexts; -using ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge; -using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumerables; +using ShardingCore.Sharding.MergeEngines.Enumerables; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge; using ShardingCore.Sharding.PaginationConfigurations; namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines @@ -56,17 +56,17 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines { if (_streamMergeContext.IsRouteNotMatch()) { - return new EmptyQueryStreamEnumerable(_streamMergeContext); - } - //本次查询没有跨库没有跨表就可以直接执行 - if (!_streamMergeContext.IsMergeQuery()) - { - return new SingleQueryStreamEnumerable(_streamMergeContext); + return new EmptyShardingEnumerable(_streamMergeContext); } + // //本次查询没有跨库没有跨表就可以直接执行 + // if (!_streamMergeContext.IsMergeQuery()) + // { + // return new SingleQueryStreamEnumerable(_streamMergeContext); + // } if (_streamMergeContext.UseUnionAllMerge()) { - return new DefaultShardingStreamEnumerable(_streamMergeContext); + return new DefaultShardingEnumerable(_streamMergeContext); } var queryMethodName = _streamMergeContext.MergeQueryCompilerContext.GetQueryMethodName(); @@ -74,13 +74,13 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines { case nameof(Enumerable.First): case nameof(Enumerable.FirstOrDefault): - return new FirstOrDefaultStreamEnumerable(_streamMergeContext); + return new FirstOrDefaultShardingEnumerable(_streamMergeContext); case nameof(Enumerable.Single): case nameof(Enumerable.SingleOrDefault): - return new SingleOrDefaultStreamEnumerable(_streamMergeContext); + return new SingleOrDefaultShardingEnumerable(_streamMergeContext); case nameof(Enumerable.Last): case nameof(Enumerable.LastOrDefault): - return new LastOrDefaultStreamEnumerable(_streamMergeContext); + return new LastOrDefaultShardingEnumerable(_streamMergeContext); } //未开启系统分表或者本次查询涉及多张分表 @@ -109,7 +109,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines } - return new DefaultShardingStreamEnumerable(_streamMergeContext); + return new DefaultShardingEnumerable(_streamMergeContext); } private IStreamEnumerable DoNoOrderAppendEnumeratorStreamMergeEngine(Type shardingEntityType) @@ -145,7 +145,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines if (useSequenceEnumeratorMergeEngine) { - return new AppendOrderSequenceStreamEnumerable(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults); + return new AppendOrderSequenceShardingEnumerable(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults); } @@ -188,7 +188,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines !_streamMergeContext.IsCrossDataSource)) || (!isShardingDataSource && isShardingTable && tableSequenceOrderConfig != null); if (useSequenceEnumeratorMergeEngine) { - return new SequenceStreamEnumerable(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults, primaryOrder.IsAsc); + return new SequenceShardingEnumerable(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults, primaryOrder.IsAsc); } var total = _shardingPageManager.Current.RouteQueryResults.Sum(o => o.QueryResult); @@ -207,7 +207,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines //skip过大reserve skip if (dataSourceUseReverse && tableUseReverse) { - return new ReverseShardingStreamEnumerable(_streamMergeContext, total); + return new ReverseShardingEnumerable(_streamMergeContext, total); } diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/AppendOrderSequenceStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/AppendOrderSequenceStreamMergeCombine.cs deleted file mode 100644 index 15b72d6e..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/AppendOrderSequenceStreamMergeCombine.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines -{ - internal class AppendOrderSequenceStreamMergeCombine: IStreamMergeCombine - { - private static readonly IStreamMergeCombine _instance; - static AppendOrderSequenceStreamMergeCombine() - { - _instance = new AppendOrderSequenceStreamMergeCombine(); - } - - private AppendOrderSequenceStreamMergeCombine() - { - - } - public static IStreamMergeCombine Instance => _instance; - public IStreamMergeAsyncEnumerator StreamMergeEnumeratorCombine(StreamMergeContext streamMergeContext, - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (streamMergeContext.HasGroupQuery()) - return new MultiAggregateOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - return new MultiOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/DefaultStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/DefaultStreamMergeCombine.cs deleted file mode 100644 index b2681dd0..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/DefaultStreamMergeCombine.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines -{ - internal class DefaultStreamMergeCombine:IStreamMergeCombine - { - - private static readonly IStreamMergeCombine _instance; - static DefaultStreamMergeCombine() - { - _instance = new DefaultStreamMergeCombine(); - } - - private DefaultStreamMergeCombine() - { - - } - public static IStreamMergeCombine Instance => _instance; - public IStreamMergeAsyncEnumerator StreamMergeEnumeratorCombine(StreamMergeContext streamMergeContext, - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (streamMergeContext.IsPaginationQuery()) - return new PaginationStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - if (streamMergeContext.HasGroupQuery()) - return new MultiAggregateOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - return new MultiOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/EmptyStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/EmptyStreamMergeCombine.cs deleted file mode 100644 index 9ca09496..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/EmptyStreamMergeCombine.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Exceptions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines -{ - internal class EmptyStreamMergeCombine:IStreamMergeCombine - { - private static readonly IStreamMergeCombine _instance; - static EmptyStreamMergeCombine() - { - _instance = new EmptyStreamMergeCombine(); - } - - private EmptyStreamMergeCombine() - { - - } - public static IStreamMergeCombine Instance => _instance; - public IStreamMergeAsyncEnumerator StreamMergeEnumeratorCombine(StreamMergeContext streamMergeContext, - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (streamsAsyncEnumerators.Length != 1) - throw new ShardingCoreInvalidOperationException($"empty query combine has more {nameof(IStreamMergeAsyncEnumerator)}"); - return streamsAsyncEnumerators[0]; - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/ReverseStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/ReverseStreamMergeCombine.cs deleted file mode 100644 index 90d67884..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/ReverseStreamMergeCombine.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines -{ - internal class ReverseStreamMergeCombine:IStreamMergeCombine - { - private static readonly IStreamMergeCombine _instance; - static ReverseStreamMergeCombine() - { - _instance = new ReverseStreamMergeCombine(); - } - - private ReverseStreamMergeCombine() - { - - } - - public static IStreamMergeCombine Instance => _instance; - public IStreamMergeAsyncEnumerator StreamMergeEnumeratorCombine(StreamMergeContext streamMergeContext, - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - var doGetStreamMergeAsyncEnumerator = DoGetStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - return new InMemoryReverseStreamMergeAsyncEnumerator(doGetStreamMergeAsyncEnumerator); - } - - private static IStreamMergeAsyncEnumerator DoGetStreamMergeAsyncEnumerator(StreamMergeContext streamMergeContext, IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (streamMergeContext.IsPaginationQuery()) - { - if (streamMergeContext.HasGroupQuery()) - { - var multiAggregateOrderStreamMergeAsyncEnumerator = new MultiAggregateOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - return new PaginationStreamMergeAsyncEnumerator(streamMergeContext, new[] { multiAggregateOrderStreamMergeAsyncEnumerator }); - } - return new PaginationStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - } - if (streamMergeContext.HasGroupQuery()) - return new MultiAggregateOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - return new MultiOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/SequenceStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/SequenceStreamMergeCombine.cs deleted file mode 100644 index bc6ce9d6..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/SequenceStreamMergeCombine.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines -{ - internal class SequenceStreamMergeCombine:IStreamMergeCombine - { - private static readonly IStreamMergeCombine _instance; - static SequenceStreamMergeCombine() - { - _instance = new SequenceStreamMergeCombine(); - } - - private SequenceStreamMergeCombine() - { - - } - - public static IStreamMergeCombine Instance => _instance; - public IStreamMergeAsyncEnumerator StreamMergeEnumeratorCombine(StreamMergeContext streamMergeContext, - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (streamMergeContext.HasGroupQuery()) - return new MultiAggregateOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - return new MultiOrderStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/SingleStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/SingleStreamMergeCombine.cs deleted file mode 100644 index 8953d675..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/EnumeratorStreamMergeEngines/StreamMergeCombines/SingleStreamMergeCombine.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Exceptions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.StreamMergeCombines -{ - internal class SingleStreamMergeCombine:IStreamMergeCombine - { - private static readonly IStreamMergeCombine _instance; - static SingleStreamMergeCombine() - { - _instance = new SingleStreamMergeCombine(); - } - - private SingleStreamMergeCombine() - { - - } - - public static IStreamMergeCombine Instance => _instance; - public IStreamMergeAsyncEnumerator StreamMergeEnumeratorCombine(StreamMergeContext streamMergeContext, - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (streamsAsyncEnumerators.Length != 1) - throw new ShardingCoreInvalidOperationException($"single query has more {nameof(IStreamMergeAsyncEnumerator)}"); - return streamsAsyncEnumerators[0]; - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/AbstractExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/AbstractExecutor.cs index f0475704..e1445f9b 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/AbstractExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/AbstractExecutor.cs @@ -4,9 +4,10 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Core; +using ShardingCore.Exceptions; using ShardingCore.Helpers; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions { @@ -56,7 +57,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions return cancelStatus == cancelled; } - public async Task> ExecuteAsync(bool async, + public abstract IShardingMerger GetShardingMerger(); + + public async Task> ExecuteAsync(bool async, DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) { @@ -71,14 +74,14 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions } } - private async Task> ExecuteAsync0(bool async, + private async Task> ExecuteAsync0(bool async, DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) { var streamMergeContext = GetStreamMergeContext(); var circuitBreaker = CreateCircuitBreaker(); var executorGroups = dataSourceSqlExecutorUnit.SqlExecutorGroups; - LinkedList result = new LinkedList(); + List result = new List(); var executorGroupsCount = executorGroups.Count; //同数据库下多组数据间采用串行 foreach (var executorGroup in executorGroups) @@ -89,7 +92,16 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions //严格限制连接数就在内存中进行聚合并且直接回收掉当前dbcontext if (dataSourceSqlExecutorUnit.ConnectionMode == ConnectionModeEnum.CONNECTION_STRICTLY) { - MergeParallelExecuteResult(result, routeQueryResults.Select(o => o.MergeResult), async); + var resultCount = result.Count; + if (resultCount > 1) + { + throw new ShardingCoreInvalidOperationException( + $"in memory merge result length error:{resultCount}"); + } + + GetShardingMerger() + .InMemoryMerge(result, routeQueryResults.Select(o => o.MergeResult).ToList()); + // MergeParallelExecuteResult(result, , async); foreach (var routeQueryResult in routeQueryResults) { var dbContext = routeQueryResult.DbContext; @@ -103,7 +115,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions { foreach (var routeQueryResult in routeQueryResults) { - result.AddLast(routeQueryResult.MergeResult); + result.Add(routeQueryResult.MergeResult); } } @@ -125,38 +137,33 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions /// /// /// - protected async Task>> GroupExecuteAsync( + protected async Task>> GroupExecuteAsync( List sqlExecutorUnits, CancellationToken cancellationToken = new CancellationToken()) { if (sqlExecutorUnits.Count <= 0) { - return new LinkedList>(); + return new List>(); } else { - var result = new LinkedList>(); - var tasks = sqlExecutorUnits .Select(sqlExecutorUnit => ExecuteUnitAsync(sqlExecutorUnit, cancellationToken)).ToArray(); var results = await TaskHelper.WhenAllFastFail(tasks); - foreach (var r in results) - { - result.AddLast(r); - } + var result = results.ToList(); return result; } } - protected virtual void MergeParallelExecuteResult(LinkedList previewResults, - IEnumerable parallelResults, bool async) - { - foreach (var parallelResult in parallelResults) - { - previewResults.AddLast(parallelResult); - } - } + // protected virtual void MergeParallelExecuteResult(List previewResults, + // IEnumerable parallelResults, bool async) + // { + // foreach (var parallelResult in parallelResults) + // { + // previewResults.Add(parallelResult); + // } + // } protected abstract Task> ExecuteUnitAsync(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/IExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/IExecutor.cs index 0518f759..34596f93 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/IExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/IExecutor.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions { @@ -16,6 +17,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions /// Email: 326308290@qq.com internal interface IExecutor { - Task> ExecuteAsync(bool async, DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()); + IShardingMerger GetShardingMerger(); + Task> ExecuteAsync(bool async, DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()); } } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/CircuitBreakers/EnumeratorCircuitBreaker.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/CircuitBreakers/EnumerableCircuitBreaker.cs similarity index 90% rename from src/ShardingCore/Sharding/MergeEngines/Executors/CircuitBreakers/EnumeratorCircuitBreaker.cs rename to src/ShardingCore/Sharding/MergeEngines/Executors/CircuitBreakers/EnumerableCircuitBreaker.cs index 5d779c92..4ed34cd3 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/CircuitBreakers/EnumeratorCircuitBreaker.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/CircuitBreakers/EnumerableCircuitBreaker.cs @@ -5,9 +5,9 @@ using ShardingCore.Sharding.Enumerators.StreamMergeAsync; namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers { - internal class EnumeratorCircuitBreaker : AbstractCircuitBreaker + internal class EnumerableCircuitBreaker : AbstractCircuitBreaker { - public EnumeratorCircuitBreaker(StreamMergeContext streamMergeContext) : base(streamMergeContext) + public EnumerableCircuitBreaker(StreamMergeContext streamMergeContext) : base(streamMergeContext) { } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/Abstractions/AbstractEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/Abstractions/AbstractEnumerableExecutor.cs similarity index 50% rename from src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/Abstractions/AbstractEnumeratorExecutor.cs rename to src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/Abstractions/AbstractEnumerableExecutor.cs index 3acdea7d..28ea8f70 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/Abstractions/AbstractEnumeratorExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/Abstractions/AbstractEnumerableExecutor.cs @@ -1,26 +1,21 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -using ShardingCore.Core; -using ShardingCore.Exceptions; using ShardingCore.Sharding.Enumerators; using ShardingCore.Sharding.Enumerators.StreamMergeAsync; using ShardingCore.Sharding.Enumerators.StreamMergeAsync.EFCore2x; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; using ShardingCore.Sharding.ShardingExecutors; - #if EFCORE2 using Microsoft.EntityFrameworkCore.Extensions.Internal; #endif -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions +namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions { /// /// @@ -28,80 +23,79 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions /// Author: xjm /// Created: 2022/5/6 12:57:40 /// Email: 326308290@qq.com - internal abstract class AbstractEnumeratorExecutor : AbstractExecutor> + internal abstract class AbstractEnumerableExecutor : AbstractExecutor> { - protected AbstractEnumeratorExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) + protected AbstractEnumerableExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { } - protected abstract IStreamMergeCombine GetStreamMergeCombine(); + // protected abstract IStreamMergeCombine GetStreamMergeCombine(); public override ICircuitBreaker CreateCircuitBreaker() { - return new EnumeratorCircuitBreaker(GetStreamMergeContext()); + return new EnumerableCircuitBreaker(GetStreamMergeContext()); } - protected override void MergeParallelExecuteResult( - LinkedList> previewResults, - IEnumerable> parallelResults, bool async) - { - var previewResultsCount = previewResults.Count; - if (previewResultsCount > 1) - { - throw new ShardingCoreInvalidOperationException( - $"{typeof(TResult)} {nameof(previewResults)} has more than one element in container"); - } + // protected override void MergeParallelExecuteResult( + // LinkedList> previewResults, + // IEnumerable> parallelResults, bool async) + // { + // var previewResultsCount = previewResults.Count; + // if (previewResultsCount > 1) + // { + // throw new ShardingCoreInvalidOperationException( + // $"{typeof(TResult)} {nameof(previewResults)} has more than one element in container"); + // } + // + // var parallelCount = parallelResults.Count(); + // if (parallelCount == 0) + // return; + // //聚合 + // if (previewResults is LinkedList> + // previewInMemoryStreamEnumeratorResults && + // parallelResults is IEnumerable> parallelStreamEnumeratorResults) + // { + // var mergeAsyncEnumerators = new LinkedList>(); + // if (previewResultsCount == 1) + // { + // mergeAsyncEnumerators.AddLast(previewInMemoryStreamEnumeratorResults.First()); + // } + // + // foreach (var parallelStreamEnumeratorResult in parallelStreamEnumeratorResults) + // { + // mergeAsyncEnumerators.AddLast(parallelStreamEnumeratorResult); + // } + // + // var combineStreamMergeAsyncEnumerator = + // CombineInMemoryStreamMergeAsyncEnumerator(mergeAsyncEnumerators.ToArray()); + // // var streamMergeContext = GetStreamMergeContext(); + // // IStreamMergeAsyncEnumerator inMemoryStreamMergeAsyncEnumerator =streamMergeContext.HasGroupQuery()&&streamMergeContext.GroupQueryMemoryMerge()? + // // new InMemoryGroupByOrderStreamMergeAsyncEnumerator(streamMergeContext,combineStreamMergeAsyncEnumerator,async): + // // new InMemoryStreamMergeAsyncEnumerator(combineStreamMergeAsyncEnumerator, async); + // var inMemoryStreamMergeAsyncEnumerator= new InMemoryStreamMergeAsyncEnumerator(combineStreamMergeAsyncEnumerator, async); + // previewInMemoryStreamEnumeratorResults.Clear(); + // previewInMemoryStreamEnumeratorResults.AddLast(inMemoryStreamMergeAsyncEnumerator); + // //合并 + // return; + // } + // + // throw new ShardingCoreInvalidOperationException( + // $"{typeof(TResult)} is not {typeof(IStreamMergeAsyncEnumerator)}"); + // } - var parallelCount = parallelResults.Count(); - if (parallelCount == 0) - return; - //聚合 - if (previewResults is LinkedList> - previewInMemoryStreamEnumeratorResults && - parallelResults is IEnumerable> parallelStreamEnumeratorResults) - { - var mergeAsyncEnumerators = new LinkedList>(); - if (previewResultsCount == 1) - { - mergeAsyncEnumerators.AddLast(previewInMemoryStreamEnumeratorResults.First()); - } - - foreach (var parallelStreamEnumeratorResult in parallelStreamEnumeratorResults) - { - mergeAsyncEnumerators.AddLast(parallelStreamEnumeratorResult); - } - - var combineStreamMergeAsyncEnumerator = - CombineInMemoryStreamMergeAsyncEnumerator(mergeAsyncEnumerators.ToArray()); - var inMemoryStreamMergeAsyncEnumerator = - new InMemoryStreamMergeAsyncEnumerator(combineStreamMergeAsyncEnumerator, async); - previewInMemoryStreamEnumeratorResults.Clear(); - previewInMemoryStreamEnumeratorResults.AddLast(inMemoryStreamMergeAsyncEnumerator); - //合并 - return; - } - - throw new ShardingCoreInvalidOperationException( - $"{typeof(TResult)} is not {typeof(IStreamMergeAsyncEnumerator)}"); - } - - /// - /// 合并成一个迭代器 - /// - /// - /// - public virtual IStreamMergeAsyncEnumerator CombineStreamMergeAsyncEnumerator( - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - return GetStreamMergeCombine() - .StreamMergeEnumeratorCombine(GetStreamMergeContext(), streamsAsyncEnumerators); - } - - public virtual IStreamMergeAsyncEnumerator CombineInMemoryStreamMergeAsyncEnumerator( - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - return CombineStreamMergeAsyncEnumerator(streamsAsyncEnumerators); - } + // /// + // /// 合并成一个迭代器 + // /// + // /// + // /// + // public abstract IStreamMergeAsyncEnumerator CombineStreamMergeAsyncEnumerator( + // IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators); + // + // public virtual IStreamMergeAsyncEnumerator CombineInMemoryStreamMergeAsyncEnumerator( + // IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) + // { + // return CombineStreamMergeAsyncEnumerator(streamsAsyncEnumerators); + // } /// /// 开启异步线程获取并发迭代器 @@ -110,7 +104,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions /// /// /// - public async Task> AsyncParallelEnumerator(IQueryable queryable, + public async Task> AsyncParallelEnumerator(IQueryable queryable, bool async, CancellationToken cancellationToken = new CancellationToken()) { @@ -118,12 +112,12 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions if (async) { var asyncEnumerator = await GetAsyncEnumerator0(queryable); - return new StreamMergeAsyncEnumerator(asyncEnumerator); + return new StreamMergeAsyncEnumerator(asyncEnumerator); } else { var enumerator = GetEnumerator0(queryable); - return new StreamMergeAsyncEnumerator(enumerator); + return new StreamMergeAsyncEnumerator(enumerator); } } @@ -132,7 +126,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions /// /// /// - public async Task> GetAsyncEnumerator0(IQueryable newQueryable) + public async Task> GetAsyncEnumerator0(IQueryable newQueryable) { #if !EFCORE2 var enumator = newQueryable.AsAsyncEnumerable().GetAsyncEnumerator(); @@ -141,7 +135,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions #endif #if EFCORE2 var enumator = - new EFCore2TryCurrentAsyncEnumerator(newQueryable.AsAsyncEnumerable().GetEnumerator()); + new EFCore2TryCurrentAsyncEnumerator(newQueryable.AsAsyncEnumerable().GetEnumerator()); await enumator.MoveNext(); return enumator; #endif @@ -152,14 +146,14 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions /// /// /// - public IEnumerator GetEnumerator0(IQueryable newQueryable) + public IEnumerator GetEnumerator0(IQueryable newQueryable) { var enumator = newQueryable.AsEnumerable().GetEnumerator(); enumator.MoveNext(); return enumator; } - protected override async Task>> ExecuteUnitAsync( + protected override async Task>> ExecuteUnitAsync( SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) { var shardingMergeResult = await ExecuteUnitAsync0(sqlExecutorUnit, cancellationToken); @@ -173,22 +167,23 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions if (DisposeInExecuteUnit(streamMergeContext, streamMergeAsyncEnumerator)) { var disConnectionStreamMergeAsyncEnumerator = - new OneAtMostElementStreamMergeAsyncEnumerator(streamMergeAsyncEnumerator); + new OneAtMostElementStreamMergeAsyncEnumerator(streamMergeAsyncEnumerator); await streamMergeContext.DbContextDisposeAsync(dbContext); - return new ShardingMergeResult>(null, + return new ShardingMergeResult>(null, disConnectionStreamMergeAsyncEnumerator); } return shardingMergeResult; } + /// /// 是否需要在执行单元中直接回收掉链接有助于提高吞吐量 /// /// /// /// - private bool DisposeInExecuteUnit(StreamMergeContext streamMergeContext, + private bool DisposeInExecuteUnit(StreamMergeContext streamMergeContext, IStreamMergeAsyncEnumerator streamMergeAsyncEnumerator) { var queryMethodName = streamMergeContext.MergeQueryCompilerContext.GetQueryMethodName(); @@ -201,7 +196,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions case nameof(Queryable.LastOrDefault): { var skip = streamMergeContext.GetSkip(); - return !hasElement||(skip is null or < 0); + return !hasElement || (skip is null or < 0); } case nameof(Queryable.Single): case nameof(Queryable.SingleOrDefault): @@ -214,7 +209,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions return false; } - protected abstract Task>> ExecuteUnitAsync0( + protected abstract Task>> ExecuteUnitAsync0( SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()); } } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/AppendOrderSequenceEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/AppendOrderSequenceEnumerableExecutor.cs similarity index 67% rename from src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/AppendOrderSequenceEnumeratorExecutor.cs rename to src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/AppendOrderSequenceEnumerableExecutor.cs index 673f4ddd..1545c8cb 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/AppendOrderSequenceEnumeratorExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/AppendOrderSequenceEnumerableExecutor.cs @@ -1,17 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators +namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables { /// /// @@ -19,17 +17,17 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators /// Author: xjm /// Created: 2022/5/6 13:12:31 /// Email: 326308290@qq.com - internal class AppendOrderSequenceEnumeratorExecutor : AbstractEnumeratorExecutor + internal class AppendOrderSequenceEnumerableExecutor : AbstractEnumerableExecutor { - private readonly IStreamMergeCombine _streamMergeCombine; + private readonly IShardingMerger> _shardingMerger; private readonly IQueryable _noPaginationQueryable; private readonly bool _async; - public AppendOrderSequenceEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine, bool async) : base(streamMergeContext) + public AppendOrderSequenceEnumerableExecutor(StreamMergeContext streamMergeContext, bool async) : base(streamMergeContext) { - _streamMergeCombine = streamMergeCombine; _noPaginationQueryable = streamMergeContext.GetOriginalQueryable().RemoveSkip().RemoveTake().As>(); _async = async; + _shardingMerger = new AppendOrderSequenceEnumerableShardingMerger(streamMergeContext,async); } protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) @@ -48,9 +46,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators return new ShardingMergeResult>(shardingDbContext, streamMergeAsyncEnumerator); } - protected override IStreamMergeCombine GetStreamMergeCombine() + public override IShardingMerger> GetShardingMerger() { - return _streamMergeCombine; + return _shardingMerger; } } } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/DefaultEnumerableExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/DefaultEnumerableExecutor.cs new file mode 100644 index 00000000..af1da627 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/DefaultEnumerableExecutor.cs @@ -0,0 +1,61 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables +{ + /// + /// + /// + /// Author: xjm + /// Created: 2022/5/6 13:00:40 + /// Email: 326308290@qq.com + internal class DefaultEnumerableExecutor : AbstractEnumerableExecutor + { + // private readonly IStreamMergeCombine _streamMergeCombine; + private readonly IShardingMerger> _shardingMerger; + private readonly bool _async; + + public DefaultEnumerableExecutor(StreamMergeContext streamMergeContext, bool async) : base(streamMergeContext) + { + // _streamMergeCombine = streamMergeCombine; + _async = async; + _shardingMerger = new DefaultEnumerableShardingMerger(streamMergeContext,async); + } + // + // protected override IStreamMergeCombine GetStreamMergeCombine() + // { + // return _streamMergeCombine; + // } + + // public override IStreamMergeAsyncEnumerator CombineInMemoryStreamMergeAsyncEnumerator( + // IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) + // { + // if (GetStreamMergeContext().IsPaginationQuery()) + // return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());//内存聚合分页不可以直接获取skip必须获取skip+take的数目 + // return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators); + // } + + protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) + { + var streamMergeContext = GetStreamMergeContext(); + var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); + var newQueryable = (IQueryable)streamMergeContext.GetReWriteQueryable() + .ReplaceDbContextQueryable(shardingDbContext); + + var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); + return new ShardingMergeResult>(shardingDbContext, streamMergeAsyncEnumerator); + } + + public override IShardingMerger> GetShardingMerger() + { + return _shardingMerger; + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/EmptyEnumerableExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/EmptyEnumerableExecutor.cs new file mode 100644 index 00000000..4f82b55b --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/EmptyEnumerableExecutor.cs @@ -0,0 +1,38 @@ +// using System; +// using System.Threading; +// using System.Threading.Tasks; +// using ShardingCore.Sharding.Enumerators; +// using ShardingCore.Sharding.MergeEngines.Abstractions; +// using ShardingCore.Sharding.MergeEngines.Common; +// using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +// using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +// +// namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables +// { +// /// +// /// +// /// +// /// Author: xjm +// /// Created: 2022/5/6 13:10:34 +// /// Email: 326308290@qq.com +// internal class EmptyShardingExecutor : AbstractEnumerableExecutor +// { +// // private readonly IShardingMerger> _shardingMerger; +// +// public EmptyShardingExecutor(StreamMergeContext streamMergeContext, bool async) : base(streamMergeContext) +// { +// // _shardingMerger = new EmptyEnumerableShardingMerger(streamMergeContext,async); +// } +// +// protected override Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) +// { +// throw new NotImplementedException(); +// } +// +// +// public override IShardingMerger> GetShardingMerger() +// { +// throw new NotImplementedException(); +// } +// } +// } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/LastOrDefaultEnumerableExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/LastOrDefaultEnumerableExecutor.cs new file mode 100644 index 00000000..c86a7b2c --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/LastOrDefaultEnumerableExecutor.cs @@ -0,0 +1,47 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; +using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables +{ + internal class LastOrDefaultEnumerableExecutor : AbstractEnumerableExecutor + { + private readonly IShardingMerger> _shardingMerger; + private readonly IQueryable _queryable; + private readonly bool _async; + + public LastOrDefaultEnumerableExecutor(StreamMergeContext streamMergeContext, IQueryable queryable, + bool async) : base(streamMergeContext) + { + _queryable = queryable; + _async = async; + _shardingMerger = new LastOrDefaultEnumerableShardingMerger(GetStreamMergeContext(), async); + } + + + protected override async Task>> ExecuteUnitAsync0( + SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) + { + var streamMergeContext = GetStreamMergeContext(); + + var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); + var newQueryable = (IQueryable)_queryable.ReplaceDbContextQueryable(shardingDbContext); + + var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); + return new ShardingMergeResult>(shardingDbContext, + streamMergeAsyncEnumerator); + } + + public override IShardingMerger> GetShardingMerger() + { + return _shardingMerger; + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/ReverseEnumerableExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/ReverseEnumerableExecutor.cs new file mode 100644 index 00000000..848a9422 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/ReverseEnumerableExecutor.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Extensions.InternalExtensions; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; +using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables +{ + /// + /// + /// + /// Author: xjm + /// Created: 2022/5/6 13:19:34 + /// Email: 326308290@qq.com + internal class ReverseEnumerableExecutor : AbstractEnumerableExecutor + { + private readonly IShardingMerger> _shardingMerger; + private readonly IOrderedQueryable _reverseOrderQueryable; + private readonly bool _async; + + public ReverseEnumerableExecutor(StreamMergeContext streamMergeContext, IOrderedQueryable reverseOrderQueryable, bool async) : base(streamMergeContext) + { + _reverseOrderQueryable = reverseOrderQueryable; + _async = async; + _shardingMerger = new ReverseEnumerableShardingMerger(streamMergeContext, async); + } + + protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) + { + var streamMergeContext = GetStreamMergeContext(); + + var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); + var newQueryable = _reverseOrderQueryable + .ReplaceDbContextQueryable(shardingDbContext).As>(); + var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); + return new ShardingMergeResult>(shardingDbContext, + streamMergeAsyncEnumerator); + } + + public override IShardingMerger> GetShardingMerger() + { + return _shardingMerger; + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/SequenceEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/SequenceEnumerableExecutor.cs similarity index 67% rename from src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/SequenceEnumeratorExecutor.cs rename to src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/SequenceEnumerableExecutor.cs index 7a9da212..00a57d21 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/SequenceEnumeratorExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/SequenceEnumerableExecutor.cs @@ -1,17 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators +namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables { /// /// @@ -19,17 +17,17 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators /// Author: xjm /// Created: 2022/5/6 13:30:08 /// Email: 326308290@qq.com - internal class SequenceEnumeratorExecutor : AbstractEnumeratorExecutor + internal class SequenceEnumerableExecutor : AbstractEnumerableExecutor { - private readonly IStreamMergeCombine _streamMergeCombine; + private readonly IShardingMerger> _shardingMerger; private readonly IQueryable _noPaginationQueryable; private readonly bool _async; - public SequenceEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine, bool async) : base(streamMergeContext) + public SequenceEnumerableExecutor(StreamMergeContext streamMergeContext, bool async) : base(streamMergeContext) { - _streamMergeCombine = streamMergeCombine; _async = async; _noPaginationQueryable = streamMergeContext.GetOriginalQueryable().RemoveSkip().RemoveTake().As>(); + _shardingMerger = new SequenceEnumerableShardingMerger(streamMergeContext, async); } protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) @@ -47,9 +45,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators return new ShardingMergeResult>(shardingDbContext, streamMergeAsyncEnumerator); } - protected override IStreamMergeCombine GetStreamMergeCombine() + public override IShardingMerger> GetShardingMerger() { - return _streamMergeCombine; + return _shardingMerger; } } } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/SingleQueryEnumerableExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/SingleQueryEnumerableExecutor.cs new file mode 100644 index 00000000..92746a19 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerables/SingleQueryEnumerableExecutor.cs @@ -0,0 +1,33 @@ +// using System; +// using System.Threading; +// using System.Threading.Tasks; +// using ShardingCore.Sharding.Enumerators; +// using ShardingCore.Sharding.MergeEngines.Abstractions; +// using ShardingCore.Sharding.MergeEngines.Common; +// using ShardingCore.Sharding.MergeEngines.Executors.Enumerables.Abstractions; +// +// namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerables +// { +// /// +// /// +// /// +// /// Author: xjm +// /// Created: 2022/5/6 18:31:17 +// /// Email: 326308290@qq.com +// internal class SingleQueryEnumerableExecutor : AbstractEnumerableExecutor +// { +// public SingleQueryEnumerableExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) +// { +// } +// protected override Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) +// { +// throw new NotImplementedException(); +// } +// +// protected override IStreamMergeCombine GetStreamMergeCombine() +// { +// throw new NotImplementedException(); +// } +// +// } +// } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/DefaultEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/DefaultEnumeratorExecutor.cs deleted file mode 100644 index da15ae43..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/DefaultEnumeratorExecutor.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators -{ - /// - /// - /// - /// Author: xjm - /// Created: 2022/5/6 13:00:40 - /// Email: 326308290@qq.com - internal class DefaultEnumeratorExecutor : AbstractEnumeratorExecutor - { - private readonly IStreamMergeCombine _streamMergeCombine; - private readonly bool _async; - - public DefaultEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine, bool async) : base(streamMergeContext) - { - _streamMergeCombine = streamMergeCombine; - _async = async; - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return _streamMergeCombine; - } - - public override IStreamMergeAsyncEnumerator CombineInMemoryStreamMergeAsyncEnumerator( - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (GetStreamMergeContext().IsPaginationQuery()) - return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());//内存聚合分页不可以直接获取skip必须获取skip+take的数目 - return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators); - } - - protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) - { - var streamMergeContext = GetStreamMergeContext(); - var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); - var newQueryable = (IQueryable)streamMergeContext.GetReWriteQueryable() - .ReplaceDbContextQueryable(shardingDbContext); - - var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); - return new ShardingMergeResult>(shardingDbContext, streamMergeAsyncEnumerator); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/EmptyQueryEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/EmptyQueryEnumeratorExecutor.cs deleted file mode 100644 index 87f4ba2e..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/EmptyQueryEnumeratorExecutor.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators -{ - /// - /// - /// - /// Author: xjm - /// Created: 2022/5/6 13:10:34 - /// Email: 326308290@qq.com - internal class EmptyQueryEnumeratorExecutor : AbstractEnumeratorExecutor - { - private readonly IStreamMergeCombine _streamMergeCombine; - private readonly bool _async; - - public EmptyQueryEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine, bool async) : base(streamMergeContext) - { - _streamMergeCombine = streamMergeCombine; - _async = async; - } - - protected override Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) - { - throw new NotImplementedException(); - } - - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return _streamMergeCombine; - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/LastOrDefaultEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/LastOrDefaultEnumeratorExecutor.cs deleted file mode 100644 index b6f3dd6f..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/LastOrDefaultEnumeratorExecutor.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Extensions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators -{ -internal class LastOrDefaultEnumeratorExecutor : AbstractEnumeratorExecutor - { - private readonly IStreamMergeCombine _streamMergeCombine; - private readonly IQueryable _queryable; - private readonly bool _async; - - public LastOrDefaultEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine,IQueryable queryable, bool async) : base(streamMergeContext) - { - _streamMergeCombine = streamMergeCombine; - _queryable = queryable; - _async = async; - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return _streamMergeCombine; - } - - public override IStreamMergeAsyncEnumerator CombineInMemoryStreamMergeAsyncEnumerator( - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (GetStreamMergeContext().IsPaginationQuery()) - return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());//内存聚合分页不可以直接获取skip必须获取skip+take的数目 - return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators); - } - - protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) - { - var streamMergeContext = GetStreamMergeContext(); - - var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); - var newQueryable = (IQueryable)_queryable.ReplaceDbContextQueryable(shardingDbContext); - - var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); - return new ShardingMergeResult>(shardingDbContext, streamMergeAsyncEnumerator); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/ReverseEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/ReverseEnumeratorExecutor.cs deleted file mode 100644 index d8e297e2..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/ReverseEnumeratorExecutor.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Extensions; -using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.Enumerators.StreamMergeAsync; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators -{ - /// - /// - /// - /// Author: xjm - /// Created: 2022/5/6 13:19:34 - /// Email: 326308290@qq.com - internal class ReverseEnumeratorExecutor : AbstractEnumeratorExecutor - { - private readonly IStreamMergeCombine _streamMergeCombine; - private readonly IOrderedQueryable _reverseOrderQueryable; - private readonly bool _async; - - public ReverseEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine, IOrderedQueryable reverseOrderQueryable, bool async) : base(streamMergeContext) - { - _streamMergeCombine = streamMergeCombine; - _reverseOrderQueryable = reverseOrderQueryable; - _async = async; - } - - protected override async Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) - { - var streamMergeContext = GetStreamMergeContext(); - - var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); - var newQueryable = _reverseOrderQueryable - .ReplaceDbContextQueryable(shardingDbContext).As>(); - var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); - return new ShardingMergeResult>(shardingDbContext, - streamMergeAsyncEnumerator); - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - return _streamMergeCombine; - } - - public override IStreamMergeAsyncEnumerator CombineInMemoryStreamMergeAsyncEnumerator( - IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) - { - if (GetStreamMergeContext().IsPaginationQuery() && GetStreamMergeContext().HasGroupQuery()) - { - var multiAggregateOrderStreamMergeAsyncEnumerator = new MultiAggregateOrderStreamMergeAsyncEnumerator(GetStreamMergeContext(), streamsAsyncEnumerators); - return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), new[] { multiAggregateOrderStreamMergeAsyncEnumerator }, 0, GetStreamMergeContext().GetPaginationReWriteTake()); - } - if (GetStreamMergeContext().IsPaginationQuery()) - return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake()); - return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators); - } - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/SingleQueryEnumeratorExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/SingleQueryEnumeratorExecutor.cs deleted file mode 100644 index 5bea0a2e..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Enumerators/SingleQueryEnumeratorExecutor.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; -using ShardingCore.Sharding.MergeEngines.Common; -using ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions; - -namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators -{ - /// - /// - /// - /// Author: xjm - /// Created: 2022/5/6 18:31:17 - /// Email: 326308290@qq.com - internal class SingleQueryEnumeratorExecutor : AbstractEnumeratorExecutor - { - public SingleQueryEnumeratorExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - } - protected override Task>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) - { - throw new NotImplementedException(); - } - - protected override IStreamMergeCombine GetStreamMergeCombine() - { - throw new NotImplementedException(); - } - - } -} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodExecutor.cs index 43692791..cce3f9a3 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodExecutor.cs @@ -1,43 +1,31 @@ -using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions { - /// - /// - /// - /// Author: xjm - /// Created: 2022/5/7 7:45:07 - /// Email: 326308290@qq.com - internal abstract class AbstractMethodExecutor : AbstractExecutor> + internal abstract class AbstractMethodExecutor : AbstractExecutor { protected AbstractMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { } - protected override async Task>> ExecuteUnitAsync(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) + protected override async Task> ExecuteUnitAsync(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) { var streamMergeContext = GetStreamMergeContext(); - var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName; - var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult; var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); var newQueryable = GetStreamMergeContext().GetReWriteQueryable() .ReplaceDbContextQueryable(shardingDbContext); var queryResult = await EFCoreQueryAsync(newQueryable, cancellationToken); - var routeQueryResult = new RouteQueryResult(dataSourceName, routeResult, queryResult); await streamMergeContext.DbContextDisposeAsync(shardingDbContext); - return new ShardingMergeResult>(null, routeQueryResult); + return new ShardingMergeResult(null, queryResult); } protected abstract Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodWrapExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodWrapExecutor.cs new file mode 100644 index 00000000..5da813ab --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/Abstractions/AbstractMethodWrapExecutor.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions +{ + /// + /// + /// + /// Author: xjm + /// Created: 2022/5/7 7:45:07 + /// Email: 326308290@qq.com + internal abstract class AbstractMethodWrapExecutor : AbstractExecutor> + { + protected AbstractMethodWrapExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) + { + } + + protected override async Task>> ExecuteUnitAsync(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken()) + { + var streamMergeContext = GetStreamMergeContext(); + var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName; + var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult; + + var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit); + var newQueryable = GetStreamMergeContext().GetReWriteQueryable() + .ReplaceDbContextQueryable(shardingDbContext); + + var queryResult = await EFCoreQueryAsync(newQueryable, cancellationToken); + var routeQueryResult = new RouteQueryResult(dataSourceName, routeResult, queryResult); + await streamMergeContext.DbContextDisposeAsync(shardingDbContext); + return new ShardingMergeResult>(null, routeQueryResult); + } + + protected abstract Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()); + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AllMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AllMethodExecutor.cs index adc8e0e7..ea12fd3b 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AllMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AllMethodExecutor.cs @@ -10,7 +10,10 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; using ShardingCore.Sharding.ShardingExecutors.QueryableCombines; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -36,6 +39,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return allCircuitBreaker; } + public override IShardingMerger GetShardingMerger() + { + return AllMethodShardingMerger.Instance; + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { var allQueryCombineResult = (AllQueryCombineResult)GetStreamMergeContext().MergeQueryCompilerContext.GetQueryCombineResult(); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AnyMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AnyMethodExecutor.cs index b3a631e7..81eefc12 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AnyMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AnyMethodExecutor.cs @@ -6,11 +6,12 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods @@ -32,6 +33,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return new AnyElementCircuitBreaker(GetStreamMergeContext()); } + public override IShardingMerger GetShardingMerger() + { + return AnyMethodShardingMerger.Instance; + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { return queryable.As>().AnyAsync(cancellationToken); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AverageMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AverageMethodWrapExecutor.cs similarity index 64% rename from src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AverageMethodExecutor.cs rename to src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AverageMethodWrapExecutor.cs index 9696ed04..8821e2b0 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AverageMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/AverageMethodWrapExecutor.cs @@ -13,6 +13,9 @@ using ShardingCore.Sharding.MergeEngines.AggregateMergeEngines; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -22,9 +25,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods /// Author: xjm /// Created: 2022/5/7 11:13:57 /// Email: 326308290@qq.com - internal class AverageMethodExecutor : AbstractMethodExecutor> + internal class AverageMethodWrapExecutor : AbstractMethodWrapExecutor> { - public AverageMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) + public AverageMethodWrapExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { } @@ -33,11 +36,16 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return new NoTripCircuitBreaker(GetStreamMergeContext()); } - protected override async Task> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) + public override IShardingMerger>> GetShardingMerger() + { + return new AverageMethodShardingMerger(); + } + + protected override async Task> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { var count = 0L; - TEntity sum = default; - var newQueryable = ((IQueryable)queryable); + TSelect sum = default; + var newQueryable = ((IQueryable)queryable); var r = await newQueryable.GroupBy(o => 1).BuildExpression().FirstOrDefaultAsync(cancellationToken); if (r != null) { @@ -48,7 +56,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { return default; } - return new AverageResult(sum, count); + return new AverageResult(sum, count); } } } diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/ContainsMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/ContainsMethodExecutor.cs index 9090a1ff..8743caa1 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/ContainsMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/ContainsMethodExecutor.cs @@ -9,7 +9,10 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; using ShardingCore.Sharding.ShardingExecutors.QueryableCombines; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -35,6 +38,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return circuitBreaker; } + public override IShardingMerger GetShardingMerger() + { + return ContainsMethodShardingMerger.Instance; + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { var constantQueryCombineResult = (ConstantQueryCombineResult)GetStreamMergeContext().MergeQueryCompilerContext.GetQueryCombineResult(); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/CountMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/CountMethodExecutor.cs index 147957d4..bb5b64e5 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/CountMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/CountMethodExecutor.cs @@ -9,6 +9,9 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -18,7 +21,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods /// Author: xjm /// Created: 2022/5/7 8:26:46 /// Email: 326308290@qq.com - internal class CountMethodExecutor : AbstractMethodExecutor + internal class CountMethodExecutor : AbstractMethodWrapExecutor { public CountMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { @@ -34,6 +37,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return circuitBreaker; } + public override IShardingMerger> GetShardingMerger() + { + return new CountMethodShardingMerger(GetStreamMergeContext()); + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { return queryable.As>().CountAsync(cancellationToken); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/LongCountMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/LongCountMethodExecutor.cs index 9031adcc..49b47fb7 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/LongCountMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/LongCountMethodExecutor.cs @@ -9,6 +9,9 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -18,7 +21,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods /// Author: xjm /// Created: 2022/5/7 8:26:46 /// Email: 326308290@qq.com - internal class LongCountMethodExecutor : AbstractMethodExecutor + internal class LongCountMethodExecutor : AbstractMethodWrapExecutor { public LongCountMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { @@ -34,6 +37,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return circuitBreaker; } + public override IShardingMerger> GetShardingMerger() + { + return new LongCountMethodShardingMerger(GetStreamMergeContext()); + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { return queryable.As>().LongCountAsync(cancellationToken); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MaxMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MaxMethodExecutor.cs index d11ecb72..d62fee1f 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MaxMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MaxMethodExecutor.cs @@ -12,6 +12,8 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods @@ -22,7 +24,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods /// Author: xjm /// Created: 2022/5/7 11:13:57 /// Email: 326308290@qq.com - internal class MaxMethodExecutor : AbstractMethodExecutor + internal class MaxMethodExecutor : AbstractMethodWrapExecutor { public MaxMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { @@ -33,6 +35,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return new AnyElementCircuitBreaker(GetStreamMergeContext()); } + public override IShardingMerger> GetShardingMerger() + { + return new MaxMethodShardingMerger(); + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MinMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MinMethodExecutor.cs index f79b12fc..b5b1c538 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MinMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/MinMethodExecutor.cs @@ -11,6 +11,9 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -20,7 +23,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods /// Author: xjm /// Created: 2022/5/7 11:13:57 /// Email: 326308290@qq.com - internal class MinMethodExecutor : AbstractMethodExecutor + internal class MinMethodExecutor : AbstractMethodWrapExecutor { public MinMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { @@ -31,6 +34,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return new AnyElementCircuitBreaker(GetStreamMergeContext()); } + public override IShardingMerger> GetShardingMerger() + { + return new MinMethodShardingMerger(); + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/SumMethodExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/SumMethodWrapExecutor.cs similarity index 77% rename from src/ShardingCore/Sharding/MergeEngines/Executors/Methods/SumMethodExecutor.cs rename to src/ShardingCore/Sharding/MergeEngines/Executors/Methods/SumMethodWrapExecutor.cs index d53d7c9f..bfc6dd28 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/SumMethodExecutor.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/Methods/SumMethodWrapExecutor.cs @@ -12,6 +12,9 @@ using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; using ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers; using ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; namespace ShardingCore.Sharding.MergeEngines.Executors.Methods { @@ -21,9 +24,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods /// Author: xjm /// Created: 2022/5/7 11:13:57 /// Email: 326308290@qq.com - internal class SumMethodExecutor : AbstractMethodExecutor + internal class SumMethodWrapExecutor : AbstractMethodExecutor { - public SumMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) + public SumMethodWrapExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext) { } @@ -32,6 +35,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods return new NoTripCircuitBreaker(GetStreamMergeContext()); } + public override IShardingMerger GetShardingMerger() + { + return new SumMethodShardingMerger(); + } + protected override Task EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken()) { var resultType = typeof(TEntity); diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AbstractEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AbstractEnumerableShardingMerger.cs new file mode 100644 index 00000000..d588e397 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AbstractEnumerableShardingMerger.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Exceptions; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal abstract class AbstractEnumerableShardingMerger:IShardingMerger> + { + private readonly StreamMergeContext _streamMergeContext; + private readonly bool _async; + + protected StreamMergeContext GetStreamMergeContext() + { + return _streamMergeContext; + } + public AbstractEnumerableShardingMerger(StreamMergeContext streamMergeContext,bool async) + { + _streamMergeContext = streamMergeContext; + _async = async; + } + public virtual IStreamMergeAsyncEnumerator StreamMerge(List> parallelResults) + { + if (_streamMergeContext.IsPaginationQuery()) + return new PaginationStreamMergeAsyncEnumerator(_streamMergeContext, parallelResults); + if (_streamMergeContext.HasGroupQuery()) + return new MultiAggregateOrderStreamMergeAsyncEnumerator(_streamMergeContext, parallelResults); + return new MultiOrderStreamMergeAsyncEnumerator(_streamMergeContext, parallelResults); + } + + + public virtual void InMemoryMerge(List> beforeInMemoryResults, List> parallelResults) + { + var previewResultsCount = beforeInMemoryResults.Count; + if (previewResultsCount > 1) + { + throw new ShardingCoreInvalidOperationException( + $"{typeof(TEntity)} {nameof(beforeInMemoryResults)} has more than one element in container"); + } + var parallelCount = parallelResults.Count; + if (parallelCount == 0) + return; + + + //聚合 + if (parallelResults is IEnumerable> parallelStreamEnumeratorResults) + { + var mergeAsyncEnumerators = new List>(parallelResults.Count); + if (previewResultsCount == 1) + { + mergeAsyncEnumerators.Add(beforeInMemoryResults.First()); + } + + foreach (var parallelStreamEnumeratorResult in parallelStreamEnumeratorResults) + { + mergeAsyncEnumerators.Add(parallelStreamEnumeratorResult); + } + + var combineStreamMergeAsyncEnumerator =StreamMerge(mergeAsyncEnumerators); + // var streamMergeContext = GetStreamMergeContext(); + // IStreamMergeAsyncEnumerator inMemoryStreamMergeAsyncEnumerator =streamMergeContext.HasGroupQuery()&&streamMergeContext.GroupQueryMemoryMerge()? + // new InMemoryGroupByOrderStreamMergeAsyncEnumerator(streamMergeContext,combineStreamMergeAsyncEnumerator,async): + // new InMemoryStreamMergeAsyncEnumerator(combineStreamMergeAsyncEnumerator, async); + var inMemoryStreamMergeAsyncEnumerator= new InMemoryStreamMergeAsyncEnumerator(combineStreamMergeAsyncEnumerator, _async); + beforeInMemoryResults.Clear(); + beforeInMemoryResults.Add(inMemoryStreamMergeAsyncEnumerator); + //合并 + return; + } + + throw new ShardingCoreInvalidOperationException( + $"{typeof(TEntity)} is not {typeof(IStreamMergeAsyncEnumerator)}"); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AllMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AllMethodShardingMerger.cs new file mode 100644 index 00000000..626c90f5 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AllMethodShardingMerger.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class AllMethodShardingMerger:IShardingMerger + { + private static readonly IShardingMerger _allShardingMerger; + + static AllMethodShardingMerger() + { + _allShardingMerger = new AllMethodShardingMerger(); + } + + public static IShardingMerger Instance => _allShardingMerger; + + public bool StreamMerge(List parallelResults) + { + return parallelResults.All(o => o); + } + + public void InMemoryMerge(List beforeInMemoryResults, List parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AnyMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AnyMethodShardingMerger.cs new file mode 100644 index 00000000..7fda017b --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AnyMethodShardingMerger.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class AnyMethodShardingMerger:IShardingMerger + { + private static readonly IShardingMerger _allShardingMerger; + + static AnyMethodShardingMerger() + { + _allShardingMerger = new AllMethodShardingMerger(); + } + + public static IShardingMerger Instance => _allShardingMerger; + + public bool StreamMerge(List parallelResults) + { + return parallelResults.Any(o => o); + } + + public void InMemoryMerge(List beforeInMemoryResults, List parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AppendOrderSequenceEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AppendOrderSequenceEnumerableShardingMerger.cs new file mode 100644 index 00000000..79a3bcf3 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AppendOrderSequenceEnumerableShardingMerger.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + /// + /// 和普通的不同因为是顺序查询所以需要忽略分页合并 + /// + /// + internal class AppendOrderSequenceEnumerableShardingMerger : AbstractEnumerableShardingMerger + { + public AppendOrderSequenceEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base( + streamMergeContext, async) + { + } + + public override IStreamMergeAsyncEnumerator StreamMerge( + List> parallelResults) + { + if (GetStreamMergeContext().HasGroupQuery()) + return new MultiAggregateOrderStreamMergeAsyncEnumerator(GetStreamMergeContext(), + parallelResults); + return new MultiOrderStreamMergeAsyncEnumerator(GetStreamMergeContext(), parallelResults); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AverageMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AverageMethodShardingMerger.cs new file mode 100644 index 00000000..da82aab2 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/AverageMethodShardingMerger.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Enumerators.AggregateExtensions; +using ShardingCore.Sharding.MergeEngines.AggregateMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class AverageMethodShardingMerger : IShardingMerger>> + { + public RouteQueryResult> StreamMerge( + List>> parallelResults) + { + if (parallelResults.IsEmpty()) + throw new InvalidOperationException("Sequence contains no elements."); + var queryable = parallelResults.Where(o => o.HasQueryResult()).Select(o => new + { + Sum = o.QueryResult.Sum, + Count = o.QueryResult.Count + }).AsQueryable(); + var sum = queryable.SumByPropertyName(nameof(AverageResult.Sum)); + var count = queryable.Sum(o => o.Count); + return new RouteQueryResult>(null, null, + new AverageResult(sum , count)); + } + + public void InMemoryMerge(List>> beforeInMemoryResults, + List>> parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} + + +// var resultList = await base.ExecuteAsync>(cancellationToken); +// if (resultList.IsEmpty()) +// throw new InvalidOperationException("Sequence contains no elements."); +// var queryable = resultList.Where(o=>o.HasQueryResult()).Select(o => new +// { +// Sum = o.QueryResult.Sum, +// Count = o.QueryResult.Count +// }).AsQueryable(); +// var sum = queryable.SumByPropertyName(nameof(AverageResult.Sum)); +// var count = queryable.Sum(o => o.Count); \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/ContainsMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/ContainsMethodShardingMerger.cs new file mode 100644 index 00000000..adf6950e --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/ContainsMethodShardingMerger.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class ContainsMethodShardingMerger:IShardingMerger + { + private static readonly IShardingMerger _allShardingMerger; + + static ContainsMethodShardingMerger() + { + _allShardingMerger = new AllMethodShardingMerger(); + } + + public static IShardingMerger Instance => _allShardingMerger; + + public bool StreamMerge(List parallelResults) + { + return parallelResults.Any(o => o); + } + + public void InMemoryMerge(List beforeInMemoryResults, List parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/CountMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/CountMethodShardingMerger.cs new file mode 100644 index 00000000..3a8a8625 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/CountMethodShardingMerger.cs @@ -0,0 +1,39 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Core.ShardingPage.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class CountMethodShardingMerger:IShardingMerger> + { + private readonly IShardingPageManager _shardingPageManager; + + public CountMethodShardingMerger(StreamMergeContext streamMergeContext) + { + _shardingPageManager =streamMergeContext.ShardingRuntimeContext.GetShardingPageManager(); + } + public RouteQueryResult StreamMerge(List> parallelResults) + { + + if (_shardingPageManager.Current != null) + { + int r = 0; + foreach (var routeQueryResult in parallelResults) + { + _shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult)); + r += routeQueryResult.QueryResult; + } + + return new RouteQueryResult(null,null,r,true); + } + return new RouteQueryResult(null,null,parallelResults.Sum(o => o.QueryResult),true); + } + + public void InMemoryMerge(List> beforeInMemoryResults, List> parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/DefaultEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/DefaultEnumerableShardingMerger.cs new file mode 100644 index 00000000..14d9a7b8 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/DefaultEnumerableShardingMerger.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; +using ShardingCore.Sharding.Enumerators; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class DefaultEnumerableShardingMerger:AbstractEnumerableShardingMerger + { + public DefaultEnumerableShardingMerger(StreamMergeContext streamMergeContext,bool async) : base(streamMergeContext,async) + { + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/EmptyEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/EmptyEnumerableShardingMerger.cs new file mode 100644 index 00000000..15827914 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/EmptyEnumerableShardingMerger.cs @@ -0,0 +1,39 @@ +// using System.Collections.Generic; +// using ShardingCore.Exceptions; +// using ShardingCore.Sharding.Enumerators; +// +// namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +// { +// internal class EmptyEnumerableShardingMerger : AbstractEnumerableShardingMerger +// { +// public EmptyEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base( +// streamMergeContext, async) +// { +// } +// +// public override IStreamMergeAsyncEnumerator StreamMerge( +// List> parallelResults) +// { +// if (parallelResults.Count != 1) +// throw new ShardingCoreInvalidOperationException( +// $"empty query combine has more {nameof(IStreamMergeAsyncEnumerator)}"); +// return parallelResults[0]; +// } +// +// public override void InMemoryMerge(List> beforeInMemoryResults, List> parallelResults) +// { +// var previewResultsCount = beforeInMemoryResults.Count; +// if (previewResultsCount > 1) +// { +// throw new ShardingCoreInvalidOperationException( +// $"{typeof(TEntity)} {nameof(beforeInMemoryResults)} has more than one element in container"); +// } +// +// var parallelCount = parallelResults.Count; +// if (parallelCount == 0) +// return; +// beforeInMemoryResults.Clear(); +// beforeInMemoryResults.Add(parallelResults[0]); +// } +// } +// } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/LastOrDefaultEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/LastOrDefaultEnumerableShardingMerger.cs new file mode 100644 index 00000000..851a6825 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/LastOrDefaultEnumerableShardingMerger.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class LastOrDefaultEnumerableShardingMerger : AbstractEnumerableShardingMerger + { + public LastOrDefaultEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base( + streamMergeContext, async) + { + } + + public override IStreamMergeAsyncEnumerator StreamMerge( + List> parallelResults) + { + if (GetStreamMergeContext().IsPaginationQuery()) + return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), parallelResults, 0, + GetStreamMergeContext().GetPaginationReWriteTake()); //内存聚合分页不可以直接获取skip必须获取skip+take的数目 + return base.StreamMerge(parallelResults); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/LongCountMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/LongCountMethodShardingMerger.cs new file mode 100644 index 00000000..aba8d683 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/LongCountMethodShardingMerger.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Core.ShardingPage.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + + internal class LongCountMethodShardingMerger:IShardingMerger> + { + private readonly IShardingPageManager _shardingPageManager; + + public LongCountMethodShardingMerger(StreamMergeContext streamMergeContext) + { + _shardingPageManager =streamMergeContext.ShardingRuntimeContext.GetShardingPageManager(); + } + public RouteQueryResult StreamMerge(List> parallelResults) + { + + if (_shardingPageManager.Current != null) + { + long r = 0; + foreach (var routeQueryResult in parallelResults) + { + _shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult)); + r += routeQueryResult.QueryResult; + } + + return new RouteQueryResult(null,null,r,true); + } + return new RouteQueryResult(null,null,parallelResults.Sum(o => o.QueryResult),true); + } + + public void InMemoryMerge(List> beforeInMemoryResults, List> parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/MaxMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/MaxMethodShardingMerger.cs new file mode 100644 index 00000000..86626d10 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/MaxMethodShardingMerger.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class MaxMethodShardingMerger : IShardingMerger> + { + public RouteQueryResult StreamMerge(List> parallelResults) + { + var result = parallelResults.Where(o => o.HasQueryResult()).Max(o => o.QueryResult); + return new RouteQueryResult(null, null, result); + // var resultType = typeof(TEntity); + // if (!resultType.IsNullableType()) + // { + // var minTResult = GetMinTResult(parallelResults); + // return new RouteQueryResult(null, null, minTResult); + // } + // else + // { + // var result = parallelResults.Where(o => o.HasQueryResult()).Min(o => o.QueryResult); + // return new RouteQueryResult(null, null, result); + // } + } + + // private TResult GetMinTResult(List> source) + // { + // var routeQueryResults = source.Where(o => o.HasQueryResult()).ToList(); + // if (routeQueryResults.IsEmpty()) + // throw new InvalidOperationException("Sequence contains no elements."); + // var min = routeQueryResults.Min(o => o.QueryResult); + // + // return ConvertNumber(min); + // } + // + // private TResult ConvertNumber(TNumber number) + // { + // if (number == null) + // return default; + // var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); + // return Expression.Lambda>(convertExpr).Compile()(); + // } + public void InMemoryMerge(List> beforeInMemoryResults, + List> parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/MinMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/MinMethodShardingMerger.cs new file mode 100644 index 00000000..8ebb7930 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/MinMethodShardingMerger.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using ShardingCore.Exceptions; +using ShardingCore.Extensions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class MinMethodShardingMerger : IShardingMerger> + { + public RouteQueryResult StreamMerge(List> parallelResults) + { + var result = parallelResults.Where(o => o.HasQueryResult()).Min(o => o.QueryResult); + return new RouteQueryResult(null, null, result); + // var resultType = typeof(TEntity); + // if (!resultType.IsNullableType()) + // { + // var minTResult = GetMinTResult(parallelResults); + // return new RouteQueryResult(null, null, minTResult); + // } + // else + // { + // var result = parallelResults.Where(o => o.HasQueryResult()).Min(o => o.QueryResult); + // return new RouteQueryResult(null, null, result); + // } + } + + // private TResult GetMinTResult(List> source) + // { + // var routeQueryResults = source.Where(o => o.HasQueryResult()).ToList(); + // if (routeQueryResults.IsEmpty()) + // throw new InvalidOperationException("Sequence contains no elements."); + // var min = routeQueryResults.Min(o => o.QueryResult); + // + // return ConvertNumber(min); + // } + // + // private TResult ConvertNumber(TNumber number) + // { + // if (number == null) + // return default; + // var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); + // return Expression.Lambda>(convertExpr).Compile()(); + // } + public void InMemoryMerge(List> beforeInMemoryResults, + List> parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} + + +// +// var resultType = typeof(TEntity); +// if (!resultType.IsNullableType()) +// { +// if (typeof(decimal) == resultType) +// { +// var result = await base.ExecuteAsync(cancellationToken); +// return GetMinTResult(result); +// } +// if (typeof(float) == resultType) +// { +// var result = await base.ExecuteAsync(cancellationToken); +// return GetMinTResult(result); +// } +// if (typeof(int) == resultType) +// { +// var result = await base.ExecuteAsync(cancellationToken); +// return GetMinTResult(result); +// } +// if (typeof(long) == resultType) +// { +// var result = await base.ExecuteAsync(cancellationToken); +// return GetMinTResult(result); +// } +// if (typeof(double) == resultType) +// { +// var result = await base.ExecuteAsync(cancellationToken); +// return GetMinTResult(result); +// } +// +// throw new ShardingCoreException($"cant calc min value, type:[{resultType}]"); +// } +// else +// { +// var result = await base.ExecuteAsync(cancellationToken); +// return result.Where(o => o.HasQueryResult()).Min(o => o.QueryResult); +// } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/ReverseEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/ReverseEnumerableShardingMerger.cs new file mode 100644 index 00000000..ee800448 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/ReverseEnumerableShardingMerger.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class ReverseEnumerableShardingMerger : AbstractEnumerableShardingMerger + { + public ReverseEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base( + streamMergeContext, async) + { + } + + public override IStreamMergeAsyncEnumerator StreamMerge( + List> parallelResults) + { + if (GetStreamMergeContext().IsPaginationQuery() && GetStreamMergeContext().HasGroupQuery()) + { + var multiAggregateOrderStreamMergeAsyncEnumerator = + new MultiAggregateOrderStreamMergeAsyncEnumerator(GetStreamMergeContext(), + parallelResults); + return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), + new[] { multiAggregateOrderStreamMergeAsyncEnumerator }, 0, + GetStreamMergeContext().GetPaginationReWriteTake()); + } + + if (GetStreamMergeContext().IsPaginationQuery()) + return new PaginationStreamMergeAsyncEnumerator(GetStreamMergeContext(), parallelResults, 0, + GetStreamMergeContext().GetPaginationReWriteTake()); + return base.StreamMerge(parallelResults); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/SequenceEnumerableShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/SequenceEnumerableShardingMerger.cs new file mode 100644 index 00000000..12f28a80 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/SequenceEnumerableShardingMerger.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators.StreamMergeAsync; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class SequenceEnumerableShardingMerger : AbstractEnumerableShardingMerger + { + public SequenceEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base( + streamMergeContext, async) + { + } + + public override IStreamMergeAsyncEnumerator StreamMerge( + List> parallelResults) + { + if (GetStreamMergeContext().HasGroupQuery()) + { + return new MultiAggregateOrderStreamMergeAsyncEnumerator(GetStreamMergeContext(), + parallelResults); + } + + return new MultiOrderStreamMergeAsyncEnumerator(GetStreamMergeContext(), parallelResults); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/SumMethodShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/SumMethodShardingMerger.cs new file mode 100644 index 00000000..800232c5 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/Executors/ShardingMergers/SumMethodShardingMerger.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Enumerators.AggregateExtensions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers +{ + internal class SumMethodShardingMerger : IShardingMerger + { + private TEntity GetSumResult(List source) + { + if (source.IsEmpty()) + return default; + var sum = source.AsQueryable().SumByConstant(); + return ConvertSum(sum); + } + + private TEntity ConvertSum(TNumber number) + { + if (number == null) + return default; + var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TEntity)); + return Expression.Lambda>(convertExpr).Compile()(); + } + +// protected override TResult DoMergeResult(List> resultList) +// { +// return GetSumResult(resultList); +// } + public TEntity StreamMerge(List parallelResults) + { + return GetSumResult(parallelResults); + } + + public void InMemoryMerge(List beforeInMemoryResults, List parallelResults) + { + beforeInMemoryResults.AddRange(parallelResults); + } + } +} + + +// private TResult GetSumResult(List> source) +// { +// if (source.IsEmpty()) +// return default; +// var sum = source.AsQueryable().SumByPropertyName(nameof(RouteQueryResult.QueryResult)); +// return ConvertSum(sum); +// } +// private TResult ConvertSum(TNumber number) +// { +// if (number == null) +// return default; +// var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); +// return Expression.Lambda>(convertExpr).Compile()(); +// } +// protected override TResult DoMergeResult(List> resultList) +// { +// return GetSumResult(resultList); +// } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/LongCountAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/LongCountAsyncInMemoryMergeEngine.cs deleted file mode 100644 index 521f7513..00000000 --- a/src/ShardingCore/Sharding/MergeEngines/LongCountAsyncInMemoryMergeEngine.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core.ShardingPage.Abstractions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; -using ShardingCore.Sharding.MergeEngines.Executors.Methods; - -namespace ShardingCore.Sharding.StreamMergeEngines -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/8/18 6:34:00 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - internal class LongCountAsyncInMemoryMergeEngine : AbstractEnsureMethodCallInMemoryAsyncMergeEngine - { - private readonly IShardingPageManager _shardingPageManager; - public LongCountAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext) - { - _shardingPageManager=streamMergeContext.ShardingRuntimeContext.GetShardingPageManager(); - } - - protected override long DoMergeResult(List> resultList) - { - - if (_shardingPageManager.Current != null) - { - long r = 0; - foreach (var routeQueryResult in resultList) - { - _shardingPageManager.Current.RouteQueryResults.Add(routeQueryResult); - r+= routeQueryResult.QueryResult; - } - - return r; - } - - return resultList.Sum(o => o.QueryResult); - } - - protected override IExecutor> CreateExecutor0(bool async) - { - return new LongCountMethodExecutor(GetStreamMergeContext()); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs new file mode 100644 index 00000000..e88f1d59 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingExecutors/ShardingExecutor.cs @@ -0,0 +1,133 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Core; +using ShardingCore.Exceptions; +using ShardingCore.Extensions; +using ShardingCore.Extensions.InternalExtensions; +using ShardingCore.Helpers; +using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.Common.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.ShardingExecutors +{ + internal class ShardingExecutor + { + private static readonly ShardingExecutor _instance; + + private ShardingExecutor() + { + } + + static ShardingExecutor() + { + _instance = new ShardingExecutor(); + } + + public static ShardingExecutor Instance => _instance; + + public TResult Execute(StreamMergeContext streamMergeContext, + IExecutor executor, bool async, IEnumerable sqlRouteUnits, + CancellationToken cancellationToken = new CancellationToken()) + { + return ExecuteAsync(streamMergeContext, executor, async, sqlRouteUnits, cancellationToken) + .WaitAndUnwrapException(); + } + public async Task ExecuteAsync(StreamMergeContext streamMergeContext, + IExecutor executor, bool async, IEnumerable sqlRouteUnits, + CancellationToken cancellationToken = new CancellationToken()) + { + var resultGroups = + Execute0(streamMergeContext, executor, async, sqlRouteUnits, cancellationToken); + var results =(await TaskHelper.WhenAllFastFail(resultGroups)).SelectMany(o => o) + .ToList(); + if (results.IsEmpty()) + throw new ShardingCoreException("sharding execute result empty"); + return executor.GetShardingMerger().StreamMerge(results); + } + + private Task>[] Execute0(StreamMergeContext streamMergeContext, + IExecutor executor, bool async, IEnumerable sqlRouteUnits, + CancellationToken cancellationToken = new CancellationToken()) + { + var waitTaskQueue = ReOrderTableTails(streamMergeContext, sqlRouteUnits) + .GroupBy(o => o.DataSourceName) + .Select(o => GetSqlExecutorGroups(streamMergeContext, o)) + .Select(dataSourceSqlExecutorUnit => + { + return Task.Run(async () => + { + if (streamMergeContext.UseUnionAllMerge()) + { + var customerDatabaseSqlSupportManager = + streamMergeContext.ShardingRuntimeContext.GetUnionAllMergeManager(); + using (customerDatabaseSqlSupportManager.CreateScope( + ((UnSupportSqlRouteUnit)dataSourceSqlExecutorUnit.SqlExecutorGroups[0].Groups[0] + .RouteUnit).TableRouteResults)) + { + return await executor.ExecuteAsync(async, dataSourceSqlExecutorUnit, + cancellationToken); + } + } + else + { + return await executor.ExecuteAsync(async, dataSourceSqlExecutorUnit, + cancellationToken); + } + }, cancellationToken); + }).ToArray(); + return waitTaskQueue; + } + + /// + /// 顺序查询从重排序 + /// + /// + /// + /// + private IEnumerable ReOrderTableTails(StreamMergeContext streamMergeContext, + IEnumerable sqlRouteUnits) + { + if (streamMergeContext.IsSeqQuery()) + { + return sqlRouteUnits.OrderByAscDescIf(o => o.TableRouteResult.ReplaceTables.First().Tail, + streamMergeContext.TailComparerNeedReverse, streamMergeContext.ShardingTailComparer); + } + + return sqlRouteUnits; + } + + /// + /// 每个数据源下的分表结果按 maxQueryConnectionsLimit 进行组合分组每组大小 maxQueryConnectionsLimit + /// ConnectionModeEnum为用户配置或者系统自动计算,哪怕是用户指定也是按照maxQueryConnectionsLimit来进行分组。 + /// + /// + /// + /// + protected DataSourceSqlExecutorUnit GetSqlExecutorGroups(StreamMergeContext streamMergeContext, + IGrouping sqlGroups) + { + var maxQueryConnectionsLimit = streamMergeContext.GetMaxQueryConnectionsLimit(); + var sqlCount = sqlGroups.Count(); + ////根据用户配置单次查询期望并发数 + //int exceptCount = + // Math.Max( + // 0 == sqlCount % maxQueryConnectionsLimit + // ? sqlCount / maxQueryConnectionsLimit + // : sqlCount / maxQueryConnectionsLimit + 1, 1); + //计算应该使用那种链接模式 + ConnectionModeEnum connectionMode = streamMergeContext.GetConnectionMode(sqlCount); + + //将SqlExecutorUnit进行分区,每个区maxQueryConnectionsLimit个 + //[1,2,3,4,5,6,7],maxQueryConnectionsLimit=3,结果就是[[1,2,3],[4,5,6],[7]] + var sqlExecutorUnitPartitions = sqlGroups.Select(o => new SqlExecutorUnit(connectionMode, o)) + .Partition(maxQueryConnectionsLimit); + + var sqlExecutorGroups = sqlExecutorUnitPartitions + .Select(o => new SqlExecutorGroup(connectionMode, o)).ToList(); + return new DataSourceSqlExecutorUnit(connectionMode, sqlExecutorGroups); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/AbstractBaseMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/AbstractBaseMergeEngine.cs new file mode 100644 index 00000000..ab1e49fa --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/AbstractBaseMergeEngine.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.Common; +using ShardingCore.Sharding.MergeEngines.Common.Abstractions; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/10/2 17:25:33 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal abstract class AbstractBaseMergeEngine + { + private readonly StreamMergeContext _streamMergeContext; + + public AbstractBaseMergeEngine(StreamMergeContext streamMergeContext) + { + _streamMergeContext = streamMergeContext; + } + + protected StreamMergeContext GetStreamMergeContext() + { + return _streamMergeContext; + } + /// + /// sql执行的路由最小单元 + /// + /// + protected virtual IEnumerable GetDefaultSqlRouteUnits() + { + if (_streamMergeContext.UseUnionAllMerge()) + { + return _streamMergeContext.ShardingRouteResult.RouteUnits.GroupBy(o=>o.DataSourceName).Select(o=>new UnSupportSqlRouteUnit(o.Key,o.Select(g=>g.TableRouteResult).ToList())); + } + return _streamMergeContext.ShardingRouteResult.RouteUnits; + + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/IShardingMerger.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/IShardingMerger.cs new file mode 100644 index 00000000..f3d9c21f --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/IShardingMerger.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions +{ + internal interface IShardingMerger + { + TResult StreamMerge(List parallelResults); + void InMemoryMerge(List beforeInMemoryResults,List parallelResults); + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/IStreamMergeCombine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/IStreamMergeCombine.cs similarity index 53% rename from src/ShardingCore/Sharding/MergeEngines/Abstractions/IStreamMergeCombine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/IStreamMergeCombine.cs index 59e47e24..d0fd699a 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/IStreamMergeCombine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/IStreamMergeCombine.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.Enumerators; -namespace ShardingCore.Sharding.MergeEngines.Abstractions +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions { internal interface IStreamMergeCombine { diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractMethodEnsureMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractMethodEnsureMergeEngine.cs new file mode 100644 index 00000000..0a66f56e --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractMethodEnsureMergeEngine.cs @@ -0,0 +1,42 @@ +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingExecutors; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 13:44:02 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal abstract class AbstractMethodEnsureMergeEngine : AbstractBaseMergeEngine,IEnsureMergeResult + { + + protected AbstractMethodEnsureMergeEngine(StreamMergeContext streamMergeContext):base(streamMergeContext) + { + } + + protected abstract IExecutor CreateExecutor(); + public virtual TResult MergeResult() + { + return MergeResultAsync().WaitAndUnwrapException(false); + } + + public virtual async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) + { + cancellationToken.ThrowIfCancellationRequested(); + if (!GetStreamMergeContext().TryPrepareExecuteContinueQuery(() => default(TResult),out var emptyQueryEnumerator)) + { + return emptyQueryEnumerator; + } + var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); + var executor = CreateExecutor(); + var result =await ShardingExecutor.Instance.ExecuteAsync(GetStreamMergeContext(),executor,true,defaultSqlRouteUnits,cancellationToken); + return result; + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractMethodEnsureWrapMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractMethodEnsureWrapMergeEngine.cs new file mode 100644 index 00000000..934ff500 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractMethodEnsureWrapMergeEngine.cs @@ -0,0 +1,45 @@ +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingExecutors; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge +{ + internal abstract class AbstractMethodEnsureWrapMergeEngine : AbstractBaseMergeEngine,IEnsureMergeResult + { + + protected AbstractMethodEnsureWrapMergeEngine(StreamMergeContext streamMergeContext):base(streamMergeContext) + { + } + + protected abstract IExecutor> CreateExecutor(); + public virtual TResult MergeResult() + { + return MergeResultAsync().WaitAndUnwrapException(false); + } + + public async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) + { + cancellationToken.ThrowIfCancellationRequested(); + if (!GetStreamMergeContext().TryPrepareExecuteContinueQuery(() => default(TResult),out var emptyQueryEnumerator)) + { + return emptyQueryEnumerator; + } + var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); + var executor = CreateExecutor(); + var result =await ShardingExecutor.Instance.ExecuteAsync>(GetStreamMergeContext(),executor,true,defaultSqlRouteUnits,cancellationToken); + return result.QueryResult; + } + // + // protected abstract TResult DoMergeResult(List> resultList); + // + // protected override IExecutor CreateExecutor(bool async) + // { + // return CreateExecutor0(async) as IExecutor; + // } + // + // protected abstract IExecutor> CreateExecutor0(bool async); + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine.cs new file mode 100644 index 00000000..7bb685bb --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine.cs @@ -0,0 +1,55 @@ +// using System.Collections.Generic; +// using System.Linq.Expressions; +// using System.Threading; +// using System.Threading.Tasks; +// using Microsoft.EntityFrameworkCore; +// using ShardingCore.Extensions; +// using ShardingCore.Sharding.Abstractions; +// using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +// using ShardingCore.Sharding.StreamMergeEngines; +// +// namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge +// { +// /* +// * @Author: xjm +// * @Description: +// * @Date: 2021/9/24 10:16:28 +// * @Ver: 1.0 +// * @Email: 326308290@qq.com +// */ +// internal abstract class AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine : AbstractMethodEnsureMergeEngine +// { +// protected AbstractTrackEnsureMethodCallWhereInMemoryAsyncMergeEngine(StreamMergeContext streamStreamMergeContext,IExecutor executor) : base(streamStreamMergeContext,executor) +// { +// } +// +// private TEntity ProcessTrackResult(TEntity current) +// { +// if (current != null) +// { +// if (GetStreamMergeContext().IsUseShardingTrack(current.GetType())) +// { +// var c = (object)current; +// var genericDbContext = GetStreamMergeContext().GetShardingDbContext().CreateGenericDbContext(c); +// var attachedEntity = genericDbContext.GetAttachedEntity(c); +// if (attachedEntity == null) +// genericDbContext.Attach(current); +// else +// { +// return (TEntity)attachedEntity; +// } +// } +// +// } +// return current; +// } +// +// protected override TEntity DoMergeResult(List> resultList) +// { +// var entity = DoMergeResult0(resultList); +// return ProcessTrackResult(entity); +// } +// +// protected abstract TEntity DoMergeResult0(List> resultList); +// } +// } diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/IEnsureMergeResult.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/IEnsureMergeResult.cs similarity index 89% rename from src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/IEnsureMergeResult.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/IEnsureMergeResult.cs index 18285bdf..a86e3c8b 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/InMemoryMerge/IEnsureMergeResult.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/InMemoryMerge/IEnsureMergeResult.cs @@ -1,7 +1,7 @@ using System.Threading; using System.Threading.Tasks; -namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge { /* * @Author: xjm diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/ShardingMergeResult.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/ShardingMergeResult.cs similarity index 60% rename from src/ShardingCore/Sharding/MergeEngines/Abstractions/ShardingMergeResult.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/ShardingMergeResult.cs index 2759043d..be105cb8 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/ShardingMergeResult.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/ShardingMergeResult.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace ShardingCore.Sharding.MergeEngines.Abstractions +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions { internal class ShardingMergeResult { diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/StreamMerge/AbstractStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/StreamMerge/AbstractStreamEnumerable.cs new file mode 100644 index 00000000..7578f98c --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/StreamMerge/AbstractStreamEnumerable.cs @@ -0,0 +1,114 @@ +using System.Collections; +using System.Collections.Generic; +using System.Threading; +using ShardingCore.Sharding.Enumerators; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingExecutors; +#if EFCORE2 +using Microsoft.EntityFrameworkCore.Extensions.Internal; +#endif + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/9/2 15:35:39 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal abstract class AbstractStreamEnumerable : AbstractBaseMergeEngine, IStreamEnumerable + { + + protected abstract IExecutor> CreateExecutor(bool async); + + protected AbstractStreamEnumerable(StreamMergeContext streamMergeContext):base(streamMergeContext) + { + } + + +#if !EFCORE2 + public IAsyncEnumerator GetAsyncEnumerator( + CancellationToken cancellationToken = new CancellationToken()) + { + return GetStreamMergeAsyncEnumerator(true, cancellationToken); + } +#endif + +#if EFCORE2 + IAsyncEnumerator IAsyncEnumerable.GetEnumerator() + { + return GetStreamMergeAsyncEnumerator(true); + } + +#endif + + public IEnumerator GetEnumerator() + { + return GetStreamMergeAsyncEnumerator(false); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Dispose() + { + GetStreamMergeContext().Dispose(); + } + + + /// + /// 获取查询的迭代器 + /// + /// + /// + /// + public virtual IStreamMergeAsyncEnumerator GetStreamMergeAsyncEnumerator(bool async,CancellationToken cancellationToken = new CancellationToken()) + { + cancellationToken.ThrowIfCancellationRequested(); + + var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); + var executor = CreateExecutor(async); + return ShardingExecutor.Instance.Execute>(GetStreamMergeContext(),executor,async,defaultSqlRouteUnits,cancellationToken); + } + + // public abstract IShardingExecutor GetShardingExecutor(); + // public abstract IShardingMerger GetShardingMerger(); + + // /// + // /// 获取路由查询的迭代器 + // /// + // /// + // /// + // /// + // public virtual IStreamMergeAsyncEnumerator[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, + // CancellationToken cancellationToken = new CancellationToken()) + // { + // cancellationToken.ThrowIfCancellationRequested(); + // var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); + // var enumeratorTasks = GetDataSourceGroupAndExecutorGroup>(async, defaultSqlRouteUnits, cancellationToken); + // var streamEnumerators = TaskHelper.WhenAllFastFail(enumeratorTasks).WaitAndUnwrapException().SelectMany(o => o).ToArray(); + // return streamEnumerators; + // } + + // /// + // /// 合并成一个迭代器 + // /// + // /// + // /// + // private IStreamMergeAsyncEnumerator CombineStreamMergeAsyncEnumerator( + // IStreamMergeAsyncEnumerator[] streamsAsyncEnumerators) + // { + // return GetStreamMergeCombine().StreamMergeEnumeratorCombine(GetStreamMergeContext(), streamsAsyncEnumerators); + // } + // + // protected override IExecutor CreateExecutor(bool async) + // { + // return CreateExecutor0(async) as IExecutor; + // } + // + // protected abstract IExecutor> CreateExecutor0(bool async); + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/Abstractions/StreamMerge/IStreamEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/StreamMerge/IStreamEnumerable.cs similarity index 77% rename from src/ShardingCore/Sharding/MergeEngines/Abstractions/StreamMerge/IStreamEnumerable.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/StreamMerge/IStreamEnumerable.cs index fb240035..23e6cdf7 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Abstractions/StreamMerge/IStreamEnumerable.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Abstractions/StreamMerge/IStreamEnumerable.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.StreamMerge { /* * @Author: xjm diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AllAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AllAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..68b679f6 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AllAsyncInMemoryMergeEngine.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 13:39:51 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class AllAsyncInMemoryMergeEngine : AbstractMethodEnsureMergeEngine + { + public AllAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + protected override IExecutor CreateExecutor() + { + return new AllMethodExecutor(GetStreamMergeContext()); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AnyAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AnyAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..1b218620 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AnyAsyncInMemoryMergeEngine.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 13:37:00 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class AnyAsyncInMemoryMergeEngine : AbstractMethodEnsureMergeEngine + { + public AnyAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + protected override IExecutor CreateExecutor() + { + + return new AnyMethodExecutor(GetStreamMergeContext()); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AverageAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AverageAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..1e5141c6 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/AverageAsyncInMemoryMergeEngine.cs @@ -0,0 +1,66 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Enumerators.AggregateExtensions; +using ShardingCore.Sharding.MergeEngines.AggregateMergeEngines; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingExecutors; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 22:15:04 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class AverageAsyncInMemoryMergeEngine : AbstractBaseMergeEngine,IEnsureMergeResult + { + public AverageAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + + protected IExecutor>> CreateExecutor() + { + return new AverageMethodWrapExecutor(GetStreamMergeContext()); + } + + public TResult MergeResult() + { + return MergeResultAsync().WaitAndUnwrapException(false); + } + + public async Task MergeResultAsync(CancellationToken cancellationToken = new CancellationToken()) + { + cancellationToken.ThrowIfCancellationRequested(); + if (!GetStreamMergeContext().TryPrepareExecuteContinueQuery(() => default(TResult),out var emptyQueryEnumerator)) + { + return emptyQueryEnumerator; + } + var defaultSqlRouteUnits = GetDefaultSqlRouteUnits(); + var executor = CreateExecutor(); + var result =await ShardingExecutor.Instance.ExecuteAsync(GetStreamMergeContext(),executor,true,defaultSqlRouteUnits,cancellationToken); + var sum = result.QueryResult.Sum; + var count = result.QueryResult.Count; + // var resultList = await base.ExecuteAsync>(cancellationToken); + // if (resultList.IsEmpty()) + // throw new InvalidOperationException("Sequence contains no elements."); + // var queryable = resultList.Where(o=>o.HasQueryResult()).Select(o => new + // { + // Sum = o.QueryResult.Sum, + // Count = o.QueryResult.Count + // }).AsQueryable(); + // var sum = queryable.SumByPropertyName(nameof(AverageResult.Sum)); + // var count = queryable.Sum(o => o.Count); + return AggregateExtension.AverageConstant(sum, count); + } + + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageMergeEngineExtension.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Averages/AverageMergeEngineExtension.cs similarity index 100% rename from src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageMergeEngineExtension.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Averages/AverageMergeEngineExtension.cs diff --git a/src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageResult.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Averages/AverageResult.cs similarity index 100% rename from src/ShardingCore/Sharding/MergeEngines/AggregateMergeEngines/AverageResult.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/Averages/AverageResult.cs diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/ContainsAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/ContainsAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..e9be494f --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/ContainsAsyncInMemoryMergeEngine.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 22:30:07 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class ContainsAsyncInMemoryMergeEngine: AbstractMethodEnsureMergeEngine + { + public ContainsAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + protected override IExecutor CreateExecutor() + { + return new ContainsMethodExecutor(GetStreamMergeContext()); + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/CountAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/CountAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..4ddb06d8 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/CountAsyncInMemoryMergeEngine.cs @@ -0,0 +1,49 @@ +using ShardingCore.Core.ShardingPage.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using System.Collections.Generic; +using System.Linq; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; + +namespace ShardingCore.Sharding.StreamMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/17 22:36:14 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class CountAsyncInMemoryMergeEngine : AbstractMethodEnsureWrapMergeEngine + { + public CountAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + // + // protected override int DoMergeResult(List> resultList) + // { + // + // if (_shardingPageManager.Current != null) + // { + // int r = 0; + // foreach (var routeQueryResult in resultList) + // { + // _shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult)); + // r += routeQueryResult.QueryResult; + // } + // + // return r; + // } + // return resultList.Sum(o => o.QueryResult); + // } + // + // protected override IExecutor> CreateExecutor0(bool async) + // { + // return new CountMethodExecutor(GetStreamMergeContext()); + // } + protected override IExecutor> CreateExecutor() + { + return new CountMethodExecutor(GetStreamMergeContext()); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/FirstOrDefaultSkipAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/FirstOrDefaultSkipAsyncInMemoryMergeEngine.cs similarity index 94% rename from src/ShardingCore/Sharding/MergeEngines/FirstOrDefaultSkipAsyncInMemoryMergeEngine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/FirstOrDefaultSkipAsyncInMemoryMergeEngine.cs index eaf6123a..cfd45379 100644 --- a/src/ShardingCore/Sharding/MergeEngines/FirstOrDefaultSkipAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/FirstOrDefaultSkipAsyncInMemoryMergeEngine.cs @@ -3,8 +3,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; namespace ShardingCore.Sharding.MergeEngines { diff --git a/src/ShardingCore/Sharding/MergeEngines/FirstSkipAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/FirstSkipAsyncInMemoryMergeEngine.cs similarity index 96% rename from src/ShardingCore/Sharding/MergeEngines/FirstSkipAsyncInMemoryMergeEngine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/FirstSkipAsyncInMemoryMergeEngine.cs index f19767a6..4e6157d8 100644 --- a/src/ShardingCore/Sharding/MergeEngines/FirstSkipAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/FirstSkipAsyncInMemoryMergeEngine.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; namespace ShardingCore.Sharding.MergeEngines { diff --git a/src/ShardingCore/Sharding/MergeEngines/LastOrDefaultSkipAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LastOrDefaultSkipAsyncInMemoryMergeEngine.cs similarity index 97% rename from src/ShardingCore/Sharding/MergeEngines/LastOrDefaultSkipAsyncInMemoryMergeEngine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LastOrDefaultSkipAsyncInMemoryMergeEngine.cs index 85c65c9c..82888f30 100644 --- a/src/ShardingCore/Sharding/MergeEngines/LastOrDefaultSkipAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LastOrDefaultSkipAsyncInMemoryMergeEngine.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; namespace ShardingCore.Sharding.MergeEngines { diff --git a/src/ShardingCore/Sharding/MergeEngines/LastSkipAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LastSkipAsyncInMemoryMergeEngine.cs similarity index 97% rename from src/ShardingCore/Sharding/MergeEngines/LastSkipAsyncInMemoryMergeEngine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LastSkipAsyncInMemoryMergeEngine.cs index e79552ed..8f28a135 100644 --- a/src/ShardingCore/Sharding/MergeEngines/LastSkipAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LastSkipAsyncInMemoryMergeEngine.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; namespace ShardingCore.Sharding.MergeEngines { diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LongCountAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LongCountAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..b00386e4 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/LongCountAsyncInMemoryMergeEngine.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using ShardingCore.Core.ShardingPage.Abstractions; +using ShardingCore.Sharding.Abstractions; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; + +namespace ShardingCore.Sharding.StreamMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 6:34:00 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class LongCountAsyncInMemoryMergeEngine : AbstractMethodEnsureWrapMergeEngine + { + public LongCountAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + protected override IExecutor> CreateExecutor() + { + return new LongCountMethodExecutor(GetStreamMergeContext()); + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/MaxAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/MaxAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..f2c3d97d --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/MaxAsyncInMemoryMergeEngine.cs @@ -0,0 +1,65 @@ +using Microsoft.EntityFrameworkCore; +using ShardingCore.Exceptions; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Abstractions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 14:44:53 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class MaxAsyncInMemoryMergeEngine : AbstractMethodEnsureWrapMergeEngine + { + public MaxAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + protected override IExecutor> CreateExecutor() + { + var resultType = typeof(TEntity); + if (!resultType.IsNullableType()) + { + if (typeof(decimal) == resultType) + { + return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(float) == resultType) + { + return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(int) == resultType) + { + return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(long) == resultType) + { + return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(double) == resultType) + { + return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + + throw new ShardingCoreException($"cant calc max value, type:[{resultType}]"); + } + else + { + return new MaxMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/MinAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/MinAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..f1ac966b --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/MinAsyncInMemoryMergeEngine.cs @@ -0,0 +1,65 @@ +using Microsoft.EntityFrameworkCore; +using ShardingCore.Exceptions; +using ShardingCore.Extensions; +using ShardingCore.Sharding.Abstractions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; +using ShardingCore.Sharding.StreamMergeEngines; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 14:40:06 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class MinAsyncInMemoryMergeEngine : AbstractMethodEnsureWrapMergeEngine + { + public MinAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + + protected override IExecutor> CreateExecutor() + { + var resultType = typeof(TEntity); + if (!resultType.IsNullableType()) + { + if (typeof(decimal) == resultType) + { + return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(float) == resultType) + { + return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(int) == resultType) + { + return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(long) == resultType) + { + return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + if (typeof(double) == resultType) + { + return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + + throw new ShardingCoreException($"cant calc min value, type:[{resultType}]"); + } + else + { + return new MinMethodExecutor(GetStreamMergeContext()) as IExecutor>; + } + } + } +} diff --git a/src/ShardingCore/Sharding/MergeEngines/RouteQueryResult.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/RouteQueryResult.cs similarity index 100% rename from src/ShardingCore/Sharding/MergeEngines/RouteQueryResult.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/RouteQueryResult.cs diff --git a/src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultSkipAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SingleOrDefaultSkipAsyncInMemoryMergeEngine.cs similarity index 96% rename from src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultSkipAsyncInMemoryMergeEngine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SingleOrDefaultSkipAsyncInMemoryMergeEngine.cs index 60b26f32..07fdaf79 100644 --- a/src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultSkipAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SingleOrDefaultSkipAsyncInMemoryMergeEngine.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; namespace ShardingCore.Sharding.MergeEngines { diff --git a/src/ShardingCore/Sharding/MergeEngines/SingleSkipAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SingleSkipAsyncInMemoryMergeEngine.cs similarity index 96% rename from src/ShardingCore/Sharding/MergeEngines/SingleSkipAsyncInMemoryMergeEngine.cs rename to src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SingleSkipAsyncInMemoryMergeEngine.cs index 3351b177..39876efe 100644 --- a/src/ShardingCore/Sharding/MergeEngines/SingleSkipAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SingleSkipAsyncInMemoryMergeEngine.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using ShardingCore.Extensions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; namespace ShardingCore.Sharding.MergeEngines { diff --git a/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SumAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SumAsyncInMemoryMergeEngine.cs new file mode 100644 index 00000000..10156b94 --- /dev/null +++ b/src/ShardingCore/Sharding/MergeEngines/ShardingMergeEngines/SumAsyncInMemoryMergeEngine.cs @@ -0,0 +1,44 @@ +using ShardingCore.Sharding.MergeEngines.Executors.Abstractions; +using ShardingCore.Sharding.MergeEngines.Executors.Methods; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; + +namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines +{ + /* + * @Author: xjm + * @Description: + * @Date: 2021/8/18 14:46:20 + * @Ver: 1.0 + * @Email: 326308290@qq.com + */ + internal class SumAsyncInMemoryMergeEngine : AbstractMethodEnsureMergeEngine + { + public SumAsyncInMemoryMergeEngine(StreamMergeContext streamStreamMergeContext) : base(streamStreamMergeContext) + { + } + // private TResult GetSumResult(List> source) + // { + // if (source.IsEmpty()) + // return default; + // var sum = source.AsQueryable().SumByPropertyName(nameof(RouteQueryResult.QueryResult)); + // return ConvertSum(sum); + // } + // private TResult ConvertSum(TNumber number) + // { + // if (number == null) + // return default; + // var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult)); + // return Expression.Lambda>(convertExpr).Compile()(); + // } + // protected override TResult DoMergeResult(List> resultList) + // { + // return GetSumResult(resultList); + // } + + + protected override IExecutor CreateExecutor() + { + return new SumMethodWrapExecutor(GetStreamMergeContext()); + } + } +} diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs index 6476315d..f56442ea 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs @@ -1,11 +1,9 @@ using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines; using ShardingCore.Sharding.ShardingExecutors.Abstractions; using ShardingCore.Sharding.StreamMergeEngines; -using ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines; using System; using System.Linq; using System.Linq.Expressions; @@ -16,6 +14,8 @@ using ShardingCore.Core; using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Sharding.MergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines; +using ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions.InMemoryMerge; #if EFCORE2 using Microsoft.EntityFrameworkCore.Internal; #endif diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingTrackQueryExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingTrackQueryExecutor.cs index 549ccd00..c957151f 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingTrackQueryExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingTrackQueryExecutor.cs @@ -9,7 +9,6 @@ using ShardingCore.Core.TrackerManagers; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Sharding.Abstractions; -using ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge; using ShardingCore.Sharding.ShardingExecutors.Abstractions; using ShardingCore.Sharding.ShardingExecutors.NativeTrackQueries; diff --git a/src/ShardingCore/Sharding/StreamMergeContext.cs b/src/ShardingCore/Sharding/StreamMergeContext.cs index 44c9544e..95360546 100644 --- a/src/ShardingCore/Sharding/StreamMergeContext.cs +++ b/src/ShardingCore/Sharding/StreamMergeContext.cs @@ -22,7 +22,6 @@ using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.ShardingConfigurations; using ShardingCore.Core.VirtualRoutes; using ShardingCore.Sharding.Enumerators; -using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common.Abstractions; using ShardingCore.Sharding.ShardingExecutors; @@ -193,6 +192,14 @@ namespace ShardingCore.Sharding { return this.GroupByContext.GroupExpression != null; } + /// + /// group 内存排序 + /// + /// + public bool GroupQueryMemoryMerge() + { + return this.GroupByContext.GroupMemoryMerge; + } public bool IsMergeQuery() {