基本架构已经完成除了部分代码需要重写,包括去掉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.EntityMetadatas;
using ShardingCore.Core.EntityShardingMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.ShardingConfigurations.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.TableRoutes;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Helpers;
@ -48,14 +46,14 @@ namespace ShardingCore.Bootstrappers
private readonly Type _shardingEntityType;
private readonly IShardingEntityConfigOptions _shardingEntityConfigOptions;
private readonly IVirtualDataSourceRouteManager _virtualDataSourceRouteManager;
private readonly IVirtualTableManager _virtualTableManager;
private readonly ITableRouteManager _tableRouteManager;
private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IJobManager _jobManager;
public EntityMetadataInitializer(
IShardingEntityConfigOptions shardingEntityConfigOptions,
IVirtualDataSourceRouteManager virtualDataSourceRouteManager,
IVirtualTableManager virtualTableManager,
ITableRouteManager tableRouteManager,
IEntityMetadataManager entityMetadataManager,
IJobManager jobManager
)
@ -66,7 +64,7 @@ namespace ShardingCore.Bootstrappers
// _queryFilterExpression = entityMetadataEnsureParams.EntityType.GetAnnotations().FirstOrDefault(o=>o.Name== QueryFilter)?.Value as Expression<Func<TEntity, bool>>;
_shardingEntityConfigOptions = shardingEntityConfigOptions;
_virtualDataSourceRouteManager = virtualDataSourceRouteManager;
_virtualTableManager = virtualTableManager;
_tableRouteManager = tableRouteManager;
_entityMetadataManager = entityMetadataManager;
_jobManager = jobManager;
}
@ -122,9 +120,7 @@ namespace ShardingCore.Bootstrappers
createEntityMetadataTableConfiguration.Configure(entityMetadataTableBuilder);
}
//创建虚拟表
var virtualTable = CreateVirtualTable(virtualTableRoute,entityMetadata);
InitVirtualTable(virtualTable);
_virtualTableManager.AddVirtualTable(virtualTable);
_tableRouteManager.AddRoute(virtualTableRoute);
//检测校验分表分库对象元数据
entityMetadata.CheckShardingTableMetadata();
//添加任务
@ -149,18 +145,5 @@ namespace ShardingCore.Bootstrappers
var instance = ShardingRuntimeContext.GetInstance().CreateInstance(virtualRouteType);
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.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
@ -12,7 +15,7 @@ namespace ShardingCore.Core.EntityMetadatas
/// 分表或者分库对象的元数据信息记录对象在ShardingCore框架下的一些简单的信息
/// </summary>
public class EntityMetadata
{
{private const string QueryFilter = "QueryFilter";
public EntityMetadata(Type entityType, Type shardingDbContextType)
{
EntityType = entityType;
@ -24,7 +27,6 @@ namespace ShardingCore.Core.EntityMetadatas
/// 分表类型 sharding entity type
/// </summary>
public Type EntityType { get; }
public Type ShardingDbContextType { get; }
@ -70,6 +72,36 @@ namespace ShardingCore.Core.EntityMetadatas
/// 分表隔离器 table sharding tail prefix
/// </summary>
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>

View File

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

View File

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

View File

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

View File

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

View File

@ -6,15 +6,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.ShardingComparision;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
{
@ -34,11 +28,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
public virtual ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; }
public virtual IShardingComparer ShardingComparer { get; } = new CSharpLanguageShardingComparer();
public virtual ITableEnsureManager TableEnsureManager { get; } =
new EmptyTableEnsureManager();
public abstract DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString,
DbContextOptionsBuilder dbContextOptionsBuilder);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.VirtualRoutes.Abstractions
{
@ -10,5 +12,11 @@ namespace ShardingCore.Core.VirtualRoutes.Abstractions
bool HasRoute(Type entityType);
IVirtualTableRoute GetRoute(Type entityType);
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.Linq;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.QueryRouteManagers;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;

View File

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

View File

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

View File

@ -1,16 +1,11 @@
using System;
using System.Collections.Generic;
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.Abstractions;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
{

View File

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

View File

@ -1,5 +1,4 @@
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.ShardingConfigurations.Abstractions;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.MergeEngines.ParallelControl;
@ -23,13 +22,30 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce();
public IShardingEntityConfigOptions EntityConfigOptions { get; private set; }
public PaginationMetadata PaginationMetadata { get; private set; }
public EntityQueryMetadata EntityQueryMetadata { get; private set; }
public virtual void Initialize(EntityMetadata entityMetadata)
{
if (!_doOnlyOnce.IsUnDo())
throw new ShardingCoreInvalidOperationException("already init");
EntityMetadata = entityMetadata;
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.Linq;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Sharding.EntityQueryConfigurations;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
using ShardingCore.Sharding.PaginationConfigurations;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes
@ -46,6 +44,24 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes
/// </summary>
/// <returns></returns>
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.Collections.Generic;
using System.Linq;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Extensions;

View File

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

View File

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

View File

@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
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
{

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

View File

@ -9,11 +9,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Extensions;
using ShardingCore.Logger;
using ShardingCore.Sharding.Abstractions;
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.Reflection;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;
namespace ShardingCore.Extensions
{

View File

@ -8,15 +8,11 @@ using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;
namespace ShardingCore.Extensions
{
@ -95,7 +91,7 @@ namespace ShardingCore.Extensions
var entityType = typeof(TEntity);
var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
var virtualDataSource = shardingDbContext.GetVirtualDataSource();
var virtualTableManager =shardingRuntimeContext.GetVirtualTableManager();
var tableRouteManager =shardingRuntimeContext.GetTableRouteManager();
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
var dataSourceNames = new Dictionary<string, Dictionary<string, BulkDicEntry<TEntity>>>();
var entitiesArray = entities as TEntity[] ?? entities.ToArray();
@ -120,13 +116,12 @@ namespace ShardingCore.Extensions
var bulkDicEntries = new Dictionary<string, BulkDicEntry<TEntity>>();
dataSourceNames.Add(virtualDataSource.DefaultDataSourceName, bulkDicEntries);
var virtualTable = virtualTableManager.GetVirtualTable(entityType);
var virtualTableRoute = virtualTable.GetVirtualRoute();
var allTails = virtualTable.GetTableAllTails().ToHashSet();
var tableRoute = tableRouteManager.GetRoute(entityType);
var allTails = tableRoute.GetTails().ToHashSet();
foreach (var entity in entitiesArray)
{
BulkShardingTableEnumerable(shardingDbContext, virtualDataSource.DefaultDataSourceName, bulkDicEntries,
routeTailFactory, virtualTable, virtualTableRoute, allTails, entity);
routeTailFactory, tableRoute, allTails, entity);
}
}
else
@ -135,14 +130,12 @@ namespace ShardingCore.Extensions
var allDataSourceNames = virtualDataSourceRoute.GetAllDataSourceNames().ToHashSet();
var entityMetadata = entityMetadataManager.TryGet(entityType);
IVirtualTable virtualTable = null;
IVirtualTableRoute virtualTableRoute = null;
IVirtualTableRoute tableRoute = null;
ISet<string> allTails = null;
if (isShardingTable)
{
virtualTable = virtualTableManager.GetVirtualTable(entityType);
virtualTableRoute = virtualTable.GetVirtualRoute();
allTails = virtualTable.GetTableAllTails().ToHashSet();
tableRoute = tableRouteManager.GetRoute(entityType);
allTails = tableRoute.GetTails().ToHashSet();
}
foreach (var entity in entitiesArray)
{
@ -162,7 +155,7 @@ namespace ShardingCore.Extensions
if (isShardingTable)
{
BulkShardingTableEnumerable(shardingDbContext, shardingDataSourceName, bulkDicEntries,
routeTailFactory, virtualTable, virtualTableRoute, allTails, entity);
routeTailFactory, tableRoute, allTails, entity);
}
else
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,
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 TEntity : class
{
var entityType = typeof(TEntity);
var shardingKey = entity.GetPropertyValue(virtualTable.EntityMetadata.ShardingTableProperty.Name);
var tail = virtualTableRoute.ShardingKeyToTail(shardingKey);
var shardingKey = entity.GetPropertyValue(tableRoute.EntityMetadata.ShardingTableProperty.Name);
var tail = tableRoute.ShardingKeyToTail(shardingKey);
if (!allTails.Contains(tail))
throw new ShardingCoreException(
$"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 virtualDataSource = shardingDbContext.GetVirtualDataSource();
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 dataSourceNames = virtualDataSource.GetDataSourceNames(where);
@ -269,7 +262,7 @@ namespace ShardingCore.Extensions
}
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())
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 shardingReadWriteManager =shardingRuntimeContext.GetService<IShardingReadWriteManager>();
var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(supportShardingReadWrite.GetType());
var shardingReadWriteContext = shardingReadWriteManager.GetCurrent();
if (shardingReadWriteContext != null)
{
if (shardingReadWriteContext.DefaultPriority > supportShardingReadWrite.ReadWriteSeparationPriority)

View File

@ -2,15 +2,10 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using ShardingCore.Core;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualTables;
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
{
var physicTable = virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new ShardingTableRouteConfig(shardingTable: entity))[0];
return physicTable.Tail;
var shardingRouteUnit = tableRouteManager.RouteTo(entity.GetType(),new ShardingTableRouteConfig(shardingTable: entity))[0];
return shardingRouteUnit.Tail;
}
public static string GetTableTail<TEntity>(this IVirtualTableManager virtualTableManager,
public static string GetTableTail<TEntity>(this ITableRouteManager tableRouteManager,
object shardingKeyValue) where TEntity : class
{
var physicTable = virtualTableManager.GetVirtualTable(typeof(TEntity)).RouteTo(new ShardingTableRouteConfig(shardingKeyValue: shardingKeyValue))[0];
return physicTable.Tail;
var shardingRouteUnit = tableRouteManager.RouteTo(typeof(TEntity),new ShardingTableRouteConfig(shardingKeyValue: shardingKeyValue))[0];
return shardingRouteUnit.Tail;
}
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.Storage;
using ShardingCore.Core;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
@ -63,10 +62,9 @@ namespace ShardingCore.Helpers
//https://github.com/dotnet/efcore/tree/b970bf29a46521f40862a01db9e276e6448d3cb0/src/EFCore.Relational/Migrations/Operations
//ColumnOperation仅替换Table
//其余其余都是将Name和Table使用分表名替换
var virtualTableManager = shardingRuntimeContext.GetVirtualTableManager();
var allVirtualTables = virtualTableManager.GetAllVirtualTables();
var shardingRuntimeModel = shardingRuntimeContext.GetShardingRuntimeModel();
var existsShardingTables = allVirtualTables.ToDictionary(o => o.EntityMetadata.VirtualTableName, o => o.GetAllPhysicTables().Select(p=>p.FullName).ToList());
var tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
var tableRoutes = tableRouteManager.GetRoutes();
var existsShardingTables = tableRoutes.ToDictionary(o => o.EntityMetadata.LogicTableName, o => o.GetTails().Select(p=>$"{o.EntityMetadata.LogicTableName}{o.EntityMetadata.TableSeparator}{p}").ToList());
//Dictionary<string, List<string>> _existsShardingTables
// = Cache.ServiceProvider.GetService<ShardingContainer>().ExistsShardingTables;
List<string> resList = new List<string>();

View File

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

View File

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

View File

@ -37,9 +37,7 @@ namespace ShardingCore.Sharding
var wrapOptionsExtension = options.FindExtension<ShardingWrapOptionsExtension>();
if (wrapOptionsExtension != null)
{
ShardingDbContextExecutor =
(IShardingDbContextExecutor)Activator.CreateInstance(
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()),this);
ShardingDbContextExecutor = new ShardingDbContextExecutor(this);
}
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 SameWithTailComparer();
IComparer<string> ShardingTailComparer();
bool CanTrip();
}
}

View File

@ -14,16 +14,14 @@ namespace ShardingCore.Sharding.MergeContexts
private readonly bool _isSequenceQuery;
private readonly bool _sameWithTailComparer;
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;
_connectionMode = connectionMode;
_isSequenceQuery = isSequenceQuery;
_sameWithTailComparer = sameWithTailComparer;
_shardingTailComparer = shardingTailComparer;
_canTrip = canTrip;
}
public int GetMaxQueryConnectionsLimit()
{
@ -49,10 +47,6 @@ namespace ShardingCore.Sharding.MergeContexts
{
return _shardingTailComparer;
}
public bool CanTrip()
{
return _canTrip;
}
}
}

View File

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

View File

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

View File

@ -2,8 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
@ -11,16 +10,16 @@ namespace ShardingCore.Sharding.MergeEngines.Common
{
public class UnSupportSqlRouteUnit:ISqlRouteUnit
{
public UnSupportSqlRouteUnit(string dataSourceName, IEnumerable<TableRouteResult> tableRouteResults)
public UnSupportSqlRouteUnit(string dataSourceName, List<TableRouteResult> tableRouteResults)
{
DataSourceName = dataSourceName;
var routeResults = tableRouteResults.ToArray();
var routeResults = tableRouteResults;
TableRouteResults = routeResults;
TableRouteResult = new TableRouteResult(new List<IPhysicTable>(0), routeResults[0].ShardingDbContextType);
TableRouteResult = new TableRouteResult(new List<ShardingRouteUnit>(0));
}
public string DataSourceName { 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())
{
cancellationToken.ThrowIfCancellationRequested();
var dataSourceName = GetStreamMergeContext().DataSourceRouteResult.IntersectDataSources.First();
var routeResult = GetStreamMergeContext().TableRouteResults[0];
var shardingDbContext = GetStreamMergeContext().CreateDbContext(dataSourceName, routeResult, ConnectionModeEnum.MEMORY_STRICTLY);
var newQueryable = (IQueryable<TEntity>)GetStreamMergeContext().GetOriginalQueryable().ReplaceDbContextQueryable(shardingDbContext);
var enumeratorParallelExecutor = new SingleQueryEnumeratorExecutor<TEntity>(GetStreamMergeContext());
var streamMergeContext = GetStreamMergeContext();
var shardingRouteResult = streamMergeContext.ShardingRouteResult;
var sqlRouteUnit = shardingRouteResult.RouteUnits.First();
var shardingDbContext = streamMergeContext.CreateDbContext(sqlRouteUnit, ConnectionModeEnum.MEMORY_STRICTLY);
var newQueryable = (IQueryable<TEntity>)streamMergeContext.GetOriginalQueryable().ReplaceDbContextQueryable(shardingDbContext);
var enumeratorParallelExecutor = new SingleQueryEnumeratorExecutor<TEntity>(streamMergeContext);
if (async)
{
var asyncEnumerator = enumeratorParallelExecutor.GetAsyncEnumerator0(newQueryable).WaitAndUnwrapException();

View File

@ -6,10 +6,9 @@ using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.Internal.Visitors;
using ShardingCore.Core.ShardingPage.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Extensions.InternalExtensions;
@ -33,13 +32,13 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
{
private readonly StreamMergeContext _streamMergeContext;
private readonly IShardingPageManager _shardingPageManager;
private readonly IVirtualTableManager _virtualTableManager;
private readonly ITableRouteManager _tableRouteManager;
private readonly IEntityMetadataManager _entityMetadataManager;
private EnumeratorStreamMergeEngineFactory(StreamMergeContext streamMergeContext)
{
_streamMergeContext = streamMergeContext;
_shardingPageManager = streamMergeContext.ShardingRuntimeContext.GetShardingPageManager();
_virtualTableManager =streamMergeContext.ShardingRuntimeContext.GetVirtualTableManager();
_tableRouteManager =streamMergeContext.ShardingRuntimeContext.GetTableRouteManager();
_entityMetadataManager = streamMergeContext.ShardingRuntimeContext.GetEntityMetadataManager();
}
@ -117,10 +116,10 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
}
if (isShardingTable)
{
var virtualTable = _virtualTableManager.GetVirtualTable(shardingEntityType);
if (virtualTable.EnablePagination)
var tableRoute = _tableRouteManager.GetRoute(shardingEntityType);
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));
}
}
@ -148,7 +147,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
PaginationSequenceConfig dataSourceSequenceOrderConfig = null;
PaginationSequenceConfig tableSequenceOrderConfig = null;
IVirtualDataSourceRoute virtualDataSourceRoute = null;
IVirtualTable virtualTable = null;
IVirtualTableRoute tableRoute = null;
bool dataSourceUseReverse = true;
bool tableUseReverse = true;
if (isShardingDataSource)
@ -162,10 +161,10 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
}
if (isShardingTable)
{
virtualTable = _virtualTableManager.GetVirtualTable(shardingEntityType);
if (virtualTable.EnablePagination)
tableRoute = _tableRouteManager.GetRoute(shardingEntityType);
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)
{
tableUseReverse =
virtualTable.EnablePagination && EntityTableReverseShardingPage(virtualTable, total);
tableRoute.EnablePagination && EntityTableReverseShardingPage(tableRoute, total);
}
@ -214,11 +213,11 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
}
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;
}

View File

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

View File

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

View File

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

View File

@ -18,12 +18,12 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
/// <typeparam name="TResult"></typeparam>
/// <param name="results"></param>
/// <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());
}
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results)
protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{
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);
}
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);
}

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();
@ -31,7 +31,7 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.CircuitBreakers
return false;
}
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results)
protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{
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;
}
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results)
protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{
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
.Where(o => o is IRouteQueryResult routeQueryResult && routeQueryResult.HasQueryResult())
.Take(2).Count() > 1;
}
protected override bool RandomConditionalTrip<TResult>(IEnumerable<TResult> results)
protected override bool RandomConditionTerminated<TResult>(IEnumerable<TResult> results)
{
return results
.Where(o => o is IRouteQueryResult routeQueryResult && routeQueryResult.HasQueryResult())

View File

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

View File

@ -47,10 +47,8 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
{
var streamMergeContext = GetStreamMergeContext();
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()
.ReplaceDbContextQueryable(shardingDbContext);

View File

@ -37,10 +37,8 @@ namespace ShardingCore.Sharding.MergeEngines.Executors.Enumerators
{
var streamMergeContext = GetStreamMergeContext();
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
.ReplaceDbContextQueryable(shardingDbContext).As<IQueryable<TResult>>();
var streamMergeAsyncEnumerator = await AsyncParallelEnumerator(newQueryable, _async, cancellationToken);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -161,7 +161,8 @@ namespace ShardingCore.Sharding.ShardingExecutors
{
//要么本次查询不追踪如果需要追踪不可以存在跨tails
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());
}
}
@ -176,15 +177,11 @@ namespace ShardingCore.Sharding.ShardingExecutors
return _queryCombineResult;
}
public TableRouteResult[] GetTableRouteResults()
public ShardingRouteResult GetShardingRouteResult()
{
return _tableRouteResults;
return _shardingRouteResult;
}
public DataSourceRouteResult GetDataSourceRouteResult()
{
return _dataSourceRouteResult;
}
/// <summary>
/// 既不可以跨库也不可以跨表,所有的分表都必须是相同后缀才可以
/// </summary>

View File

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

View File

@ -19,7 +19,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Sharding.MergeEngines.Abstractions;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
namespace ShardingCore.Sharding
@ -30,7 +32,7 @@ namespace ShardingCore.Sharding
* @Date: Monday, 25 January 2021 11:38:27
* @Email: 326308290@qq.com
*/
public class StreamMergeContext : ISeqQueryProvider, IMergeParseContext, IDisposable, IPrint
public class StreamMergeContext : IMergeParseContext, IDisposable, IPrint
#if !EFCORE2
, IAsyncDisposable
#endif
@ -49,8 +51,7 @@ namespace ShardingCore.Sharding
public SelectContext SelectContext => ParseResult.GetSelectContext();
public GroupByContext GroupByContext => ParseResult.GetGroupByContext();
public TableRouteResult[] TableRouteResults => MergeQueryCompilerContext.GetTableRouteResults();
public DataSourceRouteResult DataSourceRouteResult => MergeQueryCompilerContext.GetDataSourceRouteResult();
public ShardingRouteResult ShardingRouteResult => MergeQueryCompilerContext.GetShardingRouteResult();
/// <summary>
/// 本次查询涉及的对象
@ -113,16 +114,16 @@ namespace ShardingCore.Sharding
/// <summary>
/// 创建对应的dbcontext
/// </summary>
/// <param name="dataSourceName">data source name</param>
/// <param name="tableRouteResult"></param>
/// <param name="sqlRouteUnit">数据库路由最小单元</param>
/// <param name="connectionMode"></param>
/// <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
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)
{
_parallelDbContexts.TryAdd(dbContext, null);
@ -274,14 +275,7 @@ namespace ShardingCore.Sharding
{
if (ThrowIfQueryRouteNotMatch())
{
if (IsDataSourceRouteNotMatch())
{
throw new ShardingCoreDataSourceQueryRouteNotMatchException(MergeQueryCompilerContext.GetQueryExpression().ShardingPrint());
}
else
{
throw new ShardingCoreTableQueryRouteNotMatchException(MergeQueryCompilerContext.GetQueryExpression().ShardingPrint());
}
throw new ShardingCoreQueryRouteNotMatchException(MergeQueryCompilerContext.GetQueryExpression().ShardingPrint());
}
else
{
@ -297,12 +291,7 @@ namespace ShardingCore.Sharding
/// <returns></returns>
public bool IsRouteNotMatch()
{
return DataSourceRouteResult.IntersectDataSources.IsEmpty() || TableRouteResults.IsEmpty();
}
private bool IsDataSourceRouteNotMatch()
{
return DataSourceRouteResult.IntersectDataSources.IsEmpty();
return ShardingRouteResult.IsEmpty;
}
private bool ThrowIfQueryRouteNotMatch()
@ -336,15 +325,10 @@ namespace ShardingCore.Sharding
return OptimizeResult.IsSequenceQuery();
}
public bool CanTrip()
{
return OptimizeResult.CanTrip();
}
public string GetPrintInfo()
{
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()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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