Search in sources :

Example 1 with KCVMutation

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

the class ExpectedValueCheckingTest method testMutateManyWithLockUsesConsistentTx.

@Test
public void testMutateManyWithLockUsesConsistentTx() throws BackendException {
    final ImmutableList<Entry> adds = ImmutableList.of(StaticArrayEntry.of(DATA_COL, DATA_VAL));
    final ImmutableList<StaticBuffer> dels = ImmutableList.<StaticBuffer>of();
    Map<String, Map<StaticBuffer, KCVMutation>> mutations = ImmutableMap.<String, Map<StaticBuffer, KCVMutation>>of(STORE_NAME, ImmutableMap.<StaticBuffer, KCVMutation>of(DATA_KEY, new KCVMutation(adds, dels)));
    final KeyColumn kc = new KeyColumn(LOCK_KEY, LOCK_COL);
    // Acquire a lock
    backingLocker.writeLock(kc, consistentTx);
    // 2. Run mutateMany
    // 2.1. Check locks & expected values before mutating data
    backingLocker.checkLocks(consistentTx);
    StaticBuffer nextBuf = BufferUtil.nextBiggerBuffer(kc.getColumn());
    KeySliceQuery expectedValueQuery = new KeySliceQuery(kc.getKey(), kc.getColumn(), nextBuf);
    // expected value read must use strong consistency
    expect(backingStore.getSlice(expectedValueQuery, consistentTx)).andReturn(StaticArrayEntryList.of(StaticArrayEntry.of(LOCK_COL, LOCK_VAL)));
    // 2.2. Run mutateMany on backing manager to modify data
    // writes by txs with locks must use strong consistency
    backingManager.mutateMany(mutations, consistentTx);
    ctrl.replay();
    // Lock acquisition
    expectStore.acquireLock(LOCK_KEY, LOCK_COL, LOCK_VAL, expectTx);
    // Mutate
    expectManager.mutateMany(mutations, expectTx);
}
Also used : StaticArrayEntry(com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) KeyColumn(com.thinkaurelius.titan.diskstorage.util.KeyColumn) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) KeySliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery) Test(org.junit.Test)

Example 2 with KCVMutation

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

the class HBaseStoreManager method convertToCommands.

/**
 * Convert Titan internal Mutation representation into HBase native commands.
 *
 * @param mutations    Mutations to convert into HBase commands.
 * @param putTimestamp The timestamp to use for Put commands.
 * @param delTimestamp The timestamp to use for Delete commands.
 * @return Commands sorted by key converted from Titan internal representation.
 * @throws com.thinkaurelius.titan.diskstorage.PermanentBackendException
 */
private Map<StaticBuffer, Pair<Put, Delete>> convertToCommands(Map<String, Map<StaticBuffer, KCVMutation>> mutations, final long putTimestamp, final long delTimestamp) throws PermanentBackendException {
    Map<StaticBuffer, Pair<Put, Delete>> commandsPerKey = new HashMap<>();
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> entry : mutations.entrySet()) {
        String cfString = getCfNameForStoreName(entry.getKey());
        byte[] cfName = cfString.getBytes();
        for (Map.Entry<StaticBuffer, KCVMutation> m : entry.getValue().entrySet()) {
            byte[] key = m.getKey().as(StaticBuffer.ARRAY_FACTORY);
            KCVMutation mutation = m.getValue();
            Pair<Put, Delete> commands = commandsPerKey.get(m.getKey());
            if (commands == null) {
                commands = new Pair<>();
                commandsPerKey.put(m.getKey(), commands);
            }
            if (mutation.hasDeletions()) {
                if (commands.getSecond() == null) {
                    Delete d = new Delete(key);
                    compat.setTimestamp(d, delTimestamp);
                    commands.setSecond(d);
                }
                for (StaticBuffer b : mutation.getDeletions()) {
                    commands.getSecond().deleteColumns(cfName, b.as(StaticBuffer.ARRAY_FACTORY), delTimestamp);
                }
            }
            if (mutation.hasAdditions()) {
                if (commands.getFirst() == null) {
                    Put p = new Put(key, putTimestamp);
                    commands.setFirst(p);
                }
                for (Entry e : mutation.getAdditions()) {
                    commands.getFirst().add(cfName, e.getColumnAs(StaticBuffer.ARRAY_FACTORY), putTimestamp, e.getValueAs(StaticBuffer.ARRAY_FACTORY));
                }
            }
        }
    }
    return commandsPerKey;
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) Put(org.apache.hadoop.hbase.client.Put) Entry(com.thinkaurelius.titan.diskstorage.Entry) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Pair(org.apache.hadoop.hbase.util.Pair)

Example 3 with KCVMutation

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation in project incubator-atlas by apache.

the class HBaseStoreManager method convertToCommands.

/**
 * Convert Titan internal Mutation representation into HBase native commands.
 *
 * @param mutations    Mutations to convert into HBase commands.
 * @param putTimestamp The timestamp to use for Put commands.
 * @param delTimestamp The timestamp to use for Delete commands.
 * @return Commands sorted by key converted from Titan internal representation.
 * @throws com.thinkaurelius.titan.diskstorage.PermanentBackendException
 */
private Map<StaticBuffer, Pair<Put, Delete>> convertToCommands(Map<String, Map<StaticBuffer, KCVMutation>> mutations, final long putTimestamp, final long delTimestamp) throws PermanentBackendException {
    Map<StaticBuffer, Pair<Put, Delete>> commandsPerKey = new HashMap<>();
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> entry : mutations.entrySet()) {
        String cfString = getCfNameForStoreName(entry.getKey());
        byte[] cfName = cfString.getBytes();
        for (Map.Entry<StaticBuffer, KCVMutation> m : entry.getValue().entrySet()) {
            byte[] key = m.getKey().as(StaticBuffer.ARRAY_FACTORY);
            KCVMutation mutation = m.getValue();
            Pair<Put, Delete> commands = commandsPerKey.get(m.getKey());
            if (commands == null) {
                commands = new Pair<>();
                commandsPerKey.put(m.getKey(), commands);
            }
            if (mutation.hasDeletions()) {
                if (commands.getSecond() == null) {
                    Delete d = new Delete(key);
                    compat.setTimestamp(d, delTimestamp);
                    commands.setSecond(d);
                }
                for (StaticBuffer b : mutation.getDeletions()) {
                    commands.getSecond().deleteColumns(cfName, b.as(StaticBuffer.ARRAY_FACTORY), delTimestamp);
                }
            }
            if (mutation.hasAdditions()) {
                if (commands.getFirst() == null) {
                    Put p = new Put(key, putTimestamp);
                    commands.setFirst(p);
                }
                for (Entry e : mutation.getAdditions()) {
                    commands.getFirst().add(cfName, e.getColumnAs(StaticBuffer.ARRAY_FACTORY), putTimestamp, e.getValueAs(StaticBuffer.ARRAY_FACTORY));
                }
            }
        }
    }
    return commandsPerKey;
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) Put(org.apache.hadoop.hbase.client.Put) Entry(com.thinkaurelius.titan.diskstorage.Entry) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Pair(org.apache.hadoop.hbase.util.Pair)

Example 4 with KCVMutation

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

the class CassandraEmbeddedStoreManager method mutateMany.

/*
      * This implementation can't handle counter columns.
      *
      * The private method internal_batch_mutate in CassandraServer as of 1.2.0
      * provided most of the following method after transaction handling.
      */
@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws StorageException {
    Preconditions.checkNotNull(mutations);
    final Timestamp timestamp = getTimestamp(txh);
    int size = 0;
    for (Map<StaticBuffer, KCVMutation> mutation : mutations.values()) size += mutation.size();
    Map<StaticBuffer, RowMutation> rowMutations = new HashMap<StaticBuffer, RowMutation>(size);
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> mutEntry : mutations.entrySet()) {
        String columnFamily = mutEntry.getKey();
        for (Map.Entry<StaticBuffer, KCVMutation> titanMutation : mutEntry.getValue().entrySet()) {
            StaticBuffer key = titanMutation.getKey();
            KCVMutation mut = titanMutation.getValue();
            RowMutation rm = rowMutations.get(key);
            if (rm == null) {
                rm = new RowMutation(keySpaceName, key.asByteBuffer());
                rowMutations.put(key, rm);
            }
            if (mut.hasAdditions()) {
                for (Entry e : mut.getAdditions()) {
                    // TODO are these asByteBuffer() calls too expensive?
                    QueryPath path = new QueryPath(columnFamily, null, e.getColumn().asByteBuffer());
                    rm.add(path, e.getValue().asByteBuffer(), timestamp.additionTime);
                }
            }
            if (mut.hasDeletions()) {
                for (StaticBuffer col : mut.getDeletions()) {
                    QueryPath path = new QueryPath(columnFamily, null, col.asByteBuffer());
                    rm.delete(path, timestamp.deletionTime);
                }
            }
        }
    }
    mutate(new ArrayList<RowMutation>(rowMutations.values()), getTx(txh).getWriteConsistencyLevel().getDBConsistency());
}
Also used : HashMap(java.util.HashMap) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) QueryPath(org.apache.cassandra.db.filter.QueryPath) Entry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry) RowMutation(org.apache.cassandra.db.RowMutation) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 5 with KCVMutation

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

the class CassandraThriftKeyColumnValueStore method mutate.

@Override
public void mutate(StaticBuffer key, List<Entry> additions, List<StaticBuffer> deletions, StoreTransaction txh) throws StorageException {
    Map<StaticBuffer, KCVMutation> mutations = ImmutableMap.of(key, new KCVMutation(additions, deletions));
    mutateMany(mutations, txh);
}
Also used : StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)

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