diff --git a/src/ShardingCore/Core/PhysicTables/DefaultPhysicTable.cs b/src/ShardingCore/Core/PhysicTables/DefaultPhysicTable.cs index bf1c205b..55d6d38f 100644 --- a/src/ShardingCore/Core/PhysicTables/DefaultPhysicTable.cs +++ b/src/ShardingCore/Core/PhysicTables/DefaultPhysicTable.cs @@ -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 } } \ No newline at end of file diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingFilterVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingFilterVirtualTableRoute.cs index 7bc34e54..e8f23dad 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingFilterVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingFilterVirtualTableRoute.cs @@ -35,8 +35,13 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions /// 启用断言路由 /// protected virtual bool EnableAssertRoute => false; - public override List RouteWithPredicate(List allPhysicTables, IQueryable queryable) + public override List RouteWithPredicate(List allPhysicTables, IQueryable queryable,bool isQuery) { + if (!isQuery) + { + //后拦截器 + return AfterPhysicTableFilter(allPhysicTables,DoRouteWithPredicate(allPhysicTables,queryable)); + } //强制路由不经过断言 if (EnableHintRoute) { diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractVirtualTableRoute.cs index b0b3de47..b9c042e5 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractVirtualTableRoute.cs @@ -43,8 +43,9 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions /// /// /// + /// /// - public abstract List RouteWithPredicate(List allPhysicTables, IQueryable queryable); + public abstract List RouteWithPredicate(List allPhysicTables, IQueryable queryable,bool isQuery); /// /// 根据值路由 diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs index 372e491d..2281c9dd 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs @@ -25,8 +25,9 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes /// /// /// + /// /// - List RouteWithPredicate(List allPhysicTables,IQueryable queryable); + List RouteWithPredicate(List allPhysicTables,IQueryable queryable,bool isQuery); /// /// 根据值进行路由 diff --git a/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs b/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs index 0cf79390..a27d8c1e 100644 --- a/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs +++ b/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs @@ -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 _physicTables = new List(); + private readonly ConcurrentDictionary _physicTables = new ConcurrentDictionary(); public OneDbVirtualTable(IVirtualTableRoute virtualTableRoute) { @@ -50,16 +51,16 @@ namespace ShardingCore.Core.VirtualTables public List GetAllPhysicTables() { - return _physicTables; + return _physicTables.Keys.ToList(); } public List 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((Expression>) tableRouteConfig.GetPredicate())); + return route.RouteWithPredicate(GetAllPhysicTables(), new EnumerableQuery((Expression>) 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(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 GetTaleAllTails() { - return _physicTables.Select(o => o.Tail).ToList(); + return _physicTables.Keys.Select(o => o.Tail).ToList(); } public IVirtualTableRoute GetVirtualRoute() diff --git a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationMetadata.cs b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationMetadata.cs index afb4e8f9..c59ae3f2 100644 --- a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationMetadata.cs +++ b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationMetadata.cs @@ -17,7 +17,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations /// public class PaginationMetadata { - public ISet PaginationConfigs = new HashSet(); + public ISet PaginationConfigs = new HashSet(); /// /// 反向排序因子 diff --git a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationOrderPropertyBuilder.cs b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationOrderPropertyBuilder.cs index 784fae46..71410702 100644 --- a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationOrderPropertyBuilder.cs +++ b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationOrderPropertyBuilder.cs @@ -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); } /// @@ -28,7 +28,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations public PaginationOrderPropertyBuilder UseTailCompare(IComparer tailComparer) { - _paginationConfig.TailComparer= tailComparer ?? throw new ArgumentException(nameof(tailComparer)); + _paginationSequenceConfig.TailComparer= tailComparer ?? throw new ArgumentException(nameof(tailComparer)); return this; } /// @@ -38,7 +38,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations /// public PaginationOrderPropertyBuilder UseQueryMatch(PaginationMatchEnum paginationMatchEnum) { - _paginationConfig.PaginationMatchEnum = paginationMatchEnum; + _paginationSequenceConfig.PaginationMatchEnum = paginationMatchEnum; return this; } /// @@ -48,7 +48,7 @@ namespace ShardingCore.Sharding.PaginationConfigurations /// public PaginationOrderPropertyBuilder UseAppendIfOrderNone(int order=0) { - _paginationConfig.AppendOrder = order; + _paginationSequenceConfig.AppendOrder = order; return this; } } diff --git a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationConfig.cs b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationSequenceConfig.cs similarity index 78% rename from src/ShardingCore/Sharding/PaginationConfigurations/PaginationConfig.cs rename to src/ShardingCore/Sharding/PaginationConfigurations/PaginationSequenceConfig.cs index 4f20f7ee..5217de10 100644 --- a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationConfig.cs +++ b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationSequenceConfig.cs @@ -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 tailComparer=null) + public PaginationSequenceConfig(LambdaExpression orderPropertyExpression, PaginationMatchEnum paginationMatchEnum= PaginationMatchEnum.Owner, IComparer tailComparer=null) { - OrderPropertyExpression = orderPropertyExpression; OrderPropertyInfo = orderPropertyExpression.GetPropertyAccess(); PropertyName = OrderPropertyInfo.Name; PaginationMatchEnum = paginationMatchEnum; TailComparer = tailComparer ?? Comparer.Default; + SequenceTails = new HashSet(); } - public LambdaExpression OrderPropertyExpression { get; set; } public IComparer TailComparer { get; set; } public PaginationMatchEnum PaginationMatchEnum { get; set; } public PropertyInfo OrderPropertyInfo { get; set; } @@ -40,9 +39,11 @@ namespace ShardingCore.Sharding.PaginationConfigurations /// public int AppendOrder { get; set; } = -1; public string PropertyName { get;} + + public ISet 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() diff --git a/src/ShardingCore/Sharding/ShardingQueryExecutors/EnumeratorShardingQueryExecutor.cs b/src/ShardingCore/Sharding/ShardingQueryExecutors/EnumeratorShardingQueryExecutor.cs index ea2db0f9..39dcb56c 100644 --- a/src/ShardingCore/Sharding/ShardingQueryExecutors/EnumeratorShardingQueryExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingQueryExecutors/EnumeratorShardingQueryExecutor.cs @@ -102,21 +102,21 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors return new DefaultShardingEnumeratorAsyncStreamMergeEngine(_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; } diff --git a/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/AppenOrderSequenceEnumeratorAsyncStreamMergeEngine.cs b/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/AppenOrderSequenceEnumeratorAsyncStreamMergeEngine.cs index 5f2a3104..1e167624 100644 --- a/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/AppenOrderSequenceEnumeratorAsyncStreamMergeEngine.cs +++ b/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/AppenOrderSequenceEnumeratorAsyncStreamMergeEngine.cs @@ -23,11 +23,11 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines. */ public class AppenOrderSequenceEnumeratorAsyncStreamMergeEngine : AbstractEnumeratorAsyncStreamMergeEngine { - private readonly PaginationConfig _appendPaginationConfig; + private readonly PaginationSequenceConfig _appendPaginationSequenceConfig; private readonly ICollection> _routeQueryResults; - public AppenOrderSequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext streamMergeContext, PaginationConfig appendPaginationConfig, ICollection> routeQueryResults) : base(streamMergeContext) + public AppenOrderSequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext streamMergeContext, PaginationSequenceConfig appendPaginationSequenceConfig, ICollection> 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)(noPaginationQueryable.Skip(sequenceResult.Skip).Take(sequenceResult.Take).OrderWithExpression(new PropertyOrder[]{new PropertyOrder(_appendPaginationConfig.PropertyName,true)})) + var newQueryable = (IQueryable)(noPaginationQueryable.Skip(sequenceResult.Skip).Take(sequenceResult.Take).OrderWithExpression(new PropertyOrder[]{new PropertyOrder(_appendPaginationSequenceConfig.PropertyName,true)})) .ReplaceDbContextQueryable(shardingDbContext); return newQueryable; } diff --git a/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/SequenceEnumeratorAsyncStreamMergeEngine.cs b/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/SequenceEnumeratorAsyncStreamMergeEngine.cs index 918169be..e9cd661e 100644 --- a/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/SequenceEnumeratorAsyncStreamMergeEngine.cs +++ b/src/ShardingCore/Sharding/StreamMergeEngines/EnumeratorStreamMergeEngines/EnumeratorAsync/SequenceEnumeratorAsyncStreamMergeEngine.cs @@ -22,12 +22,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines. */ public class SequenceEnumeratorAsyncStreamMergeEngine : AbstractEnumeratorAsyncStreamMergeEngine { - private readonly PaginationConfig _orderPaginationConfig; + private readonly PaginationSequenceConfig _orderPaginationSequenceConfig; private readonly ICollection> _routeQueryResults; private readonly bool _isAsc; - public SequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext streamMergeContext, PaginationConfig orderPaginationConfig, ICollection> routeQueryResults, bool isAsc) : base(streamMergeContext) + public SequenceEnumeratorAsyncStreamMergeEngine(StreamMergeContext streamMergeContext, PaginationSequenceConfig orderPaginationSequenceConfig, ICollection> 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();