This commit is contained in:
xuejmnet 2021-09-04 21:49:13 +08:00
parent 8070b2fcca
commit 255398ba30
11 changed files with 88 additions and 43 deletions

View File

@ -21,11 +21,47 @@ namespace ShardingCore.Core.PhysicTables
OriginalName = virtualTable.GetOriginalTableName(); OriginalName = virtualTable.GetOriginalTableName();
Tail = tail; Tail = tail;
} }
public string FullName => $"{OriginalName}{TailPrefix}{Tail}"; public string FullName => $"{OriginalName}{TailPrefix}{Tail}";
public string OriginalName { get; } public string OriginalName { get; }
public string TailPrefix =>VirtualTable.ShardingConfig.TailPrefix; public string TailPrefix =>VirtualTable.ShardingConfig.TailPrefix;
public string Tail { get; } public string Tail { get; }
public Type EntityType => VirtualTable.EntityType; public Type EntityType => VirtualTable.EntityType;
public IVirtualTable VirtualTable { get; } public IVirtualTable VirtualTable { get; }
protected bool Equals(DefaultPhysicTable other)
{
return OriginalName == other.OriginalName && Tail == other.Tail && Equals(VirtualTable, other.VirtualTable);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((DefaultPhysicTable) obj);
}
#if !EFCORE2
public override int GetHashCode()
{
return HashCode.Combine(OriginalName, Tail, VirtualTable);
}
#endif
#if EFCORE2
public override int GetHashCode()
{
unchecked
{
var hashCode = (OriginalName != null ? OriginalName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Tail != null ? Tail.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (VirtualTable != null ? VirtualTable.GetHashCode() : 0);
return hashCode;
}
}
#endif
} }
} }

View File

@ -35,8 +35,13 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
/// 启用断言路由 /// 启用断言路由
/// </summary> /// </summary>
protected virtual bool EnableAssertRoute => false; protected virtual bool EnableAssertRoute => false;
public override List<IPhysicTable> RouteWithPredicate(List<IPhysicTable> allPhysicTables, IQueryable queryable) public override List<IPhysicTable> RouteWithPredicate(List<IPhysicTable> allPhysicTables, IQueryable queryable,bool isQuery)
{ {
if (!isQuery)
{
//后拦截器
return AfterPhysicTableFilter(allPhysicTables,DoRouteWithPredicate(allPhysicTables,queryable));
}
//强制路由不经过断言 //强制路由不经过断言
if (EnableHintRoute) if (EnableHintRoute)
{ {

View File

@ -43,8 +43,9 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
/// </summary> /// </summary>
/// <param name="allPhysicTables"></param> /// <param name="allPhysicTables"></param>
/// <param name="queryable"></param> /// <param name="queryable"></param>
/// <param name="isQuery"></param>
/// <returns></returns> /// <returns></returns>
public abstract List<IPhysicTable> RouteWithPredicate(List<IPhysicTable> allPhysicTables, IQueryable queryable); public abstract List<IPhysicTable> RouteWithPredicate(List<IPhysicTable> allPhysicTables, IQueryable queryable,bool isQuery);
/// <summary> /// <summary>
/// 根据值路由 /// 根据值路由

View File

@ -25,8 +25,9 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes
/// </summary> /// </summary>
/// <param name="allPhysicTables"></param> /// <param name="allPhysicTables"></param>
/// <param name="queryable"></param> /// <param name="queryable"></param>
/// <param name="isQuery"></param>
/// <returns></returns> /// <returns></returns>
List<IPhysicTable> RouteWithPredicate(List<IPhysicTable> allPhysicTables,IQueryable queryable); List<IPhysicTable> RouteWithPredicate(List<IPhysicTable> allPhysicTables,IQueryable queryable,bool isQuery);
/// <summary> /// <summary>
/// 根据值进行路由 /// 根据值进行路由

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -33,7 +34,7 @@ namespace ShardingCore.Core.VirtualTables
public PaginationMetadata PaginationMetadata { get; } public PaginationMetadata PaginationMetadata { get; }
public bool EnablePagination => PaginationMetadata != null; public bool EnablePagination => PaginationMetadata != null;
private readonly List<IPhysicTable> _physicTables = new List<IPhysicTable>(); private readonly ConcurrentDictionary<IPhysicTable,object> _physicTables = new ConcurrentDictionary<IPhysicTable,object>();
public OneDbVirtualTable(IVirtualTableRoute<T> virtualTableRoute) public OneDbVirtualTable(IVirtualTableRoute<T> virtualTableRoute)
{ {
@ -50,16 +51,16 @@ namespace ShardingCore.Core.VirtualTables
public List<IPhysicTable> GetAllPhysicTables() public List<IPhysicTable> GetAllPhysicTables()
{ {
return _physicTables; return _physicTables.Keys.ToList();
} }
public List<IPhysicTable> RouteTo(TableRouteConfig tableRouteConfig) public List<IPhysicTable> RouteTo(TableRouteConfig tableRouteConfig)
{ {
var route = _virtualTableRoute; var route = _virtualTableRoute;
if (tableRouteConfig.UseQueryable()) if (tableRouteConfig.UseQueryable())
return route.RouteWithPredicate(_physicTables, tableRouteConfig.GetQueryable()); return route.RouteWithPredicate(GetAllPhysicTables(), tableRouteConfig.GetQueryable(),true);
if (tableRouteConfig.UsePredicate()) if (tableRouteConfig.UsePredicate())
return route.RouteWithPredicate(_physicTables, new EnumerableQuery<T>((Expression<Func<T, bool>>) tableRouteConfig.GetPredicate())); return route.RouteWithPredicate(GetAllPhysicTables(), new EnumerableQuery<T>((Expression<Func<T, bool>>) tableRouteConfig.GetPredicate()),false);
object shardingKeyValue = null; object shardingKeyValue = null;
if (tableRouteConfig.UseValue()) if (tableRouteConfig.UseValue())
shardingKeyValue = tableRouteConfig.GetShardingKeyValue(); shardingKeyValue = tableRouteConfig.GetShardingKeyValue();
@ -69,7 +70,7 @@ namespace ShardingCore.Core.VirtualTables
if (shardingKeyValue != null) if (shardingKeyValue != null)
{ {
var routeWithValue = route.RouteWithValue(_physicTables, shardingKeyValue); var routeWithValue = route.RouteWithValue(GetAllPhysicTables(), shardingKeyValue);
return new List<IPhysicTable>(1) {routeWithValue}; return new List<IPhysicTable>(1) {routeWithValue};
} }
@ -79,8 +80,8 @@ namespace ShardingCore.Core.VirtualTables
public void AddPhysicTable(IPhysicTable physicTable) public void AddPhysicTable(IPhysicTable physicTable)
{ {
if (_physicTables.All(o => o.Tail != physicTable.Tail)) if (GetAllPhysicTables().All(o => o.Tail != physicTable.Tail))
_physicTables.Add(physicTable); _physicTables.TryAdd(physicTable,null);
} }
public void SetOriginalTableName(string originalTableName) public void SetOriginalTableName(string originalTableName)
@ -100,7 +101,7 @@ namespace ShardingCore.Core.VirtualTables
public List<string> GetTaleAllTails() public List<string> GetTaleAllTails()
{ {
return _physicTables.Select(o => o.Tail).ToList(); return _physicTables.Keys.Select(o => o.Tail).ToList();
} }
public IVirtualTableRoute<T> GetVirtualRoute() public IVirtualTableRoute<T> GetVirtualRoute()

View File

@ -17,7 +17,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations
/// </summary> /// </summary>
public class PaginationMetadata public class PaginationMetadata
{ {
public ISet<PaginationConfig> PaginationConfigs = new HashSet<PaginationConfig>(); public ISet<PaginationSequenceConfig> PaginationConfigs = new HashSet<PaginationSequenceConfig>();
/// <summary> /// <summary>
/// 反向排序因子 /// 反向排序因子

View File

@ -12,12 +12,12 @@ namespace ShardingCore.Sharding.PaginationConfigurations
*/ */
public class PaginationOrderPropertyBuilder public class PaginationOrderPropertyBuilder
{ {
private readonly PaginationConfig _paginationConfig; private readonly PaginationSequenceConfig _paginationSequenceConfig;
public PaginationOrderPropertyBuilder(LambdaExpression orderPropertyExpression,PaginationMetadata metadata) public PaginationOrderPropertyBuilder(LambdaExpression orderPropertyExpression,PaginationMetadata metadata)
{ {
_paginationConfig = new PaginationConfig(orderPropertyExpression); _paginationSequenceConfig = new PaginationSequenceConfig(orderPropertyExpression);
metadata.PaginationConfigs.Add(_paginationConfig); metadata.PaginationConfigs.Add(_paginationSequenceConfig);
} }
/// <summary> /// <summary>
@ -28,7 +28,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations
public PaginationOrderPropertyBuilder UseTailCompare(IComparer<string> tailComparer) public PaginationOrderPropertyBuilder UseTailCompare(IComparer<string> tailComparer)
{ {
_paginationConfig.TailComparer= tailComparer ?? throw new ArgumentException(nameof(tailComparer)); _paginationSequenceConfig.TailComparer= tailComparer ?? throw new ArgumentException(nameof(tailComparer));
return this; return this;
} }
/// <summary> /// <summary>
@ -38,7 +38,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations
/// <returns></returns> /// <returns></returns>
public PaginationOrderPropertyBuilder UseQueryMatch(PaginationMatchEnum paginationMatchEnum) public PaginationOrderPropertyBuilder UseQueryMatch(PaginationMatchEnum paginationMatchEnum)
{ {
_paginationConfig.PaginationMatchEnum = paginationMatchEnum; _paginationSequenceConfig.PaginationMatchEnum = paginationMatchEnum;
return this; return this;
} }
/// <summary> /// <summary>
@ -48,7 +48,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations
/// <returns></returns> /// <returns></returns>
public PaginationOrderPropertyBuilder UseAppendIfOrderNone(int order=0) public PaginationOrderPropertyBuilder UseAppendIfOrderNone(int order=0)
{ {
_paginationConfig.AppendOrder = order; _paginationSequenceConfig.AppendOrder = order;
return this; return this;
} }
} }

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
@ -15,18 +15,17 @@ namespace ShardingCore.Sharding.PaginationConfigurations
* @Ver: 1.0 * @Ver: 1.0
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public class PaginationConfig public class PaginationSequenceConfig
{ {
public PaginationConfig(LambdaExpression orderPropertyExpression, PaginationMatchEnum paginationMatchEnum= PaginationMatchEnum.Owner, IComparer<string> tailComparer=null) public PaginationSequenceConfig(LambdaExpression orderPropertyExpression, PaginationMatchEnum paginationMatchEnum= PaginationMatchEnum.Owner, IComparer<string> tailComparer=null)
{ {
OrderPropertyExpression = orderPropertyExpression;
OrderPropertyInfo = orderPropertyExpression.GetPropertyAccess(); OrderPropertyInfo = orderPropertyExpression.GetPropertyAccess();
PropertyName = OrderPropertyInfo.Name; PropertyName = OrderPropertyInfo.Name;
PaginationMatchEnum = paginationMatchEnum; PaginationMatchEnum = paginationMatchEnum;
TailComparer = tailComparer ?? Comparer<string>.Default; TailComparer = tailComparer ?? Comparer<string>.Default;
SequenceTails = new HashSet<string>();
} }
public LambdaExpression OrderPropertyExpression { get; set; }
public IComparer<string> TailComparer { get; set; } public IComparer<string> TailComparer { get; set; }
public PaginationMatchEnum PaginationMatchEnum { get; set; } public PaginationMatchEnum PaginationMatchEnum { get; set; }
public PropertyInfo OrderPropertyInfo { get; set; } public PropertyInfo OrderPropertyInfo { get; set; }
@ -40,9 +39,11 @@ namespace ShardingCore.Sharding.PaginationConfigurations
/// </summary> /// </summary>
public int AppendOrder { get; set; } = -1; public int AppendOrder { get; set; } = -1;
public string PropertyName { get;} public string PropertyName { get;}
public ISet<string> SequenceTails { get; }
protected bool Equals(PaginationConfig other) protected bool Equals(PaginationSequenceConfig other)
{ {
return PropertyName == other.PropertyName; return PropertyName == other.PropertyName;
} }
@ -52,7 +53,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations
if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true; if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false; if (obj.GetType() != this.GetType()) return false;
return Equals((PaginationConfig) obj); return Equals((PaginationSequenceConfig) obj);
} }
public override int GetHashCode() public override int GetHashCode()

View File

@ -102,21 +102,21 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors
return new DefaultShardingEnumeratorAsyncStreamMergeEngine<TEntity>(_streamMergeContext); return new DefaultShardingEnumeratorAsyncStreamMergeEngine<TEntity>(_streamMergeContext);
} }
private bool PaginationMatch(PaginationConfig paginationConfig) private bool PaginationMatch(PaginationSequenceConfig paginationSequenceConfig)
{ {
if (paginationConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named)) if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return typeof(TEntity) == paginationConfig.OrderPropertyInfo.DeclaringType; return typeof(TEntity) == paginationSequenceConfig.OrderPropertyInfo.DeclaringType;
return false; return false;
} }
private bool PaginationPrimaryMatch(PaginationConfig paginationConfig, PropertyOrder propertyOrder) private bool PaginationPrimaryMatch(PaginationSequenceConfig paginationSequenceConfig, PropertyOrder propertyOrder)
{ {
if (propertyOrder.PropertyExpression != paginationConfig.PropertyName) if (propertyOrder.PropertyExpression != paginationSequenceConfig.PropertyName)
return false; return false;
if (paginationConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named)) if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return typeof(TEntity) == paginationConfig.OrderPropertyInfo.DeclaringType; return typeof(TEntity) == paginationSequenceConfig.OrderPropertyInfo.DeclaringType;
return false; return false;
} }

View File

@ -23,11 +23,11 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
*/ */
public class AppenOrderSequenceEnumeratorAsyncStreamMergeEngine<TEntity> : AbstractEnumeratorAsyncStreamMergeEngine<TEntity> public class AppenOrderSequenceEnumeratorAsyncStreamMergeEngine<TEntity> : AbstractEnumeratorAsyncStreamMergeEngine<TEntity>
{ {
private readonly PaginationConfig _appendPaginationConfig; private readonly PaginationSequenceConfig _appendPaginationSequenceConfig;
private readonly ICollection<RouteQueryResult<long>> _routeQueryResults; private readonly ICollection<RouteQueryResult<long>> _routeQueryResults;
public AppenOrderSequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext<TEntity> streamMergeContext, PaginationConfig appendPaginationConfig, ICollection<RouteQueryResult<long>> routeQueryResults) : base(streamMergeContext) public AppenOrderSequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext<TEntity> streamMergeContext, PaginationSequenceConfig appendPaginationSequenceConfig, ICollection<RouteQueryResult<long>> routeQueryResults) : base(streamMergeContext)
{ {
_appendPaginationConfig = appendPaginationConfig; _appendPaginationSequenceConfig = appendPaginationSequenceConfig;
_routeQueryResults = routeQueryResults; _routeQueryResults = routeQueryResults;
} }
@ -46,12 +46,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
{ {
Tail = o.RouteResult.ReplaceTables.First().Tail, Tail = o.RouteResult.ReplaceTables.First().Tail,
RouteQueryResult = o RouteQueryResult = o
}).OrderBy(o => o.Tail, _appendPaginationConfig.TailComparer).ToList(); }).OrderBy(o => o.Tail, _appendPaginationSequenceConfig.TailComparer).ToList();
var skipCount = skip; var skipCount = skip;
var sequenceResults = new SequencePaginationList(sortRouteResults.Select(o=>o.RouteQueryResult)).Skip(skip).Take(take).ToList(); var sequenceResults = new SequencePaginationList(sortRouteResults.Select(o=>o.RouteQueryResult)).Skip(skip).Take(take).ToList();
StreamMergeContext.ReSetOrders(new [] { new PropertyOrder(_appendPaginationConfig.PropertyName, true) }); StreamMergeContext.ReSetOrders(new [] { new PropertyOrder(_appendPaginationSequenceConfig.PropertyName, true) });
var enumeratorTasks = sequenceResults.Select(sequenceResult => var enumeratorTasks = sequenceResults.Select(sequenceResult =>
{ {
var newQueryable = CreateAsyncExecuteQueryable(noPaginationQueryable, sequenceResult); var newQueryable = CreateAsyncExecuteQueryable(noPaginationQueryable, sequenceResult);
@ -66,7 +66,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
{ {
var shardingDbContext = StreamMergeContext.CreateDbContext(sequenceResult.RouteResult); var shardingDbContext = StreamMergeContext.CreateDbContext(sequenceResult.RouteResult);
DbContextQueryStore.TryAdd(sequenceResult.RouteResult, shardingDbContext); DbContextQueryStore.TryAdd(sequenceResult.RouteResult, shardingDbContext);
var newQueryable = (IQueryable<TEntity>)(noPaginationQueryable.Skip(sequenceResult.Skip).Take(sequenceResult.Take).OrderWithExpression(new PropertyOrder[]{new PropertyOrder(_appendPaginationConfig.PropertyName,true)})) var newQueryable = (IQueryable<TEntity>)(noPaginationQueryable.Skip(sequenceResult.Skip).Take(sequenceResult.Take).OrderWithExpression(new PropertyOrder[]{new PropertyOrder(_appendPaginationSequenceConfig.PropertyName,true)}))
.ReplaceDbContextQueryable(shardingDbContext); .ReplaceDbContextQueryable(shardingDbContext);
return newQueryable; return newQueryable;
} }

View File

@ -22,12 +22,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
*/ */
public class SequenceEnumeratorAsyncStreamMergeEngine<TEntity> : AbstractEnumeratorAsyncStreamMergeEngine<TEntity> public class SequenceEnumeratorAsyncStreamMergeEngine<TEntity> : AbstractEnumeratorAsyncStreamMergeEngine<TEntity>
{ {
private readonly PaginationConfig _orderPaginationConfig; private readonly PaginationSequenceConfig _orderPaginationSequenceConfig;
private readonly ICollection<RouteQueryResult<long>> _routeQueryResults; private readonly ICollection<RouteQueryResult<long>> _routeQueryResults;
private readonly bool _isAsc; private readonly bool _isAsc;
public SequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext<TEntity> streamMergeContext, PaginationConfig orderPaginationConfig, ICollection<RouteQueryResult<long>> routeQueryResults, bool isAsc) : base(streamMergeContext) public SequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext<TEntity> streamMergeContext, PaginationSequenceConfig orderPaginationSequenceConfig, ICollection<RouteQueryResult<long>> routeQueryResults, bool isAsc) : base(streamMergeContext)
{ {
_orderPaginationConfig = orderPaginationConfig; _orderPaginationSequenceConfig = orderPaginationSequenceConfig;
_routeQueryResults = routeQueryResults; _routeQueryResults = routeQueryResults;
_isAsc = isAsc; _isAsc = isAsc;
} }
@ -47,8 +47,8 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
{ {
Tail = o.RouteResult.ReplaceTables.First().Tail, Tail = o.RouteResult.ReplaceTables.First().Tail,
RouteQueryResult = o RouteQueryResult = o
}).OrderByIf(o => o.Tail, _isAsc, _orderPaginationConfig.TailComparer) }).OrderByIf(o => o.Tail, _isAsc, _orderPaginationSequenceConfig.TailComparer)
.OrderByDescendingIf(o => o.Tail, !_isAsc, _orderPaginationConfig.TailComparer).ToList(); .OrderByDescendingIf(o => o.Tail, !_isAsc, _orderPaginationSequenceConfig.TailComparer).ToList();
var sequenceResults = new SequencePaginationList(sortRouteResults.Select(o => o.RouteQueryResult)).Skip(skip).Take(take).ToList(); var sequenceResults = new SequencePaginationList(sortRouteResults.Select(o => o.RouteQueryResult)).Skip(skip).Take(take).ToList();