Search in sources :

Example 81 with StaticBuffer

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

the class ConsistentKeyLockerTest method testDeleteLocksDeletesUncheckedLocks.

/**
 * Deletion should remove previously written locks regardless of whether
 * they were ever checked; this method fakes and verifies deletion on a
 * single unchecked lock
 *
 * @throws StorageException shouldn't happen
 */
@Test
public void testDeleteLocksDeletesUncheckedLocks() throws StorageException {
    ConsistentKeyLockStatus defaultLS = makeStatusNow();
    assertFalse(defaultLS.isChecked());
    currentTimeNS++;
    // Expect a call for defaultTx's locks and the checked one
    expect(lockState.getLocksForTx(defaultTx)).andReturn(Maps.newLinkedHashMap(ImmutableMap.of(defaultLockID, defaultLS)));
    List<StaticBuffer> dels = ImmutableList.of(codec.toLockCol(defaultLS.getWriteTimestamp(TimeUnit.NANOSECONDS), defaultLockRid));
    expect(times.getApproxNSSinceEpoch()).andReturn(currentTimeNS);
    store.mutate(eq(defaultLockKey), eq(ImmutableList.<Entry>of()), eq(dels), eq(defaultTx));
    expect(mediator.unlock(defaultLockID, defaultTx)).andReturn(true);
    // lockState.release(defaultTx, defaultLockID);
    ctrl.replay();
    locker.deleteLocks(defaultTx);
}
Also used : StaticBufferEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry) Entry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ConsistentKeyLockStatus(com.thinkaurelius.titan.diskstorage.locking.consistentkey.ConsistentKeyLockStatus) Test(org.junit.Test)

Example 82 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 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 83 with StaticBuffer

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

the class CassandraThriftKeyColumnValueStore method getNamesSlice.

public Map<ByteBuffer, List<Entry>> getNamesSlice(List<StaticBuffer> keys, SliceQuery query, StoreTransaction txh) throws StorageException {
    Preconditions.checkArgument(query.getLimit() >= 0);
    if (0 == query.getLimit())
        return Collections.emptyMap();
    ColumnParent parent = new ColumnParent(columnFamily);
    /*
         * Cassandra cannot handle columnStart = columnEnd.
		 * Cassandra's Thrift getSlice() throws InvalidRequestException
		 * if columnStart = columnEnd.
		 */
    if (ByteBufferUtil.compare(query.getSliceStart(), query.getSliceEnd()) >= 0) {
        // Check for invalid arguments where columnEnd < columnStart
        if (ByteBufferUtil.isSmallerThan(query.getSliceEnd(), query.getSliceStart())) {
            throw new PermanentStorageException("columnStart=" + query.getSliceStart() + " is greater than columnEnd=" + query.getSliceEnd() + ". " + "columnStart must be less than or equal to columnEnd");
        }
        if (0 != query.getSliceStart().length() && 0 != query.getSliceEnd().length()) {
            logger.debug("Return empty list due to columnEnd==columnStart and neither empty");
            return Collections.emptyMap();
        }
    }
    // true: columnStart < columnEnd
    ConsistencyLevel consistency = getTx(txh).getReadConsistencyLevel().getThriftConsistency();
    SlicePredicate predicate = new SlicePredicate();
    SliceRange range = new SliceRange();
    range.setCount(query.getLimit());
    range.setStart(query.getSliceStart().asByteBuffer());
    range.setFinish(query.getSliceEnd().asByteBuffer());
    predicate.setSlice_range(range);
    CTConnection conn = null;
    try {
        conn = pool.borrowObject(keyspace);
        Cassandra.Client client = conn.getClient();
        List<ByteBuffer> requestKeys = new ArrayList<ByteBuffer>(keys.size());
        {
            for (StaticBuffer key : keys) {
                requestKeys.add(key.asByteBuffer());
            }
        }
        Map<ByteBuffer, List<ColumnOrSuperColumn>> rows = client.multiget_slice(requestKeys, parent, predicate, consistency);
        /*
			 * The final size of the "result" List may be at most rows.size().
			 * However, "result" could also be up to two elements smaller than
			 * rows.size(), depending on startInclusive and endInclusive
			 */
        Map<ByteBuffer, List<Entry>> results = new HashMap<ByteBuffer, List<Entry>>();
        ByteBuffer sliceEndBB = query.getSliceEnd().asByteBuffer();
        for (ByteBuffer key : rows.keySet()) {
            results.put(key, excludeLastColumn(rows.get(key), sliceEndBB));
        }
        return results;
    } catch (Exception e) {
        throw convertException(e);
    } finally {
        pool.returnObjectUnsafe(keyspace, conn);
    }
}
Also used : HashMap(java.util.HashMap) ColumnParent(org.apache.cassandra.thrift.ColumnParent) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) Cassandra(org.apache.cassandra.thrift.Cassandra) ArrayList(java.util.ArrayList) SlicePredicate(org.apache.cassandra.thrift.SlicePredicate) ByteBuffer(java.nio.ByteBuffer) StaticByteBuffer(com.thinkaurelius.titan.diskstorage.util.StaticByteBuffer) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TimedOutException(org.apache.cassandra.thrift.TimedOutException) NoSuchElementException(java.util.NoSuchElementException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) UnavailableException(org.apache.cassandra.thrift.UnavailableException) TException(org.apache.thrift.TException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) ConsistencyLevel(org.apache.cassandra.thrift.ConsistencyLevel) CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) Entry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry) ByteBufferEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.ByteBufferEntry) SliceRange(org.apache.cassandra.thrift.SliceRange) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) List(java.util.List) ArrayList(java.util.ArrayList)

Example 84 with StaticBuffer

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

Example 85 with StaticBuffer

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

the class CassandraEmbeddedStoreManager method getLocalKeyPartition.

StaticBuffer[] getLocalKeyPartition() throws StorageException {
    // getLocalPrimaryRange() returns a raw type
    @SuppressWarnings({ "rawtypes", "deprecation" }) Range<Token> range = StorageService.instance.getLocalPrimaryRange();
    Token<?> leftKeyExclusive = range.left;
    Token<?> rightKeyInclusive = range.right;
    if (leftKeyExclusive instanceof BytesToken) {
        assert rightKeyInclusive instanceof BytesToken;
        // l is exclusive, r is inclusive
        BytesToken l = (BytesToken) leftKeyExclusive;
        BytesToken r = (BytesToken) rightKeyInclusive;
        Preconditions.checkArgument(l.token.length == r.token.length, "Tokens have unequal length");
        int tokenLength = l.token.length;
        log.debug("Token length: " + tokenLength);
        byte[][] tokens = new byte[][] { l.token, r.token };
        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 = new StaticArrayBuffer(plusOne[0]);
        StaticBuffer rb = new StaticArrayBuffer(plusOne[1]);
        Preconditions.checkArgument(lb.length() == tokenLength, lb.length());
        Preconditions.checkArgument(rb.length() == tokenLength, rb.length());
        return new StaticBuffer[] { lb, rb };
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : BytesToken(org.apache.cassandra.dht.BytesToken) StaticArrayBuffer(com.thinkaurelius.titan.diskstorage.util.StaticArrayBuffer) BytesToken(org.apache.cassandra.dht.BytesToken) Token(org.apache.cassandra.dht.Token) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Aggregations

StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)92 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)16 StaticBufferEntry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry)15 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)14 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)13 ConsistentKeyLockStatus (com.thinkaurelius.titan.diskstorage.locking.consistentkey.ConsistentKeyLockStatus)13 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)12 HashMap (java.util.HashMap)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Map (java.util.Map)11 Entry (com.thinkaurelius.titan.diskstorage.Entry)10 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)10 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)9 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)8 KeySliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)7 KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)6 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)6 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)6 Instant (java.time.Instant)6