Search in sources :

Example 6 with ColumnRoutePair

use of com.actiontech.dble.sqlengine.mpp.ColumnRoutePair in project dble by actiontech.

the class RouteCalculateUnit method addShardingExpr.

public void addShardingExpr(String tableName, String columnName, Object value) {
    Map<String, Set<ColumnRoutePair>> tableColumnsMap = tablesAndConditions.get(tableName);
    if (value == null) {
        // where a=null
        return;
    }
    if (tableColumnsMap == null) {
        tableColumnsMap = new LinkedHashMap<>();
        tablesAndConditions.put(tableName, tableColumnsMap);
    }
    String upperColName = columnName.toUpperCase();
    Set<ColumnRoutePair> columnValues = tableColumnsMap.get(upperColName);
    if (columnValues == null) {
        columnValues = new LinkedHashSet<>();
        tablesAndConditions.get(tableName).put(upperColName, columnValues);
    }
    if (value instanceof Object[]) {
        for (Object item : (Object[]) value) {
            if (item == null) {
                continue;
            }
            columnValues.add(new ColumnRoutePair(item.toString()));
        }
    } else if (value instanceof RangeValue) {
        columnValues.add(new ColumnRoutePair((RangeValue) value));
    } else {
        columnValues.add(new ColumnRoutePair(value.toString()));
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair) RangeValue(com.actiontech.dble.sqlengine.mpp.RangeValue)

Example 7 with ColumnRoutePair

use of com.actiontech.dble.sqlengine.mpp.ColumnRoutePair in project dble by actiontech.

the class RouterUtil method tryRouteWithPrimaryCache.

private static boolean tryRouteWithPrimaryCache(RouteResultset rrs, Map<String, Set<String>> tablesRouteMap, LayerCachePool cachePool, Map<String, Set<ColumnRoutePair>> columnsMap, SchemaConfig schema, String tableName, String primaryKey, boolean isSelect) {
    if (cachePool == null || primaryKey == null || columnsMap.get(primaryKey) == null) {
        return false;
    }
    if (LOGGER.isDebugEnabled() && rrs.getStatement().startsWith(LoadData.LOAD_DATA_HINT) || rrs.isLoadData()) {
        // load data will not cache
        return false;
    }
    // try by primary key if found in cache
    Set<ColumnRoutePair> primaryKeyPairs = columnsMap.get(primaryKey);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("try to find cache by primary key ");
    }
    String tableKey = StringUtil.getFullName(schema.getName(), tableName, '_');
    boolean allFound = true;
    for (ColumnRoutePair pair : primaryKeyPairs) {
        // may be has multi value of primary key, eg: in(1,2,3)
        String cacheKey = pair.colValue;
        String dataNode = (String) cachePool.get(tableKey, cacheKey);
        if (dataNode == null) {
            allFound = false;
            break;
        } else {
            if (tablesRouteMap.get(tableName) == null) {
                tablesRouteMap.put(tableName, new HashSet<String>());
            }
            tablesRouteMap.get(tableName).add(dataNode);
        }
    }
    if (!allFound && isSelect) {
        // need cache primary key ->dataNode relation
        rrs.setPrimaryKey(tableKey + '.' + primaryKey);
    }
    return allFound;
}
Also used : ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair)

Example 8 with ColumnRoutePair

use of com.actiontech.dble.sqlengine.mpp.ColumnRoutePair in project dble by actiontech.

the class RouterUtil method routeWithPartition.

private static void routeWithPartition(Map<String, Set<String>> tablesRouteMap, String tableName, TableConfig tableConfig, Set<ColumnRoutePair> partitionValue) throws SQLNonTransientException {
    for (ColumnRoutePair pair : partitionValue) {
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (pair.colValue != null) {
            if (NEED_REPLACE.equals(pair.colValue)) {
                return;
            }
            Integer nodeIndex = algorithm.calculate(pair.colValue);
            if (nodeIndex == null) {
                String msg = "can't find any valid data node :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue;
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            }
            ArrayList<String> dataNodes = tableConfig.getDataNodes();
            String node;
            if (nodeIndex >= 0 && nodeIndex < dataNodes.size()) {
                node = dataNodes.get(nodeIndex);
            } else {
                node = null;
                String msg = "Can't find a valid data node for specified node index :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue + " -> " + "Index : " + nodeIndex;
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            }
            if (node != null) {
                if (tablesRouteMap.get(tableName) == null) {
                    tablesRouteMap.put(tableName, new HashSet<String>());
                }
                tablesRouteMap.get(tableName).add(node);
            }
        }
        if (pair.rangeValue != null) {
            Integer[] nodeIndexes = algorithm.calculateRange(pair.rangeValue.getBeginValue().toString(), pair.rangeValue.getEndValue().toString());
            ArrayList<String> dataNodes = tableConfig.getDataNodes();
            String node;
            for (Integer idx : nodeIndexes) {
                if (idx >= 0 && idx < dataNodes.size()) {
                    node = dataNodes.get(idx);
                } else {
                    String msg = "Can't find valid data node(s) for some of specified node indexes :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn();
                    LOGGER.info(msg);
                    throw new SQLNonTransientException(msg);
                }
                if (node != null) {
                    if (tablesRouteMap.get(tableName) == null) {
                        tablesRouteMap.put(tableName, new HashSet<String>());
                    }
                    tablesRouteMap.get(tableName).add(node);
                }
            }
        }
    }
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair)

Aggregations

ColumnRoutePair (com.actiontech.dble.sqlengine.mpp.ColumnRoutePair)8 SQLNonTransientException (java.sql.SQLNonTransientException)5 TableConfig (com.actiontech.dble.config.model.TableConfig)2 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)2 RuleConfig (com.actiontech.dble.config.model.rule.RuleConfig)1 RangeValue (com.actiontech.dble.sqlengine.mpp.RangeValue)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1