Search in sources :

Example 1 with SubQueryHandler

use of com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.SubQueryHandler in project dble by actiontech.

the class BaseHandlerBuilder method handleBlockingSubQuery.

protected void handleBlockingSubQuery() {
    if (node.getSubQueries().size() == 0) {
        return;
    }
    final ReentrantLock lock = new ReentrantLock();
    final Condition finishSubQuery = lock.newCondition();
    final AtomicBoolean finished = new AtomicBoolean(false);
    final AtomicInteger subNodes = new AtomicInteger(node.getSubQueries().size());
    final CopyOnWriteArrayList<ErrorPacket> errorPackets = new CopyOnWriteArrayList<>();
    for (ItemSubQuery itemSubQuery : node.getSubQueries()) {
        if (itemSubQuery instanceof ItemSingleRowSubQuery) {
            final SubQueryHandler tempHandler = new SingleRowSubQueryHandler(getSequenceId(), session, (ItemSingleRowSubQuery) itemSubQuery);
            if (isExplain) {
                handleSubQueryForExplain(lock, finishSubQuery, finished, subNodes, itemSubQuery.getPlanNode(), tempHandler);
            } else {
                handleSubQuery(lock, finishSubQuery, finished, subNodes, errorPackets, itemSubQuery.getPlanNode(), tempHandler);
            }
        } else if (itemSubQuery instanceof ItemInSubQuery) {
            final SubQueryHandler tempHandler = new InSubQueryHandler(getSequenceId(), session, (ItemInSubQuery) itemSubQuery);
            if (isExplain) {
                handleSubQueryForExplain(lock, finishSubQuery, finished, subNodes, itemSubQuery.getPlanNode(), tempHandler);
            } else {
                handleSubQuery(lock, finishSubQuery, finished, subNodes, errorPackets, itemSubQuery.getPlanNode(), tempHandler);
            }
        } else if (itemSubQuery instanceof ItemAllAnySubQuery) {
            final SubQueryHandler tempHandler = new AllAnySubQueryHandler(getSequenceId(), session, (ItemAllAnySubQuery) itemSubQuery);
            if (isExplain) {
                handleSubQueryForExplain(lock, finishSubQuery, finished, subNodes, itemSubQuery.getPlanNode(), tempHandler);
            } else {
                handleSubQuery(lock, finishSubQuery, finished, subNodes, errorPackets, itemSubQuery.getPlanNode(), tempHandler);
            }
        }
    }
    lock.lock();
    try {
        while (!finished.get()) {
            finishSubQuery.await();
        }
    } catch (InterruptedException e) {
        LOGGER.info("execute ScalarSubQuery " + e);
        ErrorPacket errorPackage = new ErrorPacket();
        errorPackage.setErrNo(ErrorCode.ER_UNKNOWN_ERROR);
        String errorMsg = e.getMessage() == null ? e.toString() : e.getMessage();
        errorPackage.setMessage(errorMsg.getBytes(StandardCharsets.UTF_8));
        errorPackets.add(errorPackage);
    } finally {
        lock.unlock();
    }
    if (errorPackets.size() > 0) {
        throw new MySQLOutPutException(errorPackets.get(0).getErrNo(), "", new String(errorPackets.get(0).getMessage(), StandardCharsets.UTF_8));
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) SingleRowSubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.SingleRowSubQueryHandler) InSubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.InSubQueryHandler) AllAnySubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.AllAnySubQueryHandler) SubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.SubQueryHandler) SingleRowSubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.SingleRowSubQueryHandler) ItemAllAnySubQuery(com.actiontech.dble.plan.common.item.subquery.ItemAllAnySubQuery) ItemInSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ErrorPacket(com.actiontech.dble.net.mysql.ErrorPacket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllAnySubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.AllAnySubQueryHandler) InSubQueryHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.InSubQueryHandler) ItemSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemSubQuery) ItemSingleRowSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemSingleRowSubQuery) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Aggregations

AllAnySubQueryHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.AllAnySubQueryHandler)1 InSubQueryHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.InSubQueryHandler)1 SingleRowSubQueryHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.SingleRowSubQueryHandler)1 SubQueryHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.subquery.SubQueryHandler)1 ErrorPacket (com.actiontech.dble.net.mysql.ErrorPacket)1 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)1 ItemAllAnySubQuery (com.actiontech.dble.plan.common.item.subquery.ItemAllAnySubQuery)1 ItemInSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery)1 ItemSingleRowSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemSingleRowSubQuery)1 ItemSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemSubQuery)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Condition (java.util.concurrent.locks.Condition)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1