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

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

package com.hmsonline.cassandra.triggers;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.CassandraServer;
import org.apache.cassandra.thrift.CfDef;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnDef;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.IndexType;
import org.apache.cassandra.thrift.KsDef;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CassandraStore {
    private static Logger logger = LoggerFactory.getLogger(CassandraStore.class);
    public static final int DEFAULT_PRIORITY = 0;
    private boolean initialized = false;
    private String keyspace = null;
    private String columnFamily = null;

    protected CassandraStore(String keyspace, String columnFamily) throws Exception {
        this.keyspace = keyspace;
        this.columnFamily = columnFamily;
        // Commented to avoid schema disagreement 
        // Safe to uncomment when 1.1 is released
        // this.create(new String[] {});
    }

    protected CassandraStore(String keyspace, String columnFamily, String[] indexedColumns) throws Exception {
        this.keyspace = keyspace;
        this.columnFamily = columnFamily;
        // Commented to avoid schema disagreement 
        // Safe to uncomment when 1.1 is released
        // this.create(indexedColumns);
    }

    public Cassandra.Iface getConnection(String keyspace) throws Exception {
        CassandraServer server = new CassandraServer();
        if (keyspace != null) {
            server.set_keyspace(keyspace);
        }
        return server;
    }

    public synchronized void create() throws Exception {
        this.create(new String[]{});
    }
    
    public synchronized void create(String[] indexedColumns) throws Exception {
        if (!initialized) {
            try {
                List cfDefList = new ArrayList();
                KsDef ksDef = new KsDef(this.getKeyspace(), "org.apache.cassandra.locator.SimpleStrategy", cfDefList);
                ksDef.putToStrategy_options("replication_factor", "1");
                getConnection(null).system_add_keyspace(ksDef);
            } catch (Exception e) {
                logger.debug("Did not create [" + this.getKeyspace() + ":" + this.getColumnFamily()
                        + "] (probably already there)");
            }
            try {
                CfDef columnFamily = new CfDef(this.getKeyspace(), this.getColumnFamily());
                columnFamily.setKey_validation_class("UTF8Type");
                columnFamily.setDefault_validation_class("UTF8Type");
                columnFamily.setComparator_type("UTF8Type");

                // add indexes on columns
                if (indexedColumns != null && indexedColumns.length > 0) {
                    for (Object indexedColumn : indexedColumns) {
                        if (indexedColumn != null) {
                            String indexedColumnStr = indexedColumn.toString();
                            if (StringUtils.isNotBlank(indexedColumnStr)) {
                                List columnMetadata = columnFamily.getColumn_metadata();
                                columnMetadata = columnMetadata != null ? columnMetadata : new ArrayList();
                                ColumnDef colDef = new ColumnDef();
                                colDef.setName(indexedColumnStr.getBytes());
                                colDef.index_type = IndexType.KEYS;
                                colDef.setIndex_name(keyspace + "_" + this.columnFamily + "_" + indexedColumnStr
                                        + "_INDEX");
                                columnMetadata.add(colDef);
                                columnFamily.setColumn_metadata(columnMetadata);
                            }
                        }
                    }
                }

                getConnection(this.getKeyspace()).system_add_column_family(columnFamily);
                initialized = true;
                logger.debug("Created column family [" + this.getKeyspace() + ":" + this.getColumnFamily()
                        + "]");
            } catch (Exception e) {
                logger.warn("Did not create [" + this.getKeyspace() + ":" + this.getColumnFamily()
                        + "] (probably already there)", e);
            }
        }
    }

    public String getKeyspace() {
        return keyspace;
    }

    public String getColumnFamily() {
        return columnFamily;
    }
    
    protected Mutation getMutation(String name, String value) {
    	//Defualt low priority
        return getMutation(name, ByteBufferUtil.bytes(value), 0);
    }
    
    // Utility Methods
    protected Mutation getMutation(String name, String value, int priority) {
        return getMutation(name, ByteBufferUtil.bytes(value), priority);
    }


    protected Mutation getMutation(String name, ByteBuffer value, int priority) {
        return getMutation(ByteBufferUtil.bytes(name), value, priority);
    }

    protected Mutation getMutation(ByteBuffer name, OperationType value) {
    	//Defualt low priority
        return getMutation(name, ByteBufferUtil.bytes(value.toString()), DEFAULT_PRIORITY);
    }
    
    protected Mutation getMutation(ByteBuffer name, OperationType value, int priority) {
        return getMutation(name, ByteBufferUtil.bytes(value.toString()), priority);
    }

    protected Mutation getMutation(ByteBuffer name, ByteBuffer value, int priority) {
        Column c = new Column();
        c.setName(name);
        c.setValue(value);
        c.setTimestamp((System.currentTimeMillis() * 1000) + priority);

        Mutation m = new Mutation();
        ColumnOrSuperColumn cc = new ColumnOrSuperColumn();
        cc.setColumn(c);
        m.setColumn_or_supercolumn(cc);
        return m;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy