重构聚合模块

This commit is contained in:
xuejiaming 2022-08-07 21:59:32 +08:00
parent abe56c219c
commit 52ed6e0c49
123 changed files with 2473 additions and 2124 deletions

View File

@ -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;

View File

@ -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<SysUserMod>()

View File

@ -123,6 +123,30 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
new[] { source.Expression, Expression.Quote(selector) });
return callExpression;
}
private static MethodCallExpression CreateSumByConstant<TSelect>(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;
}
/// <summary>
/// 根据属性求和
/// </summary>
@ -140,6 +164,11 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
var callExpression = CreateSumByProperty(source, property);
return source.Provider.Execute<TSelect>(callExpression);
}
public static TSelect SumByConstant<TSelect>(this IQueryable source)
{
var callExpression = CreateSumByConstant<TSelect>(source);
return source.Provider.Execute<TSelect>(callExpression);
}
/// <summary>
/// 根据属性求和
/// </summary>

View File

@ -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<T> : IInMemoryStreamMergeAsyncEnumerator<T>
{
private readonly StreamMergeContext _streamMergeContext;
private readonly bool _async;
private readonly IEnumerator<T> _inMemoryEnumerator;
private bool skip;
private int _inMemoryReallyCount;
public InMemoryGroupByOrderStreamMergeAsyncEnumerator(StreamMergeContext streamMergeContext,IStreamMergeAsyncEnumerator<T> 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<IEnumerator<T>> GetAllRowsAsync(IStreamMergeAsyncEnumerator<T> streamMergeAsyncEnumerator)
{
var list = new List<T>();
#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<T> GetAllRows(IStreamMergeAsyncEnumerator<T> streamMergeAsyncEnumerator)
{
var list = new List<T>();
#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<bool> MoveNextAsync()
{
if (skip)
{
skip = false;
return new ValueTask<bool>(null != _inMemoryEnumerator.Current);
}
return new ValueTask<bool>(_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<bool> MoveNext(CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
if (skip)
{
skip = false;
return Task.FromResult(null != _inMemoryEnumerator.Current);
}
return Task.FromResult(_inMemoryEnumerator.MoveNext());
}
#endif
}
}

View File

@ -43,7 +43,6 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
list.Add(streamMergeAsyncEnumerator.GetCurrent());
_inMemoryReallyCount++;
}
return list.GetEnumerator();
}
private IEnumerator<T> GetAllRows(IStreamMergeAsyncEnumerator<T> streamMergeAsyncEnumerator)

View File

@ -14,6 +14,10 @@ namespace ShardingCore.Sharding.MergeContexts
/// group by 表达式
/// </summary>
public LambdaExpression GroupExpression { get; set; }
/// <summary>
/// 是否内存聚合
/// </summary>
public bool GroupMemoryMerge { get; set; }
}
}

View File

@ -29,7 +29,9 @@ namespace ShardingCore.Sharding.MergeContexts
_logger = logger;
_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;
@ -72,51 +78,89 @@ 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())
{
var sort = string.Join(",", selectProperties.Select(o => $"{o.PropertyName} asc"));
reWriteQueryable = reWriteQueryable.OrderWithExpression(sort, null);
foreach (var orderProperty in selectProperties)
groupByContext.GroupMemoryMerge = false;
var sort = string.Join(",", selectGroupKeyProperties.Select(o => $"{o.PropertyName} asc"));
reWriteQueryable = reWriteQueryable.RemoveAnyOrderBy().OrderWithExpression(sort, null);
foreach (var orderProperty in selectGroupKeyProperties)
{
orders.AddLast(new PropertyOrder(orderProperty.PropertyName, true, orderProperty.OwnerType));
}
}
else
{
var groupKeys = selectGroupKeyProperties.Select(o=>o.PropertyName).ToHashSet();
bool groupMemoryMerge = false;
foreach (var propertyOrder in orders)
{
if (groupKeys.IsEmpty())
{
break;
}
else if (!mergeQueryCompilerContext.UseUnionAllMerge())
if (!groupKeys.Contains(propertyOrder.PropertyExpression))
{
//将查询的属性转换成order by 并且order和select的未聚合查询必须一致
var selectProperties = selectContext.SelectProperties.Where(o => !(o is SelectAggregateProperty));
groupMemoryMerge = true;
break;
}
groupKeys.Remove(propertyOrder.PropertyExpression);
}
//判断是否优先group key排序如果不是就是要内存聚合
groupByContext.GroupMemoryMerge = groupMemoryMerge;
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]}");
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<SelectAverageProperty>().ToList();
var selectAggregateProperties = selectContext.SelectProperties.OfType<SelectAggregateProperty>().Where(o => !(o is SelectAverageProperty)).ToList();
var averageSelectProperties =
selectContext.SelectProperties.OfType<SelectAverageProperty>().ToList();
var selectAggregateProperties = selectContext.SelectProperties.OfType<SelectAggregateProperty>()
.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,11 +188,11 @@ 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)}");
}
return new RewriteResult(combineQueryable, reWriteQueryable);

View File

@ -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();
}
/// <summary>
/// 将查询分表分库结果按每个数据源进行分组
/// 每组大小为 启动配置的<see cref="IShardingConfigOption.MaxQueryConnectionsLimit"/>数目
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="async"></param>
/// <param name="sqlRouteUnits"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<LinkedList<TResult>>[] GetDataSourceGroupAndExecutorGroup<TResult>(bool async, IEnumerable<ISqlRouteUnit> sqlRouteUnits, CancellationToken cancellationToken = new CancellationToken())
{
var executor = CreateExecutor<TResult>(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<TResult> CreateParallelExecuteControl<TResult>(IParallelExecutor<TResult> executor);
protected abstract IExecutor<TResult> CreateExecutor<TResult>(bool async);
/// <summary>
/// sql执行的路由最小单元
/// </summary>
/// <returns></returns>
protected virtual IEnumerable<ISqlRouteUnit> 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<IGrouping<string, ISqlRouteUnit>> AggregateQueryByDataSourceName(IEnumerable<ISqlRouteUnit> sqlRouteUnits)
{
return ReOrderTableTails(sqlRouteUnits).GroupBy(o => o.DataSourceName);
}
/// <summary>
/// 顺序查询从重排序
/// </summary>
/// <param name="sqlRouteUnits"></param>
/// <returns></returns>
private IEnumerable<ISqlRouteUnit> ReOrderTableTails(IEnumerable<ISqlRouteUnit> sqlRouteUnits)
{
var streamMergeContext = GetStreamMergeContext();
if (streamMergeContext.IsSeqQuery())
{
return sqlRouteUnits.OrderByAscDescIf(o => o.TableRouteResult.ReplaceTables.First().Tail, streamMergeContext.TailComparerNeedReverse, streamMergeContext.ShardingTailComparer);
}
return sqlRouteUnits;
}
/// <summary>
/// 每个数据源下的分表结果按 maxQueryConnectionsLimit 进行组合分组每组大小 maxQueryConnectionsLimit
/// ConnectionModeEnum为用户配置或者系统自动计算,哪怕是用户指定也是按照maxQueryConnectionsLimit来进行分组。
/// </summary>
/// <param name="sqlGroups"></param>
/// <returns></returns>
protected DataSourceSqlExecutorUnit GetSqlExecutorGroups(IGrouping<string, ISqlRouteUnit> 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<SqlExecutorUnit>(connectionMode, o)).ToList();
return new DataSourceSqlExecutorUnit(connectionMode, sqlExecutorGroups);
}
///// <summary>
///// 同库同组下面的并行异步执行,需要归并成一个结果
///// </summary>
///// <typeparam name="TResult"></typeparam>
///// <param name="sqlExecutorUnits"></param>
///// <param name="sqlExecutorUnitExecuteAsync"></param>
///// <param name="cancellationToken"></param>
///// <returns></returns>
//protected async Task<LinkedList<ShardingMergeResult<TResult>>> ExecuteAsync<TResult>(List<SqlExecutorUnit> sqlExecutorUnits, Func<SqlExecutorUnit, Task<ShardingMergeResult<TResult>>> sqlExecutorUnitExecuteAsync, CancellationToken cancellationToken = new CancellationToken())
//{
// if (sqlExecutorUnits.Count <= 0)
// {
// return new LinkedList<ShardingMergeResult<TResult>>();
// }
// else
// {
// var result = new LinkedList<ShardingMergeResult<TResult>>();
// Task<ShardingMergeResult<TResult>>[] 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<Task<ShardingMergeResult<TResult>>>();
// }
// 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;
// }
//}
}
}

View File

@ -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<TEntity, TResult> : AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult<TResult>
{
protected AbstractEnsureMethodCallInMemoryAsyncMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
public virtual TResult MergeResult()
{
return MergeResultAsync().WaitAndUnwrapException(false);
}
public virtual async Task<TResult> MergeResultAsync(CancellationToken cancellationToken = new CancellationToken())
{
var resultList = await base.ExecuteAsync<TResult>(cancellationToken);
return DoMergeResult(resultList);
}
protected abstract TResult DoMergeResult(List<RouteQueryResult<TResult>> resultList);
protected override IExecutor<TR> CreateExecutor<TR>(bool async)
{
return CreateExecutor0(async) as IExecutor<TR>;
}
protected abstract IExecutor<RouteQueryResult<TResult>> CreateExecutor0(bool async);
}
}

View File

@ -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<TEntity, TResult> : AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult<TResult>
{
protected AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
public virtual TResult MergeResult()
{
return MergeResultAsync().WaitAndUnwrapException(false);
}
public abstract Task<TResult> MergeResultAsync(CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -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<List<RouteQueryResult<TResult>>> ExecuteAsync<TResult>(CancellationToken cancellationToken = new CancellationToken())
{
if (!_mergeContext.TryPrepareExecuteContinueQuery(() => new List<RouteQueryResult<TResult>>(0),out var routeQueryResults))
return routeQueryResults;
var defaultSqlRouteUnits = GetDefaultSqlRouteUnits();
var waitExecuteQueue = GetDataSourceGroupAndExecutorGroup<RouteQueryResult<TResult>>(true, defaultSqlRouteUnits, cancellationToken).ToArray();
return (await TaskHelper.WhenAllFastFail(waitExecuteQueue)).SelectMany(o => o).ToList();
}
protected override StreamMergeContext GetStreamMergeContext()
{
return _mergeContext;
}
}
}

View File

@ -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<TEntity> : AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity,TEntity>
{
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<RouteQueryResult<TEntity>> resultList)
{
var entity = DoMergeResult0(resultList);
return ProcessTrackResult(entity);
}
protected abstract TEntity DoMergeResult0(List<RouteQueryResult<TEntity>> resultList);
}
}

View File

@ -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<List<RouteQueryResult<TResult>>> ExecuteAsync<TResult>(CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -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<TEntity> : AbstractBaseMergeEngine, IStreamEnumerable<TEntity>
{
private readonly StreamMergeContext _streamMergeContext;
protected override StreamMergeContext GetStreamMergeContext()
{
return _streamMergeContext;
}
protected abstract IStreamMergeCombine GetStreamMergeCombine();
protected AbstractStreamEnumerable(StreamMergeContext streamMergeContext)
{
_streamMergeContext = streamMergeContext;
}
#if !EFCORE2
public IAsyncEnumerator<TEntity> GetAsyncEnumerator(
CancellationToken cancellationToken = new CancellationToken())
{
return GetStreamMergeAsyncEnumerator(true, cancellationToken);
}
#endif
#if EFCORE2
IAsyncEnumerator<TEntity> IAsyncEnumerable<TEntity>.GetEnumerator()
{
return GetStreamMergeAsyncEnumerator(true);
}
#endif
public IEnumerator<TEntity> GetEnumerator()
{
return GetStreamMergeAsyncEnumerator(false);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Dispose()
{
_streamMergeContext.Dispose();
}
/// <summary>
/// 获取查询的迭代器
/// </summary>
/// <param name="async"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private IStreamMergeAsyncEnumerator<TEntity> GetStreamMergeAsyncEnumerator(bool async,
CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
var dbStreamMergeAsyncEnumerators = GetRouteQueryStreamMergeAsyncEnumerators(async);
if (dbStreamMergeAsyncEnumerators.IsEmpty())
throw new ShardingCoreException("GetRouteQueryStreamMergeAsyncEnumerators empty");
return CombineStreamMergeAsyncEnumerator(dbStreamMergeAsyncEnumerators);
}
/// <summary>
/// 获取路由查询的迭代器
/// </summary>
/// <param name="async"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual IStreamMergeAsyncEnumerator<TEntity>[] GetRouteQueryStreamMergeAsyncEnumerators(bool async,
CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
var defaultSqlRouteUnits = GetDefaultSqlRouteUnits();
var enumeratorTasks = GetDataSourceGroupAndExecutorGroup<IStreamMergeAsyncEnumerator<TEntity>>(async, defaultSqlRouteUnits, cancellationToken);
var streamEnumerators = TaskHelper.WhenAllFastFail(enumeratorTasks).WaitAndUnwrapException().SelectMany(o => o).ToArray();
return streamEnumerators;
}
/// <summary>
/// 合并成一个迭代器
/// </summary>
/// <param name="streamsAsyncEnumerators"></param>
/// <returns></returns>
private IStreamMergeAsyncEnumerator<TEntity> CombineStreamMergeAsyncEnumerator(
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
return GetStreamMergeCombine().StreamMergeEnumeratorCombine<TEntity>(GetStreamMergeContext(), streamsAsyncEnumerators);
}
protected override IExecutor<TResult> CreateExecutor<TResult>(bool async)
{
return CreateExecutor0(async) as IExecutor<TResult>;
}
protected abstract IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async);
}
}

View File

@ -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<TEntity, TResult, TSelect> : AbstractEnsureMethodCallInMemoryAverageAsyncMergeEngine<TEntity, TResult>
{
public AverageAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
public override async Task<TResult> MergeResultAsync(CancellationToken cancellationToken = new CancellationToken())
{
var resultList = await base.ExecuteAsync<AverageResult<TSelect>>(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<TSelect>(nameof(AverageResult<object>.Sum));
var count = queryable.Sum(o => o.Count);
return AggregateExtension.AverageConstant<TSelect, long, TResult>(sum, count);
}
protected override IExecutor<TR> CreateExecutor<TR>(bool async)
{
return new AverageMethodExecutor<TSelect>(GetStreamMergeContext()) as IExecutor<TR>;
}
}
}

View File

@ -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<TEntity, TResult> :AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult<TResult>
{
public MaxAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IExecutor<TR> CreateExecutor<TR>(bool async)
{
var resultType = typeof(TEntity);
if (!resultType.IsNullableType())
{
if (typeof(decimal) == resultType)
{
return new MaxMethodExecutor<TEntity,decimal?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(float) == resultType)
{
return new MaxMethodExecutor<TEntity, float?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(int) == resultType)
{
return new MaxMethodExecutor<TEntity, int?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(long) == resultType)
{
return new MaxMethodExecutor<TEntity, long?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(double) == resultType)
{
return new MaxMethodExecutor<TEntity, double?>(GetStreamMergeContext()) as IExecutor<TR>;
}
throw new ShardingCoreException($"cant calc max value, type:[{resultType}]");
}
else
{
return new MaxMethodExecutor<TEntity,TEntity>(GetStreamMergeContext()) as IExecutor<TR>;
}
}
public TResult MergeResult()
{
return MergeResultAsync().WaitAndUnwrapException(false);
}
public async Task<TResult> MergeResultAsync(CancellationToken cancellationToken = new CancellationToken())
{
var resultType = typeof(TEntity);
if (!resultType.IsNullableType())
{
if (typeof(decimal) == resultType)
{
var result = await base.ExecuteAsync<decimal?>(cancellationToken);
return GetMaxTResult<decimal?>(result);
}
if (typeof(float) == resultType)
{
var result = await base.ExecuteAsync<float?>(cancellationToken);
return GetMaxTResult<float?>(result);
}
if (typeof(int) == resultType)
{
var result = await base.ExecuteAsync<int?>(cancellationToken);
return GetMaxTResult<int?>(result);
}
if (typeof(long) == resultType)
{
var result = await base.ExecuteAsync<long?>(cancellationToken);
return GetMaxTResult<long?>(result);
}
if (typeof(double) == resultType)
{
var result = await base.ExecuteAsync<double?>(cancellationToken);
return GetMaxTResult<double?>(result);
}
throw new ShardingCoreException($"cant calc max value, type:[{resultType}]");
}
else
{
var result = await base.ExecuteAsync<TResult>(cancellationToken);
return result.Where(o => o.HasQueryResult()).Max(o => o.QueryResult);
}
}
private TResult GetMaxTResult<TInnerSelect>(List<RouteQueryResult<TInnerSelect>> 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<TInnerSelect>(max);
}
private TResult ConvertNumber<TNumber>(TNumber number)
{
if (number == null)
return default;
var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult));
return Expression.Lambda<Func<TResult>>(convertExpr).Compile()();
}
}
}

View File

@ -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<TEntity, TResult> : AbstractInMemoryAsyncMergeEngine, IEnsureMergeResult<TResult>
{
public MinAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IExecutor<TR> CreateExecutor<TR>(bool async)
{
var resultType = typeof(TEntity);
if (!resultType.IsNullableType())
{
if (typeof(decimal) == resultType)
{
return new MinMethodExecutor<TEntity,decimal?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(float) == resultType)
{
return new MinMethodExecutor<TEntity, float?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(int) == resultType)
{
return new MinMethodExecutor<TEntity, int?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(long) == resultType)
{
return new MinMethodExecutor<TEntity, long?>(GetStreamMergeContext()) as IExecutor<TR>;
}
if (typeof(double) == resultType)
{
return new MinMethodExecutor<TEntity, double?>(GetStreamMergeContext()) as IExecutor<TR>;
}
throw new ShardingCoreException($"cant calc min value, type:[{resultType}]");
}
else
{
return new MinMethodExecutor<TEntity, TEntity>(GetStreamMergeContext()) as IExecutor<TR>;
}
}
public TResult MergeResult()
{
return MergeResultAsync().WaitAndUnwrapException(false);
}
public async Task<TResult> MergeResultAsync(CancellationToken cancellationToken = new CancellationToken())
{
var resultType = typeof(TEntity);
if (!resultType.IsNullableType())
{
if (typeof(decimal) == resultType)
{
var result = await base.ExecuteAsync<decimal?>(cancellationToken);
return GetMinTResult<decimal?>(result);
}
if (typeof(float) == resultType)
{
var result = await base.ExecuteAsync<float?>(cancellationToken);
return GetMinTResult<float?>(result);
}
if (typeof(int) == resultType)
{
var result = await base.ExecuteAsync<int?>(cancellationToken);
return GetMinTResult<int?>(result);
}
if (typeof(long) == resultType)
{
var result = await base.ExecuteAsync<long?>(cancellationToken);
return GetMinTResult<long?>(result);
}
if (typeof(double) == resultType)
{
var result = await base.ExecuteAsync<double?>(cancellationToken);
return GetMinTResult<double?>(result);
}
throw new ShardingCoreException($"cant calc min value, type:[{resultType}]");
}
else
{
var result = await base.ExecuteAsync<TResult>(cancellationToken);
return result.Where(o => o.HasQueryResult()).Min(o => o.QueryResult);
}
}
private TResult GetMinTResult<TInnerSelect>(List<RouteQueryResult<TInnerSelect>> 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<TInnerSelect>(min);
}
private TResult ConvertNumber<TNumber>(TNumber number)
{
if (number == null)
return default;
var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult));
return Expression.Lambda<Func<TResult>>(convertExpr).Compile()();
}
}
}

View File

@ -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<TEntity, TResult> : AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity, TResult>
{
public SumAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
private TResult GetSumResult<TInnerSelect>(List<RouteQueryResult<TInnerSelect>> source)
{
if (source.IsEmpty())
return default;
var sum = source.AsQueryable().SumByPropertyName<TInnerSelect>(nameof(RouteQueryResult<TInnerSelect>.QueryResult));
return ConvertSum(sum);
}
private TResult ConvertSum<TNumber>(TNumber number)
{
if (number == null)
return default;
var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult));
return Expression.Lambda<Func<TResult>>(convertExpr).Compile()();
}
protected override TResult DoMergeResult(List<RouteQueryResult<TResult>> resultList)
{
return GetSumResult(resultList);
}
protected override IExecutor<RouteQueryResult<TResult>> CreateExecutor0(bool async)
{
return new SumMethodExecutor<TResult>(GetStreamMergeContext());
}
}
}

View File

@ -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<TEntity> : AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity, bool>
{
public AllAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
//public override async Task<bool> MergeResultAsync(CancellationToken cancellationToken = new CancellationToken())
//{
// var result = await base.ExecuteAsync(queryable =>
// {
// var allQueryCombineResult = (AllQueryCombineResult)GetStreamMergeContext().MergeQueryCompilerContext.GetQueryCombineResult();
// Expression<Func<TEntity, bool>> allPredicate = x => true;
// var predicate = allQueryCombineResult.GetAllPredicate();
// if (predicate != null)
// {
// allPredicate = (Expression<Func<TEntity, bool>>)predicate;
// }
// return ((IQueryable<TEntity>)queryable).AllAsync(allPredicate, cancellationToken);
// }, cancellationToken);
// return result.All(o => o.QueryResult);
//}
//protected override IParallelExecuteControl<TResult> CreateParallelExecuteControl<TResult>(IParallelExecutor<TResult> executor)
//{
// return AllParallelExecuteControl<TResult>.Create(GetStreamMergeContext(),executor);
//}
protected override bool DoMergeResult(List<RouteQueryResult<bool>> resultList)
{
return resultList.All(o => o.QueryResult);
}
protected override IExecutor<RouteQueryResult<bool>> CreateExecutor0(bool async)
{
return new AllMethodExecutor<TEntity>(GetStreamMergeContext());
}
}
}

View File

@ -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<TEntity> : AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity,bool>
{
public AnyAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override bool DoMergeResult(List<RouteQueryResult<bool>> resultList)
{
return resultList.Any(o => o.QueryResult);
}
protected override IExecutor<RouteQueryResult<bool>> CreateExecutor0(bool async)
{
return new AnyMethodExecutor<TEntity>(GetStreamMergeContext());
}
}
}

View File

@ -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
{

View File

@ -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<TEntity>: AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity,bool>
{
public ContainsAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override bool DoMergeResult(List<RouteQueryResult<bool>> resultList)
{
return resultList.Any(o => o.QueryResult);
}
protected override IExecutor<RouteQueryResult<bool>> CreateExecutor0(bool async)
{
return new ContainsMethodExecutor<TEntity>(GetStreamMergeContext());
}
}
}

View File

@ -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<TEntity> : AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity,int>
{
private readonly IShardingPageManager _shardingPageManager;
public CountAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
_shardingPageManager =streamMergeContext.ShardingRuntimeContext.GetShardingPageManager();
}
protected override int DoMergeResult(List<RouteQueryResult<int>> resultList)
{
if (_shardingPageManager.Current != null)
{
int r = 0;
foreach (var routeQueryResult in resultList)
{
_shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult<long>(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult));
r += routeQueryResult.QueryResult;
}
return r;
}
return resultList.Sum(o => o.QueryResult);
}
protected override IExecutor<RouteQueryResult<int>> CreateExecutor0(bool async)
{
return new CountMethodExecutor<TEntity>(GetStreamMergeContext());
}
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
internal class AppendOrderSequenceShardingEnumerable<TEntity> :AbstractStreamEnumerable<TEntity>
{
private readonly PaginationSequenceConfig _dataSourceSequenceOrderConfig;
private readonly PaginationSequenceConfig _tableSequenceOrderConfig;
private readonly ICollection<RouteQueryResult<long>> _routeQueryResults;
public AppendOrderSequenceStreamEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceOrderConfig, PaginationSequenceConfig tableSequenceOrderConfig, ICollection<RouteQueryResult<long>> routeQueryResults)
: base(streamMergeContext)
public AppendOrderSequenceShardingEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceOrderConfig, PaginationSequenceConfig tableSequenceOrderConfig, ICollection<RouteQueryResult<long>> routeQueryResults) : base(streamMergeContext)
{
_dataSourceSequenceOrderConfig = dataSourceSequenceOrderConfig;
_tableSequenceOrderConfig = tableSequenceOrderConfig;
_routeQueryResults = routeQueryResults;
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return AppendOrderSequenceStreamMergeCombine.Instance;
}
protected override IEnumerable<ISqlRouteUnit> 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<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
return new AppendOrderSequenceEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(),
async);
throw new System.NotImplementedException();
}
}
}

View File

@ -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

View File

@ -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<TEntity> :AbstractStreamEnumerable<TEntity>
{
public DefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
return new DefaultEnumerableExecutor<TEntity>(GetStreamMergeContext(), async);
}
}
}

View File

@ -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<T> : IAsyncEnumerator<T>,IEnumerator<T>
{

View File

@ -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<TEntity> :AbstractStreamEnumerable<TEntity>
{
public EmptyShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
public override IStreamMergeAsyncEnumerator<TEntity> GetStreamMergeAsyncEnumerator(bool async,
CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
var asyncEnumerator = new EmptyQueryEnumerator<TEntity>();
if (async)
{
return new StreamMergeAsyncEnumerator<TEntity>((IAsyncEnumerator<TEntity>)asyncEnumerator);
}
else
{
return new StreamMergeAsyncEnumerator<TEntity>((IEnumerator<TEntity>)asyncEnumerator);
}
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
throw new NotImplementedException();
}
}
}

View File

@ -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<TEntity> :AbstractStreamEnumerable<TEntity>
{
public FirstOrDefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
GetStreamMergeContext().ReSetTake(1);
return new DefaultEnumerableExecutor<TEntity>(GetStreamMergeContext(), async);
}
}
}

View File

@ -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<TEntity> :AbstractStreamEnumerable<TEntity>
{
public LastOrDefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> 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<TEntity>)streamMergeContext.GetReWriteQueryable().RemoveSkip().RemoveTake().RemoveAnyOrderBy().OrderWithExpression(streamMergeContext.Orders).ReTake(reTake);
return new LastOrDefaultEnumerableExecutor<TEntity>(GetStreamMergeContext(),newQueryable, async);
}
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
internal class ReverseShardingEnumerable<TEntity> :AbstractStreamEnumerable<TEntity>
{
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<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
var noPaginationNoOrderQueryable = GetStreamMergeContext().GetOriginalQueryable().RemoveSkip().RemoveTake()
.RemoveAnyOrderBy().As<IQueryable<TEntity>>();
@ -48,7 +31,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer
GetStreamMergeContext().ReverseOrder();
var reverseOrderQueryable = noPaginationNoOrderQueryable.Take((int)realSkip + (int)take)
.OrderWithExpression(GetStreamMergeContext().Orders);
return new ReverseEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(),
return new ReverseEnumerableExecutor<TEntity>(GetStreamMergeContext(),
reverseOrderQueryable, async);
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
internal class SequenceShardingEnumerable<TEntity> :AbstractStreamEnumerable<TEntity>
{
private readonly PaginationSequenceConfig _dataSourceSequenceMatchOrderConfig;
private readonly PaginationSequenceConfig _tableSequenceMatchOrderConfig;
private readonly ICollection<RouteQueryResult<long>> _routeQueryResults;
private readonly bool _isAsc;
public SequenceStreamEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceMatchOrderConfig, PaginationSequenceConfig tableSequenceMatchOrderConfig, ICollection<RouteQueryResult<long>> routeQueryResults, bool isAsc)
: base(streamMergeContext)
public SequenceShardingEnumerable(StreamMergeContext streamMergeContext, PaginationSequenceConfig dataSourceSequenceMatchOrderConfig, PaginationSequenceConfig tableSequenceMatchOrderConfig, ICollection<RouteQueryResult<long>> 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<ISqlRouteUnit> 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<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
return new SequenceEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(), async);
return new SequenceEnumerableExecutor<TEntity>(GetStreamMergeContext(), async);
}
}
}

View File

@ -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<TEntity> :AbstractStreamEnumerable<TEntity>
{
public SingleOrDefaultShardingEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor(bool async)
{
GetStreamMergeContext().ReSetTake(2);
return new DefaultEnumerableExecutor<TEntity>(GetStreamMergeContext(), async);
}
}
}

View File

@ -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;
/*

View File

@ -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<TEntity> :AbstractStreamEnumerable<TEntity>
{
public DefaultShardingStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return DefaultStreamMergeCombine.Instance;
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
{
return new DefaultEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(), async);
}
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
{
public EmptyQueryStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return EmptyStreamMergeCombine.Instance;
}
public override IStreamMergeAsyncEnumerator<TEntity>[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
var asyncEnumerator = new EmptyQueryEnumerator<TEntity>();
if (async)
{
return new[] { new StreamMergeAsyncEnumerator<TEntity>((IAsyncEnumerator<TEntity>)asyncEnumerator) };
}
else
{
return new[] { new StreamMergeAsyncEnumerator<TEntity>((IEnumerator<TEntity>)asyncEnumerator) };
}
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
{
return new EmptyQueryEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(), async);
}
}
}

View File

@ -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<TEntity>:AbstractStreamEnumerable<TEntity>
{
public FirstOrDefaultStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return DefaultStreamMergeCombine.Instance;
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
{
GetStreamMergeContext().ReSetTake(1);
return new DefaultEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(), async);
}
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
{
public LastOrDefaultStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return DefaultStreamMergeCombine.Instance;
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> 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<TEntity>)streamMergeContext.GetReWriteQueryable().RemoveSkip().RemoveTake().RemoveAnyOrderBy().OrderWithExpression(streamMergeContext.Orders).ReTake(reTake);
return new LastOrDefaultEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(), newQueryable, async);
}
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
{
public SingleOrDefaultStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return DefaultStreamMergeCombine.Instance;
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
{
GetStreamMergeContext().ReSetTake(2);
return new DefaultEnumeratorExecutor<TEntity>(GetStreamMergeContext(), GetStreamMergeCombine(), async);
}
}
}

View File

@ -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<TEntity> : AbstractStreamEnumerable<TEntity>
{
public SingleQueryStreamEnumerable(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return SingleStreamMergeCombine.Instance;
}
public override IStreamMergeAsyncEnumerator<TEntity>[] 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<TEntity>)streamMergeContext.GetOriginalQueryable().ReplaceDbContextQueryable(shardingDbContext);
var enumeratorParallelExecutor = new SingleQueryEnumeratorExecutor<TEntity>(streamMergeContext);
if (async)
{
var asyncEnumerator = enumeratorParallelExecutor.GetAsyncEnumerator0(newQueryable).WaitAndUnwrapException();
return new[] { new StreamMergeAsyncEnumerator<TEntity>(asyncEnumerator) };
}
else
{
var enumerator = enumeratorParallelExecutor.GetEnumerator0(newQueryable);
return new[] { new StreamMergeAsyncEnumerator<TEntity>(enumerator) };
}
}
protected override IExecutor<IStreamMergeAsyncEnumerator<TEntity>> CreateExecutor0(bool async)
{
return new SingleQueryEnumeratorExecutor<TEntity>(GetStreamMergeContext());
}
}
}

View File

@ -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<TEntity>(_streamMergeContext);
}
//本次查询没有跨库没有跨表就可以直接执行
if (!_streamMergeContext.IsMergeQuery())
{
return new SingleQueryStreamEnumerable<TEntity>(_streamMergeContext);
return new EmptyShardingEnumerable<TEntity>(_streamMergeContext);
}
// //本次查询没有跨库没有跨表就可以直接执行
// if (!_streamMergeContext.IsMergeQuery())
// {
// return new SingleQueryStreamEnumerable<TEntity>(_streamMergeContext);
// }
if (_streamMergeContext.UseUnionAllMerge())
{
return new DefaultShardingStreamEnumerable<TEntity>(_streamMergeContext);
return new DefaultShardingEnumerable<TEntity>(_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<TEntity>(_streamMergeContext);
return new FirstOrDefaultShardingEnumerable<TEntity>(_streamMergeContext);
case nameof(Enumerable.Single):
case nameof(Enumerable.SingleOrDefault):
return new SingleOrDefaultStreamEnumerable<TEntity>(_streamMergeContext);
return new SingleOrDefaultShardingEnumerable<TEntity>(_streamMergeContext);
case nameof(Enumerable.Last):
case nameof(Enumerable.LastOrDefault):
return new LastOrDefaultStreamEnumerable<TEntity>(_streamMergeContext);
return new LastOrDefaultShardingEnumerable<TEntity>(_streamMergeContext);
}
//未开启系统分表或者本次查询涉及多张分表
@ -109,7 +109,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
}
return new DefaultShardingStreamEnumerable<TEntity>(_streamMergeContext);
return new DefaultShardingEnumerable<TEntity>(_streamMergeContext);
}
private IStreamEnumerable<TEntity> DoNoOrderAppendEnumeratorStreamMergeEngine(Type shardingEntityType)
@ -145,7 +145,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
if (useSequenceEnumeratorMergeEngine)
{
return new AppendOrderSequenceStreamEnumerable<TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults);
return new AppendOrderSequenceShardingEnumerable<TEntity>(_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<TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults, primaryOrder.IsAsc);
return new SequenceShardingEnumerable<TEntity>(_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<TEntity>(_streamMergeContext, total);
return new ReverseShardingEnumerable<TEntity>(_streamMergeContext, total);
}

View File

@ -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<TEntity> StreamMergeEnumeratorCombine<TEntity>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
if (streamMergeContext.HasGroupQuery())
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
}
}
}

View File

@ -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<TEntity> StreamMergeEnumeratorCombine<TEntity>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
if (streamMergeContext.IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
if (streamMergeContext.HasGroupQuery())
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
}
}
}

View File

@ -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<TEntity> StreamMergeEnumeratorCombine<TEntity>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
if (streamsAsyncEnumerators.Length != 1)
throw new ShardingCoreInvalidOperationException($"empty query combine has more {nameof(IStreamMergeAsyncEnumerator<TEntity>)}");
return streamsAsyncEnumerators[0];
}
}
}

View File

@ -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<TEntity> StreamMergeEnumeratorCombine<TEntity>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
var doGetStreamMergeAsyncEnumerator = DoGetStreamMergeAsyncEnumerator(streamMergeContext, streamsAsyncEnumerators);
return new InMemoryReverseStreamMergeAsyncEnumerator<TEntity>(doGetStreamMergeAsyncEnumerator);
}
private static IStreamMergeAsyncEnumerator<TEntity> DoGetStreamMergeAsyncEnumerator<TEntity>(StreamMergeContext streamMergeContext, IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
if (streamMergeContext.IsPaginationQuery())
{
if (streamMergeContext.HasGroupQuery())
{
var multiAggregateOrderStreamMergeAsyncEnumerator = new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
return new PaginationStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, new[] { multiAggregateOrderStreamMergeAsyncEnumerator });
}
return new PaginationStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
}
if (streamMergeContext.HasGroupQuery())
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
}
}
}

View File

@ -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<TEntity> StreamMergeEnumeratorCombine<TEntity>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
if (streamMergeContext.HasGroupQuery())
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(streamMergeContext, streamsAsyncEnumerators);
}
}
}

View File

@ -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<TEntity> StreamMergeEnumeratorCombine<TEntity>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TEntity>[] streamsAsyncEnumerators)
{
if (streamsAsyncEnumerators.Length != 1)
throw new ShardingCoreInvalidOperationException($"single query has more {nameof(IStreamMergeAsyncEnumerator<TEntity>)}");
return streamsAsyncEnumerators[0];
}
}
}

View File

@ -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<LinkedList<TResult>> ExecuteAsync(bool async,
public abstract IShardingMerger<TResult> GetShardingMerger();
public async Task<List<TResult>> ExecuteAsync(bool async,
DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit,
CancellationToken cancellationToken = new CancellationToken())
{
@ -71,14 +74,14 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions
}
}
private async Task<LinkedList<TResult>> ExecuteAsync0(bool async,
private async Task<List<TResult>> ExecuteAsync0(bool async,
DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit,
CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var circuitBreaker = CreateCircuitBreaker();
var executorGroups = dataSourceSqlExecutorUnit.SqlExecutorGroups;
LinkedList<TResult> result = new LinkedList<TResult>();
List<TResult> result = new List<TResult>();
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
/// <param name="sqlExecutorUnits"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected async Task<LinkedList<ShardingMergeResult<TResult>>> GroupExecuteAsync(
protected async Task<List<ShardingMergeResult<TResult>>> GroupExecuteAsync(
List<SqlExecutorUnit> sqlExecutorUnits, CancellationToken cancellationToken = new CancellationToken())
{
if (sqlExecutorUnits.Count <= 0)
{
return new LinkedList<ShardingMergeResult<TResult>>();
return new List<ShardingMergeResult<TResult>>();
}
else
{
var result = new LinkedList<ShardingMergeResult<TResult>>();
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<TResult> previewResults,
IEnumerable<TResult> parallelResults, bool async)
{
foreach (var parallelResult in parallelResults)
{
previewResults.AddLast(parallelResult);
}
}
// protected virtual void MergeParallelExecuteResult<TResult>(List<TResult> previewResults,
// IEnumerable<TResult> parallelResults, bool async)
// {
// foreach (var parallelResult in parallelResults)
// {
// previewResults.Add(parallelResult);
// }
// }
protected abstract Task<ShardingMergeResult<TResult>> ExecuteUnitAsync(SqlExecutorUnit sqlExecutorUnit,
CancellationToken cancellationToken = new CancellationToken());

View File

@ -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<TResult>
{
Task<LinkedList<TResult>> ExecuteAsync(bool async, DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken());
IShardingMerger<TResult> GetShardingMerger();
Task<List<TResult>> ExecuteAsync(bool async, DataSourceSqlExecutorUnit dataSourceSqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -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)
{
}

View File

@ -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
{
/// <summary>
///
@ -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<TResult> : AbstractExecutor<IStreamMergeAsyncEnumerator<TResult>>
internal abstract class AbstractEnumerableExecutor<TEntity> : AbstractExecutor<IStreamMergeAsyncEnumerator<TEntity>>
{
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<IStreamMergeAsyncEnumerator<TResult>> previewResults,
IEnumerable<IStreamMergeAsyncEnumerator<TResult>> 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<IStreamMergeAsyncEnumerator<TResult>> previewResults,
// IEnumerable<IStreamMergeAsyncEnumerator<TResult>> 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<IStreamMergeAsyncEnumerator<TResult>>
// previewInMemoryStreamEnumeratorResults &&
// parallelResults is IEnumerable<IStreamMergeAsyncEnumerator<TResult>> parallelStreamEnumeratorResults)
// {
// var mergeAsyncEnumerators = new LinkedList<IStreamMergeAsyncEnumerator<TResult>>();
// if (previewResultsCount == 1)
// {
// mergeAsyncEnumerators.AddLast(previewInMemoryStreamEnumeratorResults.First());
// }
//
// foreach (var parallelStreamEnumeratorResult in parallelStreamEnumeratorResults)
// {
// mergeAsyncEnumerators.AddLast(parallelStreamEnumeratorResult);
// }
//
// var combineStreamMergeAsyncEnumerator =
// CombineInMemoryStreamMergeAsyncEnumerator(mergeAsyncEnumerators.ToArray());
// // var streamMergeContext = GetStreamMergeContext();
// // IStreamMergeAsyncEnumerator<TResult> inMemoryStreamMergeAsyncEnumerator =streamMergeContext.HasGroupQuery()&&streamMergeContext.GroupQueryMemoryMerge()?
// // new InMemoryGroupByOrderStreamMergeAsyncEnumerator<TResult>(streamMergeContext,combineStreamMergeAsyncEnumerator,async):
// // new InMemoryStreamMergeAsyncEnumerator<TResult>(combineStreamMergeAsyncEnumerator, async);
// var inMemoryStreamMergeAsyncEnumerator= new InMemoryStreamMergeAsyncEnumerator<TResult>(combineStreamMergeAsyncEnumerator, async);
// previewInMemoryStreamEnumeratorResults.Clear();
// previewInMemoryStreamEnumeratorResults.AddLast(inMemoryStreamMergeAsyncEnumerator);
// //合并
// return;
// }
//
// throw new ShardingCoreInvalidOperationException(
// $"{typeof(TResult)} is not {typeof(IStreamMergeAsyncEnumerator<TResult>)}");
// }
var parallelCount = parallelResults.Count();
if (parallelCount == 0)
return;
//聚合
if (previewResults is LinkedList<IStreamMergeAsyncEnumerator<TResult>>
previewInMemoryStreamEnumeratorResults &&
parallelResults is IEnumerable<IStreamMergeAsyncEnumerator<TResult>> parallelStreamEnumeratorResults)
{
var mergeAsyncEnumerators = new LinkedList<IStreamMergeAsyncEnumerator<TResult>>();
if (previewResultsCount == 1)
{
mergeAsyncEnumerators.AddLast(previewInMemoryStreamEnumeratorResults.First());
}
foreach (var parallelStreamEnumeratorResult in parallelStreamEnumeratorResults)
{
mergeAsyncEnumerators.AddLast(parallelStreamEnumeratorResult);
}
var combineStreamMergeAsyncEnumerator =
CombineInMemoryStreamMergeAsyncEnumerator(mergeAsyncEnumerators.ToArray());
var inMemoryStreamMergeAsyncEnumerator =
new InMemoryStreamMergeAsyncEnumerator<TResult>(combineStreamMergeAsyncEnumerator, async);
previewInMemoryStreamEnumeratorResults.Clear();
previewInMemoryStreamEnumeratorResults.AddLast(inMemoryStreamMergeAsyncEnumerator);
//合并
return;
}
throw new ShardingCoreInvalidOperationException(
$"{typeof(TResult)} is not {typeof(IStreamMergeAsyncEnumerator<TResult>)}");
}
/// <summary>
/// 合并成一个迭代器
/// </summary>
/// <param name="streamsAsyncEnumerators"></param>
/// <returns></returns>
public virtual IStreamMergeAsyncEnumerator<TResult> CombineStreamMergeAsyncEnumerator(
IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
{
return GetStreamMergeCombine()
.StreamMergeEnumeratorCombine(GetStreamMergeContext(), streamsAsyncEnumerators);
}
public virtual IStreamMergeAsyncEnumerator<TResult> CombineInMemoryStreamMergeAsyncEnumerator(
IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
{
return CombineStreamMergeAsyncEnumerator(streamsAsyncEnumerators);
}
// /// <summary>
// /// 合并成一个迭代器
// /// </summary>
// /// <param name="streamsAsyncEnumerators"></param>
// /// <returns></returns>
// public abstract IStreamMergeAsyncEnumerator<TResult> CombineStreamMergeAsyncEnumerator(
// IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators);
//
// public virtual IStreamMergeAsyncEnumerator<TResult> CombineInMemoryStreamMergeAsyncEnumerator(
// IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
// {
// return CombineStreamMergeAsyncEnumerator(streamsAsyncEnumerators);
// }
/// <summary>
/// 开启异步线程获取并发迭代器
@ -110,7 +104,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions
/// <param name="async"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IStreamMergeAsyncEnumerator<TResult>> AsyncParallelEnumerator(IQueryable<TResult> queryable,
public async Task<IStreamMergeAsyncEnumerator<TEntity>> AsyncParallelEnumerator(IQueryable<TEntity> 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<TResult>(asyncEnumerator);
return new StreamMergeAsyncEnumerator<TEntity>(asyncEnumerator);
}
else
{
var enumerator = GetEnumerator0(queryable);
return new StreamMergeAsyncEnumerator<TResult>(enumerator);
return new StreamMergeAsyncEnumerator<TEntity>(enumerator);
}
}
@ -132,7 +126,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions
/// </summary>
/// <param name="newQueryable"></param>
/// <returns></returns>
public async Task<IAsyncEnumerator<TResult>> GetAsyncEnumerator0(IQueryable<TResult> newQueryable)
public async Task<IAsyncEnumerator<TEntity>> GetAsyncEnumerator0(IQueryable<TEntity> 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<TResult>(newQueryable.AsAsyncEnumerable().GetEnumerator());
new EFCore2TryCurrentAsyncEnumerator<TEntity>(newQueryable.AsAsyncEnumerable().GetEnumerator());
await enumator.MoveNext();
return enumator;
#endif
@ -152,14 +146,14 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions
/// </summary>
/// <param name="newQueryable"></param>
/// <returns></returns>
public IEnumerator<TResult> GetEnumerator0(IQueryable<TResult> newQueryable)
public IEnumerator<TEntity> GetEnumerator0(IQueryable<TEntity> newQueryable)
{
var enumator = newQueryable.AsEnumerable().GetEnumerator();
enumator.MoveNext();
return enumator;
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync(
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TEntity>>> 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<TResult>(streamMergeAsyncEnumerator);
new OneAtMostElementStreamMergeAsyncEnumerator<TEntity>(streamMergeAsyncEnumerator);
await streamMergeContext.DbContextDisposeAsync(dbContext);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(null,
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TEntity>>(null,
disConnectionStreamMergeAsyncEnumerator);
}
return shardingMergeResult;
}
/// <summary>
/// 是否需要在执行单元中直接回收掉链接有助于提高吞吐量
/// </summary>
/// <param name="streamMergeContext"></param>
/// <param name="streamMergeAsyncEnumerator"></param>
/// <returns></returns>
private bool DisposeInExecuteUnit(StreamMergeContext streamMergeContext,
private bool DisposeInExecuteUnit<TResult>(StreamMergeContext streamMergeContext,
IStreamMergeAsyncEnumerator<TResult> streamMergeAsyncEnumerator)
{
var queryMethodName = streamMergeContext.MergeQueryCompilerContext.GetQueryMethodName();
@ -214,7 +209,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators.Abstractions
return false;
}
protected abstract Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(
protected abstract Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TEntity>>> ExecuteUnitAsync0(
SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -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
{
/// <summary>
///
@ -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<TResult> : AbstractEnumeratorExecutor<TResult>
internal class AppendOrderSequenceEnumerableExecutor<TResult> : AbstractEnumerableExecutor<TResult>
{
private readonly IStreamMergeCombine _streamMergeCombine;
private readonly IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> _shardingMerger;
private readonly IQueryable<TResult> _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<IQueryable<TResult>>();
_async = async;
_shardingMerger = new AppendOrderSequenceEnumerableShardingMerger<TResult>(streamMergeContext,async);
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
@ -48,9 +46,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext, streamMergeAsyncEnumerator);
}
protected override IStreamMergeCombine GetStreamMergeCombine()
public override IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> GetShardingMerger()
{
return _streamMergeCombine;
return _shardingMerger;
}
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/6 13:00:40
/// Email: 326308290@qq.com
internal class DefaultEnumerableExecutor<TResult> : AbstractEnumerableExecutor<TResult>
{
// private readonly IStreamMergeCombine _streamMergeCombine;
private readonly IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> _shardingMerger;
private readonly bool _async;
public DefaultEnumerableExecutor(StreamMergeContext streamMergeContext, bool async) : base(streamMergeContext)
{
// _streamMergeCombine = streamMergeCombine;
_async = async;
_shardingMerger = new DefaultEnumerableShardingMerger<TResult>(streamMergeContext,async);
}
//
// protected override IStreamMergeCombine GetStreamMergeCombine()
// {
// return _streamMergeCombine;
// }
// public override IStreamMergeAsyncEnumerator<TResult> CombineInMemoryStreamMergeAsyncEnumerator(
// IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
// {
// if (GetStreamMergeContext().IsPaginationQuery())
// return new PaginationStreamMergeAsyncEnumerator<TResult>(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());//内存聚合分页不可以直接获取skip必须获取skip+take的数目
// return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators);
// }
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit);
var newQueryable = (IQueryable<TResult>)streamMergeContext.GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext);
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext, streamMergeAsyncEnumerator);
}
public override IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> GetShardingMerger()
{
return _shardingMerger;
}
}
}

View File

@ -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
// {
// /// <summary>
// ///
// /// </summary>
// /// Author: xjm
// /// Created: 2022/5/6 13:10:34
// /// Email: 326308290@qq.com
// internal class EmptyShardingExecutor<TResult> : AbstractEnumerableExecutor<TResult>
// {
// // private readonly IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> _shardingMerger;
//
// public EmptyShardingExecutor(StreamMergeContext streamMergeContext, bool async) : base(streamMergeContext)
// {
// // _shardingMerger = new EmptyEnumerableShardingMerger<TResult>(streamMergeContext,async);
// }
//
// protected override Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
// {
// throw new NotImplementedException();
// }
//
//
// public override IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> GetShardingMerger()
// {
// throw new NotImplementedException();
// }
// }
// }

View File

@ -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<TResult> : AbstractEnumerableExecutor<TResult>
{
private readonly IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> _shardingMerger;
private readonly IQueryable<TResult> _queryable;
private readonly bool _async;
public LastOrDefaultEnumerableExecutor(StreamMergeContext streamMergeContext, IQueryable<TResult> queryable,
bool async) : base(streamMergeContext)
{
_queryable = queryable;
_async = async;
_shardingMerger = new LastOrDefaultEnumerableShardingMerger<TResult>(GetStreamMergeContext(), async);
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(
SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit);
var newQueryable = (IQueryable<TResult>)_queryable.ReplaceDbContextQueryable(shardingDbContext);
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext,
streamMergeAsyncEnumerator);
}
public override IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> GetShardingMerger()
{
return _shardingMerger;
}
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/6 13:19:34
/// Email: 326308290@qq.com
internal class ReverseEnumerableExecutor<TResult> : AbstractEnumerableExecutor<TResult>
{
private readonly IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> _shardingMerger;
private readonly IOrderedQueryable<TResult> _reverseOrderQueryable;
private readonly bool _async;
public ReverseEnumerableExecutor(StreamMergeContext streamMergeContext, IOrderedQueryable<TResult> reverseOrderQueryable, bool async) : base(streamMergeContext)
{
_reverseOrderQueryable = reverseOrderQueryable;
_async = async;
_shardingMerger = new ReverseEnumerableShardingMerger<TResult>(streamMergeContext, async);
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit);
var newQueryable = _reverseOrderQueryable
.ReplaceDbContextQueryable(shardingDbContext).As<IQueryable<TResult>>();
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext,
streamMergeAsyncEnumerator);
}
public override IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> GetShardingMerger()
{
return _shardingMerger;
}
}
}

View File

@ -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
{
/// <summary>
///
@ -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<TResult> : AbstractEnumeratorExecutor<TResult>
internal class SequenceEnumerableExecutor<TResult> : AbstractEnumerableExecutor<TResult>
{
private readonly IStreamMergeCombine _streamMergeCombine;
private readonly IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> _shardingMerger;
private readonly IQueryable<TResult> _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<IQueryable<TResult>>();
_shardingMerger = new SequenceEnumerableShardingMerger<TResult>(streamMergeContext, async);
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
@ -47,9 +45,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext, streamMergeAsyncEnumerator);
}
protected override IStreamMergeCombine GetStreamMergeCombine()
public override IShardingMerger<IStreamMergeAsyncEnumerator<TResult>> GetShardingMerger()
{
return _streamMergeCombine;
return _shardingMerger;
}
}
}

View File

@ -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
// {
// /// <summary>
// ///
// /// </summary>
// /// Author: xjm
// /// Created: 2022/5/6 18:31:17
// /// Email: 326308290@qq.com
// internal class SingleQueryEnumerableExecutor<TResult> : AbstractEnumerableExecutor<TResult>
// {
// public SingleQueryEnumerableExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
// {
// }
// protected override Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
// {
// throw new NotImplementedException();
// }
//
// protected override IStreamMergeCombine GetStreamMergeCombine()
// {
// throw new NotImplementedException();
// }
//
// }
// }

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/6 13:00:40
/// Email: 326308290@qq.com
internal class DefaultEnumeratorExecutor<TResult> : AbstractEnumeratorExecutor<TResult>
{
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<TResult> CombineInMemoryStreamMergeAsyncEnumerator(
IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
{
if (GetStreamMergeContext().IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TResult>(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());//内存聚合分页不可以直接获取skip必须获取skip+take的数目
return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators);
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit);
var newQueryable = (IQueryable<TResult>)streamMergeContext.GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext);
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext, streamMergeAsyncEnumerator);
}
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/6 13:10:34
/// Email: 326308290@qq.com
internal class EmptyQueryEnumeratorExecutor<TResult> : AbstractEnumeratorExecutor<TResult>
{
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<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return _streamMergeCombine;
}
}
}

View File

@ -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<TResult> : AbstractEnumeratorExecutor<TResult>
{
private readonly IStreamMergeCombine _streamMergeCombine;
private readonly IQueryable<TResult> _queryable;
private readonly bool _async;
public LastOrDefaultEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine,IQueryable<TResult> queryable, bool async) : base(streamMergeContext)
{
_streamMergeCombine = streamMergeCombine;
_queryable = queryable;
_async = async;
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return _streamMergeCombine;
}
public override IStreamMergeAsyncEnumerator<TResult> CombineInMemoryStreamMergeAsyncEnumerator(
IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
{
if (GetStreamMergeContext().IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TResult>(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());//内存聚合分页不可以直接获取skip必须获取skip+take的数目
return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators);
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit);
var newQueryable = (IQueryable<TResult>)_queryable.ReplaceDbContextQueryable(shardingDbContext);
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext, streamMergeAsyncEnumerator);
}
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/6 13:19:34
/// Email: 326308290@qq.com
internal class ReverseEnumeratorExecutor<TResult> : AbstractEnumeratorExecutor<TResult>
{
private readonly IStreamMergeCombine _streamMergeCombine;
private readonly IOrderedQueryable<TResult> _reverseOrderQueryable;
private readonly bool _async;
public ReverseEnumeratorExecutor(StreamMergeContext streamMergeContext, IStreamMergeCombine streamMergeCombine, IOrderedQueryable<TResult> reverseOrderQueryable, bool async) : base(streamMergeContext)
{
_streamMergeCombine = streamMergeCombine;
_reverseOrderQueryable = reverseOrderQueryable;
_async = async;
}
protected override async Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
var streamMergeContext = GetStreamMergeContext();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit);
var newQueryable = _reverseOrderQueryable
.ReplaceDbContextQueryable(shardingDbContext).As<IQueryable<TResult>>();
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);
return new ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>(shardingDbContext,
streamMergeAsyncEnumerator);
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
return _streamMergeCombine;
}
public override IStreamMergeAsyncEnumerator<TResult> CombineInMemoryStreamMergeAsyncEnumerator(
IStreamMergeAsyncEnumerator<TResult>[] streamsAsyncEnumerators)
{
if (GetStreamMergeContext().IsPaginationQuery() && GetStreamMergeContext().HasGroupQuery())
{
var multiAggregateOrderStreamMergeAsyncEnumerator = new MultiAggregateOrderStreamMergeAsyncEnumerator<TResult>(GetStreamMergeContext(), streamsAsyncEnumerators);
return new PaginationStreamMergeAsyncEnumerator<TResult>(GetStreamMergeContext(), new[] { multiAggregateOrderStreamMergeAsyncEnumerator }, 0, GetStreamMergeContext().GetPaginationReWriteTake());
}
if (GetStreamMergeContext().IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TResult>(GetStreamMergeContext(), streamsAsyncEnumerators, 0, GetStreamMergeContext().GetPaginationReWriteTake());
return base.CombineInMemoryStreamMergeAsyncEnumerator(streamsAsyncEnumerators);
}
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/6 18:31:17
/// Email: 326308290@qq.com
internal class SingleQueryEnumeratorExecutor<TResult> : AbstractEnumeratorExecutor<TResult>
{
public SingleQueryEnumeratorExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override Task<ShardingMergeResult<IStreamMergeAsyncEnumerator<TResult>>> ExecuteUnitAsync0(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}
protected override IStreamMergeCombine GetStreamMergeCombine()
{
throw new NotImplementedException();
}
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/7 7:45:07
/// Email: 326308290@qq.com
internal abstract class AbstractMethodExecutor<TResult> : AbstractExecutor<RouteQueryResult<TResult>>
internal abstract class AbstractMethodExecutor<TResult> : AbstractExecutor<TResult>
{
protected AbstractMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override async Task<ShardingMergeResult<RouteQueryResult<TResult>>> ExecuteUnitAsync(SqlExecutorUnit sqlExecutorUnit, CancellationToken cancellationToken = new CancellationToken())
protected override async Task<ShardingMergeResult<TResult>> 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<TResult>(dataSourceName, routeResult, queryResult);
await streamMergeContext.DbContextDisposeAsync(shardingDbContext);
return new ShardingMergeResult<RouteQueryResult<TResult>>(null, routeQueryResult);
return new ShardingMergeResult<TResult>(null, queryResult);
}
protected abstract Task<TResult> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken());

View File

@ -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
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/7 7:45:07
/// Email: 326308290@qq.com
internal abstract class AbstractMethodWrapExecutor<TResult> : AbstractExecutor<RouteQueryResult<TResult>>
{
protected AbstractMethodWrapExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
}
protected override async Task<ShardingMergeResult<RouteQueryResult<TResult>>> 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<TResult>(dataSourceName, routeResult, queryResult);
await streamMergeContext.DbContextDisposeAsync(shardingDbContext);
return new ShardingMergeResult<RouteQueryResult<TResult>>(null, routeQueryResult);
}
protected abstract Task<TResult> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -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<bool> GetShardingMerger()
{
return AllMethodShardingMerger.Instance;
}
protected override Task<bool> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
var allQueryCombineResult = (AllQueryCombineResult)GetStreamMergeContext().MergeQueryCompilerContext.GetQueryCombineResult();

View File

@ -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<bool> GetShardingMerger()
{
return AnyMethodShardingMerger.Instance;
}
protected override Task<bool> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
return queryable.As<IQueryable<TEntity>>().AnyAsync(cancellationToken);

View File

@ -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<TEntity> : AbstractMethodExecutor<AverageResult<TEntity>>
internal class AverageMethodWrapExecutor<TSelect> : AbstractMethodWrapExecutor<AverageResult<TSelect>>
{
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<AverageResult<TEntity>> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
public override IShardingMerger<RouteQueryResult<AverageResult<TSelect>>> GetShardingMerger()
{
return new AverageMethodShardingMerger<TSelect>();
}
protected override async Task<AverageResult<TSelect>> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
var count = 0L;
TEntity sum = default;
var newQueryable = ((IQueryable<TEntity>)queryable);
TSelect sum = default;
var newQueryable = ((IQueryable<TSelect>)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<TEntity>(sum, count);
return new AverageResult<TSelect>(sum, count);
}
}
}

View File

@ -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<bool> GetShardingMerger()
{
return ContainsMethodShardingMerger.Instance;
}
protected override Task<bool> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
var constantQueryCombineResult = (ConstantQueryCombineResult)GetStreamMergeContext().MergeQueryCompilerContext.GetQueryCombineResult();

View File

@ -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<TEntity> : AbstractMethodExecutor<int>
internal class CountMethodExecutor<TEntity> : AbstractMethodWrapExecutor<int>
{
public CountMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
@ -34,6 +37,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods
return circuitBreaker;
}
public override IShardingMerger<RouteQueryResult<int>> GetShardingMerger()
{
return new CountMethodShardingMerger(GetStreamMergeContext());
}
protected override Task<int> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
return queryable.As<IQueryable<TEntity>>().CountAsync(cancellationToken);

View File

@ -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<TEntity> : AbstractMethodExecutor<long>
internal class LongCountMethodExecutor<TEntity> : AbstractMethodWrapExecutor<long>
{
public LongCountMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
@ -34,6 +37,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods
return circuitBreaker;
}
public override IShardingMerger<RouteQueryResult<long>> GetShardingMerger()
{
return new LongCountMethodShardingMerger(GetStreamMergeContext());
}
protected override Task<long> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
return queryable.As<IQueryable<TEntity>>().LongCountAsync(cancellationToken);

View File

@ -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<TEntity,TResult> : AbstractMethodExecutor<TResult>
internal class MaxMethodExecutor<TEntity,TResult> : AbstractMethodWrapExecutor<TResult>
{
public MaxMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
@ -33,6 +35,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods
return new AnyElementCircuitBreaker(GetStreamMergeContext());
}
public override IShardingMerger<RouteQueryResult<TResult>> GetShardingMerger()
{
return new MaxMethodShardingMerger<TResult>();
}
protected override Task<TResult> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{

View File

@ -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<TEntity,TResult> : AbstractMethodExecutor<TResult>
internal class MinMethodExecutor<TEntity,TResult> : AbstractMethodWrapExecutor<TResult>
{
public MinMethodExecutor(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
@ -31,6 +34,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods
return new AnyElementCircuitBreaker(GetStreamMergeContext());
}
public override IShardingMerger<RouteQueryResult<TResult>> GetShardingMerger()
{
return new MinMethodShardingMerger<TResult>();
}
protected override Task<TResult> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{

View File

@ -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<TEntity> : AbstractMethodExecutor<TEntity>
internal class SumMethodWrapExecutor<TEntity> : AbstractMethodExecutor<TEntity>
{
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<TEntity> GetShardingMerger()
{
return new SumMethodShardingMerger<TEntity>();
}
protected override Task<TEntity> EFCoreQueryAsync(IQueryable queryable, CancellationToken cancellationToken = new CancellationToken())
{
var resultType = typeof(TEntity);

View File

@ -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<TEntity>:IShardingMerger<IStreamMergeAsyncEnumerator<TEntity>>
{
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<TEntity> StreamMerge(List<IStreamMergeAsyncEnumerator<TEntity>> parallelResults)
{
if (_streamMergeContext.IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TEntity>(_streamMergeContext, parallelResults);
if (_streamMergeContext.HasGroupQuery())
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(_streamMergeContext, parallelResults);
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(_streamMergeContext, parallelResults);
}
public virtual void InMemoryMerge(List<IStreamMergeAsyncEnumerator<TEntity>> beforeInMemoryResults, List<IStreamMergeAsyncEnumerator<TEntity>> 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<IStreamMergeAsyncEnumerator<TEntity>> parallelStreamEnumeratorResults)
{
var mergeAsyncEnumerators = new List<IStreamMergeAsyncEnumerator<TEntity>>(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<TResult> inMemoryStreamMergeAsyncEnumerator =streamMergeContext.HasGroupQuery()&&streamMergeContext.GroupQueryMemoryMerge()?
// new InMemoryGroupByOrderStreamMergeAsyncEnumerator<TResult>(streamMergeContext,combineStreamMergeAsyncEnumerator,async):
// new InMemoryStreamMergeAsyncEnumerator<TResult>(combineStreamMergeAsyncEnumerator, async);
var inMemoryStreamMergeAsyncEnumerator= new InMemoryStreamMergeAsyncEnumerator<TEntity>(combineStreamMergeAsyncEnumerator, _async);
beforeInMemoryResults.Clear();
beforeInMemoryResults.Add(inMemoryStreamMergeAsyncEnumerator);
//合并
return;
}
throw new ShardingCoreInvalidOperationException(
$"{typeof(TEntity)} is not {typeof(IStreamMergeAsyncEnumerator<TEntity>)}");
}
}
}

View File

@ -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<bool>
{
private static readonly IShardingMerger<bool> _allShardingMerger;
static AllMethodShardingMerger()
{
_allShardingMerger = new AllMethodShardingMerger();
}
public static IShardingMerger<bool> Instance => _allShardingMerger;
public bool StreamMerge(List<bool> parallelResults)
{
return parallelResults.All(o => o);
}
public void InMemoryMerge(List<bool> beforeInMemoryResults, List<bool> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}

View File

@ -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<bool>
{
private static readonly IShardingMerger<bool> _allShardingMerger;
static AnyMethodShardingMerger()
{
_allShardingMerger = new AllMethodShardingMerger();
}
public static IShardingMerger<bool> Instance => _allShardingMerger;
public bool StreamMerge(List<bool> parallelResults)
{
return parallelResults.Any(o => o);
}
public void InMemoryMerge(List<bool> beforeInMemoryResults, List<bool> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using ShardingCore.Sharding.Enumerators;
using ShardingCore.Sharding.Enumerators.StreamMergeAsync;
namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers
{
/// <summary>
/// 和普通的不同因为是顺序查询所以需要忽略分页合并
/// </summary>
/// <typeparam name="TEntity"></typeparam>
internal class AppendOrderSequenceEnumerableShardingMerger<TEntity> : AbstractEnumerableShardingMerger<TEntity>
{
public AppendOrderSequenceEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base(
streamMergeContext, async)
{
}
public override IStreamMergeAsyncEnumerator<TEntity> StreamMerge(
List<IStreamMergeAsyncEnumerator<TEntity>> parallelResults)
{
if (GetStreamMergeContext().HasGroupQuery())
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(),
parallelResults);
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(), parallelResults);
}
}
}

View File

@ -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<TSelect> : IShardingMerger<RouteQueryResult<AverageResult<TSelect>>>
{
public RouteQueryResult<AverageResult<TSelect>> StreamMerge(
List<RouteQueryResult<AverageResult<TSelect>>> 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<TSelect>(nameof(AverageResult<object>.Sum));
var count = queryable.Sum(o => o.Count);
return new RouteQueryResult<AverageResult<TSelect>>(null, null,
new AverageResult<TSelect>(sum , count));
}
public void InMemoryMerge(List<RouteQueryResult<AverageResult<TSelect>>> beforeInMemoryResults,
List<RouteQueryResult<AverageResult<TSelect>>> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}
// var resultList = await base.ExecuteAsync<AverageResult<TSelect>>(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<TSelect>(nameof(AverageResult<object>.Sum));
// var count = queryable.Sum(o => o.Count);

View File

@ -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<bool>
{
private static readonly IShardingMerger<bool> _allShardingMerger;
static ContainsMethodShardingMerger()
{
_allShardingMerger = new AllMethodShardingMerger();
}
public static IShardingMerger<bool> Instance => _allShardingMerger;
public bool StreamMerge(List<bool> parallelResults)
{
return parallelResults.Any(o => o);
}
public void InMemoryMerge(List<bool> beforeInMemoryResults, List<bool> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}

View File

@ -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<RouteQueryResult<int>>
{
private readonly IShardingPageManager _shardingPageManager;
public CountMethodShardingMerger(StreamMergeContext streamMergeContext)
{
_shardingPageManager =streamMergeContext.ShardingRuntimeContext.GetShardingPageManager();
}
public RouteQueryResult<int> StreamMerge(List<RouteQueryResult<int>> parallelResults)
{
if (_shardingPageManager.Current != null)
{
int r = 0;
foreach (var routeQueryResult in parallelResults)
{
_shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult<long>(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult));
r += routeQueryResult.QueryResult;
}
return new RouteQueryResult<int>(null,null,r,true);
}
return new RouteQueryResult<int>(null,null,parallelResults.Sum(o => o.QueryResult),true);
}
public void InMemoryMerge(List<RouteQueryResult<int>> beforeInMemoryResults, List<RouteQueryResult<int>> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}

View File

@ -0,0 +1,12 @@
using System.Collections.Generic;
using ShardingCore.Sharding.Enumerators;
namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers
{
internal class DefaultEnumerableShardingMerger<TEntity>:AbstractEnumerableShardingMerger<TEntity>
{
public DefaultEnumerableShardingMerger(StreamMergeContext streamMergeContext,bool async) : base(streamMergeContext,async)
{
}
}
}

View File

@ -0,0 +1,39 @@
// using System.Collections.Generic;
// using ShardingCore.Exceptions;
// using ShardingCore.Sharding.Enumerators;
//
// namespace ShardingCore.Sharding.MergeEngines.Executors.ShardingMergers
// {
// internal class EmptyEnumerableShardingMerger<TEntity> : AbstractEnumerableShardingMerger<TEntity>
// {
// public EmptyEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base(
// streamMergeContext, async)
// {
// }
//
// public override IStreamMergeAsyncEnumerator<TEntity> StreamMerge(
// List<IStreamMergeAsyncEnumerator<TEntity>> parallelResults)
// {
// if (parallelResults.Count != 1)
// throw new ShardingCoreInvalidOperationException(
// $"empty query combine has more {nameof(IStreamMergeAsyncEnumerator<TEntity>)}");
// return parallelResults[0];
// }
//
// public override void InMemoryMerge(List<IStreamMergeAsyncEnumerator<TEntity>> beforeInMemoryResults, List<IStreamMergeAsyncEnumerator<TEntity>> 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]);
// }
// }
// }

View File

@ -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<TEntity> : AbstractEnumerableShardingMerger<TEntity>
{
public LastOrDefaultEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base(
streamMergeContext, async)
{
}
public override IStreamMergeAsyncEnumerator<TEntity> StreamMerge(
List<IStreamMergeAsyncEnumerator<TEntity>> parallelResults)
{
if (GetStreamMergeContext().IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(), parallelResults, 0,
GetStreamMergeContext().GetPaginationReWriteTake()); //内存聚合分页不可以直接获取skip必须获取skip+take的数目
return base.StreamMerge(parallelResults);
}
}
}

View File

@ -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<RouteQueryResult<long>>
{
private readonly IShardingPageManager _shardingPageManager;
public LongCountMethodShardingMerger(StreamMergeContext streamMergeContext)
{
_shardingPageManager =streamMergeContext.ShardingRuntimeContext.GetShardingPageManager();
}
public RouteQueryResult<long> StreamMerge(List<RouteQueryResult<long>> parallelResults)
{
if (_shardingPageManager.Current != null)
{
long r = 0;
foreach (var routeQueryResult in parallelResults)
{
_shardingPageManager.Current.RouteQueryResults.Add(new RouteQueryResult<long>(routeQueryResult.DataSourceName, routeQueryResult.TableRouteResult, routeQueryResult.QueryResult));
r += routeQueryResult.QueryResult;
}
return new RouteQueryResult<long>(null,null,r,true);
}
return new RouteQueryResult<long>(null,null,parallelResults.Sum(o => o.QueryResult),true);
}
public void InMemoryMerge(List<RouteQueryResult<long>> beforeInMemoryResults, List<RouteQueryResult<long>> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}

View File

@ -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<TResult> : IShardingMerger<RouteQueryResult<TResult>>
{
public RouteQueryResult<TResult> StreamMerge(List<RouteQueryResult<TResult>> parallelResults)
{
var result = parallelResults.Where(o => o.HasQueryResult()).Max(o => o.QueryResult);
return new RouteQueryResult<TResult>(null, null, result);
// var resultType = typeof(TEntity);
// if (!resultType.IsNullableType())
// {
// var minTResult = GetMinTResult(parallelResults);
// return new RouteQueryResult<TResult>(null, null, minTResult);
// }
// else
// {
// var result = parallelResults.Where(o => o.HasQueryResult()).Min(o => o.QueryResult);
// return new RouteQueryResult<TResult>(null, null, result);
// }
}
// private TResult GetMinTResult(List<RouteQueryResult<TResult>> 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<TResult>(min);
// }
//
// private TResult ConvertNumber<TNumber>(TNumber number)
// {
// if (number == null)
// return default;
// var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult));
// return Expression.Lambda<Func<TResult>>(convertExpr).Compile()();
// }
public void InMemoryMerge(List<RouteQueryResult<TResult>> beforeInMemoryResults,
List<RouteQueryResult<TResult>> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}

View File

@ -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<TResult> : IShardingMerger<RouteQueryResult<TResult>>
{
public RouteQueryResult<TResult> StreamMerge(List<RouteQueryResult<TResult>> parallelResults)
{
var result = parallelResults.Where(o => o.HasQueryResult()).Min(o => o.QueryResult);
return new RouteQueryResult<TResult>(null, null, result);
// var resultType = typeof(TEntity);
// if (!resultType.IsNullableType())
// {
// var minTResult = GetMinTResult(parallelResults);
// return new RouteQueryResult<TResult>(null, null, minTResult);
// }
// else
// {
// var result = parallelResults.Where(o => o.HasQueryResult()).Min(o => o.QueryResult);
// return new RouteQueryResult<TResult>(null, null, result);
// }
}
// private TResult GetMinTResult(List<RouteQueryResult<TResult>> 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<TResult>(min);
// }
//
// private TResult ConvertNumber<TNumber>(TNumber number)
// {
// if (number == null)
// return default;
// var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult));
// return Expression.Lambda<Func<TResult>>(convertExpr).Compile()();
// }
public void InMemoryMerge(List<RouteQueryResult<TResult>> beforeInMemoryResults,
List<RouteQueryResult<TResult>> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}
//
// var resultType = typeof(TEntity);
// if (!resultType.IsNullableType())
// {
// if (typeof(decimal) == resultType)
// {
// var result = await base.ExecuteAsync<decimal?>(cancellationToken);
// return GetMinTResult<decimal?>(result);
// }
// if (typeof(float) == resultType)
// {
// var result = await base.ExecuteAsync<float?>(cancellationToken);
// return GetMinTResult<float?>(result);
// }
// if (typeof(int) == resultType)
// {
// var result = await base.ExecuteAsync<int?>(cancellationToken);
// return GetMinTResult<int?>(result);
// }
// if (typeof(long) == resultType)
// {
// var result = await base.ExecuteAsync<long?>(cancellationToken);
// return GetMinTResult<long?>(result);
// }
// if (typeof(double) == resultType)
// {
// var result = await base.ExecuteAsync<double?>(cancellationToken);
// return GetMinTResult<double?>(result);
// }
//
// throw new ShardingCoreException($"cant calc min value, type:[{resultType}]");
// }
// else
// {
// var result = await base.ExecuteAsync<TResult>(cancellationToken);
// return result.Where(o => o.HasQueryResult()).Min(o => o.QueryResult);
// }

View File

@ -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<TEntity> : AbstractEnumerableShardingMerger<TEntity>
{
public ReverseEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base(
streamMergeContext, async)
{
}
public override IStreamMergeAsyncEnumerator<TEntity> StreamMerge(
List<IStreamMergeAsyncEnumerator<TEntity>> parallelResults)
{
if (GetStreamMergeContext().IsPaginationQuery() && GetStreamMergeContext().HasGroupQuery())
{
var multiAggregateOrderStreamMergeAsyncEnumerator =
new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(),
parallelResults);
return new PaginationStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(),
new[] { multiAggregateOrderStreamMergeAsyncEnumerator }, 0,
GetStreamMergeContext().GetPaginationReWriteTake());
}
if (GetStreamMergeContext().IsPaginationQuery())
return new PaginationStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(), parallelResults, 0,
GetStreamMergeContext().GetPaginationReWriteTake());
return base.StreamMerge(parallelResults);
}
}
}

View File

@ -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<TEntity> : AbstractEnumerableShardingMerger<TEntity>
{
public SequenceEnumerableShardingMerger(StreamMergeContext streamMergeContext, bool async) : base(
streamMergeContext, async)
{
}
public override IStreamMergeAsyncEnumerator<TEntity> StreamMerge(
List<IStreamMergeAsyncEnumerator<TEntity>> parallelResults)
{
if (GetStreamMergeContext().HasGroupQuery())
{
return new MultiAggregateOrderStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(),
parallelResults);
}
return new MultiOrderStreamMergeAsyncEnumerator<TEntity>(GetStreamMergeContext(), parallelResults);
}
}
}

View File

@ -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<TEntity> : IShardingMerger<TEntity>
{
private TEntity GetSumResult<TInnerSelect>(List<TInnerSelect> source)
{
if (source.IsEmpty())
return default;
var sum = source.AsQueryable().SumByConstant<TInnerSelect>();
return ConvertSum(sum);
}
private TEntity ConvertSum<TNumber>(TNumber number)
{
if (number == null)
return default;
var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TEntity));
return Expression.Lambda<Func<TEntity>>(convertExpr).Compile()();
}
// protected override TResult DoMergeResult(List<RouteQueryResult<TResult>> resultList)
// {
// return GetSumResult(resultList);
// }
public TEntity StreamMerge(List<TEntity> parallelResults)
{
return GetSumResult(parallelResults);
}
public void InMemoryMerge(List<TEntity> beforeInMemoryResults, List<TEntity> parallelResults)
{
beforeInMemoryResults.AddRange(parallelResults);
}
}
}
// private TResult GetSumResult<TInnerSelect>(List<RouteQueryResult<TInnerSelect>> source)
// {
// if (source.IsEmpty())
// return default;
// var sum = source.AsQueryable().SumByPropertyName<TInnerSelect>(nameof(RouteQueryResult<TInnerSelect>.QueryResult));
// return ConvertSum(sum);
// }
// private TResult ConvertSum<TNumber>(TNumber number)
// {
// if (number == null)
// return default;
// var convertExpr = Expression.Convert(Expression.Constant(number), typeof(TResult));
// return Expression.Lambda<Func<TResult>>(convertExpr).Compile()();
// }
// protected override TResult DoMergeResult(List<RouteQueryResult<TResult>> resultList)
// {
// return GetSumResult(resultList);
// }

View File

@ -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<TEntity> : AbstractEnsureMethodCallInMemoryAsyncMergeEngine<TEntity,long>
{
private readonly IShardingPageManager _shardingPageManager;
public LongCountAsyncInMemoryMergeEngine(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{
_shardingPageManager=streamMergeContext.ShardingRuntimeContext.GetShardingPageManager();
}
protected override long DoMergeResult(List<RouteQueryResult<long>> 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<RouteQueryResult<long>> CreateExecutor0(bool async)
{
return new LongCountMethodExecutor<TEntity>(GetStreamMergeContext());
}
}
}

View File

@ -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<TResult>(StreamMergeContext streamMergeContext,
IExecutor<TResult> executor, bool async, IEnumerable<ISqlRouteUnit> sqlRouteUnits,
CancellationToken cancellationToken = new CancellationToken())
{
return ExecuteAsync<TResult>(streamMergeContext, executor, async, sqlRouteUnits, cancellationToken)
.WaitAndUnwrapException();
}
public async Task<TResult> ExecuteAsync<TResult>(StreamMergeContext streamMergeContext,
IExecutor<TResult> executor, bool async, IEnumerable<ISqlRouteUnit> sqlRouteUnits,
CancellationToken cancellationToken = new CancellationToken())
{
var resultGroups =
Execute0<TResult>(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<List<TResult>>[] Execute0<TResult>(StreamMergeContext streamMergeContext,
IExecutor<TResult> executor, bool async, IEnumerable<ISqlRouteUnit> 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;
}
/// <summary>
/// 顺序查询从重排序
/// </summary>
/// <param name="streamMergeContext"></param>
/// <param name="sqlRouteUnits"></param>
/// <returns></returns>
private IEnumerable<ISqlRouteUnit> ReOrderTableTails(StreamMergeContext streamMergeContext,
IEnumerable<ISqlRouteUnit> sqlRouteUnits)
{
if (streamMergeContext.IsSeqQuery())
{
return sqlRouteUnits.OrderByAscDescIf(o => o.TableRouteResult.ReplaceTables.First().Tail,
streamMergeContext.TailComparerNeedReverse, streamMergeContext.ShardingTailComparer);
}
return sqlRouteUnits;
}
/// <summary>
/// 每个数据源下的分表结果按 maxQueryConnectionsLimit 进行组合分组每组大小 maxQueryConnectionsLimit
/// ConnectionModeEnum为用户配置或者系统自动计算,哪怕是用户指定也是按照maxQueryConnectionsLimit来进行分组。
/// </summary>
/// <param name="streamMergeContext"></param>
/// <param name="sqlGroups"></param>
/// <returns></returns>
protected DataSourceSqlExecutorUnit GetSqlExecutorGroups(StreamMergeContext streamMergeContext,
IGrouping<string, ISqlRouteUnit> 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<SqlExecutorUnit>(connectionMode, o)).ToList();
return new DataSourceSqlExecutorUnit(connectionMode, sqlExecutorGroups);
}
}
}

View File

@ -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;
}
/// <summary>
/// sql执行的路由最小单元
/// </summary>
/// <returns></returns>
protected virtual IEnumerable<ISqlRouteUnit> 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;
}
}
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace ShardingCore.Sharding.MergeEngines.ShardingMergeEngines.Abstractions
{
internal interface IShardingMerger<TResult>
{
TResult StreamMerge(List<TResult> parallelResults);
void InMemoryMerge(List<TResult> beforeInMemoryResults,List<TResult> parallelResults);
}
}

View File

@ -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
{

View File

@ -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<TResult> : AbstractBaseMergeEngine,IEnsureMergeResult<TResult>
{
protected AbstractMethodEnsureMergeEngine(StreamMergeContext streamMergeContext):base(streamMergeContext)
{
}
protected abstract IExecutor<TResult> CreateExecutor();
public virtual TResult MergeResult()
{
return MergeResultAsync().WaitAndUnwrapException(false);
}
public virtual async Task<TResult> 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<TResult>(GetStreamMergeContext(),executor,true,defaultSqlRouteUnits,cancellationToken);
return result;
}
}
}

View File

@ -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<TResult> : AbstractBaseMergeEngine,IEnsureMergeResult<TResult>
{
protected AbstractMethodEnsureWrapMergeEngine(StreamMergeContext streamMergeContext):base(streamMergeContext)
{
}
protected abstract IExecutor<RouteQueryResult<TResult>> CreateExecutor();
public virtual TResult MergeResult()
{
return MergeResultAsync().WaitAndUnwrapException(false);
}
public async Task<TResult> 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<RouteQueryResult<TResult>>(GetStreamMergeContext(),executor,true,defaultSqlRouteUnits,cancellationToken);
return result.QueryResult;
}
//
// protected abstract TResult DoMergeResult(List<RouteQueryResult<TResult>> resultList);
//
// protected override IExecutor<TR> CreateExecutor<TR>(bool async)
// {
// return CreateExecutor0(async) as IExecutor<TR>;
// }
//
// protected abstract IExecutor<RouteQueryResult<TResult>> CreateExecutor0(bool async);
}
}

View File

@ -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<TEntity> : AbstractMethodEnsureMergeEngine<TEntity,TEntity>
// {
// 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<RouteQueryResult<TEntity>> resultList)
// {
// var entity = DoMergeResult0(resultList);
// return ProcessTrackResult(entity);
// }
//
// protected abstract TEntity DoMergeResult0(List<RouteQueryResult<TEntity>> resultList);
// }
// }

View File

@ -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

View File

@ -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<TResult>
{

Some files were not shown because too many files have changed in this diff Show More