diff --git a/samples/Sample.SqlServer/Startup.cs b/samples/Sample.SqlServer/Startup.cs index d1b3be40..ad70c7c4 100644 --- a/samples/Sample.SqlServer/Startup.cs +++ b/samples/Sample.SqlServer/Startup.cs @@ -10,6 +10,7 @@ using ShardingCore; using System; using System.Collections.Generic; using System.Diagnostics; +using ShardingCore.Helpers; using ShardingCore.Sharding.ReadWriteConfigurations; namespace Sample.SqlServer diff --git a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs index 69cc9be3..feca356f 100644 --- a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs @@ -91,18 +91,11 @@ namespace ShardingCore.VirtualRoutes.Abstractions /// public virtual int IncrementMinutes => 10; - public string[] GetJobCronExpressions() + public virtual string[] GetJobCronExpressions() { - var cronExpressions = GetCronExpressions(); - var compensateCronExpressions = GetCompensateCronExpressions(); - return cronExpressions.Concat(compensateCronExpressions).Distinct().ToArray(); + return GetCronExpressions(); } /// - /// 补偿cron防止提前创建后没有添加tail到内存中从而无法识别 - /// - /// - public abstract string[] GetCompensateCronExpressions(); - /// /// 重写改方法后请一起重写IncrementMinutes值,比如你按月分表但是你设置cron表达式为月中的时候建表, /// 那么会在月中的时候 DateTime.Now.AddMinutes(IncrementMinutes);来获取tail会导致还是当月的所以不会建表 /// diff --git a/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute.cs index 0c9e9d58..f7588e9b 100644 --- a/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute.cs @@ -2,6 +2,7 @@ using ShardingCore.Core.VirtualRoutes; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; namespace ShardingCore.VirtualRoutes.Days { @@ -11,13 +12,16 @@ namespace ShardingCore.VirtualRoutes.Days * @Date: Wednesday, 27 January 2021 08:41:05 * @Email: 326308290@qq.com */ - public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute:AbstractShardingTimeKeyDateTimeVirtualTableRoute where TEntity:class + public abstract class + AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute : + AbstractShardingTimeKeyDateTimeVirtualTableRoute where TEntity : class { /// /// begin time use fixed time eg.new DateTime(20xx,xx,xx) /// /// public abstract DateTime GetBeginTime(); + /// /// 这个方法会在程序启动的时候被调用,后续整个生命周期将不会被调用,仅用来告诉框架启动的时候有多少张TEntity对象的后缀表, /// 然后会在启动的时候添加到 @@ -26,8 +30,8 @@ namespace ShardingCore.VirtualRoutes.Days protected override List CalcTailsOnStart() { var beginTime = GetBeginTime().Date; - - var tails=new List(); + + var tails = new List(); //提前创建表 var nowTimeStamp = DateTime.Now.Date; if (beginTime > nowTimeStamp) @@ -39,8 +43,10 @@ namespace ShardingCore.VirtualRoutes.Days tails.Add(tail); currentTimeStamp = currentTimeStamp.AddDays(1); } + return tails; } + protected override string TimeFormatToTail(DateTime time) { return $"{time:yyyyMMdd}"; @@ -53,22 +59,22 @@ namespace ShardingCore.VirtualRoutes.Days { case ShardingOperatorEnum.GreaterThan: case ShardingOperatorEnum.GreaterThanOrEqual: - return tail =>String.Compare(tail, t, StringComparison.Ordinal) >= 0; + return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0; case ShardingOperatorEnum.LessThan: { var shardingKeyDate = shardingKey.Date; //处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回 if (shardingKeyDate == shardingKey) - return tail =>String.Compare(tail, t, StringComparison.Ordinal) < 0; - return tail =>String.Compare(tail, t, StringComparison.Ordinal) <= 0; + return tail => String.Compare(tail, t, StringComparison.Ordinal) < 0; + return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0; } case ShardingOperatorEnum.LessThanOrEqual: - return tail =>String.Compare(tail, t, StringComparison.Ordinal) <= 0; + return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0; case ShardingOperatorEnum.Equal: return tail => tail == t; default: { #if DEBUG - Console.WriteLine($"shardingOperator is not equal scan all table tail"); + Console.WriteLine($"shardingOperator is not equal scan all table tail"); #endif return tail => true; } @@ -85,10 +91,10 @@ namespace ShardingCore.VirtualRoutes.Days }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 * * ?" }; + var crons = base.GetJobCronExpressions().Concat(new[] { "0 0 0 * * ?" }).Distinct().ToArray(); + return crons; } - } } \ No newline at end of file diff --git a/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyLongVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyLongVirtualTableRoute.cs index 0c8d06be..8b863ca7 100644 --- a/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyLongVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Days/AbstractSimpleShardingDayKeyLongVirtualTableRoute.cs @@ -3,6 +3,7 @@ using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; namespace ShardingCore.VirtualRoutes.Days @@ -80,9 +81,11 @@ namespace ShardingCore.VirtualRoutes.Days "0 1 0 * * ?", }; } - public override string[] GetCompensateCronExpressions() + + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 * * ?" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 * * ?" }).Distinct().ToArray(); + return crons; } } } \ No newline at end of file diff --git a/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs index 94003354..a5e5d810 100644 --- a/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs @@ -3,6 +3,7 @@ using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; namespace ShardingCore.VirtualRoutes.Months @@ -76,9 +77,10 @@ namespace ShardingCore.VirtualRoutes.Months "0 1 0 1 * ?", }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 1 * ?" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 * ?"}).Distinct().ToArray(); + return crons; } } diff --git a/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyLongVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyLongVirtualTableRoute.cs index af898568..dbaf460f 100644 --- a/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyLongVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyLongVirtualTableRoute.cs @@ -3,6 +3,7 @@ using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; namespace ShardingCore.VirtualRoutes.Months @@ -79,9 +80,10 @@ namespace ShardingCore.VirtualRoutes.Months "0 1 0 1 * ?", }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 1 * ?" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 * ?"}).Distinct().ToArray(); + return crons; } } } \ No newline at end of file diff --git a/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute.cs index 3dff17bc..a7276812 100644 --- a/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute.cs @@ -3,6 +3,7 @@ using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; namespace ShardingCore.VirtualRoutes.Weeks @@ -78,9 +79,10 @@ namespace ShardingCore.VirtualRoutes.Weeks "0 1 0 ? * 2", }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 ? * 2" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 ? * 2"}).Distinct().ToArray(); + return crons; } } diff --git a/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyLongVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyLongVirtualTableRoute.cs index 449231e7..9a5d9e6e 100644 --- a/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyLongVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Weeks/AbstractSimpleShardingWeekKeyLongVirtualTableRoute.cs @@ -3,6 +3,7 @@ using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; namespace ShardingCore.VirtualRoutes.Weeks { @@ -81,9 +82,10 @@ namespace ShardingCore.VirtualRoutes.Weeks "0 1 0 ? * 2", }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 ? * 2" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 ? * 2"}).Distinct().ToArray(); + return crons; } } } \ No newline at end of file diff --git a/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute.cs index 98279e3c..0a8cf525 100644 --- a/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute.cs @@ -2,6 +2,7 @@ using ShardingCore.Core.VirtualRoutes; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; namespace ShardingCore.VirtualRoutes.Years @@ -78,9 +79,10 @@ namespace ShardingCore.VirtualRoutes.Years "0 1 0 1 1 ?", }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 1 1 ?" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 1 ?"}).Distinct().ToArray(); + return crons; } } } \ No newline at end of file diff --git a/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyLongVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyLongVirtualTableRoute.cs index a55832b6..a37b4a4f 100644 --- a/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyLongVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Years/AbstractSimpleShardingYearKeyLongVirtualTableRoute.cs @@ -3,6 +3,7 @@ using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Abstractions; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; namespace ShardingCore.VirtualRoutes.Years @@ -102,9 +103,10 @@ namespace ShardingCore.VirtualRoutes.Years "0 1 0 1 1 ?", }; } - public override string[] GetCompensateCronExpressions() + public override string[] GetJobCronExpressions() { - return new[] { "0 0 0 1 1 ?" }; + var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 1 ?"}).Distinct().ToArray(); + return crons; } } } \ No newline at end of file