Search in sources :

Example 1 with TriggerMetadata

use of org.apache.cassandra.schema.TriggerMetadata 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 TriggerMetadata

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

the class TriggersSchemaTest method removeTriggerFromCf.

@Test
public void removeTriggerFromCf() throws Exception {
    TriggerMetadata td = TriggerMetadata.create(triggerName, triggerClass);
    TableMetadata tm = CreateTableStatement.parse(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName).triggers(Triggers.of(td)).build();
    KeyspaceMetadata ksm = KeyspaceMetadata.create(ksName, KeyspaceParams.simple(1), Tables.of(tm));
    MigrationManager.announceNewKeyspace(ksm);
    TableMetadata tm1 = Schema.instance.getTableMetadata(ksName, cfName);
    TableMetadata tm2 = tm1.unbuild().triggers(tm1.triggers.without(triggerName)).build();
    MigrationManager.announceTableUpdate(tm2);
    TableMetadata tm3 = Schema.instance.getTableMetadata(ksName, cfName);
    assertTrue(tm3.triggers.isEmpty());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) TriggerMetadata(org.apache.cassandra.schema.TriggerMetadata) Test(org.junit.Test)

Example 3 with TriggerMetadata

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

the class TriggersSchemaTest method newKsContainsCfWithTrigger.

@Test
public void newKsContainsCfWithTrigger() throws Exception {
    TriggerMetadata td = TriggerMetadata.create(triggerName, triggerClass);
    TableMetadata tm = CreateTableStatement.parse(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName).triggers(Triggers.of(td)).build();
    KeyspaceMetadata ksm = KeyspaceMetadata.create(ksName, KeyspaceParams.simple(1), Tables.of(tm));
    MigrationManager.announceNewKeyspace(ksm);
    TableMetadata tm2 = Schema.instance.getTableMetadata(ksName, cfName);
    assertFalse(tm2.triggers.isEmpty());
    assertEquals(1, tm2.triggers.size());
    assertEquals(td, tm2.triggers.get(triggerName).get());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) TriggerMetadata(org.apache.cassandra.schema.TriggerMetadata) Test(org.junit.Test)

Example 4 with TriggerMetadata

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

the class TriggersSchemaTest method addTriggerToCf.

@Test
public void addTriggerToCf() throws Exception {
    TableMetadata tm1 = CreateTableStatement.parse(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName).build();
    KeyspaceMetadata ksm = KeyspaceMetadata.create(ksName, KeyspaceParams.simple(1), Tables.of(tm1));
    MigrationManager.announceNewKeyspace(ksm);
    TriggerMetadata td = TriggerMetadata.create(triggerName, triggerClass);
    TableMetadata tm2 = Schema.instance.getTableMetadata(ksName, cfName).unbuild().triggers(Triggers.of(td)).build();
    MigrationManager.announceTableUpdate(tm2);
    TableMetadata tm3 = Schema.instance.getTableMetadata(ksName, cfName);
    assertFalse(tm3.triggers.isEmpty());
    assertEquals(1, tm3.triggers.size());
    assertEquals(td, tm3.triggers.get(triggerName).get());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) TriggerMetadata(org.apache.cassandra.schema.TriggerMetadata) Test(org.junit.Test)

Aggregations

TriggerMetadata (org.apache.cassandra.schema.TriggerMetadata)4 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)3 TableMetadata (org.apache.cassandra.schema.TableMetadata)3 Test (org.junit.Test)3 CassandraException (org.apache.cassandra.exceptions.CassandraException)1 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)1 Triggers (org.apache.cassandra.schema.Triggers)1