Search in sources :

Example 36 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class DataMergeService method run.

@Override
public void run() {
    // @since 2016-03-23
    if (!running.compareAndSet(false, true)) {
        return;
    }
    // eof handler has been placed to "if (pack == END_FLAG_PACK){}" in for-statement
    // @author Uncle-pan
    // @since 2016-03-23
    boolean nulpack = false;
    try {
        // loop-on-packs
        for (; ; ) {
            final PackWraper pack = packs.poll();
            // @since 2016-03-23
            if (pack == null) {
                nulpack = true;
                break;
            }
            // eof: handling eof pack and exit
            if (pack == END_FLAG_PACK) {
                final int warningCount = 0;
                final EOFPacket eofp = new EOFPacket();
                final ByteBuffer eof = ByteBuffer.allocate(9);
                BufferUtil.writeUB3(eof, eofp.calcPacketSize());
                eof.put(eofp.packetId);
                eof.put(eofp.fieldCount);
                BufferUtil.writeUB2(eof, warningCount);
                BufferUtil.writeUB2(eof, eofp.status);
                final ServerConnection source = multiQueryHandler.getSession().getSource();
                final byte[] array = eof.array();
                multiQueryHandler.outputMergeResult(source, array, getResults(array));
                break;
            }
            // merge: sort-or-group, or simple add
            final RowDataPacket row = new RowDataPacket(fieldCount);
            row.read(pack.rowData);
            if (grouper != null) {
                grouper.addRow(row);
            } else if (sorter != null) {
                if (!sorter.addRow(row)) {
                    canDiscard.put(pack.dataNode, true);
                }
            } else {
                result.get(pack.dataNode).add(row);
            }
        }
    // rof
    } catch (final Exception e) {
        multiQueryHandler.handleDataProcessException(e);
    } finally {
        running.set(false);
    }
    // @since 2016-03-23
    if (nulpack && !packs.isEmpty()) {
        this.run();
    }
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ServerConnection (io.mycat.server.ServerConnection)36 ByteBuffer (java.nio.ByteBuffer)18 EOFPacket (io.mycat.net.mysql.EOFPacket)12 RowDataPacket (io.mycat.net.mysql.RowDataPacket)10 FieldPacket (io.mycat.net.mysql.FieldPacket)8 QueryResult (io.mycat.statistic.stat.QueryResult)8 IOException (java.io.IOException)8 FrontendConnection (io.mycat.net.FrontendConnection)4 NIOProcessor (io.mycat.net.NIOProcessor)4 OkPacket (io.mycat.net.mysql.OkPacket)4 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)4 BackendConnection (io.mycat.backend.BackendConnection)2 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)2 MySQLMessage (io.mycat.backend.mysql.MySQLMessage)2 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)2 MycatConfig (io.mycat.config.MycatConfig)2 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)2 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)2 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)2 BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)2