public void Validate(ShardingRule shardingRule, UpdateCommand sqlCommand, ParameterContext parameterContext) { String tableName = sqlCommand.Tables.First().GetTableName().GetIdentifier().GetValue(); foreach (var assignmentSegment in sqlCommand.SetAssignment.GetAssignments()) { String shardingColumn = assignmentSegment.GetColumn().GetIdentifier().GetValue(); if (shardingRule.IsShardingColumn(shardingColumn, tableName)) { var shardingColumnSetAssignmentValue = GetShardingColumnSetAssignmentValue(assignmentSegment, parameterContext); object shardingValue = null; var whereSegmentOptional = sqlCommand.Where; if (whereSegmentOptional != null) { shardingValue = GetShardingValue(whereSegmentOptional, parameterContext, shardingColumn); } if (shardingColumnSetAssignmentValue != null && shardingValue != null && shardingColumnSetAssignmentValue.Equals(shardingValue)) { continue; } throw new ShardingException( $"Can not update sharding key, logic table: [{tableName}], column: [{assignmentSegment}]."); } } }
private List <IRouteValue> GetTableShardingValues(ShardingRule shardingRule, TableRule tableRule, ShardingCondition shardingCondition) { IShardingStrategy tableShardingStrategy = shardingRule.GetTableShardingStrategy(tableRule); return(IsGettingShardingValuesFromHint(tableShardingStrategy) ? GetTableShardingValuesFromHint() : GetShardingValuesFromShardingConditions(shardingRule, tableShardingStrategy.GetShardingColumns(), shardingCondition)); }
public RouteResult Route(ShardingRule shardingRule) { ICollection <RouteResult> result = new List <RouteResult>(logicTables.Count); ICollection <String> bindingTableNames = new SortedSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var logicTable in logicTables) { var tableRule = shardingRule.FindTableRule(logicTable); if (tableRule != null) { if (!bindingTableNames.Contains(logicTable)) { result.Add(new ShardingStandardRoutingEngine(tableRule.LogicTable, sqlStatementContext, shardingConditions, properties).Route(shardingRule)); } shardingRule.FindBindingTableRule(logicTable).IfPresent(bindingTableRule => bindingTableNames.AddAll( bindingTableRule.GetTableRules().Select(o => o.LogicTable).ToList())); } } if (result.IsEmpty()) { throw new ShardingException($"Cannot find table rule and default data source with logic tables: '{logicTables}'"); } if (1 == result.Count) { return(result.First()); } return(new ShardingCartesianRoutingEngine(result).Route(shardingRule)); }
public TableToken(int startIndex, int stopIndex, IdentifierValue identifier, ISqlCommandContext <ISqlCommand> sqlCommandContext, ShardingRule shardingRule) : base(startIndex) { this.stopIndex = stopIndex; this.identifier = identifier; this.sqlCommandContext = sqlCommandContext; this.shardingRule = shardingRule; }
public ShardingMetaDataLoader(IDictionary <string, IDataSource> dataSourceMap, ShardingRule shardingRule, int maxConnectionsLimitSize, bool isCheckingMetaData) { this.dataSourceMap = dataSourceMap; this.shardingRule = shardingRule; this.maxConnectionsLimitSize = maxConnectionsLimitSize; this.isCheckingMetaData = isCheckingMetaData; }
public RouteResult Route(ShardingRule shardingRule) { if (IsDMLForModify(SqlCommandContext) && 1 != ((ITableAvailable)SqlCommandContext).GetAllTables().Count) { throw new ShardingException($"Cannot support Multiple-Table for '{SqlCommandContext.GetSqlCommand()}'."); } return(GenerateRouteResult(GetDataNodes(shardingRule, shardingRule.GetTableRule(LogicTableName)))); }
private ICollection <DataNode> RouteByMixedConditionsWithHint(ShardingRule shardingRule, TableRule tableRule) { if (shardingRule.GetDatabaseShardingStrategy(tableRule) is HintShardingStrategy) { return(Route0(shardingRule, tableRule, GetDatabaseShardingValuesFromHint(), new List <IRouteValue>(0))); } return(Route0(shardingRule, tableRule, new ArrayList <IRouteValue>(0), GetTableShardingValuesFromHint())); }
public void Validate(ShardingRule shardingRule, InsertCommand sqlCommand, ParameterContext parameterContext) { var onDuplicateKeyColumnsSegment = sqlCommand.OnDuplicateKeyColumns; if (onDuplicateKeyColumnsSegment != null && IsUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment, sqlCommand.Table.GetTableName().GetIdentifier().GetValue())) { throw new ShardingException("INSERT INTO .... ON DUPLICATE KEY UPDATE can not support update for sharding column."); } }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); foreach (var dataSourceName in shardingRule.ShardingDataSourceNames.DataSourceNames) { result.GetRouteUnits().Add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), new List <RouteMapper>(0))); } return(result); }
private bool IsUpdateShardingKey(ShardingRule shardingRule, OnDuplicateKeyColumnsSegment onDuplicateKeyColumnsSegment, String tableName) { foreach (var assignment in onDuplicateKeyColumnsSegment.GetColumns()) { if (shardingRule.IsShardingColumn(assignment.GetColumn().GetIdentifier().GetValue(), tableName)) { return(true); } } return(false); }
private ICollection <DataNode> Route0(ShardingRule shardingRule, TableRule tableRule, List <IRouteValue> databaseShardingValues, List <IRouteValue> tableShardingValues) { ICollection <String> routedDataSources = RouteDataSources(shardingRule, tableRule, databaseShardingValues); ICollection <DataNode> result = new LinkedList <DataNode>(); foreach (var routedDataSource in routedDataSources) { result.AddAll(RouteTables(shardingRule, tableRule, routedDataSource, tableShardingValues)); } return(result); }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); var logicTableNames = GetLogicTableNames(); foreach (var logicTableName in logicTableNames) { result.GetRouteUnits().AddAll(GetAllRouteUnits(shardingRule, logicTableName)); } return(result); }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); ICollection <ISet <string> > broadcastDataSourceGroup = GetBroadcastDataSourceGroup(GetDataSourceGroup(shardingRule)); foreach (var broadcastDataSource in broadcastDataSourceGroup) { var dataSourceName = GetRandomDataSourceName(broadcastDataSource); result.GetRouteUnits().Add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), new List <RouteMapper>(0))); } return(result); }
private ICollection <DataNode> GetDataNodes(ShardingRule shardingRule, TableRule tableRule) { if (IsRoutingByHint(shardingRule, tableRule)) { return(RouteByHint(shardingRule, tableRule)); } if (IsRoutingByShardingConditions(shardingRule, tableRule)) { return(RouteByShardingConditions(shardingRule, tableRule)); } return(RouteByMixedConditions(shardingRule, tableRule)); }
private static IShardingRouteEngine GetShardingRoutingEngine(ShardingRule shardingRule, ISqlCommandContext <ISqlCommand> sqlCommandContext, ShardingConditions shardingConditions, ICollection <String> tableNames, ConfigurationProperties properties) { ICollection <String> shardingTableNames = shardingRule.GetShardingLogicTableNames(tableNames); if (1 == shardingTableNames.Count || shardingRule.IsAllBindingTables(shardingTableNames)) { return(new ShardingStandardRoutingEngine(shardingTableNames.First(), sqlCommandContext, shardingConditions, properties)); } // TODO config for cartesian set return(new ShardingComplexRoutingEngine(tableNames, sqlCommandContext, shardingConditions, properties)); }
private ICollection <DataNode> RouteByMixedConditionsWithCondition(ShardingRule shardingRule, TableRule tableRule) { ICollection <DataNode> result = new LinkedList <DataNode>(); foreach (var condition in ShardingConditions.Conditions) { ICollection <DataNode> dataNodes = Route0(shardingRule, tableRule, GetDatabaseShardingValues(shardingRule, tableRule, condition), GetTableShardingValues(shardingRule, tableRule, condition)); result.AddAll(dataNodes); OriginalDataNodes.Add(dataNodes); } return(result); }
private ICollection <string> RouteDataSources(ShardingRule shardingRule, TableRule tableRule, List <IRouteValue> databaseShardingValues) { if (databaseShardingValues.IsEmpty()) { return(tableRule.GetActualDatasourceNames()); } ICollection <string> result = new HashSet <string>(shardingRule.GetDatabaseShardingStrategy(tableRule).DoSharding(tableRule.GetActualDatasourceNames(), databaseShardingValues, this.Properties)); ShardingAssert.If(result.IsEmpty(), "no database route info"); ShardingAssert.Else(tableRule.GetActualDatasourceNames().All(o => result.Contains(o)), $"Some routed data sources do not belong to configured data sources. routed data sources: `{result}`, configured data sources: `{tableRule.GetActualDatasourceNames()}`"); return(result); }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); List <RouteMapper> routingTables = new List <RouteMapper>(_logicTables.Count); foreach (var logicTable in _logicTables) { routingTables.Add(new RouteMapper(logicTable, logicTable)); } String dataSourceName = shardingRule.ShardingDataSourceNames.GetDefaultDataSourceName(); result.GetRouteUnits().Add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), routingTables)); return(result); }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); var dataSourceLogicTablesMap = GetDataSourceLogicTablesMap(); foreach (var dataSourceLogicTableMap in dataSourceLogicTablesMap) { List <ISet <String> > actualTableGroups = GetActualTableGroups(dataSourceLogicTableMap.Key, dataSourceLogicTableMap.Value); List <ISet <RouteMapper> > routingTableGroups = ToRoutingTableGroups(dataSourceLogicTableMap.Key, actualTableGroups); result.GetRouteUnits().AddAll(GetRouteUnits(dataSourceLogicTableMap.Key, routingTableGroups.Cartesian().Select(o => o.ToList()).ToHashSet())); } return(result); }
private ICollection <DataNode> RouteTables(ShardingRule shardingRule, TableRule tableRule, string routedDataSource, List <IRouteValue> tableShardingValues) { ICollection <string> availableTargetTables = tableRule.GetActualTableNames(routedDataSource); ICollection <string> routedTables = new HashSet <string>(tableShardingValues.IsEmpty() ? availableTargetTables : shardingRule.GetTableShardingStrategy(tableRule).DoSharding(availableTargetTables, tableShardingValues, this.Properties)); ShardingAssert.If(routedTables.IsEmpty(), "no table route info"); ICollection <DataNode> result = new LinkedList <DataNode>(); foreach (var routedTable in routedTables) { result.Add(new DataNode(routedDataSource, routedTable)); } return(result); }
private List <IRouteValue> GetShardingValuesFromShardingConditions(ShardingRule shardingRule, ICollection <string> shardingColumns, ShardingCondition shardingCondition) { List <IRouteValue> result = new List <IRouteValue>(shardingColumns.Count); foreach (var routeValue in shardingCondition.RouteValues) { var bindingTableRule = shardingRule.FindBindingTableRule(LogicTableName); if ((LogicTableName.Equals(routeValue.GetTableName()) || bindingTableRule != null && bindingTableRule.HasLogicTable(LogicTableName)) && shardingColumns.Contains(routeValue.GetColumnName())) { result.Add(routeValue); } } return(result); }
private ICollection <RouteUnit> GetAllRouteUnits(ShardingRule shardingRule, String logicTableName) { ICollection <RouteUnit> result = new LinkedList <RouteUnit>(); TableRule tableRule = shardingRule.GetTableRule(logicTableName); foreach (var dataNode in tableRule.ActualDataNodes) { RouteUnit routeUnit = new RouteUnit(new RouteMapper(dataNode.GetDataSourceName(), dataNode.GetDataSourceName()), new List <RouteMapper>() { new RouteMapper(logicTableName, dataNode.GetTableName()) }); result.Add(routeUnit); } return(result); }
private ICollection <ISet <string> > GetDataSourceGroup(ShardingRule shardingRule) { ICollection <ISet <string> > result = new LinkedList <ISet <string> >(); foreach (var tableRule in shardingRule.TableRules) { result.Add(tableRule.GetDataNodeGroups().Keys.ToHashSet()); } if (null != shardingRule.ShardingDataSourceNames.GetDefaultDataSourceName()) { result.Add(new HashSet <string>() { shardingRule.ShardingDataSourceNames.GetDefaultDataSourceName() }); } return(result); }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); foreach (var dataSourceName in shardingRule.ShardingDataSourceNames.DataSourceNames) { if (_dataSourceMetas.GetAllInstanceDataSourceNames().Contains(dataSourceName)) { MasterSlaveRule masterSlaveRule = shardingRule.FindMasterSlaveRule(dataSourceName); if (masterSlaveRule == null || masterSlaveRule.MasterDataSourceName.Equals(dataSourceName)) { result.GetRouteUnits().Add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), new List <RouteMapper>(0))); } } } return(result); }
public static IShardingRouteEngine NewInstance(ShardingRule shardingRule, ShardingConnectorMetaData metaData, ISqlCommandContext <ISqlCommand> sqlCommandContext, ShardingConditions shardingConditions, ConfigurationProperties properties) { var sqlStatement = sqlCommandContext.GetSqlCommand(); ICollection <String> tableNames = sqlCommandContext.GetTablesContext().GetTableNames(); if (sqlStatement is TCLCommand) { return(new ShardingDatabaseBroadcastRoutingEngine()); } if (sqlStatement is DDLCommand) { return(new ShardingTableBroadcastRoutingEngine(metaData.Schema, sqlCommandContext)); } if (sqlStatement is DALCommand) { return(GetDALRoutingEngine(shardingRule, sqlStatement, tableNames)); } if (sqlStatement is DCLCommand) { return(GetDCLRoutingEngine(sqlCommandContext, metaData)); } if (shardingRule.IsAllInDefaultDataSource(tableNames)) { return(new ShardingDefaultDatabaseRoutingEngine(tableNames)); } if (shardingRule.IsAllBroadcastTables(tableNames)) { if (sqlStatement is SelectCommand) { return(new ShardingUnicastRoutingEngine(tableNames)); } return(new ShardingDatabaseBroadcastRoutingEngine()); } if (sqlCommandContext.GetSqlCommand() is DMLCommand && tableNames.IsEmpty() && shardingRule.HasDefaultDataSourceName()) { return(new ShardingDefaultDatabaseRoutingEngine(tableNames)); } if (sqlCommandContext.GetSqlCommand() is DMLCommand && shardingConditions.IsAlwaysFalse() || tableNames.IsEmpty() || !shardingRule.TableRuleExists(tableNames)) { return(new ShardingUnicastRoutingEngine(tableNames)); } return(GetShardingRoutingEngine(shardingRule, sqlCommandContext, shardingConditions, tableNames, properties)); }
public virtual TableInfo RouteByTime(DateTime time) { ShardingRule currentRule = null; foreach (var rule in _rules) { if (time >= rule.EffectiveTime && time < rule.FailureTime) { currentRule = rule; break; } } if (currentRule == null) { return(null); } return(currentRule.GetTableInfoByTime(time)); }
private static IShardingRouteEngine GetDALRoutingEngine(ShardingRule shardingRule, ISqlCommand sqlCommand, ICollection <String> tableNames) { if (sqlCommand is UseCommand) { return(new ShardingIgnoreRoutingEngine()); } if (sqlCommand is SetCommand || sqlCommand is ResetParameterCommand || sqlCommand is ShowDatabasesCommand) { return(new ShardingDatabaseBroadcastRoutingEngine()); } if (tableNames.Any() && !shardingRule.TableRuleExists(tableNames) && shardingRule.HasDefaultDataSourceName()) { return(new ShardingDefaultDatabaseRoutingEngine(tableNames)); } if (tableNames.Any()) { return(new ShardingUnicastRoutingEngine(tableNames)); } return(new ShardingDataSourceGroupBroadcastRoutingEngine()); }
public void FilterTable() { var db = Startup.ServiceProvider.GetService <IDbAccessor>(); var q = db.GetIQueryable <Base_UnitTest>(); ShardingRule rule = new ShardingRule { EntityType = typeof(Base_UnitTest), ExpandByDateMode = ExpandByDateMode.PerMonth, ShardingField = nameof(Base_UnitTest.CreateTime), ShardingType = ShardingType.Date }; List <string> tableSuffixs = new List <string>() { "202001", "202002", "202003" }; DateTime time0 = DateTime.Parse("2019-12-01"); DateTime time1 = DateTime.Parse("2020-01-01"); DateTime time2 = DateTime.Parse("2020-02-01"); DateTime time3 = DateTime.Parse("2020-03-01"); DateTime time4 = DateTime.Parse("2020-04-01"); List <string> res; //= res = GetFilterTable(x => x.CreateTime == time1); Assert.AreEqual(res.ToJson(), _table1); res = GetFilterTable(x => x.CreateTime == time0); Assert.AreEqual(res.Count, 0); res = GetFilterTable(x => x.CreateTime == time4); Assert.AreEqual(res.Count, 0); //!= res = GetFilterTable(x => x.CreateTime != time1); Assert.AreEqual(res.ToJson(), _table23); res = GetFilterTable(x => x.CreateTime != time0); Assert.AreEqual(res.ToJson(), _table123); res = GetFilterTable(x => x.CreateTime != time4); Assert.AreEqual(res.ToJson(), _table123); //> res = GetFilterTable(x => x.CreateTime > time0); Assert.AreEqual(res.ToJson(), _table123); res = GetFilterTable(x => x.CreateTime > time2); Assert.AreEqual(res.ToJson(), _table23); res = GetFilterTable(x => x.CreateTime > time3); Assert.AreEqual(res.Count, 1); res = GetFilterTable(x => x.CreateTime > time4); Assert.AreEqual(res.Count, 0); //>= res = GetFilterTable(x => x.CreateTime > time0); Assert.AreEqual(res.ToJson(), _table123); res = GetFilterTable(x => x.CreateTime > time1); Assert.AreEqual(res.ToJson(), _table123); res = GetFilterTable(x => x.CreateTime > time2); Assert.AreEqual(res.ToJson(), _table23); res = GetFilterTable(x => x.CreateTime > time3); Assert.AreEqual(res.Count, 1); res = GetFilterTable(x => x.CreateTime > time4); Assert.AreEqual(res.Count, 0); //< res = GetFilterTable(x => x.CreateTime < time0); Assert.AreEqual(res.Count, 0); res = GetFilterTable(x => x.CreateTime < time1); Assert.AreEqual(res.ToJson(), _table1); res = GetFilterTable(x => x.CreateTime < time2); Assert.AreEqual(res.ToJson(), _table12); res = GetFilterTable(x => x.CreateTime < time3); Assert.AreEqual(res.ToJson(), _table123); res = GetFilterTable(x => x.CreateTime < time4); Assert.AreEqual(res.ToJson(), _table123); //<= res = GetFilterTable(x => x.CreateTime < time0); Assert.AreEqual(res.Count, 0); res = GetFilterTable(x => x.CreateTime < time1); Assert.AreEqual(res.ToJson(), _table1); res = GetFilterTable(x => x.CreateTime < time2); Assert.AreEqual(res.ToJson(), _table12); res = GetFilterTable(x => x.CreateTime < time3); Assert.AreEqual(res.ToJson(), _table123); res = GetFilterTable(x => x.CreateTime < time4); Assert.AreEqual(res.ToJson(), _table123); List <string> GetFilterTable(Expression <Func <Base_UnitTest, bool> > theWhere) { return(ShardingHelper.FilterTable(db.GetIQueryable <Base_UnitTest>().Where(theWhere), tableSuffixs, rule)); } }
public FilterTableByDateVisitor(List <string> allTableSuffixs, ShardingRule rule) : base(allTableSuffixs, rule) { }
public FilterTableByHashModVisitor(List <string> allTables, ShardingRule rule) : base(allTables, rule) { }