Search in sources :

Example 11 with AbstractPartitionAlgorithm

use of io.mycat.route.function.AbstractPartitionAlgorithm in project Mycat-Server by MyCATApache.

the class RouterUtil method changeCreateTable.

private static String changeCreateTable(SchemaConfig schema, String tableName, String sql) {
    if (schema.getTables().containsKey(tableName)) {
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement insertStatement = parser.parseStatement();
        if (insertStatement instanceof MySqlCreateTableStatement) {
            TableConfig tableConfig = schema.getTables().get(tableName);
            AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
            if (algorithm instanceof SlotFunction) {
                SQLColumnDefinition column = new SQLColumnDefinition();
                column.setDataType(new SQLCharacterDataType("int"));
                column.setName(new SQLIdentifierExpr("_slot"));
                column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
                ((SQLCreateTableStatement) insertStatement).getTableElementList().add(column);
                return insertStatement.toString();
            }
        }
    }
    return sql;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 12 with AbstractPartitionAlgorithm

use of io.mycat.route.function.AbstractPartitionAlgorithm in project Mycat-Server by MyCATApache.

the class RouterUtil method ruleCalculate.

/**
	 * @return dataNodeIndex -> [partitionKeysValueTuple+]
	 */
public static Set<String> ruleCalculate(TableConfig tc, Set<ColumnRoutePair> colRoutePairSet, Map<String, Integer> dataNodeSlotMap) {
    Set<String> routeNodeSet = new LinkedHashSet<String>();
    String col = tc.getRule().getColumn();
    RuleConfig rule = tc.getRule();
    AbstractPartitionAlgorithm algorithm = rule.getRuleAlgorithm();
    for (ColumnRoutePair colPair : colRoutePairSet) {
        if (colPair.colValue != null) {
            Integer nodeIndx = algorithm.calculate(colPair.colValue);
            if (nodeIndx == null) {
                throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue);
            } else {
                String dataNode = tc.getDataNodes().get(nodeIndx);
                routeNodeSet.add(dataNode);
                if (algorithm instanceof SlotFunction) {
                    dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
                }
                colPair.setNodeId(nodeIndx);
            }
        } else if (colPair.rangeValue != null) {
            Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.beginValue), String.valueOf(colPair.rangeValue.endValue));
            if (nodeRange != null) {
                /**
					 * 不能确认 colPair的 nodeid是否会有其它影响
					 */
                if (nodeRange.length == 0) {
                    routeNodeSet.addAll(tc.getDataNodes());
                } else {
                    ArrayList<String> dataNodes = tc.getDataNodes();
                    String dataNode = null;
                    for (Integer nodeId : nodeRange) {
                        dataNode = dataNodes.get(nodeId);
                        if (algorithm instanceof SlotFunction) {
                            dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
                        }
                        routeNodeSet.add(dataNode);
                    }
                }
            }
        }
    }
    return routeNodeSet;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) RuleConfig(io.mycat.config.model.rule.RuleConfig) SlotFunction(io.mycat.route.function.SlotFunction)

Aggregations

AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)12 TableConfig (io.mycat.config.model.TableConfig)7 SlotFunction (io.mycat.route.function.SlotFunction)6 ConfigException (io.mycat.config.util.ConfigException)4 SQLNonTransientException (java.sql.SQLNonTransientException)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)3 RuleConfig (io.mycat.config.model.rule.RuleConfig)3 ColumnRoutePair (io.mycat.sqlengine.mpp.ColumnRoutePair)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)2 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)2 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)2 SchemaConfig (io.mycat.config.model.SchemaConfig)2 RouteResultsetNode (io.mycat.route.RouteResultsetNode)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1