Search in sources :

Example 6 with Cassandra

use of org.apache.cassandra.thrift.Cassandra in project titan by thinkaurelius.

the class CassandraThriftStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws StorageException {
    Preconditions.checkNotNull(mutations);
    final Timestamp timestamp = getTimestamp(txh);
    ConsistencyLevel consistency = getTx(txh).getWriteConsistencyLevel().getThriftConsistency();
    // 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()) {
            StaticBuffer key = mutEntry.getKey();
            ByteBuffer keyBB = key.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) {
                // TODO where did the magic number 3 come from?
                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.asByteBuffer());
                    d.setPredicate(sp);
                    d.setTimestamp(timestamp.deletionTime);
                    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.getColumn().asByteBuffer());
                    column.setValue(ent.getValue().asByteBuffer());
                    column.setTimestamp(timestamp.additionTime);
                    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();
        client.batch_mutate(batch, consistency);
    } catch (Exception ex) {
        throw CassandraThriftKeyColumnValueStore.convertException(ex);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
}
Also used : HashMap(java.util.HashMap) Cassandra(org.apache.cassandra.thrift.Cassandra) ArrayList(java.util.ArrayList) ConsistencyLevel(org.apache.cassandra.thrift.ConsistencyLevel) Entry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry) Column(org.apache.cassandra.thrift.Column) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) Deletion(org.apache.cassandra.thrift.Deletion) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) SlicePredicate(org.apache.cassandra.thrift.SlicePredicate) ByteBuffer(java.nio.ByteBuffer) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) NotFoundException(org.apache.cassandra.thrift.NotFoundException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TException(org.apache.thrift.TException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) SchemaDisagreementException(org.apache.cassandra.thrift.SchemaDisagreementException) 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)

Aggregations

Cassandra (org.apache.cassandra.thrift.Cassandra)6 TException (org.apache.thrift.TException)6 InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)4 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)3 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)3 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)3 CTConnection (com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection)3 HashMap (java.util.HashMap)3 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)2 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConsistencyLevel (org.apache.cassandra.thrift.ConsistencyLevel)2 NotFoundException (org.apache.cassandra.thrift.NotFoundException)2 SchemaDisagreementException (org.apache.cassandra.thrift.SchemaDisagreementException)2 SlicePredicate (org.apache.cassandra.thrift.SlicePredicate)2 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)2 TFramedTransport (org.apache.thrift.transport.TFramedTransport)2 TSocket (org.apache.thrift.transport.TSocket)2