Search in sources :

Example 1 with LayerCachePool

use of io.mycat.cache.LayerCachePool in project Mycat_plus by coderczp.

the class DruidSelectParser method setLimitIFChange.

protected void setLimitIFChange(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLBinaryOpExpr one, int firstrownum, int lastrownum) {
    rrs.setLimitStart(firstrownum);
    rrs.setLimitSize(lastrownum - firstrownum);
    LayerCachePool tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
    try {
        tryRoute(schema, rrs, tableId2DataNodeCache);
    } catch (SQLNonTransientException e) {
        throw new RuntimeException(e);
    }
    if (isNeedChangeLimit(rrs)) {
        one.setRight(new SQLIntegerExpr(0));
        String curentDbType = "db2".equalsIgnoreCase(this.getCurentDbType()) ? "oracle" : getCurentDbType();
        String sql = SQLUtils.toSQLString(stmt, curentDbType);
        ;
        rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, lastrownum, false);
        // 设置改写后的sql
        getCtx().setSql(sql);
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) LayerCachePool(io.mycat.cache.LayerCachePool) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)

Example 2 with LayerCachePool

use of io.mycat.cache.LayerCachePool in project Mycat_plus by coderczp.

the class ShowCache method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    CacheService cacheService = MycatServer.getInstance().getCacheService();
    for (Map.Entry<String, CachePool> entry : cacheService.getAllCachePools().entrySet()) {
        String cacheName = entry.getKey();
        CachePool cachePool = entry.getValue();
        if (cachePool instanceof LayerCachePool) {
            for (Map.Entry<String, CacheStatic> staticsEntry : ((LayerCachePool) cachePool).getAllCacheStatic().entrySet()) {
                RowDataPacket row = getRow(cacheName + '.' + staticsEntry.getKey(), staticsEntry.getValue(), c.getCharset());
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        } else {
            RowDataPacket row = getRow(cacheName, cachePool.getCacheStatic(), c.getCharset());
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) LayerCachePool(io.mycat.cache.LayerCachePool) CachePool(io.mycat.cache.CachePool) CacheStatic(io.mycat.cache.CacheStatic) LayerCachePool(io.mycat.cache.LayerCachePool) FieldPacket(io.mycat.net.mysql.FieldPacket) Map(java.util.Map) CacheService(io.mycat.cache.CacheService)

Example 3 with LayerCachePool

use of io.mycat.cache.LayerCachePool in project Mycat-Server by MyCATApache.

the class DruidSelectParser method setLimitIFChange.

protected void setLimitIFChange(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLBinaryOpExpr one, int firstrownum, int lastrownum) {
    rrs.setLimitStart(firstrownum);
    rrs.setLimitSize(lastrownum - firstrownum);
    LayerCachePool tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
    try {
        tryRoute(schema, rrs, tableId2DataNodeCache);
    } catch (SQLNonTransientException e) {
        throw new RuntimeException(e);
    }
    if (isNeedChangeLimit(rrs)) {
        one.setRight(new SQLIntegerExpr(0));
        String curentDbType = "db2".equalsIgnoreCase(this.getCurentDbType()) ? "oracle" : getCurentDbType();
        String sql = SQLUtils.toSQLString(stmt, curentDbType);
        ;
        rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, lastrownum, false);
        // 设置改写后的sql
        getCtx().setSql(sql);
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) LayerCachePool(io.mycat.cache.LayerCachePool) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)

Example 4 with LayerCachePool

use of io.mycat.cache.LayerCachePool in project Mycat-Server by MyCATApache.

the class DefaultDruidParserTest method getParseTables.

private Object[] getParseTables(String sql) throws Exception {
    SQLStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement statement = parser.parseStatement();
    MycatSchemaStatVisitor visitor = new MycatSchemaStatVisitor();
    LayerCachePool cachePool = mock(LayerCachePool.class);
    RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
    druidParser.parser(schema, rrs, statement, sql, cachePool, visitor);
    DruidShardingParseInfo ctx = druidParser.getCtx();
    return ctx.getTables().toArray();
}
Also used : DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatSchemaStatVisitor(io.mycat.route.parser.druid.MycatSchemaStatVisitor) LayerCachePool(io.mycat.cache.LayerCachePool) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) RouteResultset(io.mycat.route.RouteResultset)

Example 5 with LayerCachePool

use of io.mycat.cache.LayerCachePool 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)

Aggregations

LayerCachePool (io.mycat.cache.LayerCachePool)8 RowDataPacket (io.mycat.net.mysql.RowDataPacket)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 CachePool (io.mycat.cache.CachePool)2 CacheService (io.mycat.cache.CacheService)2 CacheStatic (io.mycat.cache.CacheStatic)2 BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 FieldPacket (io.mycat.net.mysql.FieldPacket)2 RouteResultset (io.mycat.route.RouteResultset)2 RouteResultsetNode (io.mycat.route.RouteResultsetNode)2 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)2 MycatSchemaStatVisitor (io.mycat.route.parser.druid.MycatSchemaStatVisitor)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 Map (java.util.Map)2