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();
}
}
}
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);
}
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);
}
Aggregations