use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class ExplainHandler method handle.
public static void handle(String stmt, ServerConnection c, int offset) {
stmt = stmt.substring(offset).trim();
RouteResultset rrs = getRouteResultset(c, stmt);
if (rrs == null) {
return;
}
ByteBuffer buffer = c.allocate();
// write header
ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
byte packetId = header.getPacketId();
buffer = header.write(buffer, c, true);
// write fields
for (FieldPacket field : FIELDS) {
field.setPacketId(++packetId);
buffer = field.write(buffer, c, true);
}
// write eof
EOFPacket eof = new EOFPacket();
eof.setPacketId(++packetId);
buffer = eof.write(buffer, c, true);
if (!rrs.isNeedOptimizer()) {
// write rows
for (RouteResultsetNode node : rrs.getNodes()) {
RowDataPacket row = getRow(node, c.getCharset().getResults());
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
} else {
List<String[]> results = getComplexQueryResult(rrs, c);
for (String[] result : results) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(result[0], c.getCharset().getResults()));
row.add(StringUtil.encode(result[1], c.getCharset().getResults()));
row.add(StringUtil.encode(result[2].replaceAll("[\\t\\n\\r]", " "), c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.setPacketId(++packetId);
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class ServerLoadDataInfileHandler method tryDirectRoute.
private RouteResultset tryDirectRoute(String strSql, String[] lineList) {
RouteResultset rrs = new RouteResultset(strSql, ServerParse.INSERT);
rrs.setLoadData(true);
if (tableConfig == null && schema.getDataNode() != null) {
// default node
RouteResultsetNode rrNode = new RouteResultsetNode(schema.getDataNode(), ServerParse.INSERT, strSql);
rrs.setNodes(new RouteResultsetNode[] { rrNode });
return rrs;
} else if (tableConfig != null && tableConfig.isGlobalTable()) {
ArrayList<String> dataNodes = tableConfig.getDataNodes();
RouteResultsetNode[] rrsNodes = new RouteResultsetNode[dataNodes.size()];
for (int i = 0, dataNodesSize = dataNodes.size(); i < dataNodesSize; i++) {
String dataNode = dataNodes.get(i);
RouteResultsetNode rrNode = new RouteResultsetNode(dataNode, ServerParse.INSERT, strSql);
rrsNodes[i] = rrNode;
}
rrs.setNodes(rrsNodes);
return rrs;
} else if (tableConfig != null) {
DruidShardingParseInfo ctx = new DruidShardingParseInfo();
ctx.addTable(tableName);
if (partitionColumnIndex == -1 || partitionColumnIndex >= lineList.length) {
return null;
} else {
String value = lineList[partitionColumnIndex];
RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
routeCalculateUnit.addShardingExpr(tableName, getPartitionColumn(), parseFieldString(value, loadData.getEnclose(), loadData.getEscape()));
ctx.addRouteCalculateUnit(routeCalculateUnit);
try {
SortedSet<RouteResultsetNode> nodeSet = new TreeSet<>();
for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, false, tableId2DataNodeCache);
if (rrsTmp != null) {
Collections.addAll(nodeSet, rrsTmp.getNodes());
}
}
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
int i = 0;
for (RouteResultsetNode aNodeSet : nodeSet) {
nodes[i] = aNodeSet;
i++;
}
rrs.setNodes(nodes);
return rrs;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
return null;
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class TransformSQLJob method run.
@Override
public void run() {
try {
if (ds == null) {
RouteResultsetNode node = new RouteResultsetNode(databaseName, ServerParse.SELECT, sql);
// create new connection
PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), false, true, node, this, node);
} else {
ds.getConnection(databaseName, true, this, null);
}
} catch (Exception e) {
LOGGER.info("can't get connection for sql ,error:" + e);
ErrorPacket errPacket = new ErrorPacket();
errPacket.setPacketId(0);
errPacket.setErrNo(ErrorCode.ER_YES);
errPacket.setMessage(StringUtil.encode(e.toString(), StandardCharsets.UTF_8.toString()));
writeError(errPacket.toBytes());
}
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class RouterUtil method routeToSingleNode.
/**
* the first node as the result
*
* @param rrs
* @param dataNode
* @return
*/
public static RouteResultset routeToSingleNode(RouteResultset rrs, String dataNode) {
if (dataNode == null) {
return rrs;
}
RouteResultsetNode[] nodes = new RouteResultsetNode[1];
nodes[0] = new RouteResultsetNode(dataNode, rrs.getSqlType(), rrs.getStatement());
rrs.setNodes(nodes);
rrs.setFinishedRoute(true);
if (rrs.getCanRunInReadDB() != null) {
nodes[0].setCanRunInReadDB(rrs.getCanRunInReadDB());
}
if (rrs.getRunOnSlave() != null) {
nodes[0].setRunOnSlave(rrs.getRunOnSlave());
}
return rrs;
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class RouterUtil method routeToMultiNode.
public static RouteResultset routeToMultiNode(boolean cache, RouteResultset rrs, Collection<String> dataNodes) {
RouteResultsetNode[] nodes = new RouteResultsetNode[dataNodes.size()];
int i = 0;
RouteResultsetNode node;
for (String dataNode : dataNodes) {
node = new RouteResultsetNode(dataNode, rrs.getSqlType(), rrs.getStatement());
if (rrs.getCanRunInReadDB() != null) {
node.setCanRunInReadDB(rrs.getCanRunInReadDB());
}
if (rrs.getRunOnSlave() != null) {
nodes[0].setRunOnSlave(rrs.getRunOnSlave());
}
nodes[i++] = node;
}
rrs.setCacheAble(cache);
rrs.setNodes(nodes);
return rrs;
}
Aggregations