移除表达式缓存Expression改成自定义RoutExpression

This commit is contained in:
xuejiaming 2022-06-01 09:12:27 +08:00
parent 56a6efbbd7
commit 318e51bbf0
46 changed files with 458 additions and 413 deletions

View File

@ -76,7 +76,7 @@ namespace Sample.AutoCreateIfPresent
builder.ShardingProperty(o => o.Area);
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)

View File

@ -94,7 +94,7 @@ namespace Sample.AutoCreateIfPresent
builder.ShardingProperty(o => o.CreateTime);
}
public override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyFormat(shardingKey);
switch (shardingOperator)

View File

@ -52,7 +52,7 @@ namespace Sample.SqlServer.Shardings
return $"{dateOfMonth:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -30,7 +30,7 @@ namespace Sample.SqlServer.Shardings
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)

View File

@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingAll.VirtualDataSourceRoutes
return true;
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingAll.VirtualDataSourceRoutes
return true;
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -31,7 +31,7 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes
return true;
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes
return true;
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -17,7 +17,7 @@ namespace Sample.SqlServerShardingTable.VirtualRoutes
builder.ShardingExtraProperty(o => o.Id);
}
public override Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
public override Func<string, bool> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
switch (shardingPropertyName)
{
@ -26,7 +26,7 @@ namespace Sample.SqlServerShardingTable.VirtualRoutes
}
}
private Expression<Func<string, bool>> GetIdRouteFilter(object shardingKey,
private Func<string, bool> GetIdRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator)
{
//解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以直接返回false因为是and链接所以直接就没有结果了//return tail => false;

View File

@ -48,7 +48,7 @@ namespace Sample.SqlServerShardingTable.VirtualRoutes
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
//因为hash路由仅支持等于所以仅仅只需要写等于的情况
var t = ShardingKeyToTail(shardingKey);

View File

@ -29,7 +29,8 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions
//获取路由后缀表达式
var routeParseExpression = ShardingUtil.GetRouteParseExpression(queryable, EntityMetadata, GetRouteFilter, false);
//表达式缓存编译
var filter = CachingCompile(routeParseExpression);
// var filter = CachingCompile(routeParseExpression);
var filter = routeParseExpression.GetRoutePredicate();
//通过编译结果进行过滤
var dataSources = allDataSourceNames.Where(o => filter(o)).ToList();
return dataSources;
@ -43,7 +44,7 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions
/// <param name="shardingOperator">操作</param>
/// <param name="shardingPropertyName">操作</param>
/// <returns>如果返回true表示返回该表 第一个参数 tail 第二参数是否返回该物理表</returns>
public virtual Expression<Func<string, bool>> GetRouteFilter(object shardingKey,
public virtual Func<string, bool> GetRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
if (EntityMetadata.IsMainShardingDataSourceKey(shardingPropertyName))
@ -56,10 +57,10 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions
}
}
public abstract Expression<Func<string, bool>> GetRouteToFilter(TKey shardingKey,
public abstract Func<string, bool> GetRouteToFilter(TKey shardingKey,
ShardingOperatorEnum shardingOperator);
public virtual Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey,
public virtual Func<string, bool> GetExtraRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
throw new NotImplementedException(shardingPropertyName);

View File

@ -0,0 +1,40 @@
using System;
namespace ShardingCore.Core.VirtualRoutes
{
public class RoutePredicateExpression
{
private static readonly Func<string, bool> _defaultRoutePredicate = tail => true;
private static readonly Func<string, bool> _defaultFalseRoutePredicate = tail => false;
private readonly Func<string, bool> _routePredicate;
public RoutePredicateExpression():this(_defaultRoutePredicate)
{
}
public static RoutePredicateExpression Default => new RoutePredicateExpression();
public static RoutePredicateExpression DefaultFalse => new RoutePredicateExpression(_defaultFalseRoutePredicate);
public RoutePredicateExpression(Func<string, bool> routePredicate)
{
_routePredicate = routePredicate??throw new ArgumentNullException(nameof(routePredicate));
}
public RoutePredicateExpression And(RoutePredicateExpression routePredicateExpression)
{
var routePredicate = routePredicateExpression.GetRoutePredicate();
Func<string, bool> func = tail => _routePredicate(tail)&&routePredicate(tail);
return new RoutePredicateExpression(func);
}
public RoutePredicateExpression Or(RoutePredicateExpression routePredicateExpression)
{
var routePredicate = routePredicateExpression.GetRoutePredicate();
Func<string, bool> func = tail => _routePredicate(tail)||routePredicate(tail);
return new RoutePredicateExpression(func);
}
public Func<string, bool> GetRoutePredicate()
{
return _routePredicate;
}
}
}

View File

@ -23,7 +23,8 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
//获取路由后缀表达式
var routeParseExpression = ShardingUtil.GetRouteParseExpression(queryable, EntityMetadata, GetRouteFilter,true);
//表达式缓存编译
var filter =CachingCompile(routeParseExpression);
// var filter =CachingCompile(routeParseExpression);
var filter =routeParseExpression.GetRoutePredicate();
//通过编译结果进行过滤
var physicTables = allPhysicTables.Where(o => filter(o.Tail)).ToList();
return physicTables;
@ -37,7 +38,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
/// <param name="shardingOperator">操作</param>
/// <param name="shardingPropertyName">分表字段</param>
/// <returns>如果返回true表示返回该表 第一个参数 tail 第二参数是否返回该物理表</returns>
public virtual Expression<Func<string, bool>> GetRouteFilter(object shardingKey,
public virtual Func<string, bool> GetRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
if (EntityMetadata.IsMainShardingTableKey(shardingPropertyName))
@ -50,10 +51,10 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
}
}
public abstract Expression<Func<string, bool>> GetRouteToFilter(TKey shardingKey,
public abstract Func<string, bool> GetRouteToFilter(TKey shardingKey,
ShardingOperatorEnum shardingOperator);
public virtual Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey,
public virtual Func<string, bool> GetExtraRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
throw new NotImplementedException(shardingPropertyName);

View File

@ -29,24 +29,25 @@ namespace ShardingCore.Core.Internal.Visitors
{
private readonly EntityMetadata _entityMetadata;
private readonly Func<object, ShardingOperatorEnum, string, Expression<Func<string, bool>>> _keyToTailWithFilter;
private readonly Func<object, ShardingOperatorEnum, string, Func<string, bool>> _keyToTailWithFilter;
/// <summary>
/// 是否是分表路由
/// </summary>
private readonly bool _shardingTableRoute;
private Expression<Func<string, bool>> _where = x => true;
// private Expression<Func<string, bool>> _where = x => true;
private LambdaExpression _entityLambdaExpression;
private readonly ShardingPredicateResult _noShardingPredicateResult = new ShardingPredicateResult(false, null);
private bool isIgnoreQueryFilter;
private RoutePredicateExpression _where = RoutePredicateExpression.Default;
public QueryableRouteShardingTableDiscoverVisitor(EntityMetadata entityMetadata, Func<object, ShardingOperatorEnum, string, Expression<Func<string, bool>>> keyToTailWithFilter, bool shardingTableRoute)
public QueryableRouteShardingTableDiscoverVisitor(EntityMetadata entityMetadata, Func<object, ShardingOperatorEnum, string, Func<string, bool>> keyToTailWithFilter, bool shardingTableRoute)
{
_entityMetadata = entityMetadata;
_keyToTailWithFilter = keyToTailWithFilter;
_shardingTableRoute = shardingTableRoute;
}
public Expression<Func<string, bool>> GetRouteParseExpression()
public RoutePredicateExpression GetRouteParseExpression()
{
if (_entityMetadata.QueryFilterExpression != null && !isIgnoreQueryFilter)
@ -185,7 +186,7 @@ namespace ShardingCore.Core.Internal.Visitors
}
private Expression<Func<string, bool>> Resolve(Expression expression)
private RoutePredicateExpression Resolve(Expression expression)
{
if (expression is LambdaExpression lambda)
{
@ -211,10 +212,10 @@ namespace ShardingCore.Core.Internal.Visitors
{
return ResolveInFunc(methodCallExpression, true);
}
return o => true;
return RoutePredicateExpression.Default;
}
private Expression<Func<string, bool>> ResolveInFunc(MethodCallExpression methodCallExpression, bool @in)
private RoutePredicateExpression ResolveInFunc(MethodCallExpression methodCallExpression, bool @in)
{
if (methodCallExpression.IsEnumerableContains(methodCallExpression.Method.Name))
{
@ -244,9 +245,8 @@ namespace ShardingCore.Core.Internal.Visitors
if (arrayObject != null)
{
Expression<Func<string, bool>> contains = x => false;
if (!@in)
contains = x => true;
var contains=@in ? RoutePredicateExpression.DefaultFalse : RoutePredicateExpression.Default;
if (arrayObject is IEnumerable enumerableObj)
{
@ -254,9 +254,9 @@ namespace ShardingCore.Core.Internal.Visitors
{
var eq = _keyToTailWithFilter(shardingValue, @in ? ShardingOperatorEnum.Equal : ShardingOperatorEnum.NotEqual, shardingPredicateResult.ShardingPropertyName);
if (@in)
contains = contains.Or(eq);
contains = contains.Or(new RoutePredicateExpression(eq));
else
contains = contains.And(eq);
contains = contains.And(new RoutePredicateExpression(eq));
}
}
return contains;
@ -273,7 +273,8 @@ namespace ShardingCore.Core.Internal.Visitors
if (methodCallExpression.Object is ConstantExpression constantExpression)
{
var shardingValue = constantExpression.Value;
return _keyToTailWithFilter(shardingValue, ShardingOperatorEnum.Equal, shardingPredicateResult.ShardingPropertyName);
var keyToTailWithFilter = _keyToTailWithFilter(shardingValue, ShardingOperatorEnum.Equal, shardingPredicateResult.ShardingPropertyName);
return new RoutePredicateExpression(keyToTailWithFilter);
}
}
else
@ -294,7 +295,8 @@ namespace ShardingCore.Core.Internal.Visitors
if (shardingValue != default)
{
return _keyToTailWithFilter(shardingValue, ShardingOperatorEnum.Equal, shardingPredicateResult.ShardingPropertyName);
var keyToTailWithFilter = _keyToTailWithFilter(shardingValue, ShardingOperatorEnum.Equal, shardingPredicateResult.ShardingPropertyName);
return new RoutePredicateExpression(keyToTailWithFilter);
}
}
}
@ -306,7 +308,7 @@ namespace ShardingCore.Core.Internal.Visitors
//}
//var shardingKeyValue = GetShardingKeyValue(methodCallExpression);
return x => true;
return RoutePredicateExpression.Default;
}
private ShardingOperatorEnum GetParseCompareShardingOperatorEnum(bool conditionOnRight, ExpressionType expressionType, int compare)
@ -354,7 +356,7 @@ namespace ShardingCore.Core.Internal.Visitors
}
return ShardingOperatorEnum.UnKnown;
}
private Expression<Func<string, bool>> ParseCompare(MethodCallExpression methodCallExpression, Expression left,Expression right, ExpressionType expressionType, int compare)
private RoutePredicateExpression ParseCompare(MethodCallExpression methodCallExpression, Expression left,Expression right, ExpressionType expressionType, int compare)
{
if (left.Type == right.Type)
{
@ -363,10 +365,10 @@ namespace ShardingCore.Core.Internal.Visitors
return ParseCondition0(left, right, conditionOnRight => GetParseCompareShardingOperatorEnum(conditionOnRight, expressionType, compare));
}
}
return x => true;
return RoutePredicateExpression.Default;
}
private Expression<Func<string, bool>> ParseCondition0(Expression left, Expression right,Func<bool,ShardingOperatorEnum> shardingOperatorFunc)
private RoutePredicateExpression ParseCondition0(Expression left, Expression right,Func<bool,ShardingOperatorEnum> shardingOperatorFunc)
{
bool conditionOnRight = false;
@ -383,7 +385,7 @@ namespace ShardingCore.Core.Internal.Visitors
value = GetExpressionValue(right);
}
else
return x => true;
return RoutePredicateExpression.Default;
}
else if (IsShardingKey(right, out var predicateRightResult) && IsConstantOrMember(left))
{
@ -394,20 +396,20 @@ namespace ShardingCore.Core.Internal.Visitors
value = GetExpressionValue(left);
}
else
return x => true;
return RoutePredicateExpression.Default;
}
else
return x => true;
return RoutePredicateExpression.Default;
var op = shardingOperatorFunc(conditionOnRight);
if (shardingPropertyName == null || value == default)
return x => true;
return RoutePredicateExpression.Default;
return _keyToTailWithFilter(value, op, shardingPropertyName);
return new RoutePredicateExpression( _keyToTailWithFilter(value, op, shardingPropertyName));
}
private Expression<Func<string, bool>> ParseCondition(BinaryExpression binaryExpression)
private RoutePredicateExpression ParseCondition(BinaryExpression binaryExpression)
{
if (binaryExpression.IsNamedComparison(out var methodCallExpression))
{
@ -435,13 +437,13 @@ namespace ShardingCore.Core.Internal.Visitors
return op;
});
}
return x => true;
return RoutePredicateExpression.Default;
}
private Expression<Func<string, bool>> ParseGetWhere(BinaryExpression binaryExpression)
private RoutePredicateExpression ParseGetWhere(BinaryExpression binaryExpression)
{
Expression<Func<string, bool>> left = x => true;
Expression<Func<string, bool>> right = x => true;
RoutePredicateExpression left = RoutePredicateExpression.Default;
RoutePredicateExpression right = RoutePredicateExpression.Default;
//递归获取
if (binaryExpression.Left is BinaryExpression binaryExpression1)

View File

@ -34,6 +34,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Core\VirtualRoutes\RouteConditions" />
<Folder Include="Sharding\Visitors\GroupBys" />
</ItemGroup>
</Project>

View File

@ -29,7 +29,7 @@ namespace ShardingCore.Utils
{
///// <summary>
///// 分库路由过滤
///// <EFBFBD>ֿ<EFBFBD>·<EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>
///// </summary>
///// <typeparam name="TKey"></typeparam>
///// <param name="queryable"></param>
@ -46,14 +46,14 @@ namespace ShardingCore.Utils
// return visitor.GetDataSourceFilter();
//}
/// <summary>
/// 分表路由过滤
/// <EFBFBD>ֱ<EFBFBD>·<EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="queryable"></param>
/// <param name="entityMetadata"></param>
/// <param name="keyToTailExpression"></param>
/// <param name="shardingTableRoute">sharding table or data source</param>
/// <returns></returns>
public static Expression<Func<string, bool>> GetRouteParseExpression(IQueryable queryable, EntityMetadata entityMetadata, Func<object, ShardingOperatorEnum,string, Expression<Func<string, bool>>> keyToTailExpression,bool shardingTableRoute)
public static RoutePredicateExpression GetRouteParseExpression(IQueryable queryable, EntityMetadata entityMetadata, Func<object, ShardingOperatorEnum,string, Func<string, bool>> keyToTailExpression,bool shardingTableRoute)
{
QueryableRouteShardingTableDiscoverVisitor visitor = new QueryableRouteShardingTableDiscoverVisitor(entityMetadata, keyToTailExpression, shardingTableRoute);
@ -64,7 +64,7 @@ namespace ShardingCore.Utils
}
///// <summary>
///// 获取本次查询的所有涉及到的对象
///// <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>β<EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD>
///// </summary>
///// <param name="queryable"></param>
///// <param name="dbContextType"></param>
@ -84,7 +84,7 @@ namespace ShardingCore.Utils
// return visitor.GetQueryEntities();
//}
///// <summary>
///// 获取次需要编译的表达式解析信息
///// <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
///// </summary>
///// <param name="expression"></param>
///// <param name="dbContextType"></param>

View File

@ -49,7 +49,7 @@ namespace ShardingCore.VirtualRoutes.Days
return $"{time:yyyyMMdd}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -41,7 +41,7 @@ namespace ShardingCore.VirtualRoutes.Days
return $"{dateTime:yyyyMMdd}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -48,7 +48,7 @@ namespace ShardingCore.VirtualRoutes.Mods
return Enumerable.Range(0, Mod).Select(o => o.ToString().PadLeft(TailLength, PaddingChar)).ToList();
}
public override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);

View File

@ -65,7 +65,7 @@ namespace ShardingCore.VirtualRoutes.Mods
/// <param name="shardingKey"></param>
/// <param name="shardingOperator"></param>
/// <returns></returns>
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)

View File

@ -39,7 +39,7 @@ namespace ShardingCore.VirtualRoutes.Months
return $"{time:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -41,7 +41,7 @@ namespace ShardingCore.VirtualRoutes.Months
var datetime = ShardingCoreHelper.ConvertLongToDateTime(time);
return $"{datetime:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -41,7 +41,7 @@ namespace ShardingCore.VirtualRoutes.Weeks
return $"{currentMonday:yyyyMM}{currentMonday:dd}_{currentSunday:dd}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -50,7 +50,7 @@ namespace ShardingCore.VirtualRoutes.Weeks
return $"{currentMonday:yyyyMM}{currentMonday:dd}_{currentSunday:dd}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -37,7 +37,7 @@ namespace ShardingCore.VirtualRoutes.Years
{
return $"{time:yyyy}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -60,7 +60,7 @@ namespace ShardingCore.VirtualRoutes.Years
/// <param name="shardingKey"></param>
/// <param name="shardingOperator"></param>
/// <returns>当传入表后缀你告诉框架这个后缀是否需要被返回,分片字段如何筛选出后缀</returns>
public override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -82,47 +82,47 @@ namespace ShardingCore.Test
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var xxxx = "202102";
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
// var xxxx = "202102";
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public async Task GenericTest()
@ -825,7 +825,7 @@ namespace ShardingCore.Test
// var ids = new List<string>(){ "200", "300" };
// List<SysUserSalary> result = new List<SysUserSalary>(ids.Count);
// var routeFilter = new List<SysUserSalary>().AsQueryable().Where(o => ids.Contains(o.UserId));
// //获取的路由时间倒序
// //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>
// var tableRouteResults = _tableRouteRuleEngineFactory.Route(routeFilter)
// .Select(o => o.ReplaceTables.First().Tail).OrderByDescending(o => o).ToList();
// foreach (var tableRouteResult in tableRouteResults)
@ -869,7 +869,7 @@ namespace ShardingCore.Test
public async Task OrderFirstTest()
{
var threeMonth = new DateTime(2021, 3, 1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//第59条 1月31天2月28天
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//<EFBFBD><EFBFBD>59<EFBFBD><EFBFBD> 1<><31>31<33><31>2<EFBFBD><32>28<32><38>
Assert.NotNull(order);
Assert.Equal(59, order.Money);
Assert.Equal("C", order.Area);
@ -1164,7 +1164,7 @@ namespace ShardingCore.Test
[Fact]
public async Task OrderReadWrite()
{
//切换到只读数据库只读数据库又只配置了A数据源读取B数据源
//<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ⣬ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ȡB<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
_virtualDbContext.ReadWriteSeparationReadOnly();
var list = await _virtualDbContext.Set<Order>().Where(o => o.Money == 1).ToListAsync();
Assert.Equal(2, list.Count);

View File

@ -69,47 +69,47 @@ namespace ShardingCore.Test
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public void GenericTest()

View File

@ -17,7 +17,7 @@ namespace ShardingCore.Test.Shardings
builder.ShardingExtraProperty(o => o.Id);
}
public override Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
public override Func<string, bool> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
switch (shardingPropertyName)
{
@ -26,7 +26,7 @@ namespace ShardingCore.Test.Shardings
}
}
private Expression<Func<string, bool>> GetIdRouteFilter(object shardingKey,
private Func<string, bool> GetIdRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator)
{
//解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以随机查询一张表

View File

@ -42,7 +42,7 @@ namespace ShardingCore.Test.Shardings
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -46,7 +46,7 @@ namespace ShardingCore.Test.Shardings
return $"{dateOfMonth:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -76,47 +76,47 @@ namespace ShardingCore.Test2x
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public async Task GenericTest()

View File

@ -69,47 +69,47 @@ namespace ShardingCore.Test2x
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public void GenericTest()

View File

@ -17,7 +17,7 @@ namespace ShardingCore.Test2x.Shardings
builder.ShardingExtraProperty(o => o.Id);
}
public override Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
public override Func<string, bool> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
switch (shardingPropertyName)
{
@ -26,7 +26,7 @@ namespace ShardingCore.Test2x.Shardings
}
}
private Expression<Func<string, bool>> GetIdRouteFilter(object shardingKey,
private Func<string, bool> GetIdRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator)
{
//解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以随机查询一张表

View File

@ -42,7 +42,7 @@ namespace ShardingCore.Test2x.Shardings
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -45,7 +45,7 @@ namespace ShardingCore.Test2x.Shardings
return $"{dateOfMonth:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -76,47 +76,47 @@ namespace ShardingCore.Test3x
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public async Task GenericTest()
@ -706,7 +706,7 @@ namespace ShardingCore.Test3x
public async Task OrderFirstTest()
{
var threeMonth = new DateTime(2021, 3, 1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//第59条 1月31天2月28天
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//<EFBFBD><EFBFBD>59<EFBFBD><EFBFBD> 1<><31>31<33><31>2<EFBFBD><32>28<32><38>
Assert.NotNull(order);
Assert.Equal(59, order.Money);
Assert.Equal("C", order.Area);
@ -1001,7 +1001,7 @@ namespace ShardingCore.Test3x
[Fact]
public async Task OrderReadWrite()
{
//切换到只读数据库只读数据库又只配置了A数据源读取B数据源
//<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ⣬ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ȡB<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
_virtualDbContext.ReadWriteSeparationReadOnly();
var list = await _virtualDbContext.Set<Order>().Where(o => o.Money == 1).ToListAsync();
Assert.Equal(2, list.Count);

View File

@ -70,47 +70,47 @@ namespace ShardingCore.Test3x
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public void GenericTest()

View File

@ -17,7 +17,7 @@ namespace ShardingCore.Test3x.Shardings
builder.ShardingExtraProperty(o => o.Id);
}
public override Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
public override Func<string, bool> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
switch (shardingPropertyName)
{
@ -26,7 +26,7 @@ namespace ShardingCore.Test3x.Shardings
}
}
private Expression<Func<string, bool>> GetIdRouteFilter(object shardingKey,
private Func<string, bool> GetIdRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator)
{
//解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以随机查询一张表

View File

@ -42,7 +42,7 @@ namespace ShardingCore.Test3x.Shardings
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -46,7 +46,7 @@ namespace ShardingCore.Test3x.Shardings
return $"{dateOfMonth:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -76,47 +76,47 @@ namespace ShardingCore.Test5x
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public async Task GenericTest()
@ -706,7 +706,7 @@ namespace ShardingCore.Test5x
public async Task OrderFirstTest()
{
var threeMonth = new DateTime(2021, 3, 1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//第59条 1月31天2月28天
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//<EFBFBD><EFBFBD>59<EFBFBD><EFBFBD> 1<><31>31<33><31>2<EFBFBD><32>28<32><38>
Assert.NotNull(order);
Assert.Equal(59, order.Money);
Assert.Equal("C", order.Area);
@ -1001,7 +1001,7 @@ namespace ShardingCore.Test5x
[Fact]
public async Task OrderReadWrite()
{
//切换到只读数据库只读数据库又只配置了A数据源读取B数据源
//<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ⣬ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ȡB<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
_virtualDbContext.ReadWriteSeparationReadOnly();
var list = await _virtualDbContext.Set<Order>().Where(o => o.Money == 1).ToListAsync();
Assert.Equal(2, list.Count);

View File

@ -71,47 +71,47 @@ namespace ShardingCore.Test5x
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
}
[Fact]
public void RouteParseCompileCacheTest()
{
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
(i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
// [Fact]
// public void RouteParseCompileCacheTest()
// {
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
//
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
//
// }
[Fact]
public void GenericTest()
{

View File

@ -17,7 +17,7 @@ namespace ShardingCore.Test5x.Shardings
builder.ShardingExtraProperty(o => o.Id);
}
public override Expression<Func<string, bool>> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
public override Func<string, bool> GetExtraRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
switch (shardingPropertyName)
{
@ -26,7 +26,7 @@ namespace ShardingCore.Test5x.Shardings
}
}
private Expression<Func<string, bool>> GetIdRouteFilter(object shardingKey,
private Func<string, bool> GetIdRouteFilter(object shardingKey,
ShardingOperatorEnum shardingOperator)
{
//解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以随机查询一张表

View File

@ -42,7 +42,7 @@ namespace ShardingCore.Test5x.Shardings
}
public override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);

View File

@ -45,7 +45,7 @@ namespace ShardingCore.Test5x.Shardings
return $"{dateOfMonth:yyyyMM}";
}
public override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)