public void validationPlanItem(PlanItem item) { int priority = item.getScanningStrategy().priority; if (tableRule instanceof ShardedTableRule) { ShardedTableRule shardedTableRule = (ShardedTableRule) tableRule; switch (shardedTableRule.getScanLevel()) { case ShardedTableRule.SCANLEVEL_SHARDINGKEY: if (priority < ScanningStrategy.USE_SHARDINGKEY.priority) { throw DbException.get( ErrorCode.ALLOWED_SCANTABLE_ERROR, getName(), "shardingKey", "shardingKey"); } break; case ShardedTableRule.SCANLEVEL_UNIQUEINDEX: if (priority < ScanningStrategy.USE_UNIQUEKEY.priority) { throw DbException.get( ErrorCode.ALLOWED_SCANTABLE_ERROR, getName(), "uniqueIndex", "uniqueIndex"); } break; case ShardedTableRule.SCANLEVEL_ANYINDEX: if (priority < ScanningStrategy.USE_INDEXKEY.priority) { throw DbException.get( ErrorCode.ALLOWED_SCANTABLE_ERROR, getName(), "indexKey", "indexKey"); } break; case ShardedTableRule.SCANLEVEL_UNLIMITED: break; default: throw DbException.throwInternalError("invalid scanLevel"); } } }
/** validation the rule columns is in the table columns */ private void setRuleColumns() { if (tableRule instanceof ShardedTableRule) { ShardedTableRule shardedTableRule = (ShardedTableRule) tableRule; String[] ruleColNames = shardedTableRule.getRuleColumns(); ruleColumns = new Column[ruleColNames.length]; for (int i = 0; i < ruleColNames.length; i++) { String colName = database.identifier(ruleColNames[i]); if (!doesColumnExist(colName)) { throw DbException.get(ErrorCode.SHARDING_COLUMN_NOT_FOUND, colName, getName()); } ruleColumns[i] = getColumn(colName); } } }
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter) { PlanItem item = new PlanItem(); item.cost = Constants.COST_ROW_OFFSET; if (tableRule instanceof ShardedTableRule) { ShardedTableRule shardedTableRule = (ShardedTableRule) tableRule; int nodeCount = shardedTableRule.getObjectNodes().length; item.cost = Constants.COST_ROW_OFFSET * nodeCount; } Column[] columns = getRuleColumns(); if (columns != null && masks != null) { for (int i = 0, len = columns.length; i < len; i++) { Column column = columns[i]; int index = column.getColumnId(); int mask = masks[index]; if ((mask & IndexCondition.EQUALITY) == IndexCondition.EQUALITY) { if (i == columns.length - 1) { item.cost = Constants.COST_ROW_OFFSET; item.scanningStrategyFor(ScanningStrategy.USE_SHARDINGKEY); break; } } else { break; } } } double rowsCost = item.cost; ArrayList<Index> indexes = getIndexes(); if (indexes != null && masks != null) { for (Index index : indexes) { columns = index.getColumns(); for (int i = 0, len = columns.length; i < len; i++) { Column column = columns[i]; int columnId = column.getColumnId(); int mask = masks[columnId]; if ((mask & IndexCondition.EQUALITY) == IndexCondition.EQUALITY) { if (i == columns.length - 1 && index.getIndexType().isUnique()) { item.cost = rowsCost * 0.25d; item.scanningStrategyFor(ScanningStrategy.USE_UNIQUEKEY); break; } item.cost = Math.max(rowsCost * 0.5d, item.cost * 0.5d); item.scanningStrategyFor(ScanningStrategy.USE_INDEXKEY); } else if ((mask & IndexCondition.RANGE) == IndexCondition.RANGE) { item.cost = item.cost * 0.70d; item.scanningStrategyFor(ScanningStrategy.USE_INDEXKEY); break; } else if ((mask & IndexCondition.START) == IndexCondition.START) { item.cost = item.cost * 0.75d; item.scanningStrategyFor(ScanningStrategy.USE_INDEXKEY); break; } else if ((mask & IndexCondition.END) == IndexCondition.END) { item.cost = item.cost * 0.75d; item.scanningStrategyFor(ScanningStrategy.USE_INDEXKEY); break; } else { break; } } } } // validationPlanItem(item); return item; }