Exemplo n.º 1
0
 private void shardingKeyIndex() {
   // create shardingKey index
   if (ruleColumns != null) {
     ArrayList<Index> indexes = getIndexes();
     boolean isMatch = false;
     for (Index index : indexes) {
       Column[] columns = index.getColumns();
       if (columns.length != ruleColumns.length) {
         continue;
       }
       boolean shardingKeyIndex = true;
       for (int idx = 0; idx < columns.length; idx++) {
         if (columns[idx] != ruleColumns[idx]) {
           shardingKeyIndex = false;
           break;
         }
       }
       if (shardingKeyIndex) {
         index.getIndexType().shardingKeyIndex();
         isMatch = true;
       }
     }
     if (!isMatch) {
       List<Column> asList = Arrays.asList(ruleColumns);
       addIndex("$shardingKey", New.arrayList(asList), IndexType.createShardingKey(false));
     }
   }
 }
Exemplo n.º 2
0
 @Override
 public Index getUniqueIndex() {
   for (Index idx : indexes) {
     if (idx.getIndexType().isUnique()) {
       return idx;
     }
   }
   return null;
 }
Exemplo n.º 3
0
  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;
  }