Beispiel #1
0
  /**
   * Java version of table schema change. - Supports adding columns with default values (or null if
   * none specified) - Supports dropping columns. - Supports widening of columns.
   *
   * <p>Note, this might fail in wierd ways if you ask it to do more than what the EE version can
   * do. It's not really set up to test the negative cases.
   */
  public static void migrateTable(VoltTable source, VoltTable dest) throws Exception {
    Map<Integer, Integer> indexMap = new TreeMap<Integer, Integer>();

    for (int i = 0; i < dest.getColumnCount(); i++) {
      String destColName = dest.getColumnName(i);
      for (int j = 0; j < source.getColumnCount(); j++) {
        String srcColName = source.getColumnName(j);
        if (srcColName.equals(destColName)) {
          indexMap.put(i, j);
        }
      }
    }

    assert (dest.getRowCount() == 0);

    source.resetRowPosition();
    while (source.advanceRow()) {
      Object[] row = new Object[dest.getColumnCount()];
      // get the values from the source table or defaults
      for (int i = 0; i < dest.getColumnCount(); i++) {
        if (indexMap.containsKey(i)) {
          int sourcePos = indexMap.get(i);
          row[i] = source.get(sourcePos, source.getColumnType(sourcePos));
        } else {
          row[i] = dest.getColumnDefaultValue(i);
          // handle no default specified
          if (row[i] == TableShorthand.ColMeta.NO_DEFAULT_VALUE) {
            if (dest.getColumnNullable(i)) {
              row[i] = null;
            } else {
              throw new RuntimeException(
                  String.format(
                      "New column %s needs a default value in migration", dest.getColumnName(i)));
            }
          }
        }
        // make the values the core types of the target table
        VoltType destColType = dest.getColumnType(i);
        Class<?> descColClass = destColType.classFromType();
        row[i] = ParameterConverter.tryToMakeCompatible(descColClass, row[i]);
        // check the result type in an assert
        assert (ParameterConverter.verifyParameterConversion(row[i], descColClass));
      }

      dest.addRow(row);
    }
  }