Search in sources :

Example 21 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer 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 22 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class AstyanaxStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> batch, StoreTransaction txh) throws BackendException {
    MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().withAtomicBatch(atomicBatch).setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanax()).withRetryPolicy(retryPolicy.duplicate());
    final MaskedTimestamp commitTime = new MaskedTimestamp(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();
            ByteBuffer key = ent.getKey().asByteBuffer();
            if (titanMutation.hasDeletions()) {
                ColumnListMutation<ByteBuffer> dels = m.withRow(columnFamily, key);
                dels.setTimestamp(commitTime.getDeletionTime(times));
                for (StaticBuffer b : titanMutation.getDeletions()) dels.deleteColumn(b.as(StaticBuffer.BB_FACTORY));
            }
            if (titanMutation.hasAdditions()) {
                ColumnListMutation<ByteBuffer> upds = m.withRow(columnFamily, key);
                upds.setTimestamp(commitTime.getAdditionTime(times));
                for (Entry e : titanMutation.getAdditions()) {
                    Integer ttl = (Integer) e.getMetaData().get(EntryMetaData.TTL);
                    if (null != ttl && ttl > 0) {
                        upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY), ttl);
                    } else {
                        upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY));
                    }
                }
            }
        }
    }
    try {
        m.execute();
    } catch (ConnectionException e) {
        throw new TemporaryBackendException(e);
    }
    sleepAfterWrite(txh, commitTime);
}
Also used : ByteBuffer(java.nio.ByteBuffer) KCVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation) Entry(com.thinkaurelius.titan.diskstorage.Entry) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) MutationBatch(com.netflix.astyanax.MutationBatch) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 23 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class CassandraHelper method transformRange.

public static KeyRange transformRange(Token leftKeyExclusive, Token rightKeyInclusive) {
    if (!(leftKeyExclusive instanceof BytesToken))
        throw new UnsupportedOperationException();
    // if left part is BytesToken, right part should be too, otherwise there is no sense in the ring
    assert rightKeyInclusive instanceof BytesToken;
    // l is exclusive, r is inclusive
    BytesToken l = (BytesToken) leftKeyExclusive;
    BytesToken r = (BytesToken) rightKeyInclusive;
    byte[] leftTokenValue = l.getTokenValue();
    byte[] rightTokenValue = r.getTokenValue();
    Preconditions.checkArgument(leftTokenValue.length == rightTokenValue.length, "Tokens have unequal length");
    int tokenLength = leftTokenValue.length;
    byte[][] tokens = new byte[][] { leftTokenValue, rightTokenValue };
    byte[][] plusOne = new byte[2][tokenLength];
    for (int j = 0; j < 2; j++) {
        boolean carry = true;
        for (int i = tokenLength - 1; i >= 0; i--) {
            byte b = tokens[j][i];
            if (carry) {
                b++;
                carry = false;
            }
            if (b == 0)
                carry = true;
            plusOne[j][i] = b;
        }
    }
    StaticBuffer lb = StaticArrayBuffer.of(plusOne[0]);
    StaticBuffer rb = StaticArrayBuffer.of(plusOne[1]);
    Preconditions.checkArgument(lb.length() == tokenLength, lb.length());
    Preconditions.checkArgument(rb.length() == tokenLength, rb.length());
    return new KeyRange(lb, rb);
}
Also used : KeyRange(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange) BytesToken(org.apache.cassandra.dht.BytesToken) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 24 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class OrderedKeyValueStoreManagerAdapter method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
    Map<String, KVMutation> converted = new HashMap<String, KVMutation>(mutations.size());
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> storeEntry : mutations.entrySet()) {
        OrderedKeyValueStoreAdapter store = openDatabase(storeEntry.getKey());
        Preconditions.checkNotNull(store);
        KVMutation mut = new KVMutation();
        for (Map.Entry<StaticBuffer, KCVMutation> entry : storeEntry.getValue().entrySet()) {
            StaticBuffer key = entry.getKey();
            KCVMutation mutation = entry.getValue();
            if (mutation.hasAdditions()) {
                for (Entry addition : mutation.getAdditions()) {
                    mut.addition(store.concatenate(key, addition));
                }
            }
            if (mutation.hasDeletions()) {
                for (StaticBuffer del : mutation.getDeletions()) {
                    mut.deletion(store.concatenate(key, del));
                }
            }
        }
        converted.put(storeEntry.getKey(), mut);
    }
    manager.mutateMany(converted, txh);
}
Also used : Entry(com.thinkaurelius.titan.diskstorage.Entry) HashMap(java.util.HashMap) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class StandardLockCleanerRunnable method runWithExceptions.

private void runWithExceptions() throws BackendException {
    StaticBuffer lockKey = serializer.toLockKey(target.getKey(), target.getColumn());
    // TODO reduce LOCK_COL_END based on cutoff
    List<Entry> locks = store.getSlice(new KeySliceQuery(lockKey, LOCK_COL_START, LOCK_COL_END), tx);
    ImmutableList.Builder<StaticBuffer> b = ImmutableList.builder();
    for (Entry lc : locks) {
        TimestampRid tr = serializer.fromLockColumn(lc.getColumn(), times);
        if (tr.getTimestamp().isBefore(cutoff)) {
            log.info("Deleting expired lock on {} by rid {} with timestamp {} (before or at cutoff {})", new Object[] { target, tr.getRid(), tr.getTimestamp(), cutoff });
            b.add(lc.getColumn());
        } else {
            log.debug("Ignoring lock on {} by rid {} with timestamp {} (timestamp is after cutoff {})", new Object[] { target, tr.getRid(), tr.getTimestamp(), cutoff });
        }
    }
    List<StaticBuffer> dels = b.build();
    if (!dels.isEmpty()) {
        store.mutate(lockKey, ImmutableList.<Entry>of(), dels, tx);
        log.info("Deleted {} expired locks (before or at cutoff {})", dels.size(), cutoff);
    }
}
Also used : Entry(com.thinkaurelius.titan.diskstorage.Entry) ImmutableList(com.google.common.collect.ImmutableList) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KeySliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)

Aggregations

StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)44 Entry (com.thinkaurelius.titan.diskstorage.Entry)13 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)9 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)8 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)7 Map (java.util.Map)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 Instant (java.time.Instant)6 HashMap (java.util.HashMap)6 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)5 BiMap (com.google.common.collect.BiMap)4 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)4 EntryList (com.thinkaurelius.titan.diskstorage.EntryList)4 KeySliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)4 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)4 Delete (org.apache.hadoop.hbase.client.Delete)4 Put (org.apache.hadoop.hbase.client.Put)4 Pair (org.apache.hadoop.hbase.util.Pair)4 ImmutableList (com.google.common.collect.ImmutableList)3