Search in sources :

Example 1 with TableConfig

use of com.wplatform.ddal.config.TableConfig 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 TableConfig

use of com.wplatform.ddal.config.TableConfig in project jdbc-shards by wplatform.

the class Database method openDatabase.

private synchronized void openDatabase() {
    User systemUser = new User(this, allocateObjectId(), SYSTEM_USER_NAME);
    systemUser.setAdmin(true);
    systemUser.setUserPasswordHash(new byte[0]);
    users.put(SYSTEM_USER_NAME, systemUser);
    Schema schema = new Schema(this, allocateObjectId(), Constants.SCHEMA_MAIN, systemUser, true);
    schemas.put(schema.getName(), schema);
    Role publicRole = new Role(this, 0, Constants.PUBLIC_ROLE_NAME, true);
    roles.put(Constants.PUBLIC_ROLE_NAME, publicRole);
    Session sysSession = createSession(systemUser);
    try {
        SchemaConfig sc = configuration.getSchemaConfig();
        List<TableConfig> ctList = sc.getTables();
        for (TableConfig tableConfig : ctList) {
            String identifier = tableConfig.getName();
            identifier = identifier(identifier);
            TableMate tableMate = new TableMate(schema, allocateObjectId(), identifier);
            tableMate.setTableRouter(tableConfig.getTableRouter());
            tableMate.setShards(tableConfig.getShards());
            tableMate.setScanLevel(tableConfig.getScanLevel());
            tableMate.loadMataData(sysSession);
            if (tableConfig.isValidation()) {
                tableMate.check();
            }
            this.addSchemaObject(tableMate);
        }
    } finally {
        sysSession.close();
    }
}
Also used : Role(com.wplatform.ddal.dbobject.Role) User(com.wplatform.ddal.dbobject.User) SchemaConfig(com.wplatform.ddal.config.SchemaConfig) Schema(com.wplatform.ddal.dbobject.schema.Schema) TableConfig(com.wplatform.ddal.config.TableConfig) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 3 with TableConfig

use of com.wplatform.ddal.config.TableConfig in project jdbc-shards by wplatform.

the class XmlConfigParser method parseSchemaConfig.

private void parseSchemaConfig(XNode xNode) {
    SchemaConfig dsConfig = new SchemaConfig();
    String name = xNode.getStringAttribute("name");
    String shard = xNode.getStringAttribute("shard");
    if (StringUtils.isNullOrEmpty(name)) {
        throw new ParsingException("schema attribute 'name' is required.");
    }
    dsConfig.setName(name);
    dsConfig.setShard(shard);
    List<TableConfig> tableConfings = New.arrayList();
    List<XNode> xNodes = xNode.evalNodes("tableGroup");
    for (XNode tableGroupNode : xNodes) {
        Map<String, String> attributes = parseTableGroupAttributs(tableGroupNode);
        xNodes = tableGroupNode.evalNodes("table");
        for (XNode tableNode : xNodes) {
            TableConfig config = newTableConfig(dsConfig, attributes, tableNode);
            addToListIfNotDuplicate(tableConfings, config);
        }
    }
    xNodes = xNode.evalNodes("table");
    for (XNode tableNode : xNodes) {
        TableConfig config = newTableConfig(dsConfig, null, tableNode);
        addToListIfNotDuplicate(tableConfings, config);
    }
    dsConfig.setTables(tableConfings);
    configuration.setSchemaConfig(dsConfig);
    configuration.getTemporaryTableRouters().clear();
}
Also used : SchemaConfig(com.wplatform.ddal.config.SchemaConfig) TableConfig(com.wplatform.ddal.config.TableConfig)

Aggregations

TableConfig (com.wplatform.ddal.config.TableConfig)3 SchemaConfig (com.wplatform.ddal.config.SchemaConfig)2 Role (com.wplatform.ddal.dbobject.Role)1 User (com.wplatform.ddal.dbobject.User)1 Schema (com.wplatform.ddal.dbobject.schema.Schema)1 TableMate (com.wplatform.ddal.dbobject.table.TableMate)1 RuleExpression (com.wplatform.ddal.dispatch.rule.RuleExpression)1 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)1 TableRouter (com.wplatform.ddal.dispatch.rule.TableRouter)1