All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hmsonline.cassandra.triggers.TriggerStore Maven / Gradle / Ivy

Go to download

Cassandra Triggers is a lightweight mechanism to implement trigger-like functionality for Cassandra.

There is a newer version: 1.0.1
Show newest version
package com.hmsonline.cassandra.triggers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TriggerStore extends CassandraStore {
    private static Logger logger = LoggerFactory.getLogger(TriggerStore.class);

    public static final String KEYSPACE = "triggers";
    public static final String COLUMN_FAMILY = "Triggers";
    public static final String ENABLED = "enabled";
    public static final String PAUSED = "PAUSED";
    private static TriggerStore instance = null;

    public TriggerStore(String keyspace, String columnFamily) throws Exception {
        super(keyspace, columnFamily);
        logger.debug("Instantiated trigger store.");
    }

    public static synchronized TriggerStore getStore() throws Exception {
        if (instance == null)
            instance = new TriggerStore(KEYSPACE, COLUMN_FAMILY);
        return instance;
    }

    @SuppressWarnings("unchecked")
    public static Trigger getTrigger(String triggerClass) throws Exception {
        try {
            Class clazz = (Class) Class.forName(triggerClass);
            return clazz.newInstance();
        } catch (Exception e) {
            logger.error("Could not create trigger class [" + triggerClass + "], it will NOT run.", e);
        }
        return null;
    }

    public Map> getTriggers() throws Exception {
        // TODO: Cache this.
        Map> triggerMap = new HashMap>();
        SlicePredicate predicate = new SlicePredicate();
        SliceRange range = new SliceRange(ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, 10);
        predicate.setSlice_range(range);

        KeyRange keyRange = new KeyRange(1000);
        keyRange.setStart_key(ByteBufferUtil.bytes(""));
        keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER);
        ColumnParent parent = new ColumnParent(COLUMN_FAMILY);
        List rows = getConnection(KEYSPACE).get_range_slices(parent, predicate, keyRange,
                ConsistencyLevel.ALL);
        for (KeySlice slice : rows) {
            String columnFamily = ByteBufferUtil.string(slice.key);
            triggerMap.put(columnFamily, processRow(slice));
        }
        return triggerMap;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private List processRow(KeySlice slice) throws Exception {
        List triggers = new ArrayList();
        for (ColumnOrSuperColumn column : slice.columns) {
            String className = ByteBufferUtil.string(column.column.name);
            String enabled = ByteBufferUtil.string(column.column.value);
            if (PAUSED.equals(StringUtils.upperCase(className)) && ENABLED.equals(enabled)) {
                return new ArrayList(Arrays.asList(new Trigger[] { new PausedTrigger() }));
            } else if (enabled.equals(ENABLED)) {
                Trigger trigger = getTrigger(className);
                if (trigger != null)
                    triggers.add(trigger);
            }
        }
        return triggers;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy