Search in sources :

Example 1 with TableRouter

use of com.wplatform.ddal.dispatch.rule.TableRouter in project jdbc-shards by wplatform.

the class XmlConfigParser method newTableConfig.

/**
     * @param scConfig
     * @param template
     * @param tableNode
     * @return
     */
private TableConfig newTableConfig(SchemaConfig scConfig, Map<String, String> template, XNode tableNode) {
    TableConfig config = new TableConfig();
    String router = null;
    boolean validation = scConfig.isValidation();
    String scanLevel = "none";
    String shard = null;
    if (template != null) {
        router = template.get("router");
        String s = template.get("validation");
        if (!StringUtils.isNullOrEmpty(s)) {
            validation = Boolean.parseBoolean(s);
        }
        scanLevel = template.get("scanLevel");
        shard = template.get("shard");
    }
    String tableName = tableNode.getStringAttribute("name");
    String routerChild = tableNode.getStringAttribute("router", router);
    validation = tableNode.getBooleanAttribute("validation", validation);
    scanLevel = tableNode.getStringAttribute("scanLevel", scanLevel);
    shard = tableNode.getStringAttribute("shard", shard);
    if (StringUtils.isNullOrEmpty(shard)) {
        shard = scConfig.getShard();
    }
    if (StringUtils.isNullOrEmpty(tableName)) {
        throw new ParsingException("table attribute 'name' is required.");
    }
    if (!StringUtils.isNullOrEmpty(router) && !StringUtils.equals(router, routerChild)) {
        throw new ParsingException("table's attribute 'router' can't override tableGroup's attribute 'router'.");
    }
    if (StringUtils.isNullOrEmpty(router) && StringUtils.isNullOrEmpty(shard)) {
        throw new ParsingException("attribute 'shard' must be null if attribute 'router' was assigned.");
    }
    router = routerChild;
    config.setName(tableName);
    config.setValidation(validation);
    setTableScanLevel(config, scanLevel);
    List<String> nodes = New.arrayList();
    if (!StringUtils.isNullOrEmpty(shard)) {
        for (String string : shard.split(",")) {
            if (!string.trim().isEmpty() && !nodes.contains(string)) {
                nodes.add(string);
            }
        }
        TableNode[] tableNodes = new TableNode[nodes.size()];
        for (int i = 0; i < nodes.size(); i++) {
            tableNodes[i] = new TableNode(nodes.get(i), tableName);
        }
        config.setShards(tableNodes);
    }
    if (!StringUtils.isNullOrEmpty(router)) {
        TableRouter rawRouter = configuration.getTemporaryTableRouters().get(router);
        if (rawRouter == null) {
            throw new ParsingException("The table router '" + router + "' is not found.");
        }
        TableRouter tableRouter = new TableRouter(configuration);
        List<TableNode> partition = rawRouter.getPartition();
        List<TableNode> inited = New.arrayList(partition.size());
        for (TableNode item : partition) {
            String shardName = item.getShardName();
            String name = item.getObjectName();
            String suffix = item.getSuffix();
            if (StringUtils.isNullOrEmpty(name)) {
                name = tableName;
            }
            TableNode initedNode = new TableNode(shardName, name, suffix);
            inited.add(initedNode);
        }
        tableRouter.setId(rawRouter.getId());
        tableRouter.setPartition(inited);
        RuleExpression rawExpression = rawRouter.getRuleExpression();
        RuleExpression expression = new RuleExpression(tableRouter);
        expression.setExpression(rawExpression.getExpression());
        expression.setRuleColumns(rawExpression.getRuleColumns());
        tableRouter.setRuleExpression(expression);
        config.setTableRouter(tableRouter);
        config.setShards(null);
    }
    config.setSchemaConfig(scConfig);
    return config;
}
Also used : TableRouter(com.wplatform.ddal.dispatch.rule.TableRouter) RuleExpression(com.wplatform.ddal.dispatch.rule.RuleExpression) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableConfig(com.wplatform.ddal.config.TableConfig)

Example 2 with TableRouter

use of com.wplatform.ddal.dispatch.rule.TableRouter in project jdbc-shards by wplatform.

the class XmlRuleConfigParser method parseTableRule.

private void parseTableRule(List<XNode> list) throws Exception {
    for (XNode xNode : list) {
        String id = xNode.getStringAttribute("id");
        if (StringUtils.isNullOrEmpty(id)) {
            throw new ParsingException("Error parsing ddal-rule XML . Cause: the id attribute of 'tableRouter' element is required.");
        }
        TableRouter routeConfig = new TableRouter(null);
        routeConfig.setId(id);
        parseTableRuleChildrenXNode(routeConfig, xNode.getChildren());
        configuration.addTemporaryTableRouter(id, routeConfig);
    }
}
Also used : TableRouter(com.wplatform.ddal.dispatch.rule.TableRouter)

Aggregations

TableRouter (com.wplatform.ddal.dispatch.rule.TableRouter)2 TableConfig (com.wplatform.ddal.config.TableConfig)1 RuleExpression (com.wplatform.ddal.dispatch.rule.RuleExpression)1 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)1