Search in sources :

Example 1 with BinaryRowDataPacket

use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat_plus by coderczp.

the class SingleNodeHandler method rowResponse.

/**
 * select
 *
 * 行数据返回时触发,将行数据写入缓冲区中
 */
@Override
public void rowResponse(byte[] row, BackendConnection conn) {
    this.netOutBytes += row.length;
    this.selectRows++;
    if (isDefaultNodeShowTable || isDefaultNodeShowFullTable) {
        RowDataPacket rowDataPacket = new RowDataPacket(1);
        rowDataPacket.read(row);
        String table = StringUtil.decode(rowDataPacket.fieldValues.get(0), session.getSource().getCharset());
        if (shardingTablesSet.contains(table.toUpperCase())) {
            return;
        }
    }
    row[3] = ++packetId;
    if (prepared) {
        RowDataPacket rowDataPk = new RowDataPacket(fieldCount);
        rowDataPk.read(row);
        BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
        binRowDataPk.read(fieldPackets, rowDataPk);
        binRowDataPk.packetId = rowDataPk.packetId;
        // binRowDataPk.write(session.getSource());
        /*
			 * [fix bug] : 这里不能直接将包写到前端连接,
			 * 因为在fieldEofResponse()方法结束后buffer还没写出,
			 * 所以这里应该将包数据顺序写入buffer(如果buffer满了就写出),然后再将buffer写出
			 */
        buffer = binRowDataPk.write(buffer, session.getSource(), true);
    } else {
        MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
        if (null == middlerResultHandler) {
            buffer = session.getSource().writeToBuffer(row, allocBuffer());
        } else {
            if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                byte[] rv = ResultSetUtil.getColumnVal(row, fields, 0);
                String rowValue = rv == null ? "" : new String(rv);
                middlerResultHandler.add(rowValue);
            }
        }
    }
}
Also used : BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket)

Example 2 with BinaryRowDataPacket

use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat_plus by coderczp.

the class MultiNodeQueryHandler method outputMergeResult.

public void outputMergeResult(final ServerConnection source, final byte[] eof, List<RowDataPacket> results) {
    try {
        lock.lock();
        ByteBuffer buffer = session.getSource().allocate();
        final RouteResultset rrs = this.dataMergeSvr.getRrs();
        // 处理limit语句
        int start = rrs.getLimitStart();
        int end = start + rrs.getLimitSize();
        if (start < 0) {
            start = 0;
        }
        if (rrs.getLimitSize() < 0) {
            end = results.size();
        }
        // }
        if (end > results.size()) {
            end = results.size();
        }
        if (prepared) {
            for (int i = start; i < end; i++) {
                RowDataPacket row = results.get(i);
                BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
                binRowDataPk.read(fieldPackets, row);
                binRowDataPk.packetId = ++packetId;
                // binRowDataPk.write(source);
                buffer = binRowDataPk.write(buffer, session.getSource(), true);
            }
        } else {
            for (int i = start; i < end; i++) {
                RowDataPacket row = results.get(i);
                row.packetId = ++packetId;
                buffer = row.write(buffer, source, true);
            }
        }
        eof[3] = ++packetId;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("last packet id:" + packetId);
        }
        source.write(source.writeToBuffer(eof, buffer));
    } catch (Exception e) {
        handleDataProcessException(e);
    } finally {
        lock.unlock();
        dataMergeSvr.clear();
    }
}
Also used : BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) RouteResultset(io.mycat.route.RouteResultset)

Example 3 with BinaryRowDataPacket

use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat-Server by MyCATApache.

the class MultiNodeQueryHandler method outputMergeResult.

public void outputMergeResult(final ServerConnection source, final byte[] eof, List<RowDataPacket> results) {
    try {
        lock.lock();
        ByteBuffer buffer = session.getSource().allocate();
        final RouteResultset rrs = this.dataMergeSvr.getRrs();
        // 处理limit语句
        int start = rrs.getLimitStart();
        int end = start + rrs.getLimitSize();
        if (start < 0) {
            start = 0;
        }
        if (rrs.getLimitSize() < 0) {
            end = results.size();
        }
        // }
        if (end > results.size()) {
            end = results.size();
        }
        if (prepared) {
            for (int i = start; i < end; i++) {
                RowDataPacket row = results.get(i);
                BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
                binRowDataPk.read(fieldPackets, row);
                binRowDataPk.packetId = ++packetId;
                // binRowDataPk.write(source);
                buffer = binRowDataPk.write(buffer, session.getSource(), true);
            }
        } else {
            for (int i = start; i < end; i++) {
                RowDataPacket row = results.get(i);
                row.packetId = ++packetId;
                buffer = row.write(buffer, source, true);
            }
        }
        eof[3] = ++packetId;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("last packet id:" + packetId);
        }
        source.write(source.writeToBuffer(eof, buffer));
    } catch (Exception e) {
        handleDataProcessException(e);
    } finally {
        lock.unlock();
        dataMergeSvr.clear();
    }
}
Also used : BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) RouteResultset(io.mycat.route.RouteResultset)

Example 4 with BinaryRowDataPacket

use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat-Server by MyCATApache.

the class MultiNodeQueryHandler method rowResponse.

@Override
public void rowResponse(final byte[] row, final BackendConnection conn) {
    if (errorRepsponsed.get()) {
        // conn.close(error);
        return;
    }
    lock.lock();
    try {
        this.selectRows++;
        RouteResultsetNode rNode = (RouteResultsetNode) conn.getAttachment();
        String dataNode = rNode.getName();
        if (dataMergeSvr != null) {
            // even through discarding the all rest data, we can't
            // close the connection for tx control such as rollback or commit.
            // So the "isClosedByDiscard" variable is unnecessary.
            // @author Uncle-pan
            // @since 2016-03-25
            dataMergeSvr.onNewRecord(dataNode, row);
            MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
            if (null != middlerResultHandler) {
                if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                    byte[] rv = ResultSetUtil.getColumnVal(row, fields, 0);
                    String rowValue = rv == null ? "" : new String(rv);
                    middlerResultHandler.add(rowValue);
                }
            }
        } else {
            row[3] = ++packetId;
            RowDataPacket rowDataPkg = null;
            // cache primaryKey-> dataNode
            if (primaryKeyIndex != -1) {
                rowDataPkg = new RowDataPacket(fieldCount);
                rowDataPkg.read(row);
                String primaryKey = new String(rowDataPkg.fieldValues.get(primaryKeyIndex));
                LayerCachePool pool = MycatServer.getInstance().getRouterservice().getTableId2DataNodeCache();
                pool.putIfAbsent(priamaryKeyTable, primaryKey, dataNode);
            }
            if (prepared) {
                if (rowDataPkg == null) {
                    rowDataPkg = new RowDataPacket(fieldCount);
                    rowDataPkg.read(row);
                }
                BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
                binRowDataPk.read(fieldPackets, rowDataPkg);
                binRowDataPk.write(session.getSource());
            } else {
                // add huangyiming
                MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
                if (null == middlerResultHandler) {
                    session.getSource().write(row);
                } else {
                    if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                        String rowValue = ResultSetUtil.getColumnValAsString(row, fields, 0);
                        middlerResultHandler.add(rowValue);
                    }
                }
            }
        }
    } catch (Exception e) {
        handleDataProcessException(e);
    } finally {
        lock.unlock();
    }
}
Also used : BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RowDataPacket(io.mycat.net.mysql.RowDataPacket) BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) LayerCachePool(io.mycat.cache.LayerCachePool) IOException(java.io.IOException)

Example 5 with BinaryRowDataPacket

use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat-Server by MyCATApache.

the class SingleNodeHandler method rowResponse.

/**
 * select
 *
 * 行数据返回时触发,将行数据写入缓冲区中
 */
@Override
public void rowResponse(byte[] row, BackendConnection conn) {
    this.netOutBytes += row.length;
    this.selectRows++;
    if (isDefaultNodeShowTable || isDefaultNodeShowFullTable) {
        RowDataPacket rowDataPacket = new RowDataPacket(1);
        rowDataPacket.read(row);
        String table = StringUtil.decode(rowDataPacket.fieldValues.get(0), session.getSource().getCharset());
        if (shardingTablesSet.contains(table.toUpperCase())) {
            return;
        }
    }
    row[3] = ++packetId;
    if (prepared) {
        RowDataPacket rowDataPk = new RowDataPacket(fieldCount);
        rowDataPk.read(row);
        BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
        binRowDataPk.read(fieldPackets, rowDataPk);
        binRowDataPk.packetId = rowDataPk.packetId;
        // binRowDataPk.write(session.getSource());
        /*
			 * [fix bug] : 这里不能直接将包写到前端连接,
			 * 因为在fieldEofResponse()方法结束后buffer还没写出,
			 * 所以这里应该将包数据顺序写入buffer(如果buffer满了就写出),然后再将buffer写出
			 */
        buffer = binRowDataPk.write(buffer, session.getSource(), true);
    } else {
        MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
        if (null == middlerResultHandler) {
            buffer = session.getSource().writeToBuffer(row, allocBuffer());
        } else {
            if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                byte[] rv = ResultSetUtil.getColumnVal(row, fields, 0);
                String rowValue = rv == null ? "" : new String(rv);
                middlerResultHandler.add(rowValue);
            }
        }
    }
}
Also used : BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket)

Aggregations

BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)8 RowDataPacket (io.mycat.net.mysql.RowDataPacket)6 IOException (java.io.IOException)6 RouteResultset (io.mycat.route.RouteResultset)4 ByteBuffer (java.nio.ByteBuffer)4 LayerCachePool (io.mycat.cache.LayerCachePool)2 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)2 RouteResultsetNode (io.mycat.route.RouteResultsetNode)2