Search in sources :

Example 6 with KCVMutation

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation in project titan by thinkaurelius.

the class AstyanaxStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> batch, StoreTransaction txh) throws StorageException {
    MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanaxConsistency()).withRetryPolicy(retryPolicy.duplicate());
    final Timestamp timestamp = getTimestamp(txh);
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> batchentry : batch.entrySet()) {
        String storeName = batchentry.getKey();
        Preconditions.checkArgument(openStores.containsKey(storeName), "Store cannot be found: " + storeName);
        ColumnFamily<ByteBuffer, ByteBuffer> columnFamily = openStores.get(storeName).getColumnFamily();
        Map<StaticBuffer, KCVMutation> mutations = batchentry.getValue();
        for (Map.Entry<StaticBuffer, KCVMutation> ent : mutations.entrySet()) {
            // The CLMs for additions and deletions are separated because
            // Astyanax's operation timestamp cannot be set on a per-delete
            // or per-addition basis.
            KCVMutation titanMutation = ent.getValue();
            if (titanMutation.hasDeletions()) {
                ColumnListMutation<ByteBuffer> dels = m.withRow(columnFamily, ent.getKey().asByteBuffer());
                dels.setTimestamp(timestamp.deletionTime);
                for (StaticBuffer b : titanMutation.getDeletions()) dels.deleteColumn(b.asByteBuffer());
            }
            if (titanMutation.hasAdditions()) {
                ColumnListMutation<ByteBuffer> upds = m.withRow(columnFamily, ent.getKey().asByteBuffer());
                upds.setTimestamp(timestamp.additionTime);
                for (Entry e : titanMutation.getAdditions()) upds.putColumn(e.getColumn().asByteBuffer(), e.getValue().asByteBuffer());
            }
        }
    }
    try {
        m.execute();
    } catch (ConnectionException e) {
        throw new TemporaryStorageException(e);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) Entry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 7 with KCVMutation

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation 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

KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)6 Map (java.util.Map)6 HashMap (java.util.HashMap)5 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)3 BiMap (com.google.common.collect.BiMap)2 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)2 Entry (com.thinkaurelius.titan.diskstorage.Entry)2 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)2 ByteBuffer (java.nio.ByteBuffer)2 NavigableMap (java.util.NavigableMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 Delete (org.apache.hadoop.hbase.client.Delete)2 Put (org.apache.hadoop.hbase.client.Put)2 Pair (org.apache.hadoop.hbase.util.Pair)2 ConnectionException (com.netflix.astyanax.connectionpool.exceptions.ConnectionException)1 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)1 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)1