Exemplo n.º 1
0
  public boolean announceMigration(boolean isLocalOnly) throws RequestValidationException {
    CFMetaData cfm = Schema.instance.getCFMetaData(keyspace(), columnFamily()).copy();
    IndexTarget target = rawTarget.prepare(cfm);
    logger.debug("Updating column {} definition for index {}", target.column, indexName);
    ColumnDefinition cd = cfm.getColumnDefinition(target.column);

    if (cd.getIndexType() != null && ifNotExists) return false;

    if (properties.isCustom) {
      cd.setIndexType(IndexType.CUSTOM, properties.getOptions());
    } else if (cfm.comparator.isCompound()) {
      Map<String, String> options = Collections.emptyMap();
      // For now, we only allow indexing values for collections, but we could later allow
      // to also index map keys, so we record that this is the values we index to make our
      // lives easier then.
      if (cd.type.isCollection() && cd.type.isMultiCell())
        options =
            ImmutableMap.of(
                target.isCollectionKeys
                    ? SecondaryIndex.INDEX_KEYS_OPTION_NAME
                    : SecondaryIndex.INDEX_VALUES_OPTION_NAME,
                "");
      cd.setIndexType(IndexType.COMPOSITES, options);
    } else {
      cd.setIndexType(IndexType.KEYS, Collections.<String, String>emptyMap());
    }

    cd.setIndexName(indexName);
    cfm.addDefaultIndexNames();
    MigrationManager.announceColumnFamilyUpdate(cfm, false, isLocalOnly);
    return true;
  }
  public void announceMigration() throws RequestValidationException {
    KSMetaData ksm = Schema.instance.getKSMetaData(name);
    // In the (very) unlikely case the keyspace was dropped since validate()
    if (ksm == null) throw new InvalidRequestException("Unknown keyspace " + name);

    MigrationManager.announceKeyspaceUpdate(attrs.asKSMetadataUpdate(ksm));
  }
 public boolean announceMigration() throws RequestValidationException {
   try {
     MigrationManager.announceNewColumnFamily(getCFMetaData());
     return true;
   } catch (AlreadyExistsException e) {
     if (ifNotExists) return false;
     throw e;
   }
 }
Exemplo n.º 4
0
 /*
  * Like updateVersion, but also announces via gossip
  */
 public void updateVersionAndAnnounce() {
   updateVersion();
   MigrationManager.passiveAnnounce(version);
 }
Exemplo n.º 5
0
  public void announceMigration() throws RequestValidationException {
    CFMetaData meta = validateColumnFamily(keyspace(), columnFamily());
    CFMetaData cfm = meta.clone();

    CFDefinition cfDef = meta.getCfDef();
    CFDefinition.Name name = columnName == null ? null : cfDef.get(columnName);
    switch (oType) {
      case ADD:
        if (cfDef.isCompact)
          throw new InvalidRequestException("Cannot add new column to a compact CF");
        if (name != null) {
          switch (name.kind) {
            case KEY_ALIAS:
            case COLUMN_ALIAS:
              throw new InvalidRequestException(
                  String.format(
                      "Invalid column name %s because it conflicts with a PRIMARY KEY part",
                      columnName));
            case COLUMN_METADATA:
              throw new InvalidRequestException(
                  String.format(
                      "Invalid column name %s because it conflicts with an existing column",
                      columnName));
          }
        }

        AbstractType<?> type = validator.getType();
        if (type instanceof CollectionType) {
          if (!cfDef.isComposite)
            throw new InvalidRequestException(
                "Cannot use collection types with non-composite PRIMARY KEY");
          if (cfDef.cfm.isSuper())
            throw new InvalidRequestException(
                "Cannot use collection types with Super column family");

          Map<ByteBuffer, CollectionType> collections =
              cfDef.hasCollections
                  ? new HashMap<ByteBuffer, CollectionType>(cfDef.getCollectionType().defined)
                  : new HashMap<ByteBuffer, CollectionType>();

          collections.put(columnName.key, (CollectionType) type);
          ColumnToCollectionType newColType = ColumnToCollectionType.getInstance(collections);
          List<AbstractType<?>> ctypes =
              new ArrayList<AbstractType<?>>(((CompositeType) cfm.comparator).types);
          if (cfDef.hasCollections) ctypes.set(ctypes.size() - 1, newColType);
          else ctypes.add(newColType);
          cfm.comparator = CompositeType.getInstance(ctypes);
        }

        Integer componentIndex =
            cfDef.isComposite
                ? ((CompositeType) meta.comparator).types.size() - (cfDef.hasCollections ? 2 : 1)
                : null;
        cfm.addColumnDefinition(ColumnDefinition.regularDef(columnName.key, type, componentIndex));
        break;

      case ALTER:
        if (name == null)
          throw new InvalidRequestException(
              String.format("Column %s was not found in table %s", columnName, columnFamily()));

        switch (name.kind) {
          case KEY_ALIAS:
            AbstractType<?> newType = validator.getType();
            if (newType instanceof CounterColumnType)
              throw new InvalidRequestException(
                  String.format(
                      "counter type is not supported for PRIMARY KEY part %s", columnName));
            if (cfDef.hasCompositeKey) {
              List<AbstractType<?>> newTypes =
                  new ArrayList<AbstractType<?>>(((CompositeType) cfm.getKeyValidator()).types);
              newTypes.set(name.position, newType);
              cfm.keyValidator(CompositeType.getInstance(newTypes));
            } else {
              cfm.keyValidator(newType);
            }
            break;
          case COLUMN_ALIAS:
            assert cfDef.isComposite;
            List<AbstractType<?>> newTypes =
                new ArrayList<AbstractType<?>>(((CompositeType) cfm.comparator).types);
            newTypes.set(name.position, validator.getType());
            cfm.comparator = CompositeType.getInstance(newTypes);
            break;
          case VALUE_ALIAS:
            cfm.defaultValidator(validator.getType());
            break;
          case COLUMN_METADATA:
            ColumnDefinition column = cfm.getColumnDefinition(columnName.key);
            column.setValidator(validator.getType());
            break;
        }
        break;

      case DROP:
        if (cfDef.isCompact)
          throw new InvalidRequestException("Cannot drop columns from a compact CF");
        if (name == null)
          throw new InvalidRequestException(
              String.format("Column %s was not found in table %s", columnName, columnFamily()));

        switch (name.kind) {
          case KEY_ALIAS:
          case COLUMN_ALIAS:
            throw new InvalidRequestException(
                String.format("Cannot drop PRIMARY KEY part %s", columnName));
          case COLUMN_METADATA:
            ColumnDefinition toDelete = null;
            for (ColumnDefinition columnDef : cfm.regularColumns()) {
              if (columnDef.name.equals(columnName.key)) toDelete = columnDef;
            }
            assert toDelete != null;
            cfm.removeColumnDefinition(toDelete);
            break;
        }
        break;
      case OPTS:
        if (cfProps == null)
          throw new InvalidRequestException(
              String.format("ALTER COLUMNFAMILY WITH invoked, but no parameters found"));

        cfProps.validate();
        cfProps.applyToCFMetadata(cfm);
        break;
      case RENAME:
        for (Map.Entry<ColumnIdentifier, ColumnIdentifier> entry : renames.entrySet()) {
          ColumnIdentifier from = entry.getKey();
          ColumnIdentifier to = entry.getValue();
          cfm.renameColumn(from.key, from.toString(), to.key, to.toString());
        }
        break;
    }

    MigrationManager.announceColumnFamilyUpdate(cfm, false);
  }
 public void announceMigration() throws ConfigurationException {
   CFMetaData cfm = Schema.instance.getCFMetaData(keyspace(), columnFamily()).copy();
   cfm.addTriggerDefinition(TriggerDefinition.create(triggerName, triggerClass));
   logger.info("Adding trigger with name {} and class {}", triggerName, triggerClass);
   MigrationManager.announceColumnFamilyUpdate(cfm, false);
 }