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