优化默认路由实现

This commit is contained in:
xuejmnet 2021-02-03 08:27:20 +08:00
parent 973c09e0db
commit bd2207241f
16 changed files with 184 additions and 37 deletions

View File

@ -8,9 +8,9 @@
Release | EF Core | .NET Standard | .NET (Core) | Sql Server | Pomelo.EntityFrameworkCore.MySql
--- | --- | --- | --- | --- | ---
[5.x.x.x](https://www.nuget.org/packages/ShardingCore/5.0.0.3) | >= 5.0.x | 2.1 | 3.0+ | >= 2012 | 5.0.0-alpha.2
[3.x.x.x](https://www.nuget.org/packages/ShardingCore/3.0.0.3) | 3.1.10 | 2.0 | 2.0+ | >= 2012 | 3.2.4
[2.x.x.x](https://www.nuget.org/packages/ShardingCore/2.0.0.3) | 2.2.6 | 2.0 | 2.0+ | >= 2008 | 2.2.6
[5.x.x.x](https://www.nuget.org/packages/ShardingCore/5.0.0.4) | >= 5.0.x | 2.1 | 3.0+ | >= 2012 | 5.0.0-alpha.2
[3.x.x.x](https://www.nuget.org/packages/ShardingCore/3.0.0.4) | 3.1.10 | 2.0 | 2.0+ | >= 2012 | 3.2.4
[2.x.x.x](https://www.nuget.org/packages/ShardingCore/2.0.0.4) | 2.2.6 | 2.0 | 2.0+ | >= 2008 | 2.2.6
- [开始](#开始)
@ -39,10 +39,10 @@ Release | EF Core | .NET Standard | .NET (Core) | Sql Server | Pomelo.EntityFra
## 简介
目前该库处于初期阶段,有很多bug也希望各位多多理解,一起努力为.net生态做出一份微薄之力,目前该库支持的分可以进行完全的自定义,基本上可以满足95%以上的
目前该库处于初期阶段,有很多bug也希望各位多多理解,一起努力为.net生态做出一份微薄之力,目前该库支持的分可以进行完全的自定义,基本上可以满足95%以上的
业务需求,唯一的限制就是分表规则必须满足 x+y+z,x表示固定的表名,y表示固定的表名和表后缀之间的联系(可以为空),z表示表后缀,可以按照你自己的任意业务逻辑进行切分,
如:user_0,user_1或者user202101,user202102...当然该库同样适用于多租户模式下的隔离,该库为了支持之后的分库已经重写了之前的union all查询模式,并且支持多种api,
支持多种查询包括join group by max count min avg sum ...等一系列查询,之后可能会添加更多支持,目前该库的使用非常简单,基本上就是针对IQueryable的扩展为了保证
支持多种查询包括```join,group by,max,count,min,avg,sum``` ...等一系列查询,之后可能会添加更多支持,目前该库的使用非常简单,基本上就是针对IQueryable的扩展为了保证
该库的简介目前仅使用该库无法或者说难以实现自动建表,但是只需要配合定时任务该库即可完成24小时无人看管自动管理。该库提供了 [IShardingTableCreator](https://github.com/xuejmnet/sharding-core/blob/main/src/ShardingCore/TableCreator/IShardingTableCreator.cs)
作为建表的依赖,如果需要可以参考 [按天自动建表](https://github.com/xuejmnet/sharding-core/tree/main/samples/Samples.AutoByDate.SqlServer)
@ -80,7 +80,7 @@ Release | EF Core | .NET Standard | .NET (Core) | Sql Server | Pomelo.EntityFra
## 安装
```xml
<PackageReference Include="ShardingCore.SqlServer" Version="5.0.0.3" />
<PackageReference Include="ShardingCore.SqlServer" Version="5.0.0.4" />
```
## 配置

View File

@ -19,9 +19,5 @@ namespace Sample.SqlServer.Shardings
public SysUserModVirtualRoute() : base(3)
{
}
public override List<string> GetAllTails()
{
return new() { "0","1","2"};
}
}
}

View File

@ -13,23 +13,9 @@ namespace Samples.AutoByDate.SqlServer.Shardings
*/
public class SysUserLogByDayVirtualRoute:AbstractSimpleShardingDayKeyDateTimeVirtualRoute<SysUserLogByDay>
{
public override List<string> GetAllTails()
public override DateTime GetBeginTime()
{
var beginTime = DateTime.Now.AddDays(-2);
var tails=new List<string>();
//提前创建表
var nowTimeStamp = DateTime.Now.AddDays(1).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var tail = ShardingKeyToTail(currentTimeStamp);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddDays(1);
}
return tails;
return DateTime.Now.AddDays(-2);
}
}
}

View File

@ -27,6 +27,10 @@ namespace ShardingCore.Helpers
{
return UtcStartTime.AddMilliseconds(timeStamp).AddHours(8);
}
public static long ConvertDateTimeToLong(DateTime time)
{
return (long) (time.AddHours(-8) - UtcStartTime).TotalMilliseconds;
}
/// <summary>
/// 获取当月第一天

View File

@ -16,7 +16,25 @@ namespace ShardingCore.VirtualRoutes.Days
*/
public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualRoute<T>:AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T:class,IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = GetBeginTime();
var tails=new List<string>();
//提前创建表
var nowTimeStamp = DateTime.Now.AddDays(1).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var tail = ShardingKeyToTail(currentTimeStamp);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddDays(1);
}
return tails;
}
protected override string TimeFormatToTail(DateTime time)
{
return $"{time:yyyyMMdd}";

View File

@ -18,6 +18,26 @@ namespace ShardingCore.VirtualRoutes.Days
public abstract class AbstractSimpleShardingDayKeyLongVirtualRoute<T>:AbstractShardingTimeKeyLongVirtualRoute<T> where T:class,IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = GetBeginTime();
var tails=new List<string>();
//提前创建表
var nowTimeStamp = DateTime.Now.AddDays(1).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var currentTimeStampLong=ShardingCoreHelper.ConvertDateTimeToLong(currentTimeStamp);
var tail = TimeFormatToTail(currentTimeStampLong);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddDays(1);
}
return tails;
}
protected override string TimeFormatToTail(long time)
{
var dateTime=ShardingCoreHelper.ConvertLongToDateTime(time);

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
@ -22,6 +23,8 @@ namespace ShardingCore.VirtualRoutes.Mods
protected readonly int Mod;
protected AbstractSimpleShardingModKeyIntVirtualRoute(int mod)
{
if (mod < 1)
throw new ArgumentException($"{nameof(mod)} less than 1 ");
Mod = mod;
}
protected override int ConvertToShardingKey(object shardingKey)
@ -35,6 +38,11 @@ namespace ShardingCore.VirtualRoutes.Mods
return Math.Abs(shardingKeyInt % Mod).ToString();
}
public override List<string> GetAllTails()
{
return Enumerable.Range(0, Mod).Select(o => o.ToString()).ToList();
}
protected override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
@ -39,6 +40,10 @@ namespace ShardingCore.VirtualRoutes.Mods
{
return shardingKey.ToString();
}
public override List<string> GetAllTails()
{
return Enumerable.Range(0, Mod).Select(o => o.ToString()).ToList();
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{

View File

@ -17,6 +17,25 @@ namespace ShardingCore.VirtualRoutes.Months
*/
public abstract class AbstractSimpleShardingMonthKeyDateTimeVirtualRoute<T> : AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T : class, IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = ShardingCoreHelper.GetCurrentMonthFirstDay(GetBeginTime());
var tails=new List<string>();
//提前创建表
var nowTimeStamp =ShardingCoreHelper.GetNextMonthFirstDay(beginTime);
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var tail = ShardingKeyToTail(currentTimeStamp);
tails.Add(tail);
currentTimeStamp = ShardingCoreHelper.GetNextMonthFirstDay(currentTimeStamp);
}
return tails;
}
protected override string TimeFormatToTail(DateTime time)
{
return $"{time:yyyyMM}";

View File

@ -16,6 +16,26 @@ namespace ShardingCore.VirtualRoutes.Months
*/
public abstract class AbstractSimpleShardingMonthKeyLongVirtualRoute<T>:AbstractShardingTimeKeyLongVirtualRoute<T> where T:class,IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = ShardingCoreHelper.GetCurrentMonthFirstDay(GetBeginTime());
var tails=new List<string>();
//提前创建表
var nowTimeStamp =ShardingCoreHelper.GetNextMonthFirstDay(beginTime);
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var currentTimeStampLong = ShardingCoreHelper.ConvertDateTimeToLong(currentTimeStamp);
var tail = ShardingKeyToTail(currentTimeStampLong);
tails.Add(tail);
currentTimeStamp = ShardingCoreHelper.GetNextMonthFirstDay(currentTimeStamp);
}
return tails;
}
protected override string TimeFormatToTail(long time)
{

View File

@ -16,6 +16,25 @@ namespace ShardingCore.VirtualRoutes.Weeks
*/
public abstract class AbstractSimpleShardingWeekKeyDateTimeVirtualRoute<T> : AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T : class, IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = GetBeginTime();
var tails=new List<string>();
//提前创建表
var nowTimeStamp =beginTime.AddDays(7).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var tail = ShardingKeyToTail(currentTimeStamp);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddDays(7);
}
return tails;
}
protected override string TimeFormatToTail(DateTime time)
{
var currentMonday = ShardingCoreHelper.GetCurrentMonday(time);

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
@ -15,6 +16,26 @@ namespace ShardingCore.VirtualRoutes.Weeks
*/
public abstract class AbstractSimpleShardingWeekKeyLongVirtualRoute<T> : AbstractShardingTimeKeyLongVirtualRoute<T> where T : class, IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = GetBeginTime();
var tails=new List<string>();
//提前创建表
var nowTimeStamp =beginTime.AddDays(7).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var currentTimeStampLong = ShardingCoreHelper.ConvertDateTimeToLong(currentTimeStamp);
var tail = ShardingKeyToTail(currentTimeStampLong);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddDays(7);
}
return tails;
}
protected override string TimeFormatToTail(long time)
{
var datetime = ShardingCoreHelper.ConvertLongToDateTime(time);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Years
@ -15,6 +16,25 @@ namespace ShardingCore.VirtualRoutes.Years
*/
public abstract class AbstractSimpleShardingYearKeyDateTimeVirtualRoute<T> : AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T : class, IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = GetBeginTime();
var tails=new List<string>();
//提前创建表
var nowTimeStamp =beginTime.AddYears(1).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var tail = ShardingKeyToTail(currentTimeStamp);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddYears(1);
}
return tails;
}
protected override string TimeFormatToTail(DateTime time)
{
return $"{time:yyyy}";

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
@ -15,6 +16,26 @@ namespace ShardingCore.VirtualRoutes.Years
*/
public abstract class AbstractSimpleShardingYearKeyLongVirtualRoute<T> : AbstractShardingTimeKeyLongVirtualRoute<T> where T : class, IShardingEntity
{
public abstract DateTime GetBeginTime();
public override List<string> GetAllTails()
{
var beginTime = GetBeginTime();
var tails=new List<string>();
//提前创建表
var nowTimeStamp =beginTime.AddYears(1).Date;
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var currentTimeStampLong = ShardingCoreHelper.ConvertDateTimeToLong(currentTimeStamp);
var tail = ShardingKeyToTail(currentTimeStampLong);
tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddYears(1);
}
return tails;
}
protected override string TimeFormatToTail(long time)
{
var datetime = ShardingCoreHelper.ConvertLongToDateTime(time);

View File

@ -20,10 +20,5 @@ namespace ShardingCore.Test50.MySql.Shardings
{
}
public override List<string> GetAllTails()
{
return new() { "0","1","2"};
}
}
}

View File

@ -20,10 +20,5 @@ namespace ShardingCore.Test50.Shardings
{
}
public override List<string> GetAllTails()
{
return new() { "0","1","2"};
}
}
}