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