Search in sources :

Example 16 with InvalidRequestException

use of org.apache.cassandra.thrift.InvalidRequestException in project eiger by wlloyd.

the class AlterTableStatement method getCfDef.

public CfDef getCfDef(String keyspace) throws ConfigurationException, InvalidRequestException {
    CFMetaData meta = Schema.instance.getCFMetaData(keyspace, columnFamily);
    CfDef cfDef = meta.toAvro();
    ByteBuffer columnName = this.oType == OperationType.OPTS ? null : meta.comparator.fromString(this.columnName);
    switch(oType) {
        case ADD:
            if (cfDef.key_alias != null && cfDef.key_alias.equals(columnName))
                throw new InvalidRequestException("Invalid column name: " + this.columnName + ", because it equals to key_alias.");
            cfDef.column_metadata.add(new ColumnDefinition(columnName, TypeParser.parse(validator), null, null, null).toAvro());
            break;
        case ALTER:
            ColumnDefinition column = meta.getColumnDefinition(columnName);
            if (column == null)
                throw new InvalidRequestException(String.format("Column '%s' was not found in CF '%s'", this.columnName, columnFamily));
            column.setValidator(TypeParser.parse(validator));
            cfDef.column_metadata.add(column.toAvro());
            break;
        case DROP:
            ColumnDef toDelete = null;
            for (ColumnDef columnDef : cfDef.column_metadata) {
                if (columnDef.name.equals(columnName)) {
                    toDelete = columnDef;
                }
            }
            if (toDelete == null)
                throw new InvalidRequestException(String.format("Column '%s' was not found in CF '%s'", this.columnName, columnFamily));
            // it is impossible to use ColumnDefinition.deflate() in remove() method
            // it will throw java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.avro.util.Utf8
            // some where deep inside of Avro
            cfDef.column_metadata.remove(toDelete);
            break;
        case OPTS:
            if (cfProps == null)
                throw new InvalidRequestException(String.format("ALTER COLUMNFAMILY WITH invoked, but no parameters found"));
            cfProps.validate();
            applyPropertiesToCfDef(cfDef, cfProps);
            break;
    }
    return cfDef;
}
Also used : ColumnDef(org.apache.cassandra.db.migration.avro.ColumnDef) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) CfDef(org.apache.cassandra.db.migration.avro.CfDef) ByteBuffer(java.nio.ByteBuffer)

Aggregations

InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)16 ByteBuffer (java.nio.ByteBuffer)4 ConfigurationException (org.apache.cassandra.config.ConfigurationException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CFMetaData (org.apache.cassandra.config.CFMetaData)3 IOException (java.io.IOException)2 CfDef (org.apache.cassandra.db.migration.avro.CfDef)2 Cassandra (org.apache.cassandra.thrift.Cassandra)2 TException (org.apache.thrift.TException)2 UnknownHostException (java.net.UnknownHostException)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 HashSet (java.util.HashSet)1 Utf8 (org.apache.avro.util.Utf8)1 Permission (org.apache.cassandra.auth.Permission)1 ColumnDefinition (org.apache.cassandra.config.ColumnDefinition)1 CounterMutation (org.apache.cassandra.db.CounterMutation)1 IMutation (org.apache.cassandra.db.IMutation)1 RowMutation (org.apache.cassandra.db.RowMutation)1 QueryPath (org.apache.cassandra.db.filter.QueryPath)1