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