修改readme并且提供默认路由

This commit is contained in:
xuejmnet 2021-01-27 13:38:30 +08:00
parent ca53f4099a
commit d6c8522178
20 changed files with 726 additions and 145 deletions

View File

@ -39,13 +39,14 @@ expression
取模 |Sharding Mod | Yes | Any ClrType
大数取模范围 |Sharding Range | Yes | Any ClrType
按天/周/月/年... |Sharding By Day/Week/Month/Year... | Yes | Any ClrType
按任意方式分表 |Sharding By Customer | Yes | Any ClrType
其他 |Sharding By Other | Yes | Any ClrType
- [开始](#开始)
- [概念](#概念)
- [安装](#安装)
- [配置](#配置)
- [使用](#使用)
- [默认路由](#默认路由)
- [Api](#Api)
- [高级配置](#高级配置)
- [手动路由](#手动路由)
@ -54,7 +55,7 @@ expression
- [批量操作](#批量操作)
- [注意事项](#注意事项)
- [计划(Future)](#计划)
- [总结](#总结)
- [最后](#最后)
# 开始
@ -66,7 +67,7 @@ Server为例
本库的几个简单的核心概念:
- [Tail]
尾巴、后缀虚拟表和物理表的后缀
尾巴、后缀物理表的后缀
- [TailPrefix]
尾巴前缀虚拟表和物理表的后缀中间的字符
- [物理表]
@ -89,7 +90,7 @@ Server为例
## 配置
配置entity 推荐 fluent api 可以实现自动建表功能
配置entity 推荐 [fluent api](https://docs.microsoft.com/en-us/ef/core/modeling/) 可以实现自动建表功能
`IShardingEntity`数据库对象必须继承该接口
`ShardingKey`分表字段需要使用该特性
@ -127,8 +128,8 @@ Server为例
创建virtual
route
实现 `AbstractShardingOperatorVirtualRoute<T, TKey>`
接口
框架默认有提供几个简单的路由 `AbstractSimpleShardingModVirtualRoute<T,TKey>`
抽象
框架默认有提供几个简单的路由 [默认路由](#默认路由)
```c#
@ -194,13 +195,39 @@ route
## Api
方法 |Method
--- |---
获取集合 |ToShardingListAsync
第一条 |ShardingFirstOrDefaultAsync
最大 |ShardingMaxAsync
最小 |ShardingMinAsync
是否存在 |ShardingAnyAsync
分页 |ToShardingPageResultAsync
数目 |ShardingCountAsync
求和 |ShardingSumAsync
方法 | Method | SqlServer Unit Test | MySql Unit Test
--- |--- |--- |---
获取集合 |ToShardingListAsync |8 |8
第一条 |ShardingFirstOrDefaultAsync |5 |5
最大 |ShardingMaxAsync |0 |0
最小 |ShardingMinAsync |0 |0
是否存在 |ShardingAnyAsync |0 |0
分页 |ToShardingPageResultAsync |0 |0
数目 |ShardingCountAsync |0 |0
求和 |ShardingSumAsync |0 |0
## 默认路由
抽象abstract | 路由规则 | tail | 索引
--- |--- |--- |---
AbstractSimpleShardingModKeyIntVirtualRoute |默认int类型取模路由 |0,1,2... | `=`
AbstractSimpleShardingModKeyStringVirtualRoute |默认string类型取模路由 |0,1,2... | `=`
AbstractSimpleShardingDayKeyDateTimeVirtualRoute |默认DateTime类型取模路由 |yyyyMMdd | `>,>=,<,<=,=,contains`
AbstractSimpleShardingDayKeyLongVirtualRoute |默认long类型取模路由 |yyyyMMdd | `>,>=,<,<=,=,contains`
AbstractSimpleShardingWeekKeyDateTimeVirtualRoute |默认DateTime类型取模路由 |yyyyMMdd_dd | `>,>=,<,<=,=,contains`
AbstractSimpleShardingWeekKeyLongVirtualRoute |默认long类型取模路由 |yyyyMMdd_dd | `>,>=,<,<=,=,contains`
AbstractSimpleShardingMonthKeyDateTimeVirtualRoute |默认DateTime类型取模路由 |yyyyMM | `>,>=,<,<=,=,contains`
AbstractSimpleShardingMonthKeyLongVirtualRoute |默认long类型取模路由 |yyyyMM | `>,>=,<,<=,=,contains`
AbstractSimpleShardingYearKeyDateTimeVirtualRoute |默认DateTime类型取模路由 |yyyy | `>,>=,<,<=,=,contains`
AbstractSimpleShardingYearKeyLongVirtualRoute |默认long类型取模路由 |yyyy | `>,>=,<,<=,=,contains`
注:`contains`表示为`o=>ids.contains(o.shardingkey)`
# 最后
凭借各大开源生态圈提供的优秀代码和思路才有的这个框架,希望可以为.Net生态提供一份微薄之力,该框架本人会一直长期维护,有大神技术支持可以联系下方方式欢迎star :)
QQ群:771630778
个人QQ:326308290(欢迎技术支持提供您宝贵的意见)
个人邮箱:326308290@qq.com

View File

@ -4,6 +4,7 @@ using System.Linq.Expressions;
using Sample.SqlServer.Domain.Entities;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.VirtualRoutes;
using ShardingCore.VirtualRoutes.Mods;
namespace Sample.SqlServer.Shardings
{
@ -13,35 +14,15 @@ namespace Sample.SqlServer.Shardings
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualRoute : AbstractSimpleShardingModVirtualRoute<SysUserMod, string>
public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod>
{
public SysUserModVirtualRoute() : base(3)
{
}
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey.ToString();
}
public override List<string> GetAllTails()
{
return new() { "0","1","2"};
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
Console.WriteLine($"shardingOperator is not equal scan all table tail");
return tail => true;
}
}
}
}
}

View File

@ -18,7 +18,7 @@ namespace ShardingCore.Core.Internal.StreamMerge.ListMerge
private readonly StreamMergeContext _mergeContext;
private readonly List<IAsyncEnumerator<T>> _sources;
private readonly PriorityQueue<OrderMergeItem<T>> _queue;
private bool isFirst;
private bool skipFirst;
private IAsyncEnumerator<T> _currentEnumerator;
public OrderAsyncEnumerator(StreamMergeContext mergeContext,List<IAsyncEnumerator<T>> sources)
@ -26,7 +26,7 @@ namespace ShardingCore.Core.Internal.StreamMerge.ListMerge
_mergeContext = mergeContext;
_sources = sources;
_queue = new PriorityQueue<OrderMergeItem<T>>(sources.Count);
isFirst = true;
skipFirst = true;
SetOrderEnumerator();
}
@ -45,9 +45,9 @@ namespace ShardingCore.Core.Internal.StreamMerge.ListMerge
{
if (_queue.IsEmpty())
return false;
if (isFirst)
if (skipFirst)
{
isFirst = false;
skipFirst = false;
return true;
}
@ -84,7 +84,7 @@ namespace ShardingCore.Core.Internal.StreamMerge.ListMerge
private readonly StreamMergeContext _mergeContext;
private readonly List<IAsyncEnumerator<T>> _sources;
private readonly PriorityQueue<OrderMergeItem<T>> _queue;
private bool isFirst;
private bool skipFirst;
private IAsyncEnumerator<T> _currentEnumerator;
public OrderAsyncEnumerator(StreamMergeContext mergeContext, List<IAsyncEnumerator<T>> sources)
@ -92,7 +92,7 @@ namespace ShardingCore.Core.Internal.StreamMerge.ListMerge
_mergeContext = mergeContext;
_sources = sources;
_queue = new PriorityQueue<OrderMergeItem<T>>(sources.Count);
isFirst = true;
skipFirst = true;
SetOrderEnumerator();
}
@ -112,9 +112,9 @@ namespace ShardingCore.Core.Internal.StreamMerge.ListMerge
{
if (_queue.IsEmpty())
return false;
if (isFirst)
if (skipFirst)
{
isFirst = false;
skipFirst = false;
return true;
}

View File

@ -273,74 +273,56 @@ namespace ShardingCore.DbContexts.VirtualDbContexts
return new ShardingBatchInsertEntry<T>(groups);
}
public ShardingBatchUpdateEntry<T> BulkUpdate<T>(Expression<Func<T, bool>> @where, Expression<Func<T, T>> updateExp) where T : class
public ShardingBatchUpdateEntry<T> BulkUpdate<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> updateExp) where T : class
{
List<DbContext> dbContexts = null;
if (typeof(T).IsShardingEntity())
{
var shardingDbContexts = CreateShardingDbContext<IShardingEntity>(new EnumerableQuery<T>(where).AsQueryable());
var shardingDbContexts = CreateShardingDbContexts<IShardingEntity>(new EnumerableQuery<T>(where).AsQueryable());
dbContexts = shardingDbContexts.Select(o => (DbContext) o).ToList();
}
else
{
var dbContext = CreateNoShardingDbContext();
dbContexts = new List<DbContext>(1)
{
dbContext
GetOrCreateShardingDbContext(EMPTY_SHARDING_TAIL_ID)
};
}
return new ShardingBatchUpdateEntry<T>(@where,updateExp,dbContexts);
return new ShardingBatchUpdateEntry<T>(where,updateExp,dbContexts);
}
public ShardingBatchDeleteEntry<T> BulkDelete<T>(Expression<Func<T, bool>> @where) where T : class
public ShardingBatchDeleteEntry<T> BulkDelete<T>(Expression<Func<T, bool>> where) where T : class
{
List<DbContext> dbContexts = null;
if (typeof(T).IsShardingEntity())
{
var shardingDbContexts = CreateShardingDbContext<IShardingEntity>(new EnumerableQuery<T>(where).AsQueryable());
var shardingDbContexts = CreateShardingDbContexts<IShardingEntity>(new EnumerableQuery<T>(where).AsQueryable());
dbContexts = shardingDbContexts.Select(o => (DbContext) o).ToList();
}
else
{
var dbContext = CreateNoShardingDbContext();
dbContexts = new List<DbContext>(1)
{
dbContext
GetOrCreateShardingDbContext(EMPTY_SHARDING_TAIL_ID)
};
}
return new ShardingBatchDeleteEntry<T>(@where,dbContexts);
return new ShardingBatchDeleteEntry<T>(where,dbContexts);
}
private ShardingDbContext CreateGenericDbContext<T>(T entity) where T : class
{
var tail = EMPTY_SHARDING_TAIL_ID;
if (entity.IsShardingEntity())
{
return CreateShardingDbContext(entity as IShardingEntity);
}
else
{
return CreateNoShardingDbContext();
var physicTable = _virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new RouteConfig(null, entity as IShardingEntity, null))[0];
tail = physicTable.Tail;
}
return GetOrCreateShardingDbContext(tail);
}
private ShardingDbContext CreateNoShardingDbContext()
{
var shardingDbContext = GetOrCreateShardingDbContext(EMPTY_SHARDING_TAIL_ID);
return shardingDbContext;
}
private ShardingDbContext CreateShardingDbContext<T>(T entity) where T : class, IShardingEntity
{
var physicTable = _virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new RouteConfig(null, entity, null))[0];
var shardingDbContext = GetOrCreateShardingDbContext(physicTable.Tail);
return shardingDbContext;
}
private List<ShardingDbContext> CreateShardingDbContext<T>(IQueryable queryable) where T : class, IShardingEntity
private List<ShardingDbContext> CreateShardingDbContexts<T>(IQueryable queryable) where T : class, IShardingEntity
{
var physicTables = _virtualTableManager.GetVirtualTable(typeof(T)).RouteTo(new RouteConfig(queryable, null, null));
if (physicTables.Any())

View File

@ -1,3 +1,5 @@
using System;
namespace ShardingCore.Helpers
{
/*
@ -19,5 +21,43 @@ namespace ShardingCore.Helpers
}
return h;
}
private static readonly DateTime UtcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime ConvertLongToDateTime(long timeStamp)
{
return UtcStartTime.AddMilliseconds(timeStamp).AddHours(8);
}
/// <summary>
/// 获取当月第一天
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static DateTime GetCurrentMonthFirstDay(DateTime time)
{
return time.AddDays(1 - time.Day).Date;
}
/// <summary>
/// 获取下个月第一天
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static DateTime GetNextMonthFirstDay(DateTime time)
{
return time.AddDays(1 - time.Day).Date.AddMonths(1);
}
public static DateTime GetCurrentMonday(DateTime time)
{
DateTime dateTime1 = new DateTime(time.Year, time.Month, time.Day);
int num = (int) (time.DayOfWeek - 1);
if (num == -1)
num = 6;
return dateTime1.AddDays(-num);
}
public static DateTime GetCurrentSunday(DateTime time)
{
return GetCurrentMonday(time).AddDays(6);
}
}
}

View File

@ -1,29 +0,0 @@
using System;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Helpers;
namespace ShardingCore.VirtualRoutes
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 21 January 2021 15:03:06
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingModVirtualRoute<T,TKey>: AbstractShardingOperatorVirtualRoute<T, TKey> where T:class,IShardingEntity
{
protected readonly int Mod;
protected AbstractSimpleShardingModVirtualRoute(int mod)
{
Mod = mod;
}
public override string ShardingKeyToTail(object shardingKey)
{
var shardingKeyStr = ConvertToShardingKey(shardingKey).ToString();
return Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKeyStr) % Mod).ToString();
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Abstractions
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 12:29:19
* @Email: 326308290@qq.com
*/
public abstract class AbstractShardingTimeKeyDateTimeVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,DateTime> where T:class,IShardingEntity
{
protected override DateTime ConvertToShardingKey(object shardingKey)
{
return Convert.ToDateTime(shardingKey);
}
public override string ShardingKeyToTail(object shardingKey)
{
var time = ConvertToShardingKey(shardingKey);
return TimeFormatToTail(time);
}
protected abstract string TimeFormatToTail(DateTime time);
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Abstractions
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 13:06:01
* @Email: 326308290@qq.com
*/
public abstract class AbstractShardingTimeKeyLongVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,long> where T:class,IShardingEntity
{
protected override long ConvertToShardingKey(object shardingKey)
{
return (long)shardingKey;
}
public override string ShardingKeyToTail(object shardingKey)
{
var time = ConvertToShardingKey(shardingKey);
return TimeFormatToTail(time);
}
protected abstract string TimeFormatToTail(long time);
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Days
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 08:41:05
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualRoute<T>:AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T:class,IShardingEntity
{
protected override string TimeFormatToTail(DateTime time)
{
return $"{time:yyyyMMdd}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
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;
}
case ShardingOperatorEnum.LessThanOrEqual:
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");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Days
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 08:56:38
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingDayKeyLongVirtualRoute<T>:AbstractShardingTimeKeyLongVirtualRoute<T> where T:class,IShardingEntity
{
protected override string TimeFormatToTail(long time)
{
var dateTime=ShardingCoreHelper.ConvertLongToDateTime(time);
return $"{dateTime:yyyyMMdd}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var dateTime = ShardingCoreHelper.ConvertLongToDateTime(shardingKey);
var shardingKeyDate = dateTime.Date;
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (shardingKeyDate == dateTime)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Mods
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 08:21:25
* @Email: 326308290@qq.com
*/
/// <summary>
/// 分表字段为int的取模分表
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class AbstractSimpleShardingModKeyIntVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,int> where T:class,IShardingEntity
{
protected readonly int Mod;
protected AbstractSimpleShardingModKeyIntVirtualRoute(int mod)
{
Mod = mod;
}
protected override int ConvertToShardingKey(object shardingKey)
{
return Convert.ToInt32(shardingKey);
}
public override string ShardingKeyToTail(object shardingKey)
{
var shardingKeyInt = ConvertToShardingKey(shardingKey);
return Math.Abs(shardingKeyInt % Mod).ToString();
}
protected override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Helpers;
namespace ShardingCore.VirtualRoutes.Mods
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 08:14:30
* @Email: 326308290@qq.com
*/
/// <summary>
/// 分表字段为string的取模分表
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class AbstractSimpleShardingModKeyStringVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,string> where T:class,IShardingEntity
{
protected readonly int Mod;
protected AbstractSimpleShardingModKeyStringVirtualRoute(int mod)
{
Mod = mod;
}
/// <summary>
/// 如何将shardingkey转成对应的tail
/// </summary>
/// <param name="shardingKey"></param>
/// <returns></returns>
public override string ShardingKeyToTail(object shardingKey)
{
var shardingKeyStr = ConvertToShardingKey(shardingKey);
return Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKeyStr) % Mod).ToString();
}
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey.ToString();
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Months
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 12:27:09
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingMonthKeyDateTimeVirtualRoute<T> : AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T : class, IShardingEntity
{
protected override string TimeFormatToTail(DateTime time)
{
return $"{time:yyyyMM}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var currentMonth = ShardingCoreHelper.GetCurrentMonthFirstDay(shardingKey);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentMonth == shardingKey)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
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.Months
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 13:09:52
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingMonthKeyLongVirtualRoute<T>:AbstractShardingTimeKeyLongVirtualRoute<T> where T:class,IShardingEntity
{
protected override string TimeFormatToTail(long time)
{
var datetime = ShardingCoreHelper.ConvertLongToDateTime(time);
return $"{datetime:yyyyMM}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var dateTime = ShardingCoreHelper.ConvertLongToDateTime(shardingKey);
var currentMonth = ShardingCoreHelper.GetCurrentMonthFirstDay(dateTime);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentMonth == dateTime)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,55 @@
using System;
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.Weeks
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 12:40:27
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingWeekKeyDateTimeVirtualRoute<T> : AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T : class, IShardingEntity
{
protected override string TimeFormatToTail(DateTime time)
{
var currentMonday = ShardingCoreHelper.GetCurrentMonday(time);
var currentSunday = ShardingCoreHelper.GetCurrentSunday(time);
return $"{currentMonday:yyyyMM}{currentMonday:dd}_{currentSunday:dd}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var currentMonth = ShardingCoreHelper.GetCurrentMonday(shardingKey);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentMonth == shardingKey)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Weeks
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 13:12:50
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingWeekKeyLongVirtualRoute<T> : AbstractShardingTimeKeyLongVirtualRoute<T> where T : class, IShardingEntity
{
protected override string TimeFormatToTail(long time)
{
var datetime = ShardingCoreHelper.ConvertLongToDateTime(time);
var currentMonday = ShardingCoreHelper.GetCurrentMonday(datetime);
var currentSunday = ShardingCoreHelper.GetCurrentSunday(datetime);
return $"{currentMonday:yyyyMM}{currentMonday:dd}_{currentSunday:dd}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var dateTime = ShardingCoreHelper.ConvertLongToDateTime(shardingKey);
var currentMonth = ShardingCoreHelper.GetCurrentMonday(dateTime);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentMonth == dateTime)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Years
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 13:00:57
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingYearKeyDateTimeVirtualRoute<T> : AbstractShardingTimeKeyDateTimeVirtualRoute<T> where T : class, IShardingEntity
{
protected override string TimeFormatToTail(DateTime time)
{
return $"{time:yyyy}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var currentYear =new DateTime(shardingKey.Year);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentYear == shardingKey)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Linq.Expressions;
using ShardingCore.Core;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions;
namespace ShardingCore.VirtualRoutes.Years
{
/*
* @Author: xjm
* @Description:
* @Date: Wednesday, 27 January 2021 13:17:24
* @Email: 326308290@qq.com
*/
public abstract class AbstractSimpleShardingYearKeyLongVirtualRoute<T> : AbstractShardingTimeKeyLongVirtualRoute<T> where T : class, IShardingEntity
{
protected override string TimeFormatToTail(long time)
{
var datetime = ShardingCoreHelper.ConvertLongToDateTime(time);
return $"{datetime:yyyy}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var datetime = ShardingCoreHelper.ConvertLongToDateTime(shardingKey);
var currentYear = new DateTime(datetime.Year);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentYear == datetime)
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;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Test50.MySql.Domain.Entities;
using ShardingCore.VirtualRoutes;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test50.MySql.Shardings
{
@ -13,35 +14,16 @@ namespace ShardingCore.Test50.MySql.Shardings
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualRoute : AbstractSimpleShardingModVirtualRoute<SysUserMod, string>
public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod>
{
public SysUserModVirtualRoute() : base(3)
{
}
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey.ToString();
}
public override List<string> GetAllTails()
{
return new() { "0","1","2"};
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
Console.WriteLine($"shardingOperator is not equal scan all table tail");
return tail => true;
}
}
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Test50.Domain.Entities;
using ShardingCore.VirtualRoutes;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test50.Shardings
{
@ -13,35 +14,16 @@ namespace ShardingCore.Test50.Shardings
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualRoute : AbstractSimpleShardingModVirtualRoute<SysUserMod, string>
public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod>
{
public SysUserModVirtualRoute() : base(3)
{
}
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey.ToString();
}
public override List<string> GetAllTails()
{
return new() { "0","1","2"};
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
Console.WriteLine($"shardingOperator is not equal scan all table tail");
return tail => true;
}
}
}
}
}