Search in sources :

Example 6 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

the class CassandraThriftStoreManager method getCompressionOptions.

@Override
public Map<String, String> getCompressionOptions(String cf) throws BackendException {
    CTConnection conn = null;
    Map<String, String> result = null;
    try {
        conn = pool.borrowObject(keySpaceName);
        Cassandra.Client client = conn.getClient();
        KsDef ksDef = client.describe_keyspace(keySpaceName);
        for (CfDef cfDef : ksDef.getCf_defs()) {
            if (null != cfDef && cfDef.getName().equals(cf)) {
                result = cfDef.getCompression_options();
                break;
            }
        }
        return result;
    } catch (InvalidRequestException e) {
        log.debug("Keyspace {} does not exist", keySpaceName);
        return null;
    } catch (Exception e) {
        throw new TemporaryBackendException(e);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TException(org.apache.thrift.TException)

Example 7 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

the class CassandraThriftStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
    Preconditions.checkNotNull(mutations);
    final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
    ConsistencyLevel consistency = getTx(txh).getWriteConsistencyLevel().getThrift();
    // Generate Thrift-compatible batch_mutate() datastructure
    // key -> cf -> cassmutation
    int size = 0;
    for (Map<StaticBuffer, KCVMutation> mutation : mutations.values()) size += mutation.size();
    Map<ByteBuffer, Map<String, List<org.apache.cassandra.thrift.Mutation>>> batch = new HashMap<ByteBuffer, Map<String, List<org.apache.cassandra.thrift.Mutation>>>(size);
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> keyMutation : mutations.entrySet()) {
        String columnFamily = keyMutation.getKey();
        for (Map.Entry<StaticBuffer, KCVMutation> mutEntry : keyMutation.getValue().entrySet()) {
            ByteBuffer keyBB = mutEntry.getKey().asByteBuffer();
            // Get or create the single Cassandra Mutation object responsible for this key
            Map<String, List<org.apache.cassandra.thrift.Mutation>> cfmutation = batch.get(keyBB);
            if (cfmutation == null) {
                // Most mutations only modify the edgeStore and indexStore
                cfmutation = new HashMap<String, List<org.apache.cassandra.thrift.Mutation>>(3);
                batch.put(keyBB, cfmutation);
            }
            KCVMutation mutation = mutEntry.getValue();
            List<org.apache.cassandra.thrift.Mutation> thriftMutation = new ArrayList<org.apache.cassandra.thrift.Mutation>(mutations.size());
            if (mutation.hasDeletions()) {
                for (StaticBuffer buf : mutation.getDeletions()) {
                    Deletion d = new Deletion();
                    SlicePredicate sp = new SlicePredicate();
                    sp.addToColumn_names(buf.as(StaticBuffer.BB_FACTORY));
                    d.setPredicate(sp);
                    d.setTimestamp(commitTime.getDeletionTime(times));
                    org.apache.cassandra.thrift.Mutation m = new org.apache.cassandra.thrift.Mutation();
                    m.setDeletion(d);
                    thriftMutation.add(m);
                }
            }
            if (mutation.hasAdditions()) {
                for (Entry ent : mutation.getAdditions()) {
                    ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
                    Column column = new Column(ent.getColumnAs(StaticBuffer.BB_FACTORY));
                    column.setValue(ent.getValueAs(StaticBuffer.BB_FACTORY));
                    column.setTimestamp(commitTime.getAdditionTime(times));
                    Integer ttl = (Integer) ent.getMetaData().get(EntryMetaData.TTL);
                    if (null != ttl && ttl > 0) {
                        column.setTtl(ttl);
                    }
                    cosc.setColumn(column);
                    org.apache.cassandra.thrift.Mutation m = new org.apache.cassandra.thrift.Mutation();
                    m.setColumn_or_supercolumn(cosc);
                    thriftMutation.add(m);
                }
            }
            cfmutation.put(columnFamily, thriftMutation);
        }
    }
    CTConnection conn = null;
    try {
        conn = pool.borrowObject(keySpaceName);
        Cassandra.Client client = conn.getClient();
        if (atomicBatch) {
            client.atomic_batch_mutate(batch, consistency);
        } else {
            client.batch_mutate(batch, consistency);
        }
    } catch (Exception ex) {
        throw CassandraThriftKeyColumnValueStore.convertException(ex);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
    sleepAfterWrite(txh, commitTime);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) org.apache.cassandra.thrift(org.apache.cassandra.thrift) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TException(org.apache.thrift.TException) CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

the class CassandraThriftStoreManager method ensureKeyspaceExists.

private KsDef ensureKeyspaceExists(String keyspaceName) throws TException, BackendException {
    CTConnection connection = null;
    try {
        connection = pool.borrowObject(SYSTEM_KS);
        Cassandra.Client client = connection.getClient();
        try {
            // Side effect: throws Exception if keyspaceName doesn't exist
            // Don't remove
            client.set_keyspace(keyspaceName);
            client.set_keyspace(SYSTEM_KS);
            log.debug("Found existing keyspace {}", keyspaceName);
        } catch (InvalidRequestException e) {
            // Keyspace didn't exist; create it
            log.debug("Creating keyspace {}...", keyspaceName);
            KsDef ksdef = new KsDef().setName(keyspaceName).setCf_defs(// cannot be null but can be empty
            new LinkedList<CfDef>()).setStrategy_class(storageConfig.get(REPLICATION_STRATEGY)).setStrategy_options(strategyOptions);
            client.set_keyspace(SYSTEM_KS);
            try {
                client.system_add_keyspace(ksdef);
                retrySetKeyspace(keyspaceName, client);
                log.debug("Created keyspace {}", keyspaceName);
            } catch (InvalidRequestException ire) {
                log.error("system_add_keyspace failed for keyspace=" + keyspaceName, ire);
                throw ire;
            }
        }
        return client.describe_keyspace(keyspaceName);
    } catch (Exception e) {
        throw new TemporaryBackendException(e);
    } finally {
        pool.returnObjectUnsafe(SYSTEM_KS, connection);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) LinkedList(java.util.LinkedList) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TException(org.apache.thrift.TException)

Aggregations

CTConnection (com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection)8 TException (org.apache.thrift.TException)8 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)6 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)1 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AbstractByteOrderedPartitioner (org.apache.cassandra.dht.AbstractByteOrderedPartitioner)1 IPartitioner (org.apache.cassandra.dht.IPartitioner)1 Token (org.apache.cassandra.dht.Token)1 org.apache.cassandra.thrift (org.apache.cassandra.thrift)1 ConsistencyLevel (org.apache.cassandra.thrift.ConsistencyLevel)1