use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat_plus by coderczp.
the class MultiNodeQueryHandler method outputMergeResult.
/**
* 将汇聚结果集数据真正的发送给Mycat客户端
* @param source
* @param eof
* @param
*/
public void outputMergeResult(final ServerConnection source, final byte[] eof, Iterator<UnsafeRow> iter, AtomicBoolean isMiddleResultDone) {
try {
lock.lock();
ByteBuffer buffer = session.getSource().allocate();
final RouteResultset rrs = this.dataMergeSvr.getRrs();
/**
* 处理limit语句的start 和 end位置,将正确的结果发送给
* Mycat 客户端
*/
int start = rrs.getLimitStart();
int end = start + rrs.getLimitSize();
int index = 0;
if (start < 0)
start = 0;
if (rrs.getLimitSize() < 0)
end = Integer.MAX_VALUE;
if (prepared) {
while (iter.hasNext()) {
UnsafeRow row = iter.next();
if (index >= start) {
row.packetId = ++packetId;
BinaryRowDataPacket binRowPacket = new BinaryRowDataPacket();
binRowPacket.read(fieldPackets, row);
buffer = binRowPacket.write(buffer, source, true);
}
index++;
if (index == end) {
break;
}
}
} else {
while (iter.hasNext()) {
UnsafeRow row = iter.next();
if (index >= start) {
row.packetId = ++packetId;
buffer = row.write(buffer, source, true);
}
index++;
if (index == end) {
break;
}
}
}
eof[3] = ++packetId;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("last packet id:" + packetId);
}
// huangyiming add 中间过程缓存起来,isMiddleResultDone是确保合并部分执行完成后才会执行secondExecute
MiddlerResultHandler middlerResultHandler = source.getSession2().getMiddlerResultHandler();
if (null != middlerResultHandler) {
if (buffer.position() > 0) {
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
buffer.clear();
// 如果该操作只是一个中间过程则把结果存储起来
String str = ResultSetUtil.getColumnValAsString(data, fields, 0);
// 真的需要数据合并的时候才合并
if (rrs.isHasAggrColumn()) {
middlerResultHandler.getResult().clear();
if (str != null) {
middlerResultHandler.add(str);
}
}
}
isMiddleResultDone.set(false);
} else {
ByteBuffer byteBuffer = source.writeToBuffer(eof, buffer);
/**
* 真正的开始把Writer Buffer的数据写入到channel 中
*/
session.getSource().write(byteBuffer);
}
} catch (Exception e) {
e.printStackTrace();
handleDataProcessException(e);
} finally {
lock.unlock();
dataMergeSvr.clear();
}
}
use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat_plus by coderczp.
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();
}
}
use of io.mycat.net.mysql.BinaryRowDataPacket in project Mycat-Server by MyCATApache.
the class MultiNodeQueryHandler method outputMergeResult.
/**
* 将汇聚结果集数据真正的发送给Mycat客户端
* @param source
* @param eof
* @param
*/
public void outputMergeResult(final ServerConnection source, final byte[] eof, Iterator<UnsafeRow> iter, AtomicBoolean isMiddleResultDone) {
try {
lock.lock();
ByteBuffer buffer = session.getSource().allocate();
final RouteResultset rrs = this.dataMergeSvr.getRrs();
/**
* 处理limit语句的start 和 end位置,将正确的结果发送给
* Mycat 客户端
*/
int start = rrs.getLimitStart();
int end = start + rrs.getLimitSize();
int index = 0;
if (start < 0)
start = 0;
if (rrs.getLimitSize() < 0)
end = Integer.MAX_VALUE;
if (prepared) {
while (iter.hasNext()) {
UnsafeRow row = iter.next();
if (index >= start) {
row.packetId = ++packetId;
BinaryRowDataPacket binRowPacket = new BinaryRowDataPacket();
binRowPacket.read(fieldPackets, row);
buffer = binRowPacket.write(buffer, source, true);
}
index++;
if (index == end) {
break;
}
}
} else {
while (iter.hasNext()) {
UnsafeRow row = iter.next();
if (index >= start) {
row.packetId = ++packetId;
buffer = row.write(buffer, source, true);
}
index++;
if (index == end) {
break;
}
}
}
eof[3] = ++packetId;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("last packet id:" + packetId);
}
// huangyiming add 中间过程缓存起来,isMiddleResultDone是确保合并部分执行完成后才会执行secondExecute
MiddlerResultHandler middlerResultHandler = source.getSession2().getMiddlerResultHandler();
if (null != middlerResultHandler) {
if (buffer.position() > 0) {
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
buffer.clear();
// 如果该操作只是一个中间过程则把结果存储起来
String str = ResultSetUtil.getColumnValAsString(data, fields, 0);
// 真的需要数据合并的时候才合并
if (rrs.isHasAggrColumn()) {
middlerResultHandler.getResult().clear();
if (str != null) {
middlerResultHandler.add(str);
}
}
}
isMiddleResultDone.set(false);
} else {
ByteBuffer byteBuffer = source.writeToBuffer(eof, buffer);
/**
* 真正的开始把Writer Buffer的数据写入到channel 中
*/
session.getSource().write(byteBuffer);
}
} catch (Exception e) {
e.printStackTrace();
handleDataProcessException(e);
} finally {
lock.unlock();
dataMergeSvr.clear();
}
}
Aggregations