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();
Tail = tail;
}
public string FullName => $"{OriginalName}{TailPrefix}{Tail}";
public string OriginalName { get; }
public string TailPrefix =>VirtualTable.ShardingConfig.TailPrefix;
public string Tail { get; }
public Type EntityType => VirtualTable.EntityType;
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>
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)
{

View File

@ -43,8 +43,9 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
/// </summary>
/// <param name="allPhysicTables"></param>
/// <param name="queryable"></param>
/// <param name="isQuery"></param>
/// <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>
/// 根据值路由

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -102,21 +102,21 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors
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))
return typeof(TEntity) == paginationConfig.OrderPropertyInfo.DeclaringType;
if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return typeof(TEntity) == paginationSequenceConfig.OrderPropertyInfo.DeclaringType;
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;
if (paginationConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return typeof(TEntity) == paginationConfig.OrderPropertyInfo.DeclaringType;
if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return typeof(TEntity) == paginationSequenceConfig.OrderPropertyInfo.DeclaringType;
return false;
}

View File

@ -23,11 +23,11 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
*/
public class AppenOrderSequenceEnumeratorAsyncStreamMergeEngine<TEntity> : AbstractEnumeratorAsyncStreamMergeEngine<TEntity>
{
private readonly PaginationConfig _appendPaginationConfig;
private readonly PaginationSequenceConfig _appendPaginationSequenceConfig;
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;
}
@ -46,12 +46,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
{
Tail = o.RouteResult.ReplaceTables.First().Tail,
RouteQueryResult = o
}).OrderBy(o => o.Tail, _appendPaginationConfig.TailComparer).ToList();
}).OrderBy(o => o.Tail, _appendPaginationSequenceConfig.TailComparer).ToList();
var skipCount = skip;
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 newQueryable = CreateAsyncExecuteQueryable(noPaginationQueryable, sequenceResult);
@ -66,7 +66,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
{
var shardingDbContext = StreamMergeContext.CreateDbContext(sequenceResult.RouteResult);
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);
return newQueryable;
}

View File

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