Search in sources :

Example 1 with TableRuleConfig

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

the class XMLSchemaLoader method loadTables.

private Map<String, TableConfig> loadTables(Element node, boolean isLowerCaseNames) {
    // supprot [`] BEN GONG in table name
    Map<String, TableConfig> tables = new TableConfigMap();
    NodeList nodeList = node.getElementsByTagName("table");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element tableElement = (Element) nodeList.item(i);
        // primaryKey used for cache and autoincrement
        String primaryKey = tableElement.hasAttribute("primaryKey") ? tableElement.getAttribute("primaryKey").toUpperCase() : null;
        // if autoIncrement,it will use sequence handler
        boolean autoIncrement = false;
        if (tableElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(tableElement.getAttribute("autoIncrement"));
        }
        // limit size of the table
        boolean needAddLimit = true;
        if (tableElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(tableElement.getAttribute("needAddLimit"));
        }
        // table type is global or not
        String tableTypeStr = tableElement.hasAttribute("type") ? tableElement.getAttribute("type") : null;
        TableTypeEnum tableType = TableTypeEnum.TYPE_SHARDING_TABLE;
        if ("global".equalsIgnoreCase(tableTypeStr)) {
            tableType = TableTypeEnum.TYPE_GLOBAL_TABLE;
        }
        // dataNode of table
        TableRuleConfig tableRule = null;
        if (tableElement.hasAttribute("rule")) {
            String ruleName = tableElement.getAttribute("rule");
            tableRule = tableRules.get(ruleName);
            if (tableRule == null) {
                throw new ConfigException("rule " + ruleName + " is not found!");
            }
        }
        boolean ruleRequired = false;
        // ruleRequired?
        if (tableElement.hasAttribute("ruleRequired")) {
            ruleRequired = Boolean.parseBoolean(tableElement.getAttribute("ruleRequired"));
        }
        String dataNode = tableElement.getAttribute("dataNode");
        // distribute function
        String distPrex = "distribute(";
        boolean distTableDns = dataNode.startsWith(distPrex);
        if (distTableDns) {
            dataNode = dataNode.substring(distPrex.length(), dataNode.length() - 1);
        }
        String tableNameElement = tableElement.getAttribute("name");
        if (isLowerCaseNames) {
            tableNameElement = tableNameElement.toLowerCase();
        }
        String[] tableNames = tableNameElement.split(",");
        if (tableNames == null) {
            throw new ConfigException("table name is not found!");
        }
        for (String tableName : tableNames) {
            TableConfig table = new TableConfig(tableName, primaryKey, autoIncrement, needAddLimit, tableType, dataNode, (tableRule != null) ? tableRule.getRule() : null, ruleRequired);
            checkDataNodeExists(table.getDataNodes());
            if (table.getRule() != null) {
                checkRuleSuitTable(table);
            }
            if (distTableDns) {
                distributeDataNodes(table.getDataNodes());
            }
            if (tables.containsKey(table.getName())) {
                throw new ConfigException("table " + tableName + " duplicated!");
            }
            tables.put(table.getName(), table);
        }
        // child table must know its unique father
        if (tableNames.length == 1) {
            TableConfig parentTable = tables.get(tableNames[0]);
            // process child tables
            processChildTables(tables, parentTable, dataNode, tableElement, isLowerCaseNames);
        }
    }
    return tables;
}
Also used : TableTypeEnum(com.actiontech.dble.config.model.TableConfig.TableTypeEnum) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig)

Example 2 with TableRuleConfig

use of com.actiontech.dble.config.model.rule.TableRuleConfig 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

TableRuleConfig (com.actiontech.dble.config.model.rule.TableRuleConfig)2 ConfigException (com.actiontech.dble.config.util.ConfigException)2 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 TableTypeEnum (com.actiontech.dble.config.model.TableConfig.TableTypeEnum)1 RuleConfig (com.actiontech.dble.config.model.rule.RuleConfig)1 Node (org.w3c.dom.Node)1