基本架构已经完成除了部分代码需要重写,包括去掉virtualdatasourcemanager

This commit is contained in:
xuejiaming 2022-06-30 13:52:31 +08:00
parent e8596eabdc
commit 32ae409159
78 changed files with 1213 additions and 1259 deletions

View File

@ -10,13 +10,11 @@ using Microsoft.Extensions.Logging;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.EntityShardingMetadatas; using ShardingCore.Core.EntityShardingMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.ShardingConfigurations.Abstractions; using ShardingCore.Core.ShardingConfigurations.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Helpers; using ShardingCore.Helpers;
@ -48,14 +46,14 @@ namespace ShardingCore.Bootstrappers
private readonly Type _shardingEntityType; private readonly Type _shardingEntityType;
private readonly IShardingEntityConfigOptions _shardingEntityConfigOptions; private readonly IShardingEntityConfigOptions _shardingEntityConfigOptions;
private readonly IVirtualDataSourceRouteManager _virtualDataSourceRouteManager; private readonly IVirtualDataSourceRouteManager _virtualDataSourceRouteManager;
private readonly IVirtualTableManager _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IEntityMetadataManager _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IJobManager _jobManager; private readonly IJobManager _jobManager;
public EntityMetadataInitializer( public EntityMetadataInitializer(
IShardingEntityConfigOptions shardingEntityConfigOptions, IShardingEntityConfigOptions shardingEntityConfigOptions,
IVirtualDataSourceRouteManager virtualDataSourceRouteManager, IVirtualDataSourceRouteManager virtualDataSourceRouteManager,
IVirtualTableManager virtualTableManager, ITableRouteManager tableRouteManager,
IEntityMetadataManager entityMetadataManager, IEntityMetadataManager entityMetadataManager,
IJobManager jobManager IJobManager jobManager
) )
@ -66,7 +64,7 @@ namespace ShardingCore.Bootstrappers
// _queryFilterExpression = entityMetadataEnsureParams.EntityType.GetAnnotations().FirstOrDefault(o=>o.Name== QueryFilter)?.Value as Expression<Func<TEntity, bool>>; // _queryFilterExpression = entityMetadataEnsureParams.EntityType.GetAnnotations().FirstOrDefault(o=>o.Name== QueryFilter)?.Value as Expression<Func<TEntity, bool>>;
_shardingEntityConfigOptions = shardingEntityConfigOptions; _shardingEntityConfigOptions = shardingEntityConfigOptions;
_virtualDataSourceRouteManager = virtualDataSourceRouteManager; _virtualDataSourceRouteManager = virtualDataSourceRouteManager;
_virtualTableManager = virtualTableManager; _tableRouteManager = tableRouteManager;
_entityMetadataManager = entityMetadataManager; _entityMetadataManager = entityMetadataManager;
_jobManager = jobManager; _jobManager = jobManager;
} }
@ -122,9 +120,7 @@ namespace ShardingCore.Bootstrappers
createEntityMetadataTableConfiguration.Configure(entityMetadataTableBuilder); createEntityMetadataTableConfiguration.Configure(entityMetadataTableBuilder);
} }
//创建虚拟表 //创建虚拟表
var virtualTable = CreateVirtualTable(virtualTableRoute,entityMetadata); _tableRouteManager.AddRoute(virtualTableRoute);
InitVirtualTable(virtualTable);
_virtualTableManager.AddVirtualTable(virtualTable);
//检测校验分表分库对象元数据 //检测校验分表分库对象元数据
entityMetadata.CheckShardingTableMetadata(); entityMetadata.CheckShardingTableMetadata();
//添加任务 //添加任务
@ -149,18 +145,5 @@ namespace ShardingCore.Bootstrappers
var instance = ShardingRuntimeContext.GetInstance().CreateInstance(virtualRouteType); var instance = ShardingRuntimeContext.GetInstance().CreateInstance(virtualRouteType);
return (IVirtualTableRoute<TEntity>)instance; return (IVirtualTableRoute<TEntity>)instance;
} }
private IVirtualTable<TEntity> CreateVirtualTable(IVirtualTableRoute<TEntity> virtualTableRoute,EntityMetadata entityMetadata)
{
return new DefaultVirtualTable<TEntity>(virtualTableRoute, entityMetadata);
}
private void InitVirtualTable(IVirtualTable virtualTable)
{
foreach (var tail in virtualTable.GetVirtualRoute().GetTails())
{
var defaultPhysicTable = new DefaultPhysicTable(virtualTable, tail);
virtualTable.AddPhysicTable(defaultPhysicTable);
}
}
} }
} }

View File

@ -1,8 +1,11 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
@ -12,7 +15,7 @@ namespace ShardingCore.Core.EntityMetadatas
/// 分表或者分库对象的元数据信息记录对象在ShardingCore框架下的一些简单的信息 /// 分表或者分库对象的元数据信息记录对象在ShardingCore框架下的一些简单的信息
/// </summary> /// </summary>
public class EntityMetadata public class EntityMetadata
{ {private const string QueryFilter = "QueryFilter";
public EntityMetadata(Type entityType, Type shardingDbContextType) public EntityMetadata(Type entityType, Type shardingDbContextType)
{ {
EntityType = entityType; EntityType = entityType;
@ -24,7 +27,6 @@ namespace ShardingCore.Core.EntityMetadatas
/// 分表类型 sharding entity type /// 分表类型 sharding entity type
/// </summary> /// </summary>
public Type EntityType { get; } public Type EntityType { get; }
public Type ShardingDbContextType { get; } public Type ShardingDbContextType { get; }
@ -70,6 +72,36 @@ namespace ShardingCore.Core.EntityMetadatas
/// 分表隔离器 table sharding tail prefix /// 分表隔离器 table sharding tail prefix
/// </summary> /// </summary>
public string TableSeparator { get; private set; } = "_"; public string TableSeparator { get; private set; } = "_";
/// <summary>
/// 逻辑表名
/// </summary>
public string LogicTableName { get; private set; }
/// <summary>
/// 主键
/// </summary>
public IReadOnlyList<PropertyInfo> PrimaryKeyProperties { get; private set; }
/**
* efcore query filter
*/
public LambdaExpression QueryFilterExpression { get; private set; }
/// <summary>
/// 是否单主键
/// </summary>
public bool IsSingleKey { get; private set; }
public void SetEntityModel(IEntityType dbEntityType)
{
LogicTableName = dbEntityType.GetTableName();
QueryFilterExpression= dbEntityType.GetAnnotations().FirstOrDefault(o=>o.Name== QueryFilter)?.Value as LambdaExpression;
PrimaryKeyProperties = dbEntityType.FindPrimaryKey()?.Properties?.Select(o => o.PropertyInfo)?.ToList() ??
new List<PropertyInfo>();
IsSingleKey=PrimaryKeyProperties.Count == 1;
}
/// <summary> /// <summary>
/// 设置分库字段 /// 设置分库字段
/// </summary> /// </summary>

View File

@ -7,7 +7,7 @@ using ShardingCore.Core.ShardingPage.Abstractions;
using ShardingCore.Core.TrackerManagers; using ShardingCore.Core.TrackerManagers;
using ShardingCore.Core.UnionAllMergeShardingProviders.Abstractions; using ShardingCore.Core.UnionAllMergeShardingProviders.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
@ -25,13 +25,12 @@ namespace ShardingCore.Core
IDbContextCreator GetDbContextCreator(); IDbContextCreator GetDbContextCreator();
IEntityMetadataManager GetEntityMetadataManager(); IEntityMetadataManager GetEntityMetadataManager();
IVirtualDataSourceManager GetVirtualDataSourceManager(); IVirtualDataSourceManager GetVirtualDataSourceManager();
IVirtualTableManager GetVirtualTableManager(); ITableRouteManager GetTableRouteManager();
IRouteTailFactory GetRouteTailFactory(); IRouteTailFactory GetRouteTailFactory();
IQueryTracker GetQueryTracker(); IQueryTracker GetQueryTracker();
IUnionAllMergeManager GetUnionAllMergeManager(); IUnionAllMergeManager GetUnionAllMergeManager();
IShardingPageManager GetShardingPageManager(); IShardingPageManager GetShardingPageManager();
IShardingRuntimeModel GetShardingRuntimeModel(); void GetOrCreateShardingRuntimeModel(DbContext dbContext);
IShardingRuntimeModel GetOrCreateShardingRuntimeModel(DbContext dbContext);
object GetService(Type serviceType); object GetService(Type serviceType);
TService GetService<TService>(); TService GetService<TService>();
} }

View File

@ -1,97 +1,97 @@
using System; // using System;
using ShardingCore.Core.EntityMetadatas; // using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualTables; // using ShardingCore.Core.VirtualTables;
//
namespace ShardingCore.Core.PhysicTables // namespace ShardingCore.Core.PhysicTables
{ // {
/* // /*
* @Author: xjm // * @Author: xjm
* @Description: // * @Description:
* @Date: Friday, 18 December 2020 13:57:50 // * @Date: Friday, 18 December 2020 13:57:50
* @Email: 326308290@qq.com // * @Email: 326308290@qq.com
*/ // */
/// <summary> // /// <summary>
/// 默认的物理表 // /// 默认的物理表
/// </summary> // /// </summary>
public class DefaultPhysicTable : IPhysicTable // public class DefaultPhysicTable : IPhysicTable
{ // {
/// <summary> // /// <summary>
/// 物理表 // /// 物理表
/// </summary> // /// </summary>
/// <param name="virtualTable">虚拟表</param> // /// <param name="virtualTable">虚拟表</param>
/// <param name="tail">物理表表后缀</param> // /// <param name="tail">物理表表后缀</param>
public DefaultPhysicTable(IVirtualTable virtualTable, string tail) // public DefaultPhysicTable(IVirtualTable virtualTable, string tail)
{ // {
VirtualTable = virtualTable; // VirtualTable = virtualTable;
// OriginalName = virtualTable.GetVirtualTableName(); // // OriginalName = virtualTable.GetVirtualTableName();
Tail = tail; // Tail = tail;
EntityMetadata = VirtualTable.EntityMetadata; // EntityMetadata = VirtualTable.EntityMetadata;
EntityType = EntityMetadata.EntityType; // EntityType = EntityMetadata.EntityType;
TableSeparator = EntityMetadata.TableSeparator; // TableSeparator = EntityMetadata.TableSeparator;
} // }
//
/// <summary> // /// <summary>
/// 元数据对象 // /// 元数据对象
/// </summary> // /// </summary>
public EntityMetadata EntityMetadata { get; } // public EntityMetadata EntityMetadata { get; }
// /// <summary> // // /// <summary>
// /// 全表名称 // // /// 全表名称
// /// </summary> // // /// </summary>
// public string FullName => $"{OriginalName}{TableSeparator}{Tail}"; // // public string FullName => $"{OriginalName}{TableSeparator}{Tail}";
// /// <summary> // // /// <summary>
// /// 原始表名 // // /// 原始表名
// /// </summary> // // /// </summary>
// public string OriginalName { get; } // // public string OriginalName { get; }
/// <summary> // /// <summary>
/// 分表的表名和后置的连接器默认为下划线"_" 可以为空 // /// 分表的表名和后置的连接器默认为下划线"_" 可以为空
/// </summary> // /// </summary>
public string TableSeparator { get; } // public string TableSeparator { get; }
/// <summary> // /// <summary>
/// 分表后缀 // /// 分表后缀
/// </summary> // /// </summary>
public string Tail { get; } // public string Tail { get; }
/// <summary> // /// <summary>
/// 类型对象 // /// 类型对象
/// </summary> // /// </summary>
public Type EntityType { get; } // public Type EntityType { get; }
/// <summary> // /// <summary>
/// 所属虚拟表 // /// 所属虚拟表
/// </summary> // /// </summary>
public IVirtualTable VirtualTable { get; } // public IVirtualTable VirtualTable { get; }
protected bool Equals(DefaultPhysicTable other) // protected bool Equals(DefaultPhysicTable other)
{ // {
return Tail == other.Tail && EntityType == other.EntityType; // return Tail == other.Tail && EntityType == other.EntityType;
} // }
//
public override bool Equals(object obj) // public override bool Equals(object obj)
{ // {
if (ReferenceEquals(null, obj)) return false; // if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true; // if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false; // if (obj.GetType() != this.GetType()) return false;
return Equals((DefaultPhysicTable)obj); // return Equals((DefaultPhysicTable)obj);
} // }
//
#if !EFCORE2 // #if !EFCORE2
//
public override int GetHashCode() // public override int GetHashCode()
{ // {
return HashCode.Combine(TableSeparator, Tail, VirtualTable); // return HashCode.Combine(TableSeparator, Tail, VirtualTable);
} // }
#endif // #endif
//
#if EFCORE2 // #if EFCORE2
//
public override int GetHashCode() // public override int GetHashCode()
{ // {
unchecked // unchecked
{ // {
var hashCode = (TableSeparator != null ? TableSeparator.GetHashCode() : 0); // var hashCode = (TableSeparator != null ? TableSeparator.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Tail != null ? Tail.GetHashCode() : 0); // hashCode = (hashCode * 397) ^ (Tail != null ? Tail.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (VirtualTable != null ? VirtualTable.GetHashCode() : 0); // hashCode = (hashCode * 397) ^ (VirtualTable != null ? VirtualTable.GetHashCode() : 0);
return hashCode; // return hashCode;
} // }
} // }
#endif // #endif
//
} // }
} // }

View File

@ -1,48 +1,48 @@
using System; // using System;
using ShardingCore.Core.EntityMetadatas; // using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualTables; // using ShardingCore.Core.VirtualTables;
//
/* // /*
* @Author: xjm // * @Author: xjm
* @Description: // * @Description:
* @Date: Friday, 18 December 2020 13:54:46 // * @Date: Friday, 18 December 2020 13:54:46
* @Email: 326308290@qq.com // * @Email: 326308290@qq.com
*/ // */
namespace ShardingCore.Core.PhysicTables // namespace ShardingCore.Core.PhysicTables
{ // {
/// <summary> // /// <summary>
/// 物理表接口 // /// 物理表接口
/// </summary> // /// </summary>
public interface IPhysicTable // public interface IPhysicTable
{ // {
/// <summary> // /// <summary>
/// 对象信息 // /// 对象信息
/// </summary> // /// </summary>
EntityMetadata EntityMetadata { get; } // EntityMetadata EntityMetadata { get; }
// /// <summary> // // /// <summary>
// /// 表全称 // // /// 表全称
// /// </summary> // // /// </summary>
// string FullName { get; } // // string FullName { get; }
// /// <summary> // // /// <summary>
// /// 原表名称 // // /// 原表名称
// /// </summary> // // /// </summary>
// string OriginalName { get; } // // string OriginalName { get; }
/// <summary> // /// <summary>
/// 尾巴前缀 tail prefix // /// 尾巴前缀 tail prefix
/// </summary> // /// </summary>
string TableSeparator { get; } // string TableSeparator { get; }
/// <summary> // /// <summary>
/// 尾巴 // /// 尾巴
/// </summary> // /// </summary>
string Tail { get; } // string Tail { get; }
/// <summary> // /// <summary>
/// 映射类类型 // /// 映射类类型
/// </summary> // /// </summary>
Type EntityType { get; } // Type EntityType { get; }
/// <summary> // /// <summary>
/// 所属虚拟表 // /// 所属虚拟表
/// </summary> // /// </summary>
IVirtualTable VirtualTable { get; } // IVirtualTable VirtualTable { get; }
//
} // }
} // }

View File

@ -1,9 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.QueryRouteManagers.Abstractions namespace ShardingCore.Core.QueryRouteManagers.Abstractions
{ {

View File

@ -6,15 +6,9 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.ShardingConfigurations;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ShardingComparision; using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Sharding.ShardingComparision.Abstractions; using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
{ {
@ -34,11 +28,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
public virtual ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; } public virtual ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; }
public virtual IShardingComparer ShardingComparer { get; } = new CSharpLanguageShardingComparer(); public virtual IShardingComparer ShardingComparer { get; } = new CSharpLanguageShardingComparer();
public virtual ITableEnsureManager TableEnsureManager { get; } =
new EmptyTableEnsureManager();
public abstract DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString, public abstract DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString,
DbContextOptionsBuilder dbContextOptionsBuilder); DbContextOptionsBuilder dbContextOptionsBuilder);

View File

@ -4,7 +4,6 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ShardingComparision.Abstractions; using ShardingCore.Sharding.ShardingComparision.Abstractions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using ShardingCore.TableExists.Abstractions;
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
{ {
@ -56,10 +55,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
/// </summary> /// </summary>
IShardingComparer ShardingComparer { get; } IShardingComparer ShardingComparer { get; }
/// <summary> /// <summary>
/// 表确认管理者
/// </summary>
ITableEnsureManager TableEnsureManager { get; }
/// <summary>
/// 如何根据connectionString 配置 DbContextOptionsBuilder /// 如何根据connectionString 配置 DbContextOptionsBuilder
/// </summary> /// </summary>
/// <param name="connectionString"></param> /// <param name="connectionString"></param>

View File

@ -2,12 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.ShardingConfigurations;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;

View File

@ -1,12 +1,9 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.ShardingConfigurations; using ShardingCore.Core.ShardingConfigurations;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ShardingComparision; using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Sharding.ShardingComparision.Abstractions; using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -34,7 +31,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
public override int? ReadWriteDefaultPriority { get; } public override int? ReadWriteDefaultPriority { get; }
public override ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; } public override ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; }
public override IShardingComparer ShardingComparer { get; } public override IShardingComparer ShardingComparer { get; }
public override ITableEnsureManager TableEnsureManager { get; }
public SimpleVirtualDataSourceConfigurationParams(IServiceProvider serviceProvider,ShardingConfigOptions options) public SimpleVirtualDataSourceConfigurationParams(IServiceProvider serviceProvider,ShardingConfigOptions options)
{ {
@ -49,8 +45,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
ExtraDataSources = options.DataSourcesConfigure?.Invoke(serviceProvider)??new ConcurrentDictionary<string, string>(); ExtraDataSources = options.DataSourcesConfigure?.Invoke(serviceProvider)??new ConcurrentDictionary<string, string>();
ShardingComparer = options.ReplaceShardingComparerFactory?.Invoke(serviceProvider) ?? ShardingComparer = options.ReplaceShardingComparerFactory?.Invoke(serviceProvider) ??
new CSharpLanguageShardingComparer(); new CSharpLanguageShardingComparer();
TableEnsureManager = options.TableEnsureManagerFactory?.Invoke(serviceProvider) ??
new EmptyTableEnsureManager();
if (options.ShardingReadWriteSeparationOptions != null) if (options.ShardingReadWriteSeparationOptions != null)
{ {
if (options.ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure != null) if (options.ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure != null)

View File

@ -12,7 +12,6 @@ using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding; using ShardingCore.Sharding;

View File

@ -33,9 +33,9 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
public IVirtualDataSourceRoute GetRoute(Type entityType) public IVirtualDataSourceRoute GetRoute(Type entityType)
{ {
if (!_entityMetadataManager.IsShardingDataSource(entityType)) // if (!_entityMetadataManager.IsShardingDataSource(entityType))
throw new ShardingCoreInvalidOperationException( // throw new ShardingCoreInvalidOperationException(
$"entity type :[{entityType.FullName}] not configure sharding data source"); // $"entity type :[{entityType.FullName}] not configure sharding data source");
if (!_dataSourceVirtualRoutes.TryGetValue(entityType, out var dataSourceVirtualRoute)) if (!_dataSourceVirtualRoutes.TryGetValue(entityType, out var dataSourceVirtualRoute))
throw new ShardingCoreInvalidOperationException( throw new ShardingCoreInvalidOperationException(

View File

@ -1,132 +1,132 @@
using ShardingCore.Core.PhysicTables; // using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes; // using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Exceptions; // using ShardingCore.Exceptions;
using ShardingCore.Extensions; // using ShardingCore.Extensions;
using ShardingCore.Sharding.PaginationConfigurations; // using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Utils; // using ShardingCore.Utils;
using System; // using System;
using System.Collections.Concurrent; // using System.Collections.Concurrent;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Linq; // using System.Linq;
using System.Linq.Expressions; // using System.Linq.Expressions;
using ShardingCore.Core.EntityMetadatas; // using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.ShardingEnumerableQueries; // using ShardingCore.Core.ShardingEnumerableQueries;
using ShardingCore.Core.VirtualDatabase; // using ShardingCore.Core.VirtualDatabase;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; // using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; // using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Sharding.EntityQueryConfigurations; // using ShardingCore.Sharding.EntityQueryConfigurations;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions; // using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
//
namespace ShardingCore.Core.VirtualTables // namespace ShardingCore.Core.VirtualTables
{ // {
/* // /*
* @Author: xjm // * @Author: xjm
* @Description: // * @Description:
* @Date: Friday, 18 December 2020 14:20:12 // * @Date: Friday, 18 December 2020 14:20:12
* @Email: 326308290@qq.com // * @Email: 326308290@qq.com
*/ // */
/// <summary> // /// <summary>
/// 同数据库虚拟表 // /// 同数据库虚拟表
/// </summary> // /// </summary>
/// <typeparam name="T"></typeparam> // /// <typeparam name="T"></typeparam>
public class DefaultVirtualTable<T> : IVirtualTable<T> where T : class // public class DefaultVirtualTable<T> : IVirtualTable<T> where T : class
{ // {
public EntityMetadata EntityMetadata { get; } // public EntityMetadata EntityMetadata { get; }
private readonly IVirtualTableRoute<T> _virtualTableRoute; // private readonly IVirtualTableRoute<T> _virtualTableRoute;
//
/// <summary> // /// <summary>
/// 分页配置 // /// 分页配置
/// </summary> // /// </summary>
public PaginationMetadata PaginationMetadata { get; } // public PaginationMetadata PaginationMetadata { get; }
//
/// <summary> // /// <summary>
/// 是否启用智能分页 // /// 是否启用智能分页
/// </summary> // /// </summary>
public bool EnablePagination => PaginationMetadata != null; // public bool EnablePagination => PaginationMetadata != null;
/// <summary> // /// <summary>
/// 查询配置 // /// 查询配置
/// </summary> // /// </summary>
public EntityQueryMetadata EntityQueryMetadata { get; } // public EntityQueryMetadata EntityQueryMetadata { get; }
/// <summary> // /// <summary>
/// 是否启用表达式分片配置 // /// 是否启用表达式分片配置
/// </summary> // /// </summary>
public bool EnableEntityQuery => EntityQueryMetadata != null; // public bool EnableEntityQuery => EntityQueryMetadata != null;
//
//
private readonly ConcurrentDictionary<IPhysicTable, object> _physicTables = new ConcurrentDictionary<IPhysicTable, object>(); // private readonly ConcurrentDictionary<IPhysicTable, object> _physicTables = new ConcurrentDictionary<IPhysicTable, object>();
//
public DefaultVirtualTable(IVirtualTableRoute<T> virtualTableRoute,EntityMetadata entityMetadata) // public DefaultVirtualTable(IVirtualTableRoute<T> virtualTableRoute,EntityMetadata entityMetadata)
{ // {
EntityMetadata = entityMetadata; // EntityMetadata = entityMetadata;
_virtualTableRoute = virtualTableRoute; // _virtualTableRoute = virtualTableRoute;
var paginationConfiguration = virtualTableRoute.CreatePaginationConfiguration(); // var paginationConfiguration = virtualTableRoute.CreatePaginationConfiguration();
if (paginationConfiguration!=null) // if (paginationConfiguration!=null)
{ // {
PaginationMetadata = new PaginationMetadata(); // PaginationMetadata = new PaginationMetadata();
var paginationBuilder = new PaginationBuilder<T>(PaginationMetadata); // var paginationBuilder = new PaginationBuilder<T>(PaginationMetadata);
paginationConfiguration.Configure(paginationBuilder); // paginationConfiguration.Configure(paginationBuilder);
} // }
//
var entityQueryConfiguration = virtualTableRoute.CreateEntityQueryConfiguration(); // var entityQueryConfiguration = virtualTableRoute.CreateEntityQueryConfiguration();
if (entityQueryConfiguration != null) // if (entityQueryConfiguration != null)
{ // {
EntityQueryMetadata = new EntityQueryMetadata(); // EntityQueryMetadata = new EntityQueryMetadata();
var entityQueryBuilder = new EntityQueryBuilder<T>(EntityQueryMetadata); // var entityQueryBuilder = new EntityQueryBuilder<T>(EntityQueryMetadata);
entityQueryConfiguration.Configure(entityQueryBuilder); // entityQueryConfiguration.Configure(entityQueryBuilder);
} // }
} // }
//
public List<IPhysicTable> GetAllPhysicTables() // public List<IPhysicTable> GetAllPhysicTables()
{ // {
return _physicTables.Keys.ToList(); // return _physicTables.Keys.ToList();
} // }
//
public List<ISqlRouteUnit> RouteTo(DataSourceRouteResult dataSourceRouteResult,ShardingTableRouteConfig tableRouteConfig) // public List<ISqlRouteUnit> RouteTo(DataSourceRouteResult dataSourceRouteResult,ShardingTableRouteConfig tableRouteConfig)
{ // {
var route = _virtualTableRoute; // var route = _virtualTableRoute;
if (tableRouteConfig.UseQueryable()) // if (tableRouteConfig.UseQueryable())
return route.RouteWithPredicate(GetAllPhysicTables(), tableRouteConfig.GetQueryable(), true); // return route.RouteWithPredicate(GetAllPhysicTables(), tableRouteConfig.GetQueryable(), true);
if (tableRouteConfig.UsePredicate()) // if (tableRouteConfig.UsePredicate())
return route.RouteWithPredicate(GetAllPhysicTables(), new ShardingEmptyEnumerableQuery<T>((Expression<Func<T, bool>>)tableRouteConfig.GetPredicate()).EmptyQueryable(), false); // return route.RouteWithPredicate(GetAllPhysicTables(), new ShardingEmptyEnumerableQuery<T>((Expression<Func<T, bool>>)tableRouteConfig.GetPredicate()).EmptyQueryable(), false);
object shardingKeyValue = null; // object shardingKeyValue = null;
if (tableRouteConfig.UseValue()) // if (tableRouteConfig.UseValue())
shardingKeyValue = tableRouteConfig.GetShardingKeyValue(); // shardingKeyValue = tableRouteConfig.GetShardingKeyValue();
//
if (tableRouteConfig.UseEntity()) // if (tableRouteConfig.UseEntity())
shardingKeyValue = tableRouteConfig.GetShardingEntity().GetPropertyValue(EntityMetadata.ShardingTableProperty.Name); // shardingKeyValue = tableRouteConfig.GetShardingEntity().GetPropertyValue(EntityMetadata.ShardingTableProperty.Name);
//
if (shardingKeyValue == null) // if (shardingKeyValue == null)
throw new ShardingCoreException(" route entity queryable or sharding key value is null "); // throw new ShardingCoreException(" route entity queryable or sharding key value is null ");
var routeWithValue = route.RouteWithValue(GetAllPhysicTables(), shardingKeyValue); // var routeWithValue = route.RouteWithValue(GetAllPhysicTables(), shardingKeyValue);
return new List<IPhysicTable>(1) { routeWithValue }; // return new List<IPhysicTable>(1) { routeWithValue };
} // }
//
//
public bool AddPhysicTable(IPhysicTable physicTable) // public bool AddPhysicTable(IPhysicTable physicTable)
{ // {
if (physicTable.EntityType != EntityMetadata.EntityType) // if (physicTable.EntityType != EntityMetadata.EntityType)
throw new ShardingCoreInvalidOperationException($"virtual table entity type :[{EntityMetadata.EntityType.FullName}] physic table entity type:[{physicTable.EntityType.FullName}]"); // throw new ShardingCoreInvalidOperationException($"virtual table entity type :[{EntityMetadata.EntityType.FullName}] physic table entity type:[{physicTable.EntityType.FullName}]");
return _physicTables.TryAdd(physicTable, null); // return _physicTables.TryAdd(physicTable, null);
} // }
//
// public string GetVirtualTableName() // // public string GetVirtualTableName()
// { // // {
// return EntityMetadata.VirtualTableName; // // return EntityMetadata.VirtualTableName;
// } // // }
//
IVirtualTableRoute IVirtualTable.GetVirtualRoute() // IVirtualTableRoute IVirtualTable.GetVirtualRoute()
{ // {
return GetVirtualRoute(); // return GetVirtualRoute();
} // }
//
public List<string> GetTableAllTails() // public List<string> GetTableAllTails()
{ // {
return _physicTables.Keys.Select(o => o.Tail).ToList(); // return _physicTables.Keys.Select(o => o.Tail).ToList();
} // }
//
public IVirtualTableRoute<T> GetVirtualRoute() // public IVirtualTableRoute<T> GetVirtualRoute()
{ // {
return _virtualTableRoute; // return _virtualTableRoute;
} // }
} // }
} // }

View File

@ -1,87 +1,87 @@
using System; // using System;
using System.Collections.Generic; // using System.Collections.Generic;
using ShardingCore.Bootstrappers; // using ShardingCore.Bootstrappers;
using ShardingCore.Core.EntityMetadatas; // using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables; // using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualDatabase; // using ShardingCore.Core.VirtualDatabase;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; // using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; // using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes; // using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Sharding.EntityQueryConfigurations; // using ShardingCore.Sharding.EntityQueryConfigurations;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions; // using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
using ShardingCore.Sharding.PaginationConfigurations; // using ShardingCore.Sharding.PaginationConfigurations;
//
namespace ShardingCore.Core.VirtualTables // namespace ShardingCore.Core.VirtualTables
{ // {
/* // /*
* @Author: xjm // * @Author: xjm
* @Description:ef-core的表 // * @Description:虚拟表在系统里面被映射为ef-core的表
* @Date: Friday, 18 December 2020 14:06:31 // * @Date: Friday, 18 December 2020 14:06:31
* @Email: 326308290@qq.com // * @Email: 326308290@qq.com
*/ // */
/// <summary> // /// <summary>
/// 虚拟表 // /// 虚拟表
/// </summary> // /// </summary>
public interface IVirtualTable // public interface IVirtualTable
{ // {
EntityMetadata EntityMetadata { get; } // EntityMetadata EntityMetadata { get; }
/// <summary> // /// <summary>
/// 分页配置 // /// 分页配置
/// </summary> // /// </summary>
PaginationMetadata PaginationMetadata { get; } // PaginationMetadata PaginationMetadata { get; }
/// <summary> // /// <summary>
/// 是否启用分页配置 // /// 是否启用分页配置
/// </summary> // /// </summary>
bool EnablePagination { get; } // bool EnablePagination { get; }
/// <summary> // /// <summary>
/// 查询配置 // /// 查询配置
/// </summary> // /// </summary>
EntityQueryMetadata EntityQueryMetadata { get; } // EntityQueryMetadata EntityQueryMetadata { get; }
/// <summary> // /// <summary>
/// 是否启用表达式分片配置 // /// 是否启用表达式分片配置
/// </summary> // /// </summary>
bool EnableEntityQuery { get; } // bool EnableEntityQuery { get; }
//
/// <summary> // /// <summary>
/// 获取所有的物理表 // /// 获取所有的物理表
/// </summary> // /// </summary>
/// <returns></returns> // /// <returns></returns>
List<IPhysicTable> GetAllPhysicTables(); // List<IPhysicTable> GetAllPhysicTables();
//
/// <summary> // /// <summary>
/// 路由到具体的物理表 which physic table route // /// 路由到具体的物理表 which physic table route
/// </summary> // /// </summary>
/// <param name="dataSourceRouteResult"></param> // /// <param name="dataSourceRouteResult"></param>
/// <param name="tableRouteConfig"></param> // /// <param name="tableRouteConfig"></param>
/// <returns></returns> // /// <returns></returns>
List<ISqlRouteUnit> RouteTo(DataSourceRouteResult dataSourceRouteResult,ShardingTableRouteConfig tableRouteConfig); // List<ISqlRouteUnit> RouteTo(DataSourceRouteResult dataSourceRouteResult,ShardingTableRouteConfig tableRouteConfig);
//
/// <summary> // /// <summary>
/// 添加物理表 add physic table // /// 添加物理表 add physic table
/// </summary> // /// </summary>
/// <param name="physicTable"></param> // /// <param name="physicTable"></param>
/// <returns>添加成功</returns> // /// <returns>添加成功</returns>
bool AddPhysicTable(IPhysicTable physicTable); // bool AddPhysicTable(IPhysicTable physicTable);
// /// <summary> // // /// <summary>
// /// 获取原始表名 get original table name // // /// 获取原始表名 get original table name
// /// </summary> // // /// </summary>
// /// <returns></returns> // // /// <returns></returns>
// string GetVirtualTableName(); // // string GetVirtualTableName();
/// <summary> // /// <summary>
/// 获取当前虚拟表的路由 get this virtual table route // /// 获取当前虚拟表的路由 get this virtual table route
/// </summary> // /// </summary>
/// <returns></returns> // /// <returns></returns>
IVirtualTableRoute GetVirtualRoute(); // IVirtualTableRoute GetVirtualRoute();
/// <summary> // /// <summary>
/// 获取启动时已经存在的表后缀 get this virtual table exists tails when app start // /// 获取启动时已经存在的表后缀 get this virtual table exists tails when app start
/// <see cref="ShardingBootstrapper"/> CreateDateTables // /// <see cref="ShardingBootstrapper"/> CreateDateTables
/// </summary> // /// </summary>
/// <returns></returns> // /// <returns></returns>
List<string> GetTableAllTails(); // List<string> GetTableAllTails();
} // }
//
public interface IVirtualTable<T> : IVirtualTable where T : class // public interface IVirtualTable<T> : IVirtualTable where T : class
{ // {
new IVirtualTableRoute<T> GetVirtualRoute(); // new IVirtualTableRoute<T> GetVirtualRoute();
} // }
} // }

View File

@ -1,60 +1,61 @@
using System; // using System;
using System.Collections.Generic; // using System.Collections.Generic;
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.PhysicTables; // using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualTables; // using ShardingCore.Core.VirtualTables;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
//
namespace ShardingCore.Core.VirtualDatabase.VirtualTables // namespace ShardingCore.Core.VirtualDatabase.VirtualTables
{ // {
/* // /*
* @Author: xjm // * @Author: xjm
* @Description: api // * @Description: 用于管理虚拟表并且提供简单的操作方法api
* @Date: Friday, 18 December 2020 14:10:03 // * @Date: Friday, 18 December 2020 14:10:03
* @Email: 326308290@qq.com // * @Email: 326308290@qq.com
*/ // */
public interface IVirtualTableManager // [Obsolete]
{ // public interface IVirtualTableManager
// {
/// <summary> //
/// 添加虚拟表应用启动时 add virtual table when app start // /// <summary>
/// </summary> // /// 添加虚拟表应用启动时 add virtual table when app start
/// <param name="virtualTable">虚拟表</param> // /// </summary>
bool AddVirtualTable(IVirtualTable virtualTable); // /// <param name="virtualTable">虚拟表</param>
// bool AddVirtualTable(IVirtualTable virtualTable);
/// <summary> //
/// 获取虚拟表 get virtual table by sharding entity type // /// <summary>
/// </summary> // /// 获取虚拟表 get virtual table by sharding entity type
/// <param name="shardingEntityType"></param> // /// </summary>
/// <returns></returns> // /// <param name="shardingEntityType"></param>
IVirtualTable GetVirtualTable(Type shardingEntityType); // /// <returns></returns>
/// <summary> // IVirtualTable GetVirtualTable(Type shardingEntityType);
/// 尝试获取虚拟表 // /// <summary>
/// </summary> // /// 尝试获取虚拟表
/// <param name="shardingEntityType"></param> // /// </summary>
/// <returns></returns> // /// <param name="shardingEntityType"></param>
IVirtualTable TryGetVirtualTable(Type shardingEntityType); // /// <returns></returns>
// IVirtualTable TryGetVirtualTable(Type shardingEntityType);
/// <summary> //
/// 获取所有的虚拟表 get all virtual table // /// <summary>
/// </summary> // /// 获取所有的虚拟表 get all virtual table
/// <returns></returns> // /// </summary>
ISet<IVirtualTable> GetAllVirtualTables(); // /// <returns></returns>
// ISet<IVirtualTable> GetAllVirtualTables();
//
/// <summary> //
/// 添加物理表 add physic table // /// <summary>
/// </summary> // /// 添加物理表 add physic table
/// <param name="virtualTable"></param> // /// </summary>
/// <param name="physicTable"></param> // /// <param name="virtualTable"></param>
bool AddPhysicTable(IVirtualTable virtualTable, IPhysicTable physicTable); // /// <param name="physicTable"></param>
// bool AddPhysicTable(IVirtualTable virtualTable, IPhysicTable physicTable);
//
/// <summary> //
/// 添加物理表 add physic table // /// <summary>
/// </summary> // /// 添加物理表 add physic table
/// <param name="shardingEntityType"></param> // /// </summary>
/// <param name="physicTable"></param> // /// <param name="shardingEntityType"></param>
bool AddPhysicTable(Type shardingEntityType, IPhysicTable physicTable); // /// <param name="physicTable"></param>
} // bool AddPhysicTable(Type shardingEntityType, IPhysicTable physicTable);
} // }
// }

View File

@ -1,226 +1,226 @@
using System; // using System;
using System.Collections.Concurrent; // using System.Collections.Concurrent;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Linq; // using System.Linq;
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.EntityMetadatas; // using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables; // using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualTables; // using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions; // using ShardingCore.Exceptions;
using ShardingCore.Extensions; // using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
//
namespace ShardingCore.Core.VirtualDatabase.VirtualTables // namespace ShardingCore.Core.VirtualDatabase.VirtualTables
{ // {
/* // /*
* @Author: xjm // * @Author: xjm
* @Description: // * @Description:
* @Date: Friday, 18 December 2020 14:52:42 // * @Date: Friday, 18 December 2020 14:52:42
* @Email: 326308290@qq.com // * @Email: 326308290@qq.com
*/ // */
/// <summary> // /// <summary>
/// 同一个数据库下的虚拟表管理者 // /// 同一个数据库下的虚拟表管理者
/// </summary> // /// </summary>
public class VirtualTableManager : IVirtualTableManager // public class VirtualTableManager : IVirtualTableManager
{ // {
private readonly IEntityMetadataManager _entityMetadataManager; // private readonly IEntityMetadataManager _entityMetadataManager;
//
/// <summary> // /// <summary>
/// {entityType,virtualTableType} // /// {entityType,virtualTableType}
/// </summary> // /// </summary>
private readonly ConcurrentDictionary<Type, IVirtualTable> _shardingVirtualTables = new ConcurrentDictionary<Type, IVirtualTable>(); // private readonly ConcurrentDictionary<Type, IVirtualTable> _shardingVirtualTables = new ConcurrentDictionary<Type, IVirtualTable>();
public VirtualTableManager(IEntityMetadataManager entityMetadataManager) // public VirtualTableManager(IEntityMetadataManager entityMetadataManager)
{ // {
_entityMetadataManager = entityMetadataManager; // _entityMetadataManager = entityMetadataManager;
} // }
//
public bool AddVirtualTable(IVirtualTable virtualTable) // public bool AddVirtualTable(IVirtualTable virtualTable)
{ // {
var result = _shardingVirtualTables.TryAdd(virtualTable.EntityMetadata.EntityType, virtualTable); // var result = _shardingVirtualTables.TryAdd(virtualTable.EntityMetadata.EntityType, virtualTable);
return result; // return result;
} // }
/// <summary> // /// <summary>
/// 获取对应的虚拟表 // /// 获取对应的虚拟表
/// </summary> // /// </summary>
/// <param name="shardingEntityType"></param> // /// <param name="shardingEntityType"></param>
/// <returns></returns> // /// <returns></returns>
public IVirtualTable GetVirtualTable(Type shardingEntityType) // public IVirtualTable GetVirtualTable(Type shardingEntityType)
{ // {
if (!_entityMetadataManager.IsShardingTable(shardingEntityType)) // if (!_entityMetadataManager.IsShardingTable(shardingEntityType))
throw new ShardingCoreInvalidOperationException(shardingEntityType.FullName); // throw new ShardingCoreInvalidOperationException(shardingEntityType.FullName);
if (!_shardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable)) // if (!_shardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable))
throw new ShardingCoreException($"virtual table not found entity: {shardingEntityType.FullName}"); // throw new ShardingCoreException($"virtual table not found entity: {shardingEntityType.FullName}");
return virtualTable; // return virtualTable;
} // }
//
public IVirtualTable TryGetVirtualTable(Type shardingEntityType) // public IVirtualTable TryGetVirtualTable(Type shardingEntityType)
{ // {
if (!_entityMetadataManager.IsShardingTable(shardingEntityType)) // if (!_entityMetadataManager.IsShardingTable(shardingEntityType))
throw new ShardingCoreInvalidOperationException(shardingEntityType.FullName); // throw new ShardingCoreInvalidOperationException(shardingEntityType.FullName);
if (!_shardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable)) // if (!_shardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable))
return null; // return null;
return virtualTable; // return virtualTable;
} // }
public ISet<IVirtualTable> GetAllVirtualTables() // public ISet<IVirtualTable> GetAllVirtualTables()
{ // {
return _shardingVirtualTables.Select(o => o.Value).ToHashSet(); // return _shardingVirtualTables.Select(o => o.Value).ToHashSet();
} // }
//
public bool AddPhysicTable(IVirtualTable virtualTable, IPhysicTable physicTable) // public bool AddPhysicTable(IVirtualTable virtualTable, IPhysicTable physicTable)
{ // {
return AddPhysicTable(virtualTable.EntityMetadata.EntityType, physicTable); // return AddPhysicTable(virtualTable.EntityMetadata.EntityType, physicTable);
} // }
//
public bool AddPhysicTable(Type shardingEntityType, IPhysicTable physicTable) // public bool AddPhysicTable(Type shardingEntityType, IPhysicTable physicTable)
{ // {
if (!_shardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable)) // if (!_shardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable))
throw new ShardingCoreException($"virtual table not found entity: {shardingEntityType.FullName}"); // throw new ShardingCoreException($"virtual table not found entity: {shardingEntityType.FullName}");
return virtualTable.AddPhysicTable(physicTable); // return virtualTable.AddPhysicTable(physicTable);
} // }
//
//
//
//
///// <summary> // ///// <summary>
///// {sharidngDbContextType:{entityType,virtualTableType}} // ///// {sharidngDbContextType:{entityType,virtualTableType}}
///// </summary> // ///// </summary>
//private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, IVirtualTable>> _shardingVirtualTables = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IVirtualTable>>(); // //private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, IVirtualTable>> _shardingVirtualTables = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IVirtualTable>>();
//private readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, IVirtualTable>> _shardingVirtualTaleVirtualTables = new ConcurrentDictionary<Type, ConcurrentDictionary<string, IVirtualTable>>(); // //private readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, IVirtualTable>> _shardingVirtualTaleVirtualTables = new ConcurrentDictionary<Type, ConcurrentDictionary<string, IVirtualTable>>();
//public VirtualTableManager() // //public VirtualTableManager()
//{ // //{
//
//} // //}
//
//private void CheckShardingDbContextType(Type shardingDbContextType) // //private void CheckShardingDbContextType(Type shardingDbContextType)
//{ // //{
// if (!shardingDbContextType.IsShardingDbContext()) // // if (!shardingDbContextType.IsShardingDbContext())
// throw new ShardingCoreException( // // throw new ShardingCoreException(
// $"{shardingDbContextType.FullName} must impl {nameof(IShardingDbContext)}"); // // $"{shardingDbContextType.FullName} must impl {nameof(IShardingDbContext)}");
//} // //}
//
//private void CheckShardingTableEntityType(Type shardingEntityType) // //private void CheckShardingTableEntityType(Type shardingEntityType)
//{ // //{
// if (!shardingEntityType.IsShardingTable()) // // if (!shardingEntityType.IsShardingTable())
// throw new ShardingCoreException( // // throw new ShardingCoreException(
// $"{shardingEntityType.FullName} must impl {nameof(IShardingTable)}"); // // $"{shardingEntityType.FullName} must impl {nameof(IShardingTable)}");
//} // //}
//private string CreateShardingEntityTypeKey(Type shardingDbContextType,Type entityType) // //private string CreateShardingEntityTypeKey(Type shardingDbContextType,Type entityType)
//{ // //{
// return $"{shardingDbContextType.FullName}{entityType.FullName}"; // // return $"{shardingDbContextType.FullName}{entityType.FullName}";
//} // //}
//private string CreateShardingTableNameKey(Type shardingDbContextType,string originalTableName) // //private string CreateShardingTableNameKey(Type shardingDbContextType,string originalTableName)
//{ // //{
// return $"{shardingDbContextType.FullName}{originalTableName}"; // // return $"{shardingDbContextType.FullName}{originalTableName}";
//} // //}
//
//public void AddVirtualTable(Type shardingDbContextType,IVirtualTable virtualTable) // //public void AddVirtualTable(Type shardingDbContextType,IVirtualTable virtualTable)
//{ // //{
// CheckShardingDbContextType(shardingDbContextType); // // CheckShardingDbContextType(shardingDbContextType);
//
// var innerShardingVirtualTables = _shardingVirtualTables.GetOrAdd(shardingDbContextType, // // var innerShardingVirtualTables = _shardingVirtualTables.GetOrAdd(shardingDbContextType,
// key => new ConcurrentDictionary<Type, IVirtualTable>()); // // key => new ConcurrentDictionary<Type, IVirtualTable>());
//
// if (!innerShardingVirtualTables.ContainsKey(virtualTable.EntityType)) // // if (!innerShardingVirtualTables.ContainsKey(virtualTable.EntityType))
// { // // {
// innerShardingVirtualTables.TryAdd(virtualTable.EntityType, virtualTable); // // innerShardingVirtualTables.TryAdd(virtualTable.EntityType, virtualTable);
// } // // }
//
// var innerShardingOriginalTableVirtualTables = _shardingVirtualTaleVirtualTables.GetOrAdd(shardingDbContextType,type=>new ConcurrentDictionary<string, IVirtualTable>()); // // var innerShardingOriginalTableVirtualTables = _shardingVirtualTaleVirtualTables.GetOrAdd(shardingDbContextType,type=>new ConcurrentDictionary<string, IVirtualTable>());
//
// if (!innerShardingOriginalTableVirtualTables.ContainsKey(virtualTable.GetVirtualTableName())) // // if (!innerShardingOriginalTableVirtualTables.ContainsKey(virtualTable.GetVirtualTableName()))
// { // // {
// innerShardingOriginalTableVirtualTables.TryAdd(virtualTable.GetVirtualTableName(), virtualTable); // // innerShardingOriginalTableVirtualTables.TryAdd(virtualTable.GetVirtualTableName(), virtualTable);
// } // // }
//} // //}
//
//public IVirtualTable GetVirtualTable(Type shardingDbContextType,Type shardingEntityType) // //public IVirtualTable GetVirtualTable(Type shardingDbContextType,Type shardingEntityType)
//{ // //{
// CheckShardingDbContextType(shardingDbContextType); // // CheckShardingDbContextType(shardingDbContextType);
// CheckShardingTableEntityType(shardingEntityType); // // CheckShardingTableEntityType(shardingEntityType);
//
// var shardingKey = CreateShardingEntityTypeKey(shardingDbContextType, shardingEntityType); // // var shardingKey = CreateShardingEntityTypeKey(shardingDbContextType, shardingEntityType);
// if(!_shardingVirtualTables.TryGetValue(shardingDbContextType,out var innerShardingVirtualTables) || innerShardingVirtualTables.IsEmpty()) // // if(!_shardingVirtualTables.TryGetValue(shardingDbContextType,out var innerShardingVirtualTables) || innerShardingVirtualTables.IsEmpty())
// throw new ShardingVirtualTableNotFoundException(shardingDbContextType.FullName); // // throw new ShardingVirtualTableNotFoundException(shardingDbContextType.FullName);
//
// if (!innerShardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable)||virtualTable==null) // // if (!innerShardingVirtualTables.TryGetValue(shardingEntityType, out var virtualTable)||virtualTable==null)
// throw new ShardingVirtualTableNotFoundException(shardingEntityType.FullName); // // throw new ShardingVirtualTableNotFoundException(shardingEntityType.FullName);
// return virtualTable; // // return virtualTable;
//} // //}
//
//
//public IVirtualTable<T> GetVirtualTable<TDbContext, T>() where T : class, IShardingTable where TDbContext : DbContext, IShardingDbContext // //public IVirtualTable<T> GetVirtualTable<TDbContext, T>() where T : class, IShardingTable where TDbContext : DbContext, IShardingDbContext
//{ // //{
// return (IVirtualTable<T>)GetVirtualTable(typeof(TDbContext), typeof(T)); // // return (IVirtualTable<T>)GetVirtualTable(typeof(TDbContext), typeof(T));
//} // //}
//
//public IVirtualTable GetVirtualTable(Type shardingDbContextType, string originalTableName) // //public IVirtualTable GetVirtualTable(Type shardingDbContextType, string originalTableName)
//{ // //{
// CheckShardingDbContextType(shardingDbContextType); // // CheckShardingDbContextType(shardingDbContextType);
// if (!_shardingVirtualTaleVirtualTables.TryGetValue(shardingDbContextType, out var innerShardingOriginalTableVirtualTables) || innerShardingOriginalTableVirtualTables.IsEmpty()) // // if (!_shardingVirtualTaleVirtualTables.TryGetValue(shardingDbContextType, out var innerShardingOriginalTableVirtualTables) || innerShardingOriginalTableVirtualTables.IsEmpty())
// throw new ShardingVirtualTableNotFoundException(shardingDbContextType.FullName); // // throw new ShardingVirtualTableNotFoundException(shardingDbContextType.FullName);
// if(!innerShardingOriginalTableVirtualTables.TryGetValue(originalTableName,out var virtualTable)|| virtualTable==null) // // if(!innerShardingOriginalTableVirtualTables.TryGetValue(originalTableName,out var virtualTable)|| virtualTable==null)
// throw new ShardingVirtualTableNotFoundException(originalTableName); // // throw new ShardingVirtualTableNotFoundException(originalTableName);
// return virtualTable; // // return virtualTable;
//} // //}
//
//public IVirtualTable GetVirtualTable<TDbContext>(string originalTableName) where TDbContext : DbContext, IShardingDbContext // //public IVirtualTable GetVirtualTable<TDbContext>(string originalTableName) where TDbContext : DbContext, IShardingDbContext
//{ // //{
// return GetVirtualTable(typeof(TDbContext),originalTableName); // // return GetVirtualTable(typeof(TDbContext),originalTableName);
//} // //}
//
//public IVirtualTable TryGetVirtualTable(Type shardingDbContextType,string originalTableName) // //public IVirtualTable TryGetVirtualTable(Type shardingDbContextType,string originalTableName)
//{ // //{
// CheckShardingDbContextType(shardingDbContextType); // // CheckShardingDbContextType(shardingDbContextType);
// if (!_shardingVirtualTaleVirtualTables.TryGetValue(shardingDbContextType, // // if (!_shardingVirtualTaleVirtualTables.TryGetValue(shardingDbContextType,
// out var innerShardingOriginalTableVirtualTables) || innerShardingOriginalTableVirtualTables.IsEmpty()) // // out var innerShardingOriginalTableVirtualTables) || innerShardingOriginalTableVirtualTables.IsEmpty())
// return null; // // return null;
// if (!innerShardingOriginalTableVirtualTables.TryGetValue(originalTableName, out var virtualTable) || virtualTable == null) // // if (!innerShardingOriginalTableVirtualTables.TryGetValue(originalTableName, out var virtualTable) || virtualTable == null)
// return null; // // return null;
// return virtualTable; // // return virtualTable;
//} // //}
//
//public IVirtualTable TryGetVirtualTablee<TDbContext>(string originalTableName) where TDbContext : DbContext, IShardingDbContext // //public IVirtualTable TryGetVirtualTablee<TDbContext>(string originalTableName) where TDbContext : DbContext, IShardingDbContext
//{ // //{
// return TryGetVirtualTable(typeof(TDbContext), originalTableName); // // return TryGetVirtualTable(typeof(TDbContext), originalTableName);
//} // //}
//
//
//public List<IVirtualTable> GetAllVirtualTables(Type shardingDbContextType) // //public List<IVirtualTable> GetAllVirtualTables(Type shardingDbContextType)
//{ // //{
// if (!_shardingVirtualTaleVirtualTables.TryGetValue(shardingDbContextType, // // if (!_shardingVirtualTaleVirtualTables.TryGetValue(shardingDbContextType,
// out var innerShardingOriginalTableVirtualTables) || innerShardingOriginalTableVirtualTables.IsEmpty()) // // out var innerShardingOriginalTableVirtualTables) || innerShardingOriginalTableVirtualTables.IsEmpty())
// return new List<IVirtualTable>(); // // return new List<IVirtualTable>();
// var keyPrefix = shardingDbContextType.FullName; // // var keyPrefix = shardingDbContextType.FullName;
// return innerShardingOriginalTableVirtualTables.Values.ToList(); // // return innerShardingOriginalTableVirtualTables.Values.ToList();
//} // //}
//
//public List<IVirtualTable> GetAllVirtualTables<TDbContext>() where TDbContext : DbContext, IShardingDbContext // //public List<IVirtualTable> GetAllVirtualTables<TDbContext>() where TDbContext : DbContext, IShardingDbContext
//{ // //{
// return GetAllVirtualTables(typeof(TDbContext)); // // return GetAllVirtualTables(typeof(TDbContext));
//} // //}
//
//public void AddPhysicTable(Type shardingDbContextType,IVirtualTable virtualTable, IPhysicTable physicTable) // //public void AddPhysicTable(Type shardingDbContextType,IVirtualTable virtualTable, IPhysicTable physicTable)
//{ // //{
// AddPhysicTable(shardingDbContextType, virtualTable.EntityType, physicTable); // // AddPhysicTable(shardingDbContextType, virtualTable.EntityType, physicTable);
//} // //}
//
//public void AddPhysicTable<TDbContext>(IVirtualTable virtualTable, IPhysicTable physicTable) where TDbContext : DbContext, IShardingDbContext // //public void AddPhysicTable<TDbContext>(IVirtualTable virtualTable, IPhysicTable physicTable) where TDbContext : DbContext, IShardingDbContext
//{ // //{
// AddPhysicTable(typeof(TDbContext),virtualTable.EntityType, physicTable); // // AddPhysicTable(typeof(TDbContext),virtualTable.EntityType, physicTable);
//} // //}
//
//
//public void AddPhysicTable(Type shardingDbContextType,Type shardingEntityType, IPhysicTable physicTable) // //public void AddPhysicTable(Type shardingDbContextType,Type shardingEntityType, IPhysicTable physicTable)
//{ // //{
// var virtualTable = GetVirtualTable(shardingDbContextType,shardingEntityType); // // var virtualTable = GetVirtualTable(shardingDbContextType,shardingEntityType);
// virtualTable.AddPhysicTable(physicTable); // // virtualTable.AddPhysicTable(physicTable);
//} // //}
//
//
//public void AddPhysicTable<TDbContext>(Type shardingEntityType, IPhysicTable physicTable) where TDbContext : DbContext, IShardingDbContext // //public void AddPhysicTable<TDbContext>(Type shardingEntityType, IPhysicTable physicTable) where TDbContext : DbContext, IShardingDbContext
//{ // //{
// var virtualTable = GetVirtualTable(typeof(TDbContext),shardingEntityType); // // var virtualTable = GetVirtualTable(typeof(TDbContext),shardingEntityType);
// virtualTable.AddPhysicTable(physicTable); // // virtualTable.AddPhysicTable(physicTable);
//} // //}
} // }
} // }

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.VirtualRoutes.Abstractions namespace ShardingCore.Core.VirtualRoutes.Abstractions
{ {
@ -10,5 +12,11 @@ namespace ShardingCore.Core.VirtualRoutes.Abstractions
bool HasRoute(Type entityType); bool HasRoute(Type entityType);
IVirtualTableRoute GetRoute(Type entityType); IVirtualTableRoute GetRoute(Type entityType);
List<IVirtualTableRoute> GetRoutes(); List<IVirtualTableRoute> GetRoutes();
bool AddRoute(IVirtualTableRoute route);
List<ShardingRouteUnit> RouteTo(Type entityType,
ShardingTableRouteConfig shardingTableRouteConfig);
List<ShardingRouteUnit> RouteTo(Type entityType,DataSourceRouteResult dataSourceRouteResult,
ShardingTableRouteConfig shardingTableRouteConfig);
} }
} }

View File

@ -1,10 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.QueryRouteManagers; using ShardingCore.Core.QueryRouteManagers;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;

View File

@ -2,9 +2,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Utils; using ShardingCore.Utils;

View File

@ -18,6 +18,9 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine
{ {
IntersectDataSources = intersectDataSources; IntersectDataSources = intersectDataSources;
} }
public DataSourceRouteResult(string dataSource):this(new HashSet<string>(){dataSource})
{
}
/// <summary> /// <summary>
/// 交集 /// 交集
/// </summary> /// </summary>

View File

@ -1,16 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.QueryRouteManagers; using ShardingCore.Core.QueryRouteManagers;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
{ {

View File

@ -1,4 +1,3 @@
using ShardingCore.Core.PhysicTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Utils; using ShardingCore.Utils;

View File

@ -1,5 +1,4 @@
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.ShardingConfigurations.Abstractions; using ShardingCore.Core.ShardingConfigurations.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Sharding.MergeEngines.ParallelControl; using ShardingCore.Sharding.MergeEngines.ParallelControl;
@ -23,13 +22,30 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce(); private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce();
public IShardingEntityConfigOptions EntityConfigOptions { get; private set; } public IShardingEntityConfigOptions EntityConfigOptions { get; private set; }
public PaginationMetadata PaginationMetadata { get; private set; }
public EntityQueryMetadata EntityQueryMetadata { get; private set; }
public virtual void Initialize(EntityMetadata entityMetadata) public virtual void Initialize(EntityMetadata entityMetadata)
{ {
if (!_doOnlyOnce.IsUnDo()) if (!_doOnlyOnce.IsUnDo())
throw new ShardingCoreInvalidOperationException("already init"); throw new ShardingCoreInvalidOperationException("already init");
EntityMetadata = entityMetadata; EntityMetadata = entityMetadata;
EntityConfigOptions =ShardingRuntimeContext.GetInstance().GetRequiredShardingEntityConfigOption(entityMetadata.ShardingDbContextType); EntityConfigOptions =ShardingRuntimeContext.GetInstance().GetRequiredShardingEntityConfigOption(entityMetadata.ShardingDbContextType);
var paginationConfiguration = CreatePaginationConfiguration();
if (paginationConfiguration!=null)
{
PaginationMetadata = new PaginationMetadata();
var paginationBuilder = new PaginationBuilder<T>(PaginationMetadata);
paginationConfiguration.Configure(paginationBuilder);
}
var entityQueryConfiguration = CreateEntityQueryConfiguration();
if (entityQueryConfiguration != null)
{
EntityQueryMetadata = new EntityQueryMetadata();
var entityQueryBuilder = new EntityQueryBuilder<T>(EntityQueryMetadata);
entityQueryConfiguration.Configure(entityQueryBuilder);
}
} }

View File

@ -2,10 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Sharding.EntityQueryConfigurations; using ShardingCore.Sharding.EntityQueryConfigurations;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
using ShardingCore.Sharding.PaginationConfigurations; using ShardingCore.Sharding.PaginationConfigurations;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes namespace ShardingCore.Core.VirtualRoutes.TableRoutes
@ -46,6 +44,24 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
List<string> GetTails(); List<string> GetTails();
/// <summary>
/// 分页配置
/// </summary>
PaginationMetadata PaginationMetadata { get; }
/// <summary>
/// 是否启用智能分页
/// </summary>
bool EnablePagination => PaginationMetadata != null;
/// <summary>
/// 查询配置
/// </summary>
EntityQueryMetadata EntityQueryMetadata { get; }
/// <summary>
/// 是否启用表达式分片配置
/// </summary>
bool EnableEntityQuery => EntityQueryMetadata != null;
} }

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Extensions; using ShardingCore.Extensions;

View File

@ -1,9 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualTables;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{ {

View File

@ -1,16 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.ShardingDatabaseProviders;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.MergeEngines.Common; using ShardingCore.Sharding.MergeEngines.Common;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions; using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
@ -26,17 +19,13 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
public class TableRouteRuleEngine : ITableRouteRuleEngine public class TableRouteRuleEngine : ITableRouteRuleEngine
{ {
private readonly ITableRouteManager _tableRouteManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IVirtualTableManager _virtualTableManager;
private readonly IEntityMetadataManager _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IShardingDatabaseProvider _shardingDatabaseProvider;
public TableRouteRuleEngine(ITableRouteManager tableRouteManager, IVirtualTableManager virtualTableManager, public TableRouteRuleEngine(ITableRouteManager tableRouteManager,
IEntityMetadataManager entityMetadataManager, IShardingDatabaseProvider shardingDatabaseProvider) IEntityMetadataManager entityMetadataManager)
{ {
_tableRouteManager = tableRouteManager; _tableRouteManager = tableRouteManager;
_virtualTableManager = virtualTableManager;
_entityMetadataManager = entityMetadataManager; _entityMetadataManager = entityMetadataManager;
_shardingDatabaseProvider = shardingDatabaseProvider;
} }
public ShardingRouteResult Route(TableRouteRuleContext tableRouteRuleContext) public ShardingRouteResult Route(TableRouteRuleContext tableRouteRuleContext)

View File

@ -1,12 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{ {

View File

@ -0,0 +1,90 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.ShardingEnumerableQueries;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes
{
public class TableRouteManager : ITableRouteManager
{
private readonly IVirtualDataSource _virtualDataSource;
private readonly ConcurrentDictionary<Type, IVirtualTableRoute> _tableRoutes = new();
public TableRouteManager(IVirtualDataSource virtualDataSource)
{
_virtualDataSource = virtualDataSource;
}
public bool HasRoute(Type entityType)
{
return _tableRoutes.ContainsKey(entityType);
}
public IVirtualTableRoute GetRoute(Type entityType)
{
if (!_tableRoutes.TryGetValue(entityType, out var tableRoute))
throw new ShardingCoreInvalidOperationException(
$"entity type :[{entityType.FullName}] not found table route");
return tableRoute;
}
public List<IVirtualTableRoute> GetRoutes()
{
return _tableRoutes.Values.ToList();
}
public bool AddRoute(IVirtualTableRoute route)
{
if (!route.EntityMetadata.IsShardingTable())
throw new ShardingCoreInvalidOperationException(
$"{route.EntityMetadata.EntityType.FullName} should configure sharding table");
return _tableRoutes.TryAdd(route.EntityMetadata.EntityType, route);
}
public List<ShardingRouteUnit> RouteTo(Type entityType, ShardingTableRouteConfig shardingTableRouteConfig)
{
var dataSourceRouteResult = new DataSourceRouteResult(_virtualDataSource.DefaultDataSourceName);
return RouteTo(entityType, dataSourceRouteResult, shardingTableRouteConfig);
}
public List<ShardingRouteUnit> RouteTo(Type entityType, DataSourceRouteResult dataSourceRouteResult,
ShardingTableRouteConfig tableRouteConfig)
{
var route = GetRoute(entityType);
if (tableRouteConfig.UseQueryable())
return route.RouteWithPredicate(dataSourceRouteResult, tableRouteConfig.GetQueryable(), true);
if (tableRouteConfig.UsePredicate())
{
var shardingEmptyEnumerableQuery = (IShardingEmptyEnumerableQuery)Activator.CreateInstance(
typeof(ShardingEmptyEnumerableQuery<>).GetGenericType0(entityType),
tableRouteConfig.GetPredicate());
return route.RouteWithPredicate(dataSourceRouteResult, shardingEmptyEnumerableQuery!.EmptyQueryable(),
false);
}
object shardingKeyValue = null;
if (tableRouteConfig.UseValue())
shardingKeyValue = tableRouteConfig.GetShardingKeyValue();
if (tableRouteConfig.UseEntity())
shardingKeyValue = tableRouteConfig.GetShardingEntity()
.GetPropertyValue(route.EntityMetadata.ShardingTableProperty.Name);
if (shardingKeyValue == null)
throw new ShardingCoreException(" route entity queryable or sharding key value is null ");
var shardingRouteUnit = route.RouteWithValue(dataSourceRouteResult, shardingKeyValue);
return new List<ShardingRouteUnit>(1) { shardingRouteUnit };
}
}
}

View File

@ -1,5 +1,4 @@
 using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Internal;
using ShardingCore.Core; using ShardingCore.Core;
@ -11,9 +10,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Utils; using ShardingCore.Utils;
@ -26,7 +23,7 @@ namespace ShardingCore.EFCores
* @Ver: 1.0 * @Ver: 1.0
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public class ShardingInternalDbSet<TEntity> : InternalDbSet<TEntity> public class ShardingInternalDbSet<TEntity> : InternalDbSet<TEntity>
where TEntity : class where TEntity : class
{ {
@ -38,11 +35,10 @@ namespace ShardingCore.EFCores
public ShardingInternalDbSet(DbContext context, string entityTypeName) : base(context, entityTypeName) public ShardingInternalDbSet(DbContext context, string entityTypeName) : base(context, entityTypeName)
{ {
_context = (IShardingDbContext)context; _context = (IShardingDbContext)context;
_shardingRuntimeContext=context.GetService<IShardingRuntimeContext>(); _shardingRuntimeContext = context.GetService<IShardingRuntimeContext>();
} }
#endif #endif
#if EFCORE2 || EFCORE3 #if EFCORE2 || EFCORE3
public ShardingInternalDbSet(DbContext context) : base(context) public ShardingInternalDbSet(DbContext context) : base(context)
{ {
_context = (IShardingDbContext)context; _context = (IShardingDbContext)context;
@ -62,22 +58,22 @@ namespace ShardingCore.EFCores
return _virtualDataSource; return _virtualDataSource;
} }
} }
private IVirtualTableManager _virtualTableManager;
protected IVirtualTableManager VirtualTableManager private ITableRouteManager _tableRouteManager;
protected ITableRouteManager TableRouteManager
{ {
get get
{ {
if (null == _virtualTableManager) if (null == _tableRouteManager)
{ {
_virtualTableManager = _shardingRuntimeContext.GetVirtualTableManager(); _tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
} }
return _virtualTableManager; return _tableRouteManager;
} }
} }
private IEntityMetadataManager _entityMetadataManager; private IEntityMetadataManager _entityMetadataManager;
protected IEntityMetadataManager EntityMetadataManager protected IEntityMetadataManager EntityMetadataManager
@ -86,7 +82,7 @@ namespace ShardingCore.EFCores
{ {
if (null == _entityMetadataManager) if (null == _entityMetadataManager)
{ {
_entityMetadataManager =_shardingRuntimeContext.GetEntityMetadataManager(); _entityMetadataManager = _shardingRuntimeContext.GetEntityMetadataManager();
} }
return _entityMetadataManager; return _entityMetadataManager;
@ -118,11 +114,11 @@ namespace ShardingCore.EFCores
{ {
var genericDbContext = _context.CreateGenericDbContext(entity); var genericDbContext = _context.CreateGenericDbContext(entity);
return await genericDbContext.Set<TEntity>().AddAsync(entity, cancellationToken); return await genericDbContext.Set<TEntity>().AddAsync(entity, cancellationToken);
} }
#endif #endif
#if EFCORE2 #if EFCORE2
public override async Task<EntityEntry<TEntity>> AddAsync(TEntity entity, CancellationToken cancellationToken = new CancellationToken()) public override async Task<EntityEntry<TEntity>> AddAsync(TEntity entity, CancellationToken cancellationToken =
new CancellationToken())
{ {
var genericDbContext = _context.CreateGenericDbContext(entity); var genericDbContext = _context.CreateGenericDbContext(entity);
return await genericDbContext.Set<TEntity>().AddAsync(entity, cancellationToken); return await genericDbContext.Set<TEntity>().AddAsync(entity, cancellationToken);
@ -176,11 +172,10 @@ namespace ShardingCore.EFCores
/// </summary> /// </summary>
public override void AddRange(params TEntity[] entities) public override void AddRange(params TEntity[] entities)
{ {
var aggregateToDic = AggregateToDic(entities); var aggregateToDic = AggregateToDic(entities);
foreach (var aggregateKv in aggregateToDic) foreach (var aggregateKv in aggregateToDic)
{ {
aggregateKv.Key.Set<TEntity>().AddRange(aggregateKv.Value); aggregateKv.Key.Set<TEntity>().AddRange(aggregateKv.Value);
} }
} }
@ -195,7 +190,7 @@ namespace ShardingCore.EFCores
var aggregateToDic = AggregateToDic(entities); var aggregateToDic = AggregateToDic(entities);
foreach (var aggregateKv in aggregateToDic) foreach (var aggregateKv in aggregateToDic)
{ {
await aggregateKv.Key.Set<TEntity>().AddRangeAsync(aggregateKv.Value); await aggregateKv.Key.Set<TEntity>().AddRangeAsync(aggregateKv.Value);
} }
} }
@ -270,11 +265,10 @@ namespace ShardingCore.EFCores
IEnumerable<TEntity> entities, IEnumerable<TEntity> entities,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var aggregateToDic = AggregateToDic(entities); var aggregateToDic = AggregateToDic(entities);
foreach (var aggregateKv in aggregateToDic) foreach (var aggregateKv in aggregateToDic)
{ {
await aggregateKv.Key.Set<TEntity>().AddRangeAsync(aggregateKv.Value,cancellationToken); await aggregateKv.Key.Set<TEntity>().AddRangeAsync(aggregateKv.Value, cancellationToken);
} }
} }
@ -309,7 +303,6 @@ namespace ShardingCore.EFCores
{ {
aggregateKv.Key.Set<TEntity>().RemoveRange(aggregateKv.Value); aggregateKv.Key.Set<TEntity>().RemoveRange(aggregateKv.Value);
} }
} }
/// <summary> /// <summary>
@ -329,7 +322,7 @@ namespace ShardingCore.EFCores
private Dictionary<DbContext, IEnumerable<TEntity>> AggregateToDic(IEnumerable<TEntity> entities) private Dictionary<DbContext, IEnumerable<TEntity>> AggregateToDic(IEnumerable<TEntity> entities)
{ {
return entities.Select(o => return entities.Select(o =>
{ {
var dbContext = _context.CreateGenericDbContext(o); var dbContext = _context.CreateGenericDbContext(o);
return new return new
@ -337,7 +330,7 @@ namespace ShardingCore.EFCores
DbContext = dbContext, DbContext = dbContext,
Entity = o Entity = o
}; };
}).GroupBy(g => g.DbContext).ToDictionary(o=>o.Key,o=>o.Select(g=>g.Entity)); }).GroupBy(g => g.DbContext).ToDictionary(o => o.Key, o => o.Select(g => g.Entity));
} }
public override TEntity Find(params object[] keyValues) public override TEntity Find(params object[] keyValues)
@ -347,6 +340,7 @@ namespace ShardingCore.EFCores
{ {
return primaryKeyFindDbContext.Set<TEntity>().Find(keyValues); return primaryKeyFindDbContext.Set<TEntity>().Find(keyValues);
} }
return base.Find(keyValues); return base.Find(keyValues);
} }
@ -358,6 +352,7 @@ namespace ShardingCore.EFCores
{ {
return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues); return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues);
} }
return base.FindAsync(keyValues); return base.FindAsync(keyValues);
} }
@ -368,6 +363,7 @@ namespace ShardingCore.EFCores
{ {
return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues, cancellationToken); return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues, cancellationToken);
} }
return base.FindAsync(keyValues, cancellationToken); return base.FindAsync(keyValues, cancellationToken);
} }
#endif #endif
@ -398,30 +394,26 @@ namespace ShardingCore.EFCores
if (keyValues.Length == 1) if (keyValues.Length == 1)
{ {
var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity)); var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity));
//单key字段 //单key字段
if (null != entityMetadata) if (null != entityMetadata && entityMetadata.IsSingleKey)
{ {
var shardingRuntimeModel = _shardingRuntimeContext.GetOrCreateShardingRuntimeModel((DbContext)_context); var isShardingDataSource = entityMetadata.IsShardingDataSource();
var shardingEntityType = shardingRuntimeModel.GetShardingEntityType(typeof(TEntity)); var shardingDataSourceFieldIsKey = entityMetadata.ShardingDataSourceFieldIsKey();
if(shardingEntityType!=null&&shardingEntityType.IsSingleKey) if (isShardingDataSource && !shardingDataSourceFieldIsKey)
return null;
var isShardingTable = entityMetadata.IsShardingTable();
var shardingTableFieldIsKey = entityMetadata.ShardingTableFieldIsKey();
if (isShardingTable && !shardingTableFieldIsKey)
return null;
var primaryKeyValue = keyValues[0];
if (primaryKeyValue != null)
{ {
var isShardingDataSource = entityMetadata.IsShardingDataSource(); var dataSourceName = GetDataSourceName(primaryKeyValue);
var shardingDataSourceFieldIsKey = entityMetadata.ShardingDataSourceFieldIsKey(); var tableTail = TableRouteManager.GetTableTail<TEntity>(primaryKeyValue);
if (isShardingDataSource && !shardingDataSourceFieldIsKey) var routeTail = _shardingRuntimeContext.GetRouteTailFactory().Create(tableTail);
return null; ;
var isShardingTable = entityMetadata.IsShardingTable(); return _context.GetDbContext(dataSourceName, false, routeTail);
var shardingTableFieldIsKey = entityMetadata.ShardingTableFieldIsKey();
if (isShardingTable && !shardingTableFieldIsKey)
return null;
var primaryKeyValue = keyValues[0];
if (primaryKeyValue != null)
{
var dataSourceName = GetDataSourceName(primaryKeyValue);
var tableTail = VirtualTableManager.GetTableTail<TEntity>(primaryKeyValue);
var routeTail = _shardingRuntimeContext.GetRouteTailFactory().Create(tableTail);;
return _context.GetDbContext(dataSourceName, false, routeTail);
}
} }
} }
} }
@ -431,11 +423,9 @@ namespace ShardingCore.EFCores
private string GetDataSourceName(object shardingKeyValue) private string GetDataSourceName(object shardingKeyValue)
{ {
if (!EntityMetadataManager.IsShardingDataSource(typeof(TEntity))) if (!EntityMetadataManager.IsShardingDataSource(typeof(TEntity)))
return VirtualDataSource.DefaultDataSourceName; return VirtualDataSource.DefaultDataSourceName;
return VirtualDataSource.GetDataSourceName<TEntity>(shardingKeyValue); return VirtualDataSource.GetDataSourceName<TEntity>(shardingKeyValue);
} }
} }
} }

View File

@ -9,11 +9,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Extensions;
using ShardingCore.Logger; using ShardingCore.Logger;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils; using ShardingCore.Utils;

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShardingCore.Exceptions
{
[ExcludeFromCodeCoverage]
public class ShardingCoreDataSourceQueryRouteNotMatchException : ShardingCoreQueryRouteNotMatchException
{
public ShardingCoreDataSourceQueryRouteNotMatchException(string message) : base(message)
{
}
public ShardingCoreDataSourceQueryRouteNotMatchException(string message, Exception innerException) : base(message, innerException)
{
}
}
}

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShardingCore.Exceptions
{
[ExcludeFromCodeCoverage]
public class ShardingCoreTableQueryRouteNotMatchException : ShardingCoreQueryRouteNotMatchException
{
public ShardingCoreTableQueryRouteNotMatchException(string message) : base(message)
{
}
public ShardingCoreTableQueryRouteNotMatchException(string message, Exception innerException) : base(message, innerException)
{
}
}
}

View File

@ -5,11 +5,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;
namespace ShardingCore.Extensions namespace ShardingCore.Extensions
{ {

View File

@ -8,15 +8,11 @@ using System.Text;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;
namespace ShardingCore.Extensions namespace ShardingCore.Extensions
{ {
@ -95,7 +91,7 @@ namespace ShardingCore.Extensions
var entityType = typeof(TEntity); var entityType = typeof(TEntity);
var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory(); var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
var virtualDataSource = shardingDbContext.GetVirtualDataSource(); var virtualDataSource = shardingDbContext.GetVirtualDataSource();
var virtualTableManager =shardingRuntimeContext.GetVirtualTableManager(); var tableRouteManager =shardingRuntimeContext.GetTableRouteManager();
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
var dataSourceNames = new Dictionary<string, Dictionary<string, BulkDicEntry<TEntity>>>(); var dataSourceNames = new Dictionary<string, Dictionary<string, BulkDicEntry<TEntity>>>();
var entitiesArray = entities as TEntity[] ?? entities.ToArray(); var entitiesArray = entities as TEntity[] ?? entities.ToArray();
@ -120,13 +116,12 @@ namespace ShardingCore.Extensions
var bulkDicEntries = new Dictionary<string, BulkDicEntry<TEntity>>(); var bulkDicEntries = new Dictionary<string, BulkDicEntry<TEntity>>();
dataSourceNames.Add(virtualDataSource.DefaultDataSourceName, bulkDicEntries); dataSourceNames.Add(virtualDataSource.DefaultDataSourceName, bulkDicEntries);
var virtualTable = virtualTableManager.GetVirtualTable(entityType); var tableRoute = tableRouteManager.GetRoute(entityType);
var virtualTableRoute = virtualTable.GetVirtualRoute(); var allTails = tableRoute.GetTails().ToHashSet();
var allTails = virtualTable.GetTableAllTails().ToHashSet();
foreach (var entity in entitiesArray) foreach (var entity in entitiesArray)
{ {
BulkShardingTableEnumerable(shardingDbContext, virtualDataSource.DefaultDataSourceName, bulkDicEntries, BulkShardingTableEnumerable(shardingDbContext, virtualDataSource.DefaultDataSourceName, bulkDicEntries,
routeTailFactory, virtualTable, virtualTableRoute, allTails, entity); routeTailFactory, tableRoute, allTails, entity);
} }
} }
else else
@ -135,14 +130,12 @@ namespace ShardingCore.Extensions
var allDataSourceNames = virtualDataSourceRoute.GetAllDataSourceNames().ToHashSet(); var allDataSourceNames = virtualDataSourceRoute.GetAllDataSourceNames().ToHashSet();
var entityMetadata = entityMetadataManager.TryGet(entityType); var entityMetadata = entityMetadataManager.TryGet(entityType);
IVirtualTable virtualTable = null; IVirtualTableRoute tableRoute = null;
IVirtualTableRoute virtualTableRoute = null;
ISet<string> allTails = null; ISet<string> allTails = null;
if (isShardingTable) if (isShardingTable)
{ {
virtualTable = virtualTableManager.GetVirtualTable(entityType); tableRoute = tableRouteManager.GetRoute(entityType);
virtualTableRoute = virtualTable.GetVirtualRoute(); allTails = tableRoute.GetTails().ToHashSet();
allTails = virtualTable.GetTableAllTails().ToHashSet();
} }
foreach (var entity in entitiesArray) foreach (var entity in entitiesArray)
{ {
@ -162,7 +155,7 @@ namespace ShardingCore.Extensions
if (isShardingTable) if (isShardingTable)
{ {
BulkShardingTableEnumerable(shardingDbContext, shardingDataSourceName, bulkDicEntries, BulkShardingTableEnumerable(shardingDbContext, shardingDataSourceName, bulkDicEntries,
routeTailFactory, virtualTable, virtualTableRoute, allTails, entity); routeTailFactory, tableRoute, allTails, entity);
} }
else else
BulkNoShardingTableEnumerable(shardingDbContext, shardingDataSourceName, bulkDicEntries, BulkNoShardingTableEnumerable(shardingDbContext, shardingDataSourceName, bulkDicEntries,
@ -175,14 +168,14 @@ namespace ShardingCore.Extensions
} }
private static void BulkShardingTableEnumerable<TShardingDbContext, TEntity>(TShardingDbContext shardingDbContext, string dataSourceName, Dictionary<string, BulkDicEntry<TEntity>> dataSourceBulkDicEntries, private static void BulkShardingTableEnumerable<TShardingDbContext, TEntity>(TShardingDbContext shardingDbContext, string dataSourceName, Dictionary<string, BulkDicEntry<TEntity>> dataSourceBulkDicEntries,
IRouteTailFactory routeTailFactory, IVirtualTable virtualTable, IVirtualTableRoute virtualTableRoute, ISet<string> allTails, TEntity entity) IRouteTailFactory routeTailFactory,IVirtualTableRoute tableRoute, ISet<string> allTails, TEntity entity)
where TShardingDbContext : DbContext, IShardingDbContext where TShardingDbContext : DbContext, IShardingDbContext
where TEntity : class where TEntity : class
{ {
var entityType = typeof(TEntity); var entityType = typeof(TEntity);
var shardingKey = entity.GetPropertyValue(virtualTable.EntityMetadata.ShardingTableProperty.Name); var shardingKey = entity.GetPropertyValue(tableRoute.EntityMetadata.ShardingTableProperty.Name);
var tail = virtualTableRoute.ShardingKeyToTail(shardingKey); var tail = tableRoute.ShardingKeyToTail(shardingKey);
if (!allTails.Contains(tail)) if (!allTails.Contains(tail))
throw new ShardingCoreException( throw new ShardingCoreException(
$"sharding key route not match entity:{entityType.FullName},sharding key:{shardingKey},sharding tail:{tail}"); $"sharding key route not match entity:{entityType.FullName},sharding key:{shardingKey},sharding tail:{tail}");
@ -253,7 +246,7 @@ namespace ShardingCore.Extensions
var shardingRuntimeContext = shardingDbContext.GetRequireService<IShardingRuntimeContext>(); var shardingRuntimeContext = shardingDbContext.GetRequireService<IShardingRuntimeContext>();
var virtualDataSource = shardingDbContext.GetVirtualDataSource(); var virtualDataSource = shardingDbContext.GetVirtualDataSource();
var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory(); var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
var virtualTableManager = shardingRuntimeContext.GetVirtualTableManager();// (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(shardingDbContext.GetType())); var tableRouteManager = shardingRuntimeContext.GetTableRouteManager();// (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(shardingDbContext.GetType()));
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();// (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(shardingDbContext.GetType())); var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();// (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(shardingDbContext.GetType()));
var dataSourceNames = virtualDataSource.GetDataSourceNames(where); var dataSourceNames = virtualDataSource.GetDataSourceNames(where);
@ -269,7 +262,7 @@ namespace ShardingCore.Extensions
} }
if (entityMetadataManager.IsShardingTable(entityType)) if (entityMetadataManager.IsShardingTable(entityType))
{ {
var physicTables = virtualTableManager.GetVirtualTable(entityType).RouteTo(new ShardingTableRouteConfig(predicate: @where)); var physicTables = tableRouteManager.RouteTo(entityType,new DataSourceRouteResult(dataSourceName),new ShardingTableRouteConfig(predicate: @where));
if (physicTables.IsEmpty()) if (physicTables.IsEmpty())
throw new ShardingCoreException($"{where.ShardingPrint()} cant found any physic table"); throw new ShardingCoreException($"{where.ShardingPrint()} cant found any physic table");

View File

@ -59,7 +59,7 @@ namespace ShardingCore.Extensions
{ {
var shardingRuntimeContext = ((DbContext)supportShardingReadWrite).GetRequireService<IShardingRuntimeContext>(); var shardingRuntimeContext = ((DbContext)supportShardingReadWrite).GetRequireService<IShardingRuntimeContext>();
var shardingReadWriteManager =shardingRuntimeContext.GetService<IShardingReadWriteManager>(); var shardingReadWriteManager =shardingRuntimeContext.GetService<IShardingReadWriteManager>();
var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(supportShardingReadWrite.GetType()); var shardingReadWriteContext = shardingReadWriteManager.GetCurrent();
if (shardingReadWriteContext != null) if (shardingReadWriteContext != null)
{ {
if (shardingReadWriteContext.DefaultPriority > supportShardingReadWrite.ReadWriteSeparationPriority) if (shardingReadWriteContext.DefaultPriority > supportShardingReadWrite.ReadWriteSeparationPriority)

View File

@ -2,15 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualTables;
namespace ShardingCore.Extensions namespace ShardingCore.Extensions
{ {
@ -72,17 +67,17 @@ namespace ShardingCore.Extensions
public static string GetTableTail<TEntity>(this IVirtualTableManager virtualTableManager, public static string GetTableTail<TEntity>(this ITableRouteManager tableRouteManager,
TEntity entity) where TEntity : class TEntity entity) where TEntity : class
{ {
var physicTable = virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new ShardingTableRouteConfig(shardingTable: entity))[0]; var shardingRouteUnit = tableRouteManager.RouteTo(entity.GetType(),new ShardingTableRouteConfig(shardingTable: entity))[0];
return physicTable.Tail; return shardingRouteUnit.Tail;
} }
public static string GetTableTail<TEntity>(this IVirtualTableManager virtualTableManager, public static string GetTableTail<TEntity>(this ITableRouteManager tableRouteManager,
object shardingKeyValue) where TEntity : class object shardingKeyValue) where TEntity : class
{ {
var physicTable = virtualTableManager.GetVirtualTable(typeof(TEntity)).RouteTo(new ShardingTableRouteConfig(shardingKeyValue: shardingKeyValue))[0]; var shardingRouteUnit = tableRouteManager.RouteTo(typeof(TEntity),new ShardingTableRouteConfig(shardingKeyValue: shardingKeyValue))[0];
return physicTable.Tail; return shardingRouteUnit.Tail;
} }
public static bool IsVirtualDataSourceRoute(this Type routeType) public static bool IsVirtualDataSourceRoute(this Type routeType)
{ {

View File

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualTables;
namespace ShardingCore.Extensions
{
public static class VirtualTableManagerExtension
{
public static IVirtualTable<TEntity> GetVirtualTable<TEntity>(this IVirtualTableManager virtualTableManager) where TEntity:class
{
return (IVirtualTable<TEntity>)virtualTableManager.GetVirtualTable(typeof(TEntity));
}
}
}

View File

@ -11,7 +11,6 @@ using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations; using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
@ -63,10 +62,9 @@ namespace ShardingCore.Helpers
//https://github.com/dotnet/efcore/tree/b970bf29a46521f40862a01db9e276e6448d3cb0/src/EFCore.Relational/Migrations/Operations //https://github.com/dotnet/efcore/tree/b970bf29a46521f40862a01db9e276e6448d3cb0/src/EFCore.Relational/Migrations/Operations
//ColumnOperation仅替换Table //ColumnOperation仅替换Table
//其余其余都是将Name和Table使用分表名替换 //其余其余都是将Name和Table使用分表名替换
var virtualTableManager = shardingRuntimeContext.GetVirtualTableManager(); var tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
var allVirtualTables = virtualTableManager.GetAllVirtualTables(); var tableRoutes = tableRouteManager.GetRoutes();
var shardingRuntimeModel = shardingRuntimeContext.GetShardingRuntimeModel(); var existsShardingTables = tableRoutes.ToDictionary(o => o.EntityMetadata.LogicTableName, o => o.GetTails().Select(p=>$"{o.EntityMetadata.LogicTableName}{o.EntityMetadata.TableSeparator}{p}").ToList());
var existsShardingTables = allVirtualTables.ToDictionary(o => o.EntityMetadata.VirtualTableName, o => o.GetAllPhysicTables().Select(p=>p.FullName).ToList());
//Dictionary<string, List<string>> _existsShardingTables //Dictionary<string, List<string>> _existsShardingTables
// = Cache.ServiceProvider.GetService<ShardingContainer>().ExistsShardingTables; // = Cache.ServiceProvider.GetService<ShardingContainer>().ExistsShardingTables;
List<string> resList = new List<string>(); List<string> resList = new List<string>();

View File

@ -3,7 +3,7 @@ using System.Threading.Tasks;
namespace ShardingCore.Jobs.Abstaractions namespace ShardingCore.Jobs.Abstaractions
{ {
internal interface IJob public interface IJob
{ {
string JobName { get; } string JobName { get; }
string[] GetCronExpressions(); string[] GetCronExpressions();

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ShardingCore.Core;
using ShardingCore.Jobs.Impls; using ShardingCore.Jobs.Impls;
namespace ShardingCore.Jobs.Abstaractions namespace ShardingCore.Jobs.Abstaractions

View File

@ -37,9 +37,7 @@ namespace ShardingCore.Sharding
var wrapOptionsExtension = options.FindExtension<ShardingWrapOptionsExtension>(); var wrapOptionsExtension = options.FindExtension<ShardingWrapOptionsExtension>();
if (wrapOptionsExtension != null) if (wrapOptionsExtension != null)
{ {
ShardingDbContextExecutor = ShardingDbContextExecutor = new ShardingDbContextExecutor(this);
(IShardingDbContextExecutor)Activator.CreateInstance(
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()),this);
} }
IsExecutor = wrapOptionsExtension == null; IsExecutor = wrapOptionsExtension == null;

View File

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShardingCore.Sharding.Abstractions
{
public interface ISeqQueryProvider
{
/// <summary>
/// 是否是顺序查询
/// </summary>
/// <returns></returns>
bool IsSeqQuery();
/// <summary>
/// 是否可以终端:本次查询n张表,链接数限制m,当n>m时则会出现串行查询才需要中断
/// </summary>
/// <returns></returns>
bool CanTrip();
}
}

View File

@ -19,6 +19,5 @@ namespace ShardingCore.Sharding.MergeContexts
bool IsSequenceQuery(); bool IsSequenceQuery();
bool SameWithTailComparer(); bool SameWithTailComparer();
IComparer<string> ShardingTailComparer(); IComparer<string> ShardingTailComparer();
bool CanTrip();
} }
} }

View File

@ -14,16 +14,14 @@ namespace ShardingCore.Sharding.MergeContexts
private readonly bool _isSequenceQuery; private readonly bool _isSequenceQuery;
private readonly bool _sameWithTailComparer; private readonly bool _sameWithTailComparer;
private readonly IComparer<string> _shardingTailComparer; private readonly IComparer<string> _shardingTailComparer;
private readonly bool _canTrip;
public OptimizeResult(int maxQueryConnectionsLimit, ConnectionModeEnum connectionMode,bool isSequenceQuery,bool sameWithTailComparer,IComparer<string> shardingTailComparer,bool canTrip) public OptimizeResult(int maxQueryConnectionsLimit, ConnectionModeEnum connectionMode,bool isSequenceQuery,bool sameWithTailComparer,IComparer<string> shardingTailComparer)
{ {
_maxQueryConnectionsLimit = maxQueryConnectionsLimit; _maxQueryConnectionsLimit = maxQueryConnectionsLimit;
_connectionMode = connectionMode; _connectionMode = connectionMode;
_isSequenceQuery = isSequenceQuery; _isSequenceQuery = isSequenceQuery;
_sameWithTailComparer = sameWithTailComparer; _sameWithTailComparer = sameWithTailComparer;
_shardingTailComparer = shardingTailComparer; _shardingTailComparer = shardingTailComparer;
_canTrip = canTrip;
} }
public int GetMaxQueryConnectionsLimit() public int GetMaxQueryConnectionsLimit()
{ {
@ -49,10 +47,6 @@ namespace ShardingCore.Sharding.MergeContexts
{ {
return _shardingTailComparer; return _shardingTailComparer;
} }
public bool CanTrip()
{
return _canTrip;
}
} }
} }

View File

@ -2,52 +2,50 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using ShardingCore.Core.VirtualRoutes.Abstractions;
using System.Threading.Tasks; using ShardingCore.Core.VirtualRoutes.TableRoutes;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
using ShardingCore.Core.Internal.Visitors.Selects;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Sharding.EntityQueryConfigurations; using ShardingCore.Sharding.EntityQueryConfigurations;
using ShardingCore.Sharding.ShardingExecutors;
using ShardingCore.Sharding.ShardingExecutors.Abstractions; using ShardingCore.Sharding.ShardingExecutors.Abstractions;
namespace ShardingCore.Sharding.MergeContexts namespace ShardingCore.Sharding.MergeContexts
{ {
public sealed class QueryableOptimizeEngine: IQueryableOptimizeEngine public sealed class QueryableOptimizeEngine : IQueryableOptimizeEngine
{ {
private readonly IVirtualTableManager _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
public QueryableOptimizeEngine(IVirtualTableManager virtualTableManager) public QueryableOptimizeEngine(ITableRouteManager tableRouteManager)
{ {
_virtualTableManager = virtualTableManager; _tableRouteManager = tableRouteManager;
} }
public IOptimizeResult Optimize(IMergeQueryCompilerContext mergeQueryCompilerContext, IParseResult parseResult, public IOptimizeResult Optimize(IMergeQueryCompilerContext mergeQueryCompilerContext, IParseResult parseResult,
IQueryable rewriteQueryable) IQueryable rewriteQueryable)
{ {
var shardingDbContext = mergeQueryCompilerContext.GetShardingDbContext(); var shardingDbContext = mergeQueryCompilerContext.GetShardingDbContext();
var maxParallelExecuteCount = shardingDbContext.GetVirtualDataSource().ConfigurationParams.MaxQueryConnectionsLimit; var maxParallelExecuteCount =
shardingDbContext.GetVirtualDataSource().ConfigurationParams.MaxQueryConnectionsLimit;
var connectionMode = shardingDbContext.GetVirtualDataSource().ConfigurationParams.ConnectionMode; var connectionMode = shardingDbContext.GetVirtualDataSource().ConfigurationParams.ConnectionMode;
IComparer<string> shardingTailComparer = Comparer<string>.Default; IComparer<string> shardingTailComparer = Comparer<string>.Default;
bool sameWithTailComparer = true; bool sameWithTailComparer = true;
bool sequenceQuery = false; bool sequenceQuery = false;
if (mergeQueryCompilerContext.IsSingleShardingEntityQuery() && mergeQueryCompilerContext.IsCrossTable() && !mergeQueryCompilerContext.UseUnionAllMerge()) if (mergeQueryCompilerContext.IsSingleShardingEntityQuery() && mergeQueryCompilerContext.IsCrossTable() &&
!mergeQueryCompilerContext.UseUnionAllMerge())
{ {
var singleShardingEntityType = mergeQueryCompilerContext.GetSingleShardingEntityType(); var singleShardingEntityType = mergeQueryCompilerContext.GetSingleShardingEntityType();
var virtualTable = _virtualTableManager.GetVirtualTable(singleShardingEntityType); var tableRoute = _tableRouteManager.GetRoute(singleShardingEntityType);
if (virtualTable.EnableEntityQuery) if (tableRoute.EnableEntityQuery)
{ {
if (virtualTable.EntityQueryMetadata.DefaultTailComparer != null) if (tableRoute.EntityQueryMetadata.DefaultTailComparer != null)
{ {
shardingTailComparer = virtualTable.EntityQueryMetadata.DefaultTailComparer; shardingTailComparer = tableRoute.EntityQueryMetadata.DefaultTailComparer;
} }
sameWithTailComparer = virtualTable.EntityQueryMetadata.DefaultTailComparerNeedReverse;
string methodName = mergeQueryCompilerContext.IsEnumerableQuery() ?
EntityQueryMetadata.QUERY_ENUMERATOR :
((MethodCallExpression)mergeQueryCompilerContext.GetQueryExpression()).Method.Name;
if (virtualTable.EntityQueryMetadata.TryGetConnectionsLimit(methodName, out var limit)) sameWithTailComparer = tableRoute.EntityQueryMetadata.DefaultTailComparerNeedReverse;
string methodName = mergeQueryCompilerContext.IsEnumerableQuery()
? EntityQueryMetadata.QUERY_ENUMERATOR
: ((MethodCallExpression)mergeQueryCompilerContext.GetQueryExpression()).Method.Name;
if (tableRoute.EntityQueryMetadata.TryGetConnectionsLimit(methodName, out var limit))
{ {
maxParallelExecuteCount = Math.Min(limit, maxParallelExecuteCount); maxParallelExecuteCount = Math.Min(limit, maxParallelExecuteCount);
} }
@ -61,7 +59,7 @@ namespace ShardingCore.Sharding.MergeContexts
} }
else else
{ {
if (TryGetSequenceQuery(parseResult, singleShardingEntityType, virtualTable, methodName, if (TryGetSequenceQuery(parseResult, singleShardingEntityType, tableRoute, methodName,
out var tailComparerIsAsc)) out var tailComparerIsAsc))
{ {
sequenceQuery = true; sequenceQuery = true;
@ -74,13 +72,14 @@ namespace ShardingCore.Sharding.MergeContexts
} }
} }
maxParallelExecuteCount = mergeQueryCompilerContext.GetMaxQueryConnectionsLimit() ?? maxParallelExecuteCount; maxParallelExecuteCount =
mergeQueryCompilerContext.GetMaxQueryConnectionsLimit() ?? maxParallelExecuteCount;
connectionMode = mergeQueryCompilerContext.GetConnectionMode() ?? connectionMode; connectionMode = mergeQueryCompilerContext.GetConnectionMode() ?? connectionMode;
var canTrip = mergeQueryCompilerContext.GetTableRouteResults().Length > maxParallelExecuteCount;
return new OptimizeResult(maxParallelExecuteCount, connectionMode, sequenceQuery, sameWithTailComparer, return new OptimizeResult(maxParallelExecuteCount, connectionMode, sequenceQuery, sameWithTailComparer,
shardingTailComparer, canTrip); shardingTailComparer);
} }
/// <summary> /// <summary>
/// 是否需要判断order /// 是否需要判断order
/// </summary> /// </summary>
@ -91,26 +90,28 @@ namespace ShardingCore.Sharding.MergeContexts
{ {
if ((methodName == null || if ((methodName == null ||
nameof(Queryable.First) == methodName || nameof(Queryable.First) == methodName ||
nameof(Queryable.FirstOrDefault) == methodName || nameof(Queryable.FirstOrDefault) == methodName ||
nameof(Queryable.Last) == methodName || nameof(Queryable.Last) == methodName ||
nameof(Queryable.LastOrDefault) == methodName || nameof(Queryable.LastOrDefault) == methodName ||
nameof(Queryable.Single) == methodName || nameof(Queryable.Single) == methodName ||
nameof(Queryable.SingleOrDefault) == methodName || nameof(Queryable.SingleOrDefault) == methodName ||
EntityQueryMetadata.QUERY_ENUMERATOR == methodName) && EntityQueryMetadata.QUERY_ENUMERATOR == methodName) &&
propertyOrders.Length > 0) propertyOrders.Length > 0)
return true; return true;
return false; return false;
} }
/// <summary> /// <summary>
/// 尝试获取当前方法是否采用顺序查询,如果有先判断排序没有的情况下判断默认 /// 尝试获取当前方法是否采用顺序查询,如果有先判断排序没有的情况下判断默认
/// </summary> /// </summary>
/// <param name="parseResult"></param> /// <param name="parseResult"></param>
/// <param name="singleShardingEntityType"></param> /// <param name="singleShardingEntityType"></param>
/// <param name="virtualTable"></param> /// <param name="tableRoute"></param>
/// <param name="methodName"></param> /// <param name="methodName"></param>
/// <param name="tailComparerIsAsc"></param> /// <param name="tailComparerIsAsc"></param>
/// <returns></returns> /// <returns></returns>
private bool TryGetSequenceQuery(IParseResult parseResult, Type singleShardingEntityType, IVirtualTable virtualTable, string methodName, out bool tailComparerIsAsc) private bool TryGetSequenceQuery(IParseResult parseResult, Type singleShardingEntityType,
IVirtualTableRoute tableRoute, string methodName, out bool tailComparerIsAsc)
{ {
var propertyOrders = parseResult.GetOrderByContext().PropertyOrders.ToArray(); var propertyOrders = parseResult.GetOrderByContext().PropertyOrders.ToArray();
var effectOrder = EffectOrder(methodName, propertyOrders); var effectOrder = EffectOrder(methodName, propertyOrders);
@ -122,10 +123,15 @@ namespace ShardingCore.Sharding.MergeContexts
var primaryOrderPropertyName = primaryOrder.PropertyExpression; var primaryOrderPropertyName = primaryOrder.PropertyExpression;
if (!primaryOrderPropertyName.Contains(".")) if (!primaryOrderPropertyName.Contains("."))
{ {
if (virtualTable.EnableEntityQuery && virtualTable.EntityQueryMetadata.TryContainsComparerOrder(primaryOrderPropertyName, out var seqQueryOrderMatch) if (tableRoute.EnableEntityQuery && tableRoute.EntityQueryMetadata.TryContainsComparerOrder(
&& (primaryOrder.OwnerType == singleShardingEntityType || seqQueryOrderMatch.OrderMatch.HasFlag(SeqOrderMatchEnum.Named)))//要么必须是当前对象查询要么就是名称一样 primaryOrderPropertyName, out var seqQueryOrderMatch)
&& (primaryOrder.OwnerType == singleShardingEntityType ||
seqQueryOrderMatch.OrderMatch.HasFlag(
SeqOrderMatchEnum.Named))) //要么必须是当前对象查询要么就是名称一样
{ {
tailComparerIsAsc = seqQueryOrderMatch.IsSameAsShardingTailComparer ? primaryOrder.IsAsc : !primaryOrder.IsAsc; tailComparerIsAsc = seqQueryOrderMatch.IsSameAsShardingTailComparer
? primaryOrder.IsAsc
: !primaryOrder.IsAsc;
//如果是获取最后一个还需要再次翻转 //如果是获取最后一个还需要再次翻转
if (nameof(Queryable.Last) == methodName || nameof(Queryable.LastOrDefault) == methodName) if (nameof(Queryable.Last) == methodName || nameof(Queryable.LastOrDefault) == methodName)
{ {
@ -135,23 +141,31 @@ namespace ShardingCore.Sharding.MergeContexts
return true; return true;
} }
} }
tailComparerIsAsc = true; tailComparerIsAsc = true;
return false; return false;
} }
if (virtualTable.EnableEntityQuery && methodName != null &&
virtualTable.EntityQueryMetadata.TryGetDefaultSequenceQueryTrip(methodName, out var defaultAsc)) if (tableRoute.EnableEntityQuery && methodName != null &&
tableRoute.EntityQueryMetadata.TryGetDefaultSequenceQueryTrip(methodName, out var defaultAsc))
{ {
tailComparerIsAsc = defaultAsc; tailComparerIsAsc = defaultAsc;
return true; return true;
} }
//Max和Min //Max和Min
if (nameof(Queryable.Max) == methodName || nameof(Queryable.Min) == methodName) if (nameof(Queryable.Max) == methodName || nameof(Queryable.Min) == methodName)
{ {
//如果是max或者min //如果是max或者min
if (virtualTable.EnableEntityQuery && parseResult.GetSelectContext().SelectProperties.Count == 1 && virtualTable.EntityQueryMetadata.TryContainsComparerOrder(parseResult.GetSelectContext().SelectProperties[0].PropertyName, out var seqQueryOrderMatch) if (tableRoute.EnableEntityQuery && parseResult.GetSelectContext().SelectProperties.Count == 1 &&
&& (parseResult.GetSelectContext().SelectProperties[0].OwnerType == singleShardingEntityType || seqQueryOrderMatch.OrderMatch.HasFlag(SeqOrderMatchEnum.Named))) tableRoute.EntityQueryMetadata.TryContainsComparerOrder(
parseResult.GetSelectContext().SelectProperties[0].PropertyName, out var seqQueryOrderMatch)
&& (parseResult.GetSelectContext().SelectProperties[0].OwnerType == singleShardingEntityType ||
seqQueryOrderMatch.OrderMatch.HasFlag(SeqOrderMatchEnum.Named)))
{ {
tailComparerIsAsc = seqQueryOrderMatch.IsSameAsShardingTailComparer ? nameof(Queryable.Min) == methodName : nameof(Queryable.Max) == methodName; tailComparerIsAsc = seqQueryOrderMatch.IsSameAsShardingTailComparer
? nameof(Queryable.Min) == methodName
: nameof(Queryable.Max) == methodName;
return true; return true;
} }
} }
@ -160,4 +174,4 @@ namespace ShardingCore.Sharding.MergeContexts
return false; return false;
} }
} }
} }

View File

@ -76,17 +76,13 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions
/// <returns></returns> /// <returns></returns>
protected virtual IEnumerable<ISqlRouteUnit> GetDefaultSqlRouteUnits() protected virtual IEnumerable<ISqlRouteUnit> GetDefaultSqlRouteUnits()
{ {
var streamMergeContext = GetStreamMergeContext(); var useUnionAllMerge = UseUnionAllMerge();
return streamMergeContext.DataSourceRouteResult.IntersectDataSources.SelectMany( if (useUnionAllMerge)
dataSourceName => {
{ return GetStreamMergeContext().ShardingRouteResult.RouteUnits.GroupBy(o=>o.DataSourceName).Select(o=>new UnSupportSqlRouteUnit(o.Key,o.Select(g=>g.TableRouteResult).ToList()));
if (UseUnionAllMerge()) }
{ return GetStreamMergeContext().ShardingRouteResult.RouteUnits;
return new []{ (ISqlRouteUnit)new UnSupportSqlRouteUnit(dataSourceName, streamMergeContext.TableRouteResults) };
}
return streamMergeContext.TableRouteResults.Select(routeResult =>
(ISqlRouteUnit)new SqlRouteUnit(dataSourceName, routeResult));
});
} }
protected virtual IEnumerable<IGrouping<string, ISqlRouteUnit>> AggregateQueryByDataSourceName(IEnumerable<ISqlRouteUnit> sqlRouteUnits) protected virtual IEnumerable<IGrouping<string, ISqlRouteUnit>> AggregateQueryByDataSourceName(IEnumerable<ISqlRouteUnit> sqlRouteUnits)
{ {

View File

@ -2,8 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions; using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
@ -11,16 +10,16 @@ namespace ShardingCore.Sharding.MergeEngines.Common
{ {
public class UnSupportSqlRouteUnit:ISqlRouteUnit public class UnSupportSqlRouteUnit:ISqlRouteUnit
{ {
public UnSupportSqlRouteUnit(string dataSourceName, IEnumerable<TableRouteResult> tableRouteResults) public UnSupportSqlRouteUnit(string dataSourceName, List<TableRouteResult> tableRouteResults)
{ {
DataSourceName = dataSourceName; DataSourceName = dataSourceName;
var routeResults = tableRouteResults.ToArray(); var routeResults = tableRouteResults;
TableRouteResults = routeResults; TableRouteResults = routeResults;
TableRouteResult = new TableRouteResult(new List<IPhysicTable>(0), routeResults[0].ShardingDbContextType); TableRouteResult = new TableRouteResult(new List<ShardingRouteUnit>(0));
} }
public string DataSourceName { get; } public string DataSourceName { get; }
public TableRouteResult TableRouteResult { get; } public TableRouteResult TableRouteResult { get; }
public IEnumerable<TableRouteResult> TableRouteResults { get; } public List<TableRouteResult> TableRouteResults { get; }
} }
} }

View File

@ -34,11 +34,12 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer
public override IStreamMergeAsyncEnumerator<TEntity>[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, CancellationToken cancellationToken = new CancellationToken()) public override IStreamMergeAsyncEnumerator<TEntity>[] GetRouteQueryStreamMergeAsyncEnumerators(bool async, CancellationToken cancellationToken = new CancellationToken())
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var dataSourceName = GetStreamMergeContext().DataSourceRouteResult.IntersectDataSources.First(); var streamMergeContext = GetStreamMergeContext();
var routeResult = GetStreamMergeContext().TableRouteResults[0]; var shardingRouteResult = streamMergeContext.ShardingRouteResult;
var shardingDbContext = GetStreamMergeContext().CreateDbContext(dataSourceName, routeResult, ConnectionModeEnum.MEMORY_STRICTLY); var sqlRouteUnit = shardingRouteResult.RouteUnits.First();
var newQueryable = (IQueryable<TEntity>)GetStreamMergeContext().GetOriginalQueryable().ReplaceDbContextQueryable(shardingDbContext); var shardingDbContext = streamMergeContext.CreateDbContext(sqlRouteUnit, ConnectionModeEnum.MEMORY_STRICTLY);
var enumeratorParallelExecutor = new SingleQueryEnumeratorExecutor<TEntity>(GetStreamMergeContext()); var newQueryable = (IQueryable<TEntity>)streamMergeContext.GetOriginalQueryable().ReplaceDbContextQueryable(shardingDbContext);
var enumeratorParallelExecutor = new SingleQueryEnumeratorExecutor<TEntity>(streamMergeContext);
if (async) if (async)
{ {
var asyncEnumerator = enumeratorParallelExecutor.GetAsyncEnumerator0(newQueryable).WaitAndUnwrapException(); var asyncEnumerator = enumeratorParallelExecutor.GetAsyncEnumerator0(newQueryable).WaitAndUnwrapException();

View File

@ -6,10 +6,9 @@ using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.Internal.Visitors; using ShardingCore.Core.Internal.Visitors;
using ShardingCore.Core.ShardingPage.Abstractions; using ShardingCore.Core.ShardingPage.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualTables; using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Extensions.InternalExtensions;
@ -33,13 +32,13 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
{ {
private readonly StreamMergeContext _streamMergeContext; private readonly StreamMergeContext _streamMergeContext;
private readonly IShardingPageManager _shardingPageManager; private readonly IShardingPageManager _shardingPageManager;
private readonly IVirtualTableManager _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IEntityMetadataManager _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private EnumeratorStreamMergeEngineFactory(StreamMergeContext streamMergeContext) private EnumeratorStreamMergeEngineFactory(StreamMergeContext streamMergeContext)
{ {
_streamMergeContext = streamMergeContext; _streamMergeContext = streamMergeContext;
_shardingPageManager = streamMergeContext.ShardingRuntimeContext.GetShardingPageManager(); _shardingPageManager = streamMergeContext.ShardingRuntimeContext.GetShardingPageManager();
_virtualTableManager =streamMergeContext.ShardingRuntimeContext.GetVirtualTableManager(); _tableRouteManager =streamMergeContext.ShardingRuntimeContext.GetTableRouteManager();
_entityMetadataManager = streamMergeContext.ShardingRuntimeContext.GetEntityMetadataManager(); _entityMetadataManager = streamMergeContext.ShardingRuntimeContext.GetEntityMetadataManager();
} }
@ -117,10 +116,10 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
} }
if (isShardingTable) if (isShardingTable)
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(shardingEntityType); var tableRoute = _tableRouteManager.GetRoute(shardingEntityType);
if (virtualTable.EnablePagination) if (tableRoute.EnablePagination)
{ {
tableSequenceOrderConfig = virtualTable.PaginationMetadata.PaginationConfigs.OrderByDescending(o => o.AppendOrder) tableSequenceOrderConfig = tableRoute.PaginationMetadata.PaginationConfigs.OrderByDescending(o => o.AppendOrder)
.FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName)); .FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName));
} }
} }
@ -148,7 +147,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
PaginationSequenceConfig dataSourceSequenceOrderConfig = null; PaginationSequenceConfig dataSourceSequenceOrderConfig = null;
PaginationSequenceConfig tableSequenceOrderConfig = null; PaginationSequenceConfig tableSequenceOrderConfig = null;
IVirtualDataSourceRoute virtualDataSourceRoute = null; IVirtualDataSourceRoute virtualDataSourceRoute = null;
IVirtualTable virtualTable = null; IVirtualTableRoute tableRoute = null;
bool dataSourceUseReverse = true; bool dataSourceUseReverse = true;
bool tableUseReverse = true; bool tableUseReverse = true;
if (isShardingDataSource) if (isShardingDataSource)
@ -162,10 +161,10 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
} }
if (isShardingTable) if (isShardingTable)
{ {
virtualTable = _virtualTableManager.GetVirtualTable(shardingEntityType); tableRoute = _tableRouteManager.GetRoute(shardingEntityType);
if (virtualTable.EnablePagination) if (tableRoute.EnablePagination)
{ {
tableSequenceOrderConfig = orderCount == 1 ? GetPaginationFullMatch(virtualTable.PaginationMetadata.PaginationConfigs, primaryOrder) : GetPaginationPrimaryMatch(virtualTable.PaginationMetadata.PaginationConfigs, primaryOrder); tableSequenceOrderConfig = orderCount == 1 ? GetPaginationFullMatch(tableRoute.PaginationMetadata.PaginationConfigs, primaryOrder) : GetPaginationPrimaryMatch(tableRoute.PaginationMetadata.PaginationConfigs, primaryOrder);
} }
} }
@ -186,7 +185,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
if (isShardingTable) if (isShardingTable)
{ {
tableUseReverse = tableUseReverse =
virtualTable.EnablePagination && EntityTableReverseShardingPage(virtualTable, total); tableRoute.EnablePagination && EntityTableReverseShardingPage(tableRoute, total);
} }
@ -214,11 +213,11 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
} }
return false; return false;
} }
private bool EntityTableReverseShardingPage(IVirtualTable virtualTable, long total) private bool EntityTableReverseShardingPage(IVirtualTableRoute tableRoute, long total)
{ {
if (virtualTable.PaginationMetadata.EnableReverseShardingPage && _streamMergeContext.Take.GetValueOrDefault() > 0) if (tableRoute.PaginationMetadata.EnableReverseShardingPage && _streamMergeContext.Take.GetValueOrDefault() > 0)
{ {
if (virtualTable.PaginationMetadata.IsUseReverse(_streamMergeContext.Skip.GetValueOrDefault(), total)) if (tableRoute.PaginationMetadata.IsUseReverse(_streamMergeContext.Skip.GetValueOrDefault(), total))
{ {
return true; return true;
} }

View File

@ -100,7 +100,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions
} }
} }
if (IsCancelled()|| circuitBreaker.IsTrip(result)) if (IsCancelled()|| circuitBreaker.Terminated(result))
break; break;
} }

View File

@ -14,11 +14,11 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Abstractions
/// <typeparam name="TResult"></typeparam> /// <typeparam name="TResult"></typeparam>
/// <param name="results"></param> /// <param name="results"></param>
/// <returns></returns> /// <returns></returns>
bool IsTrip<TResult>(IEnumerable<TResult> results); bool Terminated<TResult>(IEnumerable<TResult> results);
/// <summary> /// <summary>
/// 跳闸 /// 跳闸
/// </summary> /// </summary>
void Trip(); void Terminated0();
void Register(Action afterTrip); void Register(Action afterTrip);

View File

@ -4,13 +4,13 @@ using ShardingCore.Sharding.MergeEngines.Executors.Abstractions;
namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
internal abstract class AbstractCircuitBreaker: ICircuitBreaker internal abstract class AbstractCircuitBreaker : ICircuitBreaker
{ {
private readonly StreamMergeContext _streamMergeContext; private readonly StreamMergeContext _streamMergeContext;
private const int TRIP = 1; private const int TERMINATED = 1;
private const int UNTRIP = 0; private const int UNTERMINATED = 0;
private int _trip = UNTRIP; private int _terminated = UNTERMINATED;
private Action _afterTrip; private Action _afterTerminated;
protected AbstractCircuitBreaker(StreamMergeContext streamMergeContext) protected AbstractCircuitBreaker(StreamMergeContext streamMergeContext)
{ {
@ -21,27 +21,24 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
return _streamMergeContext; return _streamMergeContext;
} }
public bool IsTrip<TResult>(IEnumerable<TResult> results)
{
if (_trip == TRIP) public bool Terminated<TResult>(IEnumerable<TResult> results)
{
if (_terminated == TERMINATED)
return true; return true;
if (_streamMergeContext.IsSeqQuery()) if (_streamMergeContext.IsSeqQuery())
{ {
if (_streamMergeContext.CanTrip()) if (OrderConditionTerminated(results))
{ {
if (SeqConditionalTrip(results)) Terminated0();
{ return true;
Trip();
return true;
}
} }
} }
else else
{ {
if (RandomConditionalTrip(results)) if (RandomConditionTerminated(results))
{ {
Trip(); Terminated0();
return true; return true;
} }
} }
@ -49,18 +46,18 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
return false; return false;
} }
protected abstract bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results); protected abstract bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results);
protected abstract bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results); protected abstract bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results);
public void Trip() public void Terminated0()
{ {
_trip = TRIP; _terminated = TERMINATED;
_afterTrip?.Invoke(); _afterTerminated?.Invoke();
} }
public void Register(Action afterTrip) public void Register(Action afterTrip)
{ {
_afterTrip = afterTrip; _afterTerminated = afterTrip;
} }
} }
} }

View File

@ -10,13 +10,13 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
} }
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
//只要有一个是false就拉闸 //只要有一个是false就拉闸
return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult==false); return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult==false);
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
//只要有一个是false就拉闸 //只要有一个是false就拉闸
return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult == false); return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult == false);

View File

@ -9,12 +9,12 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
public AnyCircuitBreaker(StreamMergeContext streamMergeContext) : base(streamMergeContext) public AnyCircuitBreaker(StreamMergeContext streamMergeContext) : base(streamMergeContext)
{ {
} }
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult); return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult);
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult); return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult);
} }

View File

@ -18,12 +18,12 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
/// <typeparam name="TResult"></typeparam> /// <typeparam name="TResult"></typeparam>
/// <param name="results"></param> /// <param name="results"></param>
/// <returns></returns> /// <returns></returns>
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results.Any(o=>o is IRouteQueryResult routeQueryResult&& routeQueryResult.HasQueryResult()); return results.Any(o=>o is IRouteQueryResult routeQueryResult&& routeQueryResult.HasQueryResult());
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return false; return false;
} }

View File

@ -10,12 +10,12 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
} }
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult); return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult);
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult); return results.Any(o => o is RouteQueryResult<bool> routeQueryResult && routeQueryResult.QueryResult);
} }

View File

@ -11,7 +11,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
} }
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
var parseContext = (IMergeParseContext)GetStreamMergeContext(); var parseContext = (IMergeParseContext)GetStreamMergeContext();
@ -31,7 +31,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
return false; return false;
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return false; return false;
} }

View File

@ -8,12 +8,12 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
} }
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return false; return false;
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return false; return false;
} }

View File

@ -10,14 +10,14 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
{ {
} }
protected override bool SeqConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool OrderConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results return results
.Where(o => o is IRouteQueryResult routeQueryResult && routeQueryResult.HasQueryResult()) .Where(o => o is IRouteQueryResult routeQueryResult && routeQueryResult.HasQueryResult())
.Take(2).Count() > 1; .Take(2).Count() > 1;
} }
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results) protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{ {
return results return results
.Where(o => o is IRouteQueryResult routeQueryResult && routeQueryResult.HasQueryResult()) .Where(o => o is IRouteQueryResult routeQueryResult && routeQueryResult.HasQueryResult())

View File

@ -36,8 +36,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
{ {
var streamMergeContext = GetStreamMergeContext(); var streamMergeContext = GetStreamMergeContext();
var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode); var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode);
var sequenceResult = sqlExecutorUnit.RouteUnit.As<SqlSequenceRouteUnit>().SequenceResult; var sqlSequenceRouteUnit = sqlExecutorUnit.RouteUnit.As<SqlSequenceRouteUnit>();
var shardingDbContext = streamMergeContext.CreateDbContext(sequenceResult.DSName, sequenceResult.TableRouteResult, connectionMode); var sequenceResult = sqlSequenceRouteUnit.SequenceResult;
var shardingDbContext = streamMergeContext.CreateDbContext(sqlSequenceRouteUnit, connectionMode);
var newQueryable = _noPaginationQueryable var newQueryable = _noPaginationQueryable
.Skip(sequenceResult.Skip) .Skip(sequenceResult.Skip)
.Take(sequenceResult.Take) .Take(sequenceResult.Take)

View File

@ -47,10 +47,8 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
{ {
var streamMergeContext = GetStreamMergeContext(); var streamMergeContext = GetStreamMergeContext();
var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode); var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode);
var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName;
var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult;
var shardingDbContext = streamMergeContext.CreateDbContext(dataSourceName, routeResult, connectionMode); var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit, connectionMode);
var newQueryable = (IQueryable<TResult>)streamMergeContext.GetReWriteQueryable() var newQueryable = (IQueryable<TResult>)streamMergeContext.GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext); .ReplaceDbContextQueryable(shardingDbContext);

View File

@ -37,10 +37,8 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
{ {
var streamMergeContext = GetStreamMergeContext(); var streamMergeContext = GetStreamMergeContext();
var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode); var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode);
var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName;
var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult;
var shardingDbContext = streamMergeContext.CreateDbContext(dataSourceName, routeResult, connectionMode); var shardingDbContext = streamMergeContext.CreateDbContext(sqlExecutorUnit.RouteUnit, connectionMode);
var newQueryable = _reverseOrderQueryable var newQueryable = _reverseOrderQueryable
.ReplaceDbContextQueryable(shardingDbContext).As<IQueryable<TResult>>(); .ReplaceDbContextQueryable(shardingDbContext).As<IQueryable<TResult>>();
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken); var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);

View File

@ -36,8 +36,9 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
{ {
var streamMergeContext = GetStreamMergeContext(); var streamMergeContext = GetStreamMergeContext();
var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode); var connectionMode = streamMergeContext.RealConnectionMode(sqlExecutorUnit.ConnectionMode);
var sequenceResult = sqlExecutorUnit.RouteUnit.As<SqlSequenceRouteUnit>().SequenceResult; var sqlSequenceRouteUnit = sqlExecutorUnit.RouteUnit.As<SqlSequenceRouteUnit>();
var shardingDbContext = streamMergeContext.CreateDbContext(sequenceResult.DSName, sequenceResult.TableRouteResult, connectionMode); var sequenceResult = sqlSequenceRouteUnit.SequenceResult;
var shardingDbContext = streamMergeContext.CreateDbContext(sqlSequenceRouteUnit, connectionMode);
var newQueryable = _noPaginationQueryable var newQueryable = _noPaginationQueryable
.Skip(sequenceResult.Skip) .Skip(sequenceResult.Skip)
.Take(sequenceResult.Take) .Take(sequenceResult.Take)

View File

@ -30,7 +30,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions
var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName; var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName;
var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult; var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult;
var shardingDbContext = GetStreamMergeContext().CreateDbContext(dataSourceName, routeResult, connectionMode); var shardingDbContext = GetStreamMergeContext().CreateDbContext(sqlExecutorUnit.RouteUnit, connectionMode);
var newQueryable = GetStreamMergeContext().GetReWriteQueryable() var newQueryable = GetStreamMergeContext().GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext); .ReplaceDbContextQueryable(shardingDbContext);

View File

@ -30,7 +30,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Methods.Abstractions
var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName; var dataSourceName = sqlExecutorUnit.RouteUnit.DataSourceName;
var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult; var routeResult = sqlExecutorUnit.RouteUnit.TableRouteResult;
var shardingDbContext = GetStreamMergeContext().CreateDbContext(dataSourceName, routeResult, connectionMode); var shardingDbContext = GetStreamMergeContext().CreateDbContext(sqlExecutorUnit.RouteUnit, connectionMode);
var newQueryable = GetStreamMergeContext().GetReWriteQueryable() var newQueryable = GetStreamMergeContext().GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext); .ReplaceDbContextQueryable(shardingDbContext);

View File

@ -6,14 +6,12 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.DbContextCreator;
using ShardingCore.Exceptions; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
@ -37,7 +35,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
//private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>> _dbContextCaches = new ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>>(); //private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>> _dbContextCaches = new ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>>();
private readonly ConcurrentDictionary<string, IDataSourceDbContext> _dbContextCaches = new ConcurrentDictionary<string, IDataSourceDbContext>(); private readonly ConcurrentDictionary<string, IDataSourceDbContext> _dbContextCaches = new ConcurrentDictionary<string, IDataSourceDbContext>();
private readonly IVirtualDataSource _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IDbContextCreator _dbContextCreator; private readonly IDbContextCreator _dbContextCreator;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly ActualConnectionStringManager _actualConnectionStringManager; private readonly ActualConnectionStringManager _actualConnectionStringManager;
@ -60,10 +58,12 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
public ShardingDbContextExecutor(DbContext shardingDbContext) public ShardingDbContextExecutor(DbContext shardingDbContext)
{ {
_shardingDbContext = shardingDbContext; _shardingDbContext = shardingDbContext;
//初始化
var shardingRuntimeContext = shardingDbContext.GetRequireService<IShardingRuntimeContext>(); var shardingRuntimeContext = shardingDbContext.GetRequireService<IShardingRuntimeContext>();
shardingRuntimeContext.GetOrCreateShardingRuntimeModel(shardingDbContext);
var virtualDataSourceManager = shardingRuntimeContext.GetVirtualDataSourceManager(); var virtualDataSourceManager = shardingRuntimeContext.GetVirtualDataSourceManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource();
_virtualTableManager = shardingRuntimeContext.GetVirtualTableManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_dbContextCreator = shardingRuntimeContext.GetDbContextCreator(); _dbContextCreator = shardingRuntimeContext.GetDbContextCreator();
_entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_routeTailFactory = shardingRuntimeContext.GetRouteTailFactory(); _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
@ -133,7 +133,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
{ {
if (!_entityMetadataManager.IsShardingTable(entity.GetType())) if (!_entityMetadataManager.IsShardingTable(entity.GetType()))
return string.Empty; return string.Empty;
return _virtualTableManager.GetTableTail(entity); return _tableRouteManager.GetTableTail(entity);
} }
#endregion #endregion

View File

@ -22,14 +22,14 @@ namespace ShardingCore.Sharding
public DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder) public DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder)
{ {
SameConnectionDbContextOptionsCreator(dbConnection, dbContextOptionsBuilder); SameConnectionDbContextOptionsCreator(dbConnection, dbContextOptionsBuilder);
dbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>(); dbContextOptionsBuilder.UseInnerDbContextSharding();
return dbContextOptionsBuilder; return dbContextOptionsBuilder;
} }
public DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString, DbContextOptionsBuilder dbContextOptionsBuilder) public DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString, DbContextOptionsBuilder dbContextOptionsBuilder)
{ {
DefaultQueryDbContextOptionsCreator(connectionString, dbContextOptionsBuilder); DefaultQueryDbContextOptionsCreator(connectionString, dbContextOptionsBuilder);
dbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>(); dbContextOptionsBuilder.UseInnerDbContextSharding();
return dbContextOptionsBuilder; return dbContextOptionsBuilder;
} }
} }

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.Sharding.ShardingExecutors.QueryableCombines; using ShardingCore.Sharding.ShardingExecutors.QueryableCombines;
@ -13,8 +14,7 @@ namespace ShardingCore.Sharding.ShardingExecutors.Abstractions
public interface IMergeQueryCompilerContext : IQueryCompilerContext public interface IMergeQueryCompilerContext : IQueryCompilerContext
{ {
QueryCombineResult GetQueryCombineResult(); QueryCombineResult GetQueryCombineResult();
TableRouteResult[] GetTableRouteResults(); ShardingRouteResult GetShardingRouteResult();
DataSourceRouteResult GetDataSourceRouteResult();
bool IsCrossTable(); bool IsCrossTable();
bool IsCrossDataSource(); bool IsCrossDataSource();

View File

@ -161,7 +161,8 @@ namespace ShardingCore.Sharding.ShardingExecutors
{ {
//要么本次查询不追踪如果需要追踪不可以存在跨tails //要么本次查询不追踪如果需要追踪不可以存在跨tails
var routeTailFactory = _shardingRuntimeContext.GetRouteTailFactory(); var routeTailFactory = _shardingRuntimeContext.GetRouteTailFactory();
var dbContext = GetShardingDbContext().GetDbContext(_dataSourceRouteResult.IntersectDataSources.First(), IsParallelQuery(), routeTailFactory.Create(_tableRouteResults[0])); var sqlRouteUnit = _shardingRouteResult.RouteUnits.First();
var dbContext = GetShardingDbContext().GetDbContext(sqlRouteUnit.DataSourceName, IsParallelQuery(), routeTailFactory.Create(sqlRouteUnit.TableRouteResult));
_queryCompilerExecutor = new QueryCompilerExecutor(dbContext, GetQueryExpression()); _queryCompilerExecutor = new QueryCompilerExecutor(dbContext, GetQueryExpression());
} }
} }
@ -176,15 +177,11 @@ namespace ShardingCore.Sharding.ShardingExecutors
return _queryCombineResult; return _queryCombineResult;
} }
public TableRouteResult[] GetTableRouteResults() public ShardingRouteResult GetShardingRouteResult()
{ {
return _tableRouteResults; return _shardingRouteResult;
} }
public DataSourceRouteResult GetDataSourceRouteResult()
{
return _dataSourceRouteResult;
}
/// <summary> /// <summary>
/// 既不可以跨库也不可以跨表,所有的分表都必须是相同后缀才可以 /// 既不可以跨库也不可以跨表,所有的分表都必须是相同后缀才可以
/// </summary> /// </summary>

View File

@ -67,7 +67,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
{ {
_logger.LogDebug($"table route results:{shardingRouteResult}"); _logger.LogDebug($"table route results:{shardingRouteResult}");
} }
var mergeCombineCompilerContext = MergeQueryCompilerContext.Create(queryCompilerContext, queryCombineResult, sqlRouteUnits); var mergeCombineCompilerContext = MergeQueryCompilerContext.Create(queryCompilerContext, queryCombineResult, shardingRouteResult);
return mergeCombineCompilerContext; return mergeCombineCompilerContext;
} }

View File

@ -19,7 +19,9 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Abstractions;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Sharding namespace ShardingCore.Sharding
@ -30,7 +32,7 @@ namespace ShardingCore.Sharding
* @Date: Monday, 25 January 2021 11:38:27 * @Date: Monday, 25 January 2021 11:38:27
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public class StreamMergeContext : ISeqQueryProvider, IMergeParseContext, IDisposable, IPrint public class StreamMergeContext : IMergeParseContext, IDisposable, IPrint
#if !EFCORE2 #if !EFCORE2
, IAsyncDisposable , IAsyncDisposable
#endif #endif
@ -49,8 +51,7 @@ namespace ShardingCore.Sharding
public SelectContext SelectContext => ParseResult.GetSelectContext(); public SelectContext SelectContext => ParseResult.GetSelectContext();
public GroupByContext GroupByContext => ParseResult.GetGroupByContext(); public GroupByContext GroupByContext => ParseResult.GetGroupByContext();
public TableRouteResult[] TableRouteResults => MergeQueryCompilerContext.GetTableRouteResults(); public ShardingRouteResult ShardingRouteResult => MergeQueryCompilerContext.GetShardingRouteResult();
public DataSourceRouteResult DataSourceRouteResult => MergeQueryCompilerContext.GetDataSourceRouteResult();
/// <summary> /// <summary>
/// 本次查询涉及的对象 /// 本次查询涉及的对象
@ -113,16 +114,16 @@ namespace ShardingCore.Sharding
/// <summary> /// <summary>
/// 创建对应的dbcontext /// 创建对应的dbcontext
/// </summary> /// </summary>
/// <param name="dataSourceName">data source name</param> /// <param name="sqlRouteUnit">数据库路由最小单元</param>
/// <param name="tableRouteResult"></param>
/// <param name="connectionMode"></param> /// <param name="connectionMode"></param>
/// <returns></returns> /// <returns></returns>
public DbContext CreateDbContext(string dataSourceName, TableRouteResult tableRouteResult, ConnectionModeEnum connectionMode) public DbContext CreateDbContext(ISqlRouteUnit sqlRouteUnit, ConnectionModeEnum connectionMode)
{ {
var routeTail = _routeTailFactory.Create(tableRouteResult);
var routeTail = _routeTailFactory.Create(sqlRouteUnit.TableRouteResult);
//如果开启了读写分离或者本次查询是跨表的表示本次查询的dbcontext是不存储的用完后就直接dispose //如果开启了读写分离或者本次查询是跨表的表示本次查询的dbcontext是不存储的用完后就直接dispose
var parallelQuery = IsParallelQuery(); var parallelQuery = IsParallelQuery();
var dbContext = GetShardingDbContext().GetDbContext(dataSourceName, parallelQuery, routeTail); var dbContext = GetShardingDbContext().GetDbContext(sqlRouteUnit.DataSourceName, parallelQuery, routeTail);
if (parallelQuery && RealConnectionMode(connectionMode) == ConnectionModeEnum.MEMORY_STRICTLY) if (parallelQuery && RealConnectionMode(connectionMode) == ConnectionModeEnum.MEMORY_STRICTLY)
{ {
_parallelDbContexts.TryAdd(dbContext, null); _parallelDbContexts.TryAdd(dbContext, null);
@ -274,14 +275,7 @@ namespace ShardingCore.Sharding
{ {
if (ThrowIfQueryRouteNotMatch()) if (ThrowIfQueryRouteNotMatch())
{ {
if (IsDataSourceRouteNotMatch()) throw new ShardingCoreQueryRouteNotMatchException(MergeQueryCompilerContext.GetQueryExpression().ShardingPrint());
{
throw new ShardingCoreDataSourceQueryRouteNotMatchException(MergeQueryCompilerContext.GetQueryExpression().ShardingPrint());
}
else
{
throw new ShardingCoreTableQueryRouteNotMatchException(MergeQueryCompilerContext.GetQueryExpression().ShardingPrint());
}
} }
else else
{ {
@ -297,12 +291,7 @@ namespace ShardingCore.Sharding
/// <returns></returns> /// <returns></returns>
public bool IsRouteNotMatch() public bool IsRouteNotMatch()
{ {
return DataSourceRouteResult.IntersectDataSources.IsEmpty() || TableRouteResults.IsEmpty(); return ShardingRouteResult.IsEmpty;
}
private bool IsDataSourceRouteNotMatch()
{
return DataSourceRouteResult.IntersectDataSources.IsEmpty();
} }
private bool ThrowIfQueryRouteNotMatch() private bool ThrowIfQueryRouteNotMatch()
@ -336,15 +325,10 @@ namespace ShardingCore.Sharding
return OptimizeResult.IsSequenceQuery(); return OptimizeResult.IsSequenceQuery();
} }
public bool CanTrip()
{
return OptimizeResult.CanTrip();
}
public string GetPrintInfo() public string GetPrintInfo()
{ {
return return
$"stream merge context:[max query connections limit:{GetMaxQueryConnectionsLimit()}],[is use read write separation:{IsUseReadWriteSeparation()}],[is parallel query:{IsParallelQuery()}],[is not support sharding:{UseUnionAllMerge()}],[is sequence query:{IsSeqQuery()}],[can trip:{CanTrip()}],[is route not match:{IsRouteNotMatch()}],[throw if query route not match:{ThrowIfQueryRouteNotMatch()}],[is pagination query:{IsPaginationQuery()}],[has group query:{HasGroupQuery()}],[is merge query:{IsMergeQuery()}],[is single sharding entity query:{IsSingleShardingEntityQuery()}]"; $"stream merge context:[max query connections limit:{GetMaxQueryConnectionsLimit()}],[is use read write separation:{IsUseReadWriteSeparation()}],[is parallel query:{IsParallelQuery()}],[is not support sharding:{UseUnionAllMerge()}],[is sequence query:{IsSeqQuery()}],[is route not match:{IsRouteNotMatch()}],[throw if query route not match:{ThrowIfQueryRouteNotMatch()}],[is pagination query:{IsPaginationQuery()}],[has group query:{HasGroupQuery()}],[is merge query:{IsMergeQuery()}],[is single sharding entity query:{IsSingleShardingEntityQuery()}]";
} }
public int? GetSkip() public int? GetSkip()

View File

@ -11,8 +11,6 @@ using Microsoft.EntityFrameworkCore.Query;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase; using ShardingCore.Core.VirtualDatabase;
using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.Visitors; using ShardingCore.Sharding.Visitors;

View File

@ -1,39 +1,39 @@
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; // using Microsoft.Extensions.DependencyInjection;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; // using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Data.Common; // using System.Data.Common;
//
namespace ShardingCore.TableExists.Abstractions // namespace ShardingCore.TableExists.Abstractions
{ // {
public abstract class AbstractTableEnsureManager<TShardingDbContext> : ITableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext // public abstract class AbstractTableEnsureManager<TShardingDbContext> : ITableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{ // {
protected IRouteTailFactory RouteTailFactory { get; } // protected IRouteTailFactory RouteTailFactory { get; }
protected AbstractTableEnsureManager() // protected AbstractTableEnsureManager(IRouteTailFactory routeTailFactory)
{ // {
RouteTailFactory = ShardingContainer.GetService<IRouteTailFactory>(); // RouteTailFactory = routeTailFactory;
} // }
public ISet<string> GetExistTables(string dataSourceName) // public ISet<string> GetExistTables(string dataSourceName)
{ // {
using (var scope = ShardingContainer.ServiceProvider.CreateScope()) // using (var scope = ShardingContainer.ServiceProvider.CreateScope())
{ // {
var shardingDbContext = scope.ServiceProvider.GetService<TShardingDbContext>(); // var shardingDbContext = scope.ServiceProvider.GetService<TShardingDbContext>();
return GetExistTables(shardingDbContext, dataSourceName); // return GetExistTables(shardingDbContext, dataSourceName);
} // }
} // }
//
public ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName) // public ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName)
{ // {
using (var dbContext = // using (var dbContext =
shardingDbContext.GetDbContext(dataSourceName, true, RouteTailFactory.Create(string.Empty))) // shardingDbContext.GetDbContext(dataSourceName, true, RouteTailFactory.Create(string.Empty)))
{ // {
var dbConnection = dbContext.Database.GetDbConnection(); // var dbConnection = dbContext.Database.GetDbConnection();
dbConnection.Open(); // dbConnection.Open();
return DoGetExistTables(dbConnection, dataSourceName); // return DoGetExistTables(dbConnection, dataSourceName);
} // }
} // }
//
public abstract ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName); // public abstract ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName);
} // }
} // }

View File

@ -1,16 +1,16 @@
using System; // using System;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Linq; // using System.Linq;
using System.Text; // using System.Text;
using System.Threading.Tasks; // using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
//
namespace ShardingCore.TableExists.Abstractions // namespace ShardingCore.TableExists.Abstractions
{ // {
public interface ITableEnsureManager // public interface ITableEnsureManager
{ // {
ISet<string> GetExistTables(string dataSourceName); // ISet<string> GetExistTables(string dataSourceName);
ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName); // ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName);
} // }
} // }

View File

@ -1,24 +1,24 @@
using System; // using System;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Linq; // using System.Linq;
using System.Text; // using System.Text;
using System.Threading.Tasks; // using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
using ShardingCore.TableExists.Abstractions; // using ShardingCore.TableExists.Abstractions;
//
namespace ShardingCore.TableExists // namespace ShardingCore.TableExists
{ // {
public class EmptyTableEnsureManager : ITableEnsureManager // public class EmptyTableEnsureManager : ITableEnsureManager
{ // {
public ISet<string> GetExistTables(string dataSourceName) // public ISet<string> GetExistTables(string dataSourceName)
{ // {
return new HashSet<string>(); // return new HashSet<string>();
} // }
//
public ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName) // public ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName)
{ // {
return new HashSet<string>(); // return new HashSet<string>();
} // }
} // }
} // }

View File

@ -1,38 +1,38 @@
using System; // using System;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Data.Common; // using System.Data.Common;
using System.Linq; // using System.Linq;
using System.Text; // using System.Text;
using System.Threading.Tasks; // using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
using ShardingCore.TableExists.Abstractions; // using ShardingCore.TableExists.Abstractions;
//
namespace ShardingCore.TableExists // namespace ShardingCore.TableExists
{ // {
public class MySqlTableEnsureManager<TShardingDbContext> : AbstractTableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext // public class MySqlTableEnsureManager<TShardingDbContext> : AbstractTableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{ // {
private const string Tables = "Tables"; // private const string Tables = "Tables";
private const string TABLE_SCHEMA = "TABLE_SCHEMA"; // private const string TABLE_SCHEMA = "TABLE_SCHEMA";
private const string TABLE_NAME = "TABLE_NAME"; // private const string TABLE_NAME = "TABLE_NAME";
//
public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName) // public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName)
{ // {
var database = connection.Database; // var database = connection.Database;
ISet<string> result = new HashSet<string>(); // ISet<string> result = new HashSet<string>();
using (var dataTable = connection.GetSchema(Tables)) // using (var dataTable = connection.GetSchema(Tables))
{ // {
for (int i = 0; i < dataTable.Rows.Count; i++) // for (int i = 0; i < dataTable.Rows.Count; i++)
{ // {
var schema = dataTable.Rows[i][TABLE_SCHEMA]; // var schema = dataTable.Rows[i][TABLE_SCHEMA];
if (database.Equals($"{schema}", StringComparison.OrdinalIgnoreCase)) // if (database.Equals($"{schema}", StringComparison.OrdinalIgnoreCase))
{ // {
var tableName = dataTable.Rows[i][TABLE_NAME]; // var tableName = dataTable.Rows[i][TABLE_NAME];
result.Add($"{tableName}"); // result.Add($"{tableName}");
} // }
} // }
} // }
return result; // return result;
} // }
} // }
} // }

View File

@ -1,27 +1,27 @@
using Microsoft.EntityFrameworkCore; // using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions; // using ShardingCore.Sharding.Abstractions;
using ShardingCore.TableExists.Abstractions; // using ShardingCore.TableExists.Abstractions;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Data.Common; // using System.Data.Common;
//
namespace ShardingCore.TableExists // namespace ShardingCore.TableExists
{ // {
public class SqlServerTableEnsureManager<TShardingDbContext> : AbstractTableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext // public class SqlServerTableEnsureManager<TShardingDbContext> : AbstractTableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{ // {
private const string Tables = "Tables"; // private const string Tables = "Tables";
private const string TABLE_NAME = "TABLE_NAME"; // private const string TABLE_NAME = "TABLE_NAME";
//
public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName) // public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName)
{ // {
ISet<string> result = new HashSet<string>(); // ISet<string> result = new HashSet<string>();
using (var dataTable = connection.GetSchema(Tables)) // using (var dataTable = connection.GetSchema(Tables))
{ // {
for (int i = 0; i < dataTable.Rows.Count; i++) // for (int i = 0; i < dataTable.Rows.Count; i++)
{ // {
result.Add(dataTable.Rows[i][TABLE_NAME].ToString()); // result.Add(dataTable.Rows[i][TABLE_NAME].ToString());
} // }
} // }
return result; // return result;
} // }
} // }
} // }