修改部分demo

This commit is contained in:
xuejiaming 2024-02-12 21:25:02 +08:00
parent 30c73fc949
commit faeba5159d
9 changed files with 85 additions and 27 deletions

View File

@ -35,6 +35,7 @@
- 版本号第二位是efcore版本号使用对应的版本号即可 - 版本号第二位是efcore版本号使用对应的版本号即可
- 最后两位版本号使用最大即可 - 最后两位版本号使用最大即可
- efcore8使用shardingcore7.8.x.x
- efcore7使用shardingcore7.7.x.x - efcore7使用shardingcore7.7.x.x
- efcore6使用shardingcore7.6.x.x - efcore6使用shardingcore7.6.x.x
- efcore5使用shardingcore7.5.x.x - efcore5使用shardingcore7.5.x.x

View File

@ -34,6 +34,8 @@ high performance lightweight solution for efcore sharding table and sharding dat
- first version is shardingcore version - first version is shardingcore version
- second version is efcore version - second version is efcore version
- other version use last version - other version use last version
-
- efcore8 use shardingcore7.8.x.x
- efcore7 use shardingcore7.7.x.x - efcore7 use shardingcore7.7.x.x

View File

@ -16,6 +16,7 @@ namespace ShardingCore6x
public string Payer { get; set; } public string Payer { get; set; }
public OrderStatusEnum OrderStatus { get; set; } public OrderStatusEnum OrderStatus { get; set; }
} }
public enum OrderStatusEnum public enum OrderStatusEnum

View File

@ -1,12 +1,15 @@
using System.Diagnostics; using System.Diagnostics;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using MySqlConnector; using MySqlConnector;
using Sample.MySql.DbContexts; using Sample.MySql.DbContexts;
using Sample.MySql.Domain.Entities; using Sample.MySql.Domain.Entities;
using Sample.MySql.multi; using Sample.MySql.multi;
using Sample.MySql.Shardings; using Sample.MySql.Shardings;
using ShardingCore;
using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes;
@ -63,14 +66,16 @@ namespace Sample.MySql.Controllers
[Route("[controller]/[action]")] [Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase public class WeatherForecastController : ControllerBase
{ {
private readonly IShardingRuntimeContext<DefaultShardingDbContext> _defaultShardingRuntimeContext;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly UnShardingDbContext _unShardingDbContext; private readonly UnShardingDbContext _unShardingDbContext;
private readonly DefaultShardingDbContext _defaultTableDbContext; private readonly DefaultShardingDbContext _defaultTableDbContext;
private readonly IShardingRuntimeContext _shardingRuntimeContext; private readonly IShardingRuntimeContext _shardingRuntimeContext;
private readonly ABC _abc; private readonly ABC _abc;
public WeatherForecastController(IServiceProvider serviceProvider,UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext) public WeatherForecastController(IShardingRuntimeContext<DefaultShardingDbContext> defaultShardingRuntimeContext,IServiceProvider serviceProvider,UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext)
{ {
_defaultShardingRuntimeContext = defaultShardingRuntimeContext;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_unShardingDbContext = unShardingDbContext; _unShardingDbContext = unShardingDbContext;
_defaultTableDbContext = defaultTableDbContext; _defaultTableDbContext = defaultTableDbContext;
@ -80,6 +85,25 @@ namespace Sample.MySql.Controllers
public IQueryable<SysTest> GetAll() public IQueryable<SysTest> GetAll()
{ {
var dbContextOptionsBuilder = new DbContextOptionsBuilder<DefaultShardingDbContext>();
var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator();
dbContextOptionsBuilder.UseDefaultSharding<DefaultShardingDbContext>(_shardingRuntimeContext);
using (var dbContext = new DefaultShardingDbContext(dbContextOptionsBuilder.Options))
{
}
foreach (var dataSourceDbContext in _defaultTableDbContext.GetShardingExecutor().GetCurrentDbContexts())
{
foreach (var keyValuePair in dataSourceDbContext.Value.GetCurrentContexts())
{
}
}
((IResettableService)_defaultTableDbContext).ResetState();
var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager(); var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager();
// dataSourceRouteManager.GetRoute() // dataSourceRouteManager.GetRoute()
var routeManager = _shardingRuntimeContext.GetTableRouteManager(); var routeManager = _shardingRuntimeContext.GetTableRouteManager();
@ -96,8 +120,8 @@ namespace Sample.MySql.Controllers
var tableRouteManager = routeManager; var tableRouteManager = routeManager;
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod)); var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod));
var tails = tableRoute.GetTails();
var virtualTableRoute = (ITailAppendable)tableRoute; var virtualTableRoute = (ITailAppendable)tableRoute;
//一定要先在路由里面添加尾巴 //一定要先在路由里面添加尾巴
virtualTableRoute.Append("20220921"); virtualTableRoute.Append("20220921");
shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921"); shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
@ -129,6 +153,7 @@ namespace Sample.MySql.Controllers
// { // {
// //
// } // }
var dateTime = new DateTime(2021,1,1); var dateTime = new DateTime(2021,1,1);
var x211 = await (from ut in _defaultTableDbContext.Set<SysTest>().UseUnionAllMerge() var x211 = await (from ut in _defaultTableDbContext.Set<SysTest>().UseUnionAllMerge()
join uu in _defaultTableDbContext.Set<SysUserLogByMonth>() join uu in _defaultTableDbContext.Set<SysUserLogByMonth>()

View File

@ -11,10 +11,15 @@ namespace Sample.MySql.Shardings
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
//只返回当前和历史
private static readonly List<string> tails = new List<string>()
{
"current",
"history"
};
public override List<string> GetAllDataSourceNames() public override List<string> GetAllDataSourceNames()
{ {
throw new NotImplementedException(); return tails;
} }
public override bool AddDataSourceName(string dataSourceName) public override bool AddDataSourceName(string dataSourceName)
@ -29,6 +34,7 @@ namespace Sample.MySql.Shardings
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator) public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{ {
//判断过滤查询历史还是现在
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }

View File

@ -7,6 +7,7 @@ using Sample.MySql.Domain.Entities;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Months; using ShardingCore.VirtualRoutes.Months;
namespace Sample.MySql.Shardings namespace Sample.MySql.Shardings
@ -41,27 +42,42 @@ namespace Sample.MySql.Shardings
builder.ShardingProperty(o => o.Time); builder.ShardingProperty(o => o.Time);
} }
// protected override List<TableRouteUnit> AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List<TableRouteUnit> shardingRouteUnits) protected override bool RouteIgnoreDataSource => false;
// { public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
// if (shardingRouteUnits.Count > 10)
// {
// _logger.LogInformation("截断前:"+string.Join(",",shardingRouteUnits.Select(o=>o.Tail)));
// //这边你要自己做顺序处理阶段
// var result= shardingRouteUnits.OrderByDescending(o=>o.Tail).Take(10).ToList();
// _logger.LogInformation("截断后:"+string.Join(",",result.Select(o=>o.Tail)));
// return result;
// }
// return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits);
// }
protected override List<TableRouteUnit> AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List<TableRouteUnit> shardingRouteUnits)
{ {
Console.WriteLine("AfterShardingRouteUnitFilter:"+shardingRouteUnits.Count); var currentMonthFirstDay = ShardingCoreHelper.GetCurrentMonthFirstDay(DateTime.Now);
if (shardingRouteUnits.Count > 10)//如果本次命中表过多 var matchDataSource = currentMonthFirstDay<shardingKey?"history":"current";
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{ {
return shardingRouteUnits.Take(10).ToList();//自己排序截断选择最新的10张自己加orderBy case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail =>
{
var strings = tail.Split(".");
var ds = strings[0];
var yyyyMM = strings[1];
return matchDataSource==ds && String.Compare(yyyyMM, 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;
}
} }
return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits);
} }
} }
} }

View File

@ -159,6 +159,12 @@ namespace Sample.MySql
builder builder
.AddInterceptors(new MySaveChangeInterceptor()); .AddInterceptors(new MySaveChangeInterceptor());
}); });
o.AddDefaultDataSource("current",
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
o.AddExtraDataSource(sp => new Dictionary<string, string>()
{
{ "history", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" }
});
o.AddDefaultDataSource("ds0", o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
o.AddExtraDataSource(sp => new Dictionary<string, string>() o.AddExtraDataSource(sp => new Dictionary<string, string>()

View File

@ -41,19 +41,19 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations
/// <summary> /// <summary>
/// 使用当前属性order和comparer有关联 /// 使用当前属性order和comparer有关联
/// ShardingTailComparer参数 tailComparer如果是正序,reverse是false那么表示ShardingTailComparer最后采用倒序 /// ShardingTailComparer参数 tailComparer如果是正序,reverse是false那么表示ShardingTailComparer最后采用倒序
/// whenAscIsSameAsShardingTailComparer,true表示当前添加的属性也是采用倒序,false表示当前添加的属性使用正序 /// sameAsShardingTailComparer,true表示当前添加的属性也是采用倒序,false表示当前添加的属性使用正序
/// 如果不添加AddOrder方法默认采用ShardingTailComparer在这个例子里就是倒序 /// 如果不添加AddOrder方法默认采用ShardingTailComparer在这个例子里就是倒序
/// </summary> /// </summary>
/// <typeparam name="TProperty"></typeparam> /// <typeparam name="TProperty"></typeparam>
/// <param name="primaryOrderPropertyExpression"></param> /// <param name="primaryOrderPropertyExpression"></param>
/// <param name="whenAscIsSameAsShardingTailComparer"> /// <param name="sameAsShardingTailComparer">
/// true:当前属性正序和comparer正序一样,false:当前属性倒序和comparer正序一样 /// true:当前属性正序和comparer正序一样,false:当前属性倒序和comparer正序一样
/// </param> /// </param>
/// <param name="seqOrderMatch"></param> /// <param name="seqOrderMatch"></param>
/// <returns></returns> /// <returns></returns>
public EntityQueryBuilder<TEntity> AddOrder<TProperty>(Expression<Func<TEntity, TProperty>> primaryOrderPropertyExpression,bool whenAscIsSameAsShardingTailComparer = true,SeqOrderMatchEnum seqOrderMatch=SeqOrderMatchEnum.Owner) public EntityQueryBuilder<TEntity> AddOrder<TProperty>(Expression<Func<TEntity, TProperty>> primaryOrderPropertyExpression,bool sameAsShardingTailComparer = true,SeqOrderMatchEnum seqOrderMatch=SeqOrderMatchEnum.Owner)
{ {
_entityQueryMetadata.AddSeqComparerOrder(primaryOrderPropertyExpression.GetPropertyAccess().Name, whenAscIsSameAsShardingTailComparer, seqOrderMatch); _entityQueryMetadata.AddSeqComparerOrder(primaryOrderPropertyExpression.GetPropertyAccess().Name, sameAsShardingTailComparer, seqOrderMatch);
return this; return this;
} }
/// <summary> /// <summary>

View File

@ -106,6 +106,7 @@ namespace ShardingCore.Test
} }
}; };
}, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime);
}) })
.AddShardingCore(); .AddShardingCore();
} }