Search in sources :

Example 6 with TableNode

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

the class DefineCommandExecutor method execute.

/**
 * execute DDL use default sql translator
 *
 * @param nodes
 */
public void execute(TableNode[] nodes) {
    session.checkCanceled();
    List<JdbcWorker<Integer>> workers = New.arrayList(nodes.length);
    for (TableNode node : nodes) {
        String sql = doTranslate(node);
        List<Parameter> items = getPrepared().getParameters();
        List<Value> params = New.arrayList(items.size());
        for (Parameter parameter : items) {
            params.add(parameter.getParamValue());
        }
        workers.add(createUpdateWorker(node.getShardName(), sql, params));
    }
    addRuningJdbcWorkers(workers);
    try {
        // DDL statement returns nothing.
        if (workers.size() > 1) {
            // MILLISECONDS
            int queryTimeout = getQueryTimeout();
            if (queryTimeout > 0) {
                jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                jdbcExecutor.invokeAll(workers);
            }
        } else if (workers.size() == 1) {
            workers.get(0).doWork();
        }
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Value(com.wplatform.ddal.value.Value) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 7 with TableNode

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

the class PreparedRoutingExecutor method updateRows.

protected int updateRows(TableMate table, List<Row> rows) {
    Map<BatchKey, List<List<Value>>> batches = New.hashMap();
    session.checkCanceled();
    for (Row row : rows) {
        RoutingResult result = routingHandler.doRoute(table, row);
        TableNode[] selectNodes = result.getSelectNodes();
        for (TableNode node : selectNodes) {
            StatementBuilder sqlBuff = new StatementBuilder();
            List<Value> params = doTranslate(node, row, sqlBuff);
            BatchKey batchKey = new BatchKey(node.getShardName(), sqlBuff.toString());
            List<List<Value>> batchArgs = batches.get(batchKey);
            if (batchArgs == null) {
                batchArgs = New.arrayList(10);
                batches.put(batchKey, batchArgs);
            }
            batchArgs.add(params);
        }
    }
    List<JdbcWorker<Integer[]>> workers = New.arrayList(batches.size());
    for (Map.Entry<BatchKey, List<List<Value>>> entry : batches.entrySet()) {
        String shardName = entry.getKey().shardName;
        String sql = entry.getKey().sql;
        List<List<Value>> array = entry.getValue();
        workers.add(createBatchUpdateWorker(shardName, sql, array));
    }
    try {
        addRuningJdbcWorkers(workers);
        int affectRows = 0;
        if (workers.size() > 1) {
            // MILLISECONDS
            int queryTimeout = getQueryTimeout();
            List<Future<Integer[]>> invokeAll;
            if (queryTimeout > 0) {
                invokeAll = jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                invokeAll = jdbcExecutor.invokeAll(workers);
            }
            for (Future<Integer[]> future : invokeAll) {
                Integer[] integers = future.get();
                for (Integer integer : integers) {
                    affectRows += integer;
                }
            }
        } else if (workers.size() == 1) {
            Integer[] integers = workers.get(0).doWork();
            for (Integer integer : integers) {
                affectRows += integer;
            }
        }
        return affectRows;
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } catch (ExecutionException e) {
        throw DbException.convert(e.getCause());
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer[]> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) RoutingResult(com.wplatform.ddal.dispatch.rule.RoutingResult) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Future(java.util.concurrent.Future) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow) Map(java.util.Map)

Example 8 with TableNode

use of com.wplatform.ddal.dispatch.rule.TableNode 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 9 with TableNode

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

the class TableMate method loadMataData.

public void loadMataData(Session session) {
    TableNode[] nodes = getPartitionNode();
    if (nodes == null || nodes.length < 1) {
        throw new IllegalStateException();
    }
    TableNode matadataNode = nodes[0];
    String tableName = matadataNode.getCompositeObjectName();
    String shardName = matadataNode.getShardName();
    try {
        trace.debug("Try to load {0} metadata from table {1}.{2}", getName(), shardName, tableName);
        readMataData(session, matadataNode);
        trace.debug("Load the {0} metadata success.", getName());
        initException = null;
    } catch (DbException e) {
        trace.debug("Fail to load {0} metadata from table {1}.{2}. error: {3}", getName(), shardName, tableName, e.getCause().getMessage());
        initException = e;
        Column[] cols = {};
        setColumns(cols);
        scanIndex = new IndexMate(this, 0, null, IndexColumn.wrap(cols), IndexType.createNonUnique());
        indexes.add(scanIndex);
    }
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) IndexMate(com.wplatform.ddal.dbobject.index.IndexMate) DbException(com.wplatform.ddal.message.DbException)

Example 10 with TableNode

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

the class DropTableExecutor method executeDrop.

private void executeDrop(DropTable next) {
    String tableName = next.getTableName();
    TableMate table = findTableMate(tableName);
    if (table != null) {
        TableNode[] nodes = table.getPartitionNode();
        execute(nodes);
        table.markDeleted();
    }
    next = prepared.getNext();
    if (next != null) {
        executeDrop(next);
    }
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Aggregations

TableNode (com.wplatform.ddal.dispatch.rule.TableNode)15 TableMate (com.wplatform.ddal.dbobject.table.TableMate)7 Map (java.util.Map)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 Column (com.wplatform.ddal.dbobject.table.Column)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)3 Value (com.wplatform.ddal.value.Value)3 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)2 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)2 DbException (com.wplatform.ddal.message.DbException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1 CreateIndex (com.wplatform.ddal.command.ddl.CreateIndex)1 Insert (com.wplatform.ddal.command.dml.Insert)1 Query (com.wplatform.ddal.command.dml.Query)1 Parameter (com.wplatform.ddal.command.expression.Parameter)1 TableConfig (com.wplatform.ddal.config.TableConfig)1 IndexMate (com.wplatform.ddal.dbobject.index.IndexMate)1