Search in sources :

Example 1 with TableConfigMap

use of io.mycat.config.model.TableConfigMap in project Mycat-Server by MyCATApache.

the class XMLSchemaLoader method loadTables.

private Map<String, TableConfig> loadTables(Element node) {
    // Map<String, TableConfig> tables = new HashMap<String, TableConfig>();
    // 支持表名中包含引号[`] BEN GONG
    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);
        String tableNameElement = tableElement.getAttribute("name").toUpperCase();
        //TODO:路由, 增加对动态日期表的支持
        String tableNameSuffixElement = tableElement.getAttribute("nameSuffix").toUpperCase();
        if (!"".equals(tableNameSuffixElement)) {
            if (tableNameElement.split(",").length > 1) {
                throw new ConfigException("nameSuffix " + tableNameSuffixElement + ", require name parameter cannot multiple breaks!");
            }
            //前缀用来标明日期格式
            tableNameElement = doTableNameSuffix(tableNameElement, tableNameSuffixElement);
        }
        //记录主键,用于之后路由分析,以及启用自增长主键
        String[] tableNames = tableNameElement.split(",");
        String primaryKey = tableElement.hasAttribute("primaryKey") ? tableElement.getAttribute("primaryKey").toUpperCase() : null;
        //记录是否主键自增,默认不是,(启用全局sequence handler)
        boolean autoIncrement = false;
        if (tableElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(tableElement.getAttribute("autoIncrement"));
        }
        //记录是否需要加返回结果集限制,默认需要加
        boolean needAddLimit = true;
        if (tableElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(tableElement.getAttribute("needAddLimit"));
        }
        //记录type,是否为global
        String tableTypeStr = tableElement.hasAttribute("type") ? tableElement.getAttribute("type") : null;
        int tableType = TableConfig.TYPE_GLOBAL_DEFAULT;
        if ("global".equalsIgnoreCase(tableTypeStr)) {
            tableType = TableConfig.TYPE_GLOBAL_TABLE;
        }
        //记录dataNode,就是分布在哪些dataNode上
        String dataNode = tableElement.getAttribute("dataNode");
        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;
        //记录是否绑定有分片规则
        if (tableElement.hasAttribute("ruleRequired")) {
            ruleRequired = Boolean.parseBoolean(tableElement.getAttribute("ruleRequired"));
        }
        if (tableNames == null) {
            throw new ConfigException("table name is not found!");
        }
        //distribute函数,重新编排dataNode
        String distPrex = "distribute(";
        boolean distTableDns = dataNode.startsWith(distPrex);
        if (distTableDns) {
            dataNode = dataNode.substring(distPrex.length(), dataNode.length() - 1);
        }
        //分表功能
        String subTables = tableElement.getAttribute("subTables");
        for (int j = 0; j < tableNames.length; j++) {
            String tableName = tableNames[j];
            TableRuleConfig tableRuleConfig = tableRule;
            if (tableRuleConfig != null) {
                //对于实现TableRuleAware的function进行特殊处理  根据每个表新建个实例
                RuleConfig rule = tableRuleConfig.getRule();
                if (rule.getRuleAlgorithm() instanceof TableRuleAware) {
                    tableRuleConfig = (TableRuleConfig) ObjectUtil.copyObject(tableRuleConfig);
                    tableRules.remove(tableRuleConfig.getName());
                    String newRuleName = tableRuleConfig.getName() + "_" + tableName;
                    tableRuleConfig.setName(newRuleName);
                    TableRuleAware tableRuleAware = (TableRuleAware) tableRuleConfig.getRule().getRuleAlgorithm();
                    tableRuleAware.setRuleName(newRuleName);
                    tableRuleAware.setTableName(tableName);
                    tableRuleConfig.getRule().getRuleAlgorithm().init();
                    tableRules.put(newRuleName, tableRuleConfig);
                }
            }
            TableConfig table = new TableConfig(tableName, primaryKey, autoIncrement, needAddLimit, tableType, dataNode, getDbType(dataNode), (tableRuleConfig != null) ? tableRuleConfig.getRule() : null, ruleRequired, null, false, null, null, subTables);
            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!");
            }
            //放入map
            tables.put(table.getName(), table);
        }
        //只有tableName配置的是单个表(没有逗号)的时候才能有子表
        if (tableNames.length == 1) {
            TableConfig table = tables.get(tableNames[0]);
            // process child tables
            processChildTables(tables, table, dataNode, tableElement);
        }
    }
    return tables;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) TableRuleAware(io.mycat.route.function.TableRuleAware) ConfigException(io.mycat.config.util.ConfigException) TableConfigMap(io.mycat.config.model.TableConfigMap) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig) TableConfig(io.mycat.config.model.TableConfig) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)1 TableConfigMap (io.mycat.config.model.TableConfigMap)1 RuleConfig (io.mycat.config.model.rule.RuleConfig)1 TableRuleConfig (io.mycat.config.model.rule.TableRuleConfig)1 ConfigException (io.mycat.config.util.ConfigException)1 TableRuleAware (io.mycat.route.function.TableRuleAware)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1