Search in sources :

Example 1 with RuleConfig

use of com.actiontech.dble.config.model.rule.RuleConfig in project dble by actiontech.

the class XMLRuleLoader method loadRule.

private RuleConfig loadRule(Element element) throws SQLSyntaxErrorException {
    Element columnsEle = ConfigUtil.loadElement(element, "columns");
    String column = columnsEle.getTextContent();
    if (StringUtil.isEmpty(column)) {
        throw new ConfigException("no rule column is found");
    }
    String[] columns = SplitUtil.split(column, ',', true);
    if (columns.length > 1) {
        throw new ConfigException("table rule coulmns has multi values:" + columnsEle.getTextContent());
    }
    Element algorithmEle = ConfigUtil.loadElement(element, "algorithm");
    String algorithmName = algorithmEle.getTextContent();
    if (StringUtil.isEmpty(algorithmName)) {
        throw new ConfigException("algorithm is null or empty");
    }
    AbstractPartitionAlgorithm algorithm = functions.get(algorithmName);
    if (algorithm == null) {
        throw new ConfigException("can't find function of name :" + algorithmName);
    }
    return new RuleConfig(column.toUpperCase(), algorithmName, algorithm);
}
Also used : Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) RuleConfig(com.actiontech.dble.config.model.rule.RuleConfig) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig)

Example 2 with RuleConfig

use of com.actiontech.dble.config.model.rule.RuleConfig in project dble by actiontech.

the class RouterUtil method ruleCalculate.

public static Set<String> ruleCalculate(TableConfig tc, Set<ColumnRoutePair> colRoutePairSet) {
    Set<String> routeNodeSet = new LinkedHashSet<>();
    String col = tc.getRule().getColumn();
    RuleConfig rule = tc.getRule();
    AbstractPartitionAlgorithm algorithm = rule.getRuleAlgorithm();
    for (ColumnRoutePair colPair : colRoutePairSet) {
        if (colPair.colValue != null) {
            Integer nodeIndex = algorithm.calculate(colPair.colValue);
            if (nodeIndex == null) {
                throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue);
            } else {
                String dataNode = tc.getDataNodes().get(nodeIndex);
                routeNodeSet.add(dataNode);
                colPair.setNodeId(nodeIndex);
            }
        } else if (colPair.rangeValue != null) {
            Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.getBeginValue()), String.valueOf(colPair.rangeValue.getEndValue()));
            if (nodeRange != null) {
                /**
                 * not sure  colPair's nodeid has other effect
                 */
                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);
                        routeNodeSet.add(dataNode);
                    }
                }
            }
        }
    }
    return routeNodeSet;
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair) RuleConfig(com.actiontech.dble.config.model.rule.RuleConfig)

Example 3 with RuleConfig

use of com.actiontech.dble.config.model.rule.RuleConfig in project dble by actiontech.

the class XMLRuleLoader method loadTableRules.

/**
 * tableRule tag:
 * <tableRule name="sharding-by-month">
 * <rule>
 * <columns>create_date</columns>
 * <algorithm>partbymonth</algorithm>
 * </rule>
 * </tableRule>
 *
 * @param root
 * @throws SQLSyntaxErrorException
 */
private void loadTableRules(Element root) throws SQLSyntaxErrorException {
    NodeList list = root.getElementsByTagName("tableRule");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            String name = e.getAttribute("name");
            if (StringUtil.isEmpty(name)) {
                throw new ConfigException("name is null or empty");
            }
            if (tableRules.containsKey(name)) {
                throw new ConfigException("table rule " + name + " duplicated!");
            }
            NodeList ruleNodes = e.getElementsByTagName("rule");
            int length = ruleNodes.getLength();
            if (length > 1) {
                throw new ConfigException("only one rule can defined :" + name);
            }
            // rule has only one element now. Maybe it will not contains one rule in feature
            // RuleConfig:rule->function
            RuleConfig rule = loadRule((Element) ruleNodes.item(0));
            tableRules.put(name, new TableRuleConfig(name, rule));
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) RuleConfig(com.actiontech.dble.config.model.rule.RuleConfig) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig)

Aggregations

RuleConfig (com.actiontech.dble.config.model.rule.RuleConfig)3 TableRuleConfig (com.actiontech.dble.config.model.rule.TableRuleConfig)2 ConfigException (com.actiontech.dble.config.util.ConfigException)2 Element (org.w3c.dom.Element)2 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)1 ColumnRoutePair (com.actiontech.dble.sqlengine.mpp.ColumnRoutePair)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1