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

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

package com.hmsonline.cassandra.triggers;

import java.nio.ByteBuffer;
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.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
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;
    private long lastFetchTime = -1;
    private static final int REFRESH_INTERVAL = 1000 * 30; // 30 seconds
    Map> triggerMap = null;
    Map triggerCache = 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, Map cache) throws Exception {
        try {
            if(cache.get(triggerClass) == null) {
                Class clazz = (Class) Class.forName(triggerClass);
                Trigger trigger = clazz.newInstance();
                cache.put(triggerClass, trigger);
            }
            return cache.get(triggerClass);
        } catch (Exception e) {
            logger.error("Could not create trigger class [" + triggerClass + "], it will NOT run.", e);
        }
        return null;
    }

    public synchronized Map> getTriggers() throws Exception {
        long currentTime = System.currentTimeMillis();
        long timeSinceRefresh = currentTime - this.lastFetchTime;
        if (timeSinceRefresh > REFRESH_INTERVAL) {
            this.lastFetchTime = currentTime;
            this.triggerMap = new HashMap>();
            this.triggerCache = 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, triggerCache));
            }
        }
        if(logger.isDebugEnabled()) {
            logger.debug("time in getTriggers: " + (System.currentTimeMillis() - currentTime));
        }
        return triggerMap;
    }

    public void insertTrigger(String keyspace, String columnFamily, String triggerName) throws InvalidRequestException,
            UnavailableException, TimedOutException, TException, Exception {
        List slice = new ArrayList();
        slice.add(getMutation(triggerName, ENABLED));
        Map>> mutationMap = new HashMap>>();
        Map> cfMutations = new HashMap>();
        cfMutations.put(COLUMN_FAMILY, slice);

        ByteBuffer rowKey = ByteBufferUtil.bytes(keyspace + ":" + columnFamily);
        mutationMap.put(rowKey, cfMutations);
        getConnection(KEYSPACE).batch_mutate(mutationMap, ConsistencyLevel.ALL);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private List processRow(KeySlice slice, Map cache) 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, cache);
                if (trigger != null)
                    triggers.add(trigger);
            }
        }
        return triggers;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy