Search in sources :

Example 1 with StaticArrayBuffer

use of com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer in project titan by thinkaurelius.

the class ConsistentKeyLockerSerializer method fromLockColumn.

public TimestampRid fromLockColumn(StaticBuffer lockKey, TimestampProvider provider) {
    ReadBuffer r = lockKey.asReadBuffer();
    int len = r.length();
    long tsNS = r.getLong();
    len -= 8;
    byte[] curRid = new byte[len];
    for (int i = 0; r.hasRemaining(); i++) {
        curRid[i] = r.getByte();
    }
    StaticBuffer rid = new StaticArrayBuffer(curRid);
    Instant time = provider.getTime(tsNS);
    return new TimestampRid(time, rid);
}
Also used : ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) Instant(java.time.Instant) StaticArrayBuffer(com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 2 with StaticArrayBuffer

use of com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer in project titan by thinkaurelius.

the class MultiWriteKeyColumnValueStoreTest method generateMutation.

public Map<StaticBuffer, KCVMutation> generateMutation(int keyCount, int columnCount, Map<StaticBuffer, KCVMutation> deleteFrom) {
    Map<StaticBuffer, KCVMutation> result = new HashMap<StaticBuffer, KCVMutation>(keyCount);
    Random keyRand = new Random(keyCount);
    Random colRand = new Random(columnCount);
    final int keyLength = 8;
    final int colLength = 6;
    Iterator<Map.Entry<StaticBuffer, KCVMutation>> deleteIter = null;
    List<Entry> lastDeleteIterResult = null;
    if (null != deleteFrom) {
        deleteIter = deleteFrom.entrySet().iterator();
    }
    for (int ik = 0; ik < keyCount; ik++) {
        byte[] keyBuf = new byte[keyLength];
        keyRand.nextBytes(keyBuf);
        StaticBuffer key = new StaticArrayBuffer(keyBuf);
        List<Entry> additions = new LinkedList<Entry>();
        List<StaticBuffer> deletions = new LinkedList<StaticBuffer>();
        for (int ic = 0; ic < columnCount; ic++) {
            boolean deleteSucceeded = false;
            if (null != deleteIter && 1 == ic % 2) {
                if (null == lastDeleteIterResult || lastDeleteIterResult.isEmpty()) {
                    while (deleteIter.hasNext()) {
                        Map.Entry<StaticBuffer, KCVMutation> ent = deleteIter.next();
                        if (ent.getValue().hasAdditions() && !ent.getValue().getAdditions().isEmpty()) {
                            lastDeleteIterResult = ent.getValue().getAdditions();
                            break;
                        }
                    }
                }
                if (null != lastDeleteIterResult && !lastDeleteIterResult.isEmpty()) {
                    Entry e = lastDeleteIterResult.get(0);
                    lastDeleteIterResult.remove(0);
                    deletions.add(e.getColumn());
                    deleteSucceeded = true;
                }
            }
            if (!deleteSucceeded) {
                byte[] colBuf = new byte[colLength];
                colRand.nextBytes(colBuf);
                StaticBuffer col = new StaticArrayBuffer(colBuf);
                additions.add(StaticArrayEntry.of(col, col));
            }
        }
        KCVMutation m = new KCVMutation(additions, deletions);
        result.put(key, m);
    }
    return result;
}
Also used : StaticArrayBuffer(com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer) StaticArrayEntry(com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with StaticArrayBuffer

use of com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer in project titan by thinkaurelius.

the class CassandraBinaryRecordReader method completeNextKV.

private KV completeNextKV() throws IOException {
    KV completedKV = null;
    boolean hasNext;
    do {
        hasNext = reader.nextKeyValue();
        if (!hasNext) {
            completedKV = incompleteKV;
            incompleteKV = null;
        } else {
            StaticArrayBuffer key = StaticArrayBuffer.of(reader.getCurrentKey());
            SortedMap<ByteBuffer, Cell> valueSortedMap = reader.getCurrentValue();
            List<Entry> entries = new ArrayList<>(valueSortedMap.size());
            for (Map.Entry<ByteBuffer, Cell> ent : valueSortedMap.entrySet()) {
                ByteBuffer col = ent.getKey();
                ByteBuffer val = ent.getValue().value();
                entries.add(StaticArrayEntry.of(StaticArrayBuffer.of(col), StaticArrayBuffer.of(val)));
            }
            if (null == incompleteKV) {
                // Initialization; this should happen just once in an instance's lifetime
                incompleteKV = new KV(key);
            } else if (!incompleteKV.key.equals(key)) {
                // The underlying Cassandra reader has just changed to a key we haven't seen yet
                // This implies that there will be no more entries for the prior key
                completedKV = incompleteKV;
                incompleteKV = new KV(key);
            }
            incompleteKV.addEntries(entries);
        }
    /* Loop ends when either
             * A) the cassandra reader ran out of data
             * or
             * B) the cassandra reader switched keys, thereby completing a KV */
    } while (hasNext && null == completedKV);
    return completedKV;
}
Also used : Entry(com.thinkaurelius.titan.diskstorage.Entry) StaticArrayEntry(com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry) ArrayList(java.util.ArrayList) StaticArrayBuffer(com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer) ByteBuffer(java.nio.ByteBuffer) Cell(org.apache.cassandra.db.Cell) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 4 with StaticArrayBuffer

use of com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer in project titan by thinkaurelius.

the class MultiWriteKeyColumnValueStoreTest method mutateState.

/**
     * Pseudorandomly change the supplied {@code state}.
     * <p/>
     * This method removes {@code min(maxDeletionCount, S)} entries from the
     * maps in {@code state.values()}, where {@code S} is the sum of the sizes
     * of the maps in {@code state.values()}; this method then adds
     * {@code additionCount} pseudorandomly generated entries spread across
     * {@code state.values()}, potentially adding new keys to {@code state}
     * since they are randomly generated. This method then returns a map of keys
     * to Mutations representing the changes it has made to {@code state}.
     *
     * @param state            Maps keys -> columns -> values
     * @param maxDeletionCount Remove at most this many entries from state
     * @param additionCount    Add exactly this many entries to state
     * @return A KCVMutation map
     */
public Map<StaticBuffer, KCVEntryMutation> mutateState(Map<StaticBuffer, Map<StaticBuffer, StaticBuffer>> state, int maxDeletionCount, int additionCount) {
    final int keyLength = 8;
    final int colLength = 16;
    Map<StaticBuffer, KCVEntryMutation> result = new HashMap<StaticBuffer, KCVEntryMutation>();
    // deletion pass
    int dels = 0;
    StaticBuffer key = null, col = null;
    Entry entry = null;
    Iterator<StaticBuffer> keyIter = state.keySet().iterator();
    while (keyIter.hasNext() && dels < maxDeletionCount) {
        key = keyIter.next();
        Iterator<Map.Entry<StaticBuffer, StaticBuffer>> colIter = state.get(key).entrySet().iterator();
        while (colIter.hasNext() && dels < maxDeletionCount) {
            Map.Entry<StaticBuffer, StaticBuffer> colEntry = colIter.next();
            entry = StaticArrayEntry.of(colEntry.getKey(), colEntry.getValue());
            if (!result.containsKey(key)) {
                KCVEntryMutation m = new KCVEntryMutation(new LinkedList<Entry>(), new LinkedList<Entry>());
                result.put(key, m);
            }
            result.get(key).deletion(entry);
            dels++;
            colIter.remove();
            if (state.get(key).isEmpty()) {
                assert !colIter.hasNext();
                keyIter.remove();
            }
        }
    }
    // addition pass
    for (int i = 0; i < additionCount; i++) {
        while (true) {
            byte[] keyBuf = new byte[keyLength];
            rand.nextBytes(keyBuf);
            key = new StaticArrayBuffer(keyBuf);
            byte[] colBuf = new byte[colLength];
            rand.nextBytes(colBuf);
            col = new StaticArrayBuffer(colBuf);
            if (!state.containsKey(key) || !state.get(key).containsKey(col)) {
                break;
            }
        }
        if (!state.containsKey(key)) {
            Map<StaticBuffer, StaticBuffer> m = new HashMap<StaticBuffer, StaticBuffer>();
            state.put(key, m);
        }
        state.get(key).put(col, col);
        if (!result.containsKey(key)) {
            KCVEntryMutation m = new KCVEntryMutation(new LinkedList<Entry>(), new LinkedList<Entry>());
            result.put(key, m);
        }
        result.get(key).addition(StaticArrayEntry.of(col, col));
    }
    return result;
}
Also used : StaticArrayBuffer(com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer) KCVEntryMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.cache.KCVEntryMutation) StaticArrayEntry(com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

StaticArrayBuffer (com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer)4 StaticArrayEntry (com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Entry (com.thinkaurelius.titan.diskstorage.Entry)1 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)1 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)1 KCVEntryMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.cache.KCVEntryMutation)1 ByteBuffer (java.nio.ByteBuffer)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 Cell (org.apache.cassandra.db.Cell)1