From faeba5159df8e2dbaf7c3fe9ecf97db8724d7a0f Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 12 Feb 2024 21:25:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-zh.md | 1 + README.md | 2 + benchmarks/ShardingCoreBenchmark/Order.cs | 1 + .../Controllers/WeatherForecastController.cs | 29 ++++++++++- .../Shardings/SysUserLogByMonthDSRoute.cs | 12 +++-- .../Shardings/SysUserLogByMonthRoute.cs | 52 ++++++++++++------- samples/Sample.MySql/Startup.cs | 6 +++ .../EntityQueryBuilder.cs | 8 +-- test/ShardingCore.Test/Startup.cs | 1 + 9 files changed, 85 insertions(+), 27 deletions(-) diff --git a/README-zh.md b/README-zh.md index 6fe42e7d..1c38be20 100644 --- a/README-zh.md +++ b/README-zh.md @@ -35,6 +35,7 @@ - 版本号第二位是efcore版本号使用对应的版本号即可 - 最后两位版本号使用最大即可 +- efcore8使用shardingcore7.8.x.x, - efcore7使用shardingcore7.7.x.x, - efcore6使用shardingcore7.6.x.x, - efcore5使用shardingcore7.5.x.x, diff --git a/README.md b/README.md index 2613e413..52a712c8 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ high performance lightweight solution for efcore sharding table and sharding dat - first version is shardingcore version - second version is efcore version - other version use last version +- +- efcore8 use shardingcore7.8.x.x - efcore7 use shardingcore7.7.x.x diff --git a/benchmarks/ShardingCoreBenchmark/Order.cs b/benchmarks/ShardingCoreBenchmark/Order.cs index caddcffd..2f2bf36a 100644 --- a/benchmarks/ShardingCoreBenchmark/Order.cs +++ b/benchmarks/ShardingCoreBenchmark/Order.cs @@ -16,6 +16,7 @@ namespace ShardingCore6x public string Payer { get; set; } public OrderStatusEnum OrderStatus { get; set; } + } public enum OrderStatusEnum diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 8f5f41a1..3d45d89c 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -1,12 +1,15 @@ using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Storage; using MySqlConnector; using Sample.MySql.DbContexts; using Sample.MySql.Domain.Entities; using Sample.MySql.multi; using Sample.MySql.Shardings; +using ShardingCore; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualRoutes.TableRoutes; @@ -63,14 +66,16 @@ namespace Sample.MySql.Controllers [Route("[controller]/[action]")] public class WeatherForecastController : ControllerBase { + private readonly IShardingRuntimeContext _defaultShardingRuntimeContext; private readonly IServiceProvider _serviceProvider; private readonly UnShardingDbContext _unShardingDbContext; private readonly DefaultShardingDbContext _defaultTableDbContext; private readonly IShardingRuntimeContext _shardingRuntimeContext; private readonly ABC _abc; - public WeatherForecastController(IServiceProvider serviceProvider,UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext) + public WeatherForecastController(IShardingRuntimeContext defaultShardingRuntimeContext,IServiceProvider serviceProvider,UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext) { + _defaultShardingRuntimeContext = defaultShardingRuntimeContext; _serviceProvider = serviceProvider; _unShardingDbContext = unShardingDbContext; _defaultTableDbContext = defaultTableDbContext; @@ -80,6 +85,25 @@ namespace Sample.MySql.Controllers public IQueryable GetAll() { + var dbContextOptionsBuilder = new DbContextOptionsBuilder(); + var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator(); + dbContextOptionsBuilder.UseDefaultSharding(_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(); // dataSourceRouteManager.GetRoute() var routeManager = _shardingRuntimeContext.GetTableRouteManager(); @@ -96,8 +120,8 @@ namespace Sample.MySql.Controllers var tableRouteManager = routeManager; //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod)); - var tails = tableRoute.GetTails(); var virtualTableRoute = (ITailAppendable)tableRoute; + //一定要先在路由里面添加尾巴 virtualTableRoute.Append("20220921"); shardingTableCreator.CreateTable("ds0","20220921"); @@ -129,6 +153,7 @@ namespace Sample.MySql.Controllers // { // // } + var dateTime = new DateTime(2021,1,1); var x211 = await (from ut in _defaultTableDbContext.Set().UseUnionAllMerge() join uu in _defaultTableDbContext.Set() diff --git a/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs b/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs index 7130b8f4..45b1cee2 100644 --- a/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs +++ b/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs @@ -11,10 +11,15 @@ namespace Sample.MySql.Shardings { throw new NotImplementedException(); } - - public override List GetAllDataSourceNames() + //只返回当前和历史 + private static readonly List tails = new List() { - throw new NotImplementedException(); + "current", + "history" + }; + public override List GetAllDataSourceNames() + { + return tails; } public override bool AddDataSourceName(string dataSourceName) @@ -29,6 +34,7 @@ namespace Sample.MySql.Shardings public override Func GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator) { + //判断过滤查询历史还是现在 throw new NotImplementedException(); } } diff --git a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs index 1cf9c401..ac04e7dd 100644 --- a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs +++ b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs @@ -7,6 +7,7 @@ using Sample.MySql.Domain.Entities; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; +using ShardingCore.Helpers; using ShardingCore.VirtualRoutes.Months; namespace Sample.MySql.Shardings @@ -41,27 +42,42 @@ namespace Sample.MySql.Shardings builder.ShardingProperty(o => o.Time); } - // protected override List AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List shardingRouteUnits) - // { - // 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 AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List shardingRouteUnits) + protected override bool RouteIgnoreDataSource => false; + public override Func GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator) { - Console.WriteLine("AfterShardingRouteUnitFilter:"+shardingRouteUnits.Count); - if (shardingRouteUnits.Count > 10)//如果本次命中表过多 + var currentMonthFirstDay = ShardingCoreHelper.GetCurrentMonthFirstDay(DateTime.Now); + var matchDataSource = currentMonthFirstDay + { + 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); } } } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 08a4579c..684dd8ca 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -159,6 +159,12 @@ namespace Sample.MySql builder .AddInterceptors(new MySaveChangeInterceptor()); }); + o.AddDefaultDataSource("current", + "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); + o.AddExtraDataSource(sp => new Dictionary() + { + { "history", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" } + }); o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); o.AddExtraDataSource(sp => new Dictionary() diff --git a/src/ShardingCore/Sharding/EntityQueryConfigurations/EntityQueryBuilder.cs b/src/ShardingCore/Sharding/EntityQueryConfigurations/EntityQueryBuilder.cs index 4f1da019..31ca3638 100644 --- a/src/ShardingCore/Sharding/EntityQueryConfigurations/EntityQueryBuilder.cs +++ b/src/ShardingCore/Sharding/EntityQueryConfigurations/EntityQueryBuilder.cs @@ -41,19 +41,19 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations /// /// 使用当前属性order和comparer有关联 /// ShardingTailComparer参数 tailComparer如果是正序,reverse是false那么表示ShardingTailComparer最后采用倒序 - /// whenAscIsSameAsShardingTailComparer,true表示当前添加的属性也是采用倒序,false表示当前添加的属性使用正序 + /// sameAsShardingTailComparer,true表示当前添加的属性也是采用倒序,false表示当前添加的属性使用正序 /// 如果不添加AddOrder方法默认采用ShardingTailComparer在这个例子里就是倒序 /// /// /// - /// + /// /// true:当前属性正序和comparer正序一样,false:当前属性倒序和comparer正序一样 /// /// /// - public EntityQueryBuilder AddOrder(Expression> primaryOrderPropertyExpression,bool whenAscIsSameAsShardingTailComparer = true,SeqOrderMatchEnum seqOrderMatch=SeqOrderMatchEnum.Owner) + public EntityQueryBuilder AddOrder(Expression> primaryOrderPropertyExpression,bool sameAsShardingTailComparer = true,SeqOrderMatchEnum seqOrderMatch=SeqOrderMatchEnum.Owner) { - _entityQueryMetadata.AddSeqComparerOrder(primaryOrderPropertyExpression.GetPropertyAccess().Name, whenAscIsSameAsShardingTailComparer, seqOrderMatch); + _entityQueryMetadata.AddSeqComparerOrder(primaryOrderPropertyExpression.GetPropertyAccess().Name, sameAsShardingTailComparer, seqOrderMatch); return this; } /// diff --git a/test/ShardingCore.Test/Startup.cs b/test/ShardingCore.Test/Startup.cs index 6f018ecb..1480504c 100644 --- a/test/ShardingCore.Test/Startup.cs +++ b/test/ShardingCore.Test/Startup.cs @@ -106,6 +106,7 @@ namespace ShardingCore.Test } }; }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); + }) .AddShardingCore(); }