Search in sources :

Example 1 with JdbcWorker

use of com.wplatform.ddal.excutor.JdbcWorker 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 2 with JdbcWorker

use of com.wplatform.ddal.excutor.JdbcWorker 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 3 with JdbcWorker

use of com.wplatform.ddal.excutor.JdbcWorker in project jdbc-shards by wplatform.

the class PreparedRoutingExecutor method invokeUpdateRow.

/**
 * @param result
 * @param row
 * @return
 */
private int invokeUpdateRow(RoutingResult result, Row row) {
    List<JdbcWorker<Integer>> workers = New.arrayList(result.tableNodeCount());
    TableNode[] selectNodes = result.getSelectNodes();
    for (TableNode node : selectNodes) {
        StatementBuilder sqlBuff = new StatementBuilder();
        List<Value> params = doTranslate(node, row, sqlBuff);
        workers.add(createUpdateWorker(node.getShardName(), sqlBuff.toString(), params));
    }
    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) {
                affectRows += future.get();
            }
        } else if (workers.size() == 1) {
            affectRows = workers.get(0).doWork();
        }
        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) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Value(com.wplatform.ddal.value.Value) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3 Value (com.wplatform.ddal.value.Value)3 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 Parameter (com.wplatform.ddal.command.expression.Parameter)1 RoutingResult (com.wplatform.ddal.dispatch.rule.RoutingResult)1 Row (com.wplatform.ddal.result.Row)1 SearchRow (com.wplatform.ddal.result.SearchRow)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1