Search in sources :

Example 1 with Triggers

use of org.apache.cassandra.schema.Triggers in project cassandra by apache.

the class TriggerExecutor method executeInternal.

/**
     * Switch class loader before using the triggers for the column family, if
     * not loaded them with the custom class loader.
     */
private List<Mutation> executeInternal(PartitionUpdate update) {
    Triggers triggers = update.metadata().triggers;
    if (triggers.isEmpty())
        return null;
    List<Mutation> tmutations = Lists.newLinkedList();
    Thread.currentThread().setContextClassLoader(customClassLoader);
    try {
        for (TriggerMetadata td : triggers) {
            ITrigger trigger = cachedTriggers.get(td.classOption);
            if (trigger == null) {
                trigger = loadTriggerInstance(td.classOption);
                cachedTriggers.put(td.classOption, trigger);
            }
            Collection<Mutation> temp = trigger.augment(update);
            if (temp != null)
                tmutations.addAll(temp);
        }
        return tmutations;
    } catch (CassandraException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(String.format("Exception while executing trigger on table with ID: %s", update.metadata().id), ex);
    } finally {
        Thread.currentThread().setContextClassLoader(parent);
    }
}
Also used : Triggers(org.apache.cassandra.schema.Triggers) CassandraException(org.apache.cassandra.exceptions.CassandraException) CassandraException(org.apache.cassandra.exceptions.CassandraException) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) TriggerMetadata(org.apache.cassandra.schema.TriggerMetadata)

Example 2 with Triggers

use of org.apache.cassandra.schema.Triggers in project cassandra by apache.

the class CreateTriggerStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws ConfigurationException, InvalidRequestException {
    TableMetadata current = Schema.instance.getTableMetadata(keyspace(), columnFamily());
    Triggers triggers = current.triggers;
    if (triggers.get(triggerName).isPresent()) {
        if (ifNotExists)
            return null;
        else
            throw new InvalidRequestException(String.format("Trigger %s already exists", triggerName));
    }
    TableMetadata updated = current.unbuild().triggers(triggers.with(TriggerMetadata.create(triggerName, triggerClass))).build();
    logger.info("Adding trigger with name {} and class {}", triggerName, triggerClass);
    MigrationManager.announceTableUpdate(updated, isLocalOnly);
    return new Event.SchemaChange(Event.SchemaChange.Change.UPDATED, Event.SchemaChange.Target.TABLE, keyspace(), columnFamily());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) Triggers(org.apache.cassandra.schema.Triggers) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException)

Example 3 with Triggers

use of org.apache.cassandra.schema.Triggers in project cassandra by apache.

the class DropTriggerStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws ConfigurationException, InvalidRequestException {
    TableMetadata current = Schema.instance.getTableMetadata(keyspace(), columnFamily());
    Triggers triggers = current.triggers;
    if (!triggers.get(triggerName).isPresent()) {
        if (ifExists)
            return null;
        else
            throw new InvalidRequestException(String.format("Trigger %s was not found", triggerName));
    }
    logger.info("Dropping trigger with name {}", triggerName);
    TableMetadata updated = current.unbuild().triggers(triggers.without(triggerName)).build();
    MigrationManager.announceTableUpdate(updated, isLocalOnly);
    return new Event.SchemaChange(Event.SchemaChange.Change.UPDATED, Event.SchemaChange.Target.TABLE, keyspace(), columnFamily());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) Triggers(org.apache.cassandra.schema.Triggers) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException)

Aggregations

InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 Triggers (org.apache.cassandra.schema.Triggers)3 TableMetadata (org.apache.cassandra.schema.TableMetadata)2 CassandraException (org.apache.cassandra.exceptions.CassandraException)1 TriggerMetadata (org.apache.cassandra.schema.TriggerMetadata)1