Search in sources :

Example 1 with RoutingResult

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

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

the class PreparedRoutingExecutor method updateRow.

protected int updateRow(TableMate table, Row row, List<IndexCondition> where) {
    session.checkCanceled();
    RoutingResult result = routingHandler.doRoute(table, session, where);
    return invokeUpdateRow(result, row);
}
Also used : RoutingResult(com.wplatform.ddal.dispatch.rule.RoutingResult)

Example 3 with RoutingResult

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

the class PreparedRoutingExecutor method updateRow.

protected int updateRow(TableMate table, Row row) {
    session.checkCanceled();
    RoutingResult result = routingHandler.doRoute(table, row);
    return invokeUpdateRow(result, row);
}
Also used : RoutingResult(com.wplatform.ddal.dispatch.rule.RoutingResult)

Aggregations

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