Search in sources :

Example 6 with RowMutation

use of org.apache.cassandra.db.RowMutation in project eiger by wlloyd.

the class DeleteStatement method mutationForKey.

public RowMutation mutationForKey(ByteBuffer key, String keyspace, Long timestamp, ClientState clientState, List<String> variables) throws InvalidRequestException {
    RowMutation rm = new RowMutation(keyspace, key);
    CFMetaData metadata = validateColumnFamily(keyspace, columnFamily);
    QueryProcessor.validateKeyAlias(metadata, keyName);
    AbstractType<?> comparator = metadata.getComparatorFor(null);
    if (columns.size() < 1) {
        // No columns, delete the row
        rm.delete(new QueryPath(columnFamily), (timestamp == null) ? getTimestamp(clientState) : timestamp);
    } else {
        // Delete specific columns
        for (Term column : columns) {
            ByteBuffer columnName = column.getByteBuffer(comparator, variables);
            validateColumnName(columnName);
            rm.delete(new QueryPath(columnFamily, null, columnName), (timestamp == null) ? getTimestamp(clientState) : timestamp);
        }
    }
    return rm;
}
Also used : QueryPath(org.apache.cassandra.db.filter.QueryPath) RowMutation(org.apache.cassandra.db.RowMutation) CFMetaData(org.apache.cassandra.config.CFMetaData) ByteBuffer(java.nio.ByteBuffer)

Example 7 with RowMutation

use of org.apache.cassandra.db.RowMutation in project eiger by wlloyd.

the class UpdateStatement method mutationForKey.

/**
     * Compute a row mutation for a single key
     *
     *
     * @param keyspace working keyspace
     * @param key key to change
     * @param metadata information about CF
     * @param timestamp global timestamp to use for every key mutation
     *
     * @param clientState
     * @return row mutation
     *
     * @throws InvalidRequestException on the wrong request
     */
private IMutation mutationForKey(String keyspace, ByteBuffer key, CFMetaData metadata, Long timestamp, ClientState clientState, List<String> variables) throws InvalidRequestException {
    AbstractType<?> comparator = getComparator(keyspace);
    // if true we need to wrap RowMutation into CounterMutation
    boolean hasCounterColumn = false;
    RowMutation rm = new RowMutation(keyspace, key);
    for (Map.Entry<Term, Operation> column : getColumns().entrySet()) {
        ByteBuffer colName = column.getKey().getByteBuffer(comparator, variables);
        Operation op = column.getValue();
        if (op.isUnary()) {
            if (hasCounterColumn)
                throw new InvalidRequestException("Mix of commutative and non-commutative operations is not allowed.");
            ByteBuffer colValue = op.a.getByteBuffer(getValueValidator(keyspace, colName), variables);
            validateColumn(metadata, colName, colValue);
            rm.add(new QueryPath(columnFamily, null, colName), colValue, (timestamp == null) ? getTimestamp(clientState) : timestamp, getTimeToLive());
        } else {
            hasCounterColumn = true;
            if (!column.getKey().getText().equals(op.a.getText()))
                throw new InvalidRequestException("Only expressions like X = X + <long> are supported.");
            long value;
            try {
                value = Long.parseLong(op.b.getText());
            } catch (NumberFormatException e) {
                throw new InvalidRequestException(String.format("'%s' is an invalid value, should be a long.", op.b.getText()));
            }
            rm.addCounter(new QueryPath(columnFamily, null, colName), value, timestamp, timestamp, null);
        }
    }
    return (hasCounterColumn) ? new CounterMutation(rm, getConsistencyLevel()) : rm;
}
Also used : ByteBuffer(java.nio.ByteBuffer) QueryPath(org.apache.cassandra.db.filter.QueryPath) CounterMutation(org.apache.cassandra.db.CounterMutation) RowMutation(org.apache.cassandra.db.RowMutation) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException)

Example 8 with RowMutation

use of org.apache.cassandra.db.RowMutation in project eiger by wlloyd.

the class BatchMutateTransactionUtil method convertToInternalMutations.

public static List<IMutation> convertToInternalMutations(String keyspace, Map<ByteBuffer, Map<String, List<Mutation>>> mutation_map, ByteBuffer coordinatorKey) throws InvalidRequestException {
    //the timestamp and localCommitTime are set when we apply the transaction, so we'll set them to invalid values here
    long timestamp = Long.MIN_VALUE;
    long localCommitTime = Long.MIN_VALUE;
    List<IMutation> rowMutations = new ArrayList<IMutation>();
    //Note, permission was checked when the thrift interface received the transaction.
    for (Map.Entry<ByteBuffer, Map<String, List<Mutation>>> mutationEntry : mutation_map.entrySet()) {
        ByteBuffer key = mutationEntry.getKey();
        // We need to separate row mutation for standard cf and counter cf (that will be encapsulated in a
        // CounterMutation) because it doesn't follow the same code path
        RowMutation rmStandard = null;
        RowMutation rmCounter = null;
        Map<String, List<Mutation>> columnFamilyToMutations = mutationEntry.getValue();
        for (Map.Entry<String, List<Mutation>> columnFamilyMutations : columnFamilyToMutations.entrySet()) {
            String cfName = columnFamilyMutations.getKey();
            CFMetaData metadata = ThriftValidation.validateColumnFamily(keyspace, cfName);
            ThriftValidation.validateKey(metadata, key);
            RowMutation rm;
            if (metadata.getDefaultValidator().isCommutative()) {
                ThriftValidation.validateCommutativeForWrite(metadata, ConsistencyLevel.ONE);
                rmCounter = rmCounter == null ? new RowMutation(keyspace, key) : rmCounter;
                rm = rmCounter;
            } else {
                rmStandard = rmStandard == null ? new RowMutation(keyspace, key) : rmStandard;
                rm = rmStandard;
            }
            for (Mutation mutation : columnFamilyMutations.getValue()) {
                ThriftValidation.validateMutation(metadata, mutation);
                if (mutation.deletion != null) {
                    rm.deleteColumnOrSuperColumn(cfName, mutation.deletion, timestamp, localCommitTime, coordinatorKey);
                }
                if (mutation.column_or_supercolumn != null) {
                    rm.addColumnOrSuperColumn(cfName, mutation.column_or_supercolumn, timestamp, localCommitTime, coordinatorKey);
                }
            }
        }
        if (rmStandard != null && !rmStandard.isEmpty())
            rowMutations.add(rmStandard);
        if (rmCounter != null && !rmCounter.isEmpty())
            rowMutations.add(new org.apache.cassandra.db.CounterMutation(rmCounter, ConsistencyLevel.ONE));
    }
    logger.debug("Mutations are {}", rowMutations);
    return rowMutations;
}
Also used : IMutation(org.apache.cassandra.db.IMutation) ByteBuffer(java.nio.ByteBuffer) CounterMutation(org.apache.cassandra.db.CounterMutation) RowMutation(org.apache.cassandra.db.RowMutation) CFMetaData(org.apache.cassandra.config.CFMetaData) CounterMutation(org.apache.cassandra.db.CounterMutation) RowMutation(org.apache.cassandra.db.RowMutation) Mutation(org.apache.cassandra.thrift.Mutation) IMutation(org.apache.cassandra.db.IMutation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with RowMutation

use of org.apache.cassandra.db.RowMutation in project eiger by wlloyd.

the class SerializationsTest method makeSSTable.

private static SSTableReader makeSSTable() {
    Table t = Table.open("Keyspace1");
    for (int i = 0; i < 100; i++) {
        RowMutation rm = new RowMutation(t.name, ByteBufferUtil.bytes(Long.toString(System.nanoTime())));
        rm.add(new QueryPath("Standard1", null, ByteBufferUtil.bytes("cola")), ByteBufferUtil.bytes("value"), 0);
        try {
            rm.apply();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    try {
        t.getColumnFamilyStore("Standard1").forceBlockingFlush();
        return t.getColumnFamilyStore("Standard1").getSSTables().iterator().next();
    } catch (Exception any) {
        throw new RuntimeException(any);
    }
}
Also used : QueryPath(org.apache.cassandra.db.filter.QueryPath) Table(org.apache.cassandra.db.Table) SSTable(org.apache.cassandra.io.sstable.SSTable) RowMutation(org.apache.cassandra.db.RowMutation) IOException(java.io.IOException) IOException(java.io.IOException)

Example 10 with RowMutation

use of org.apache.cassandra.db.RowMutation in project eiger by wlloyd.

the class CompactionsPurgeTest method testCompactionPurgeCachedRow.

@Test
public void testCompactionPurgeCachedRow() throws IOException, ExecutionException, InterruptedException {
    CompactionManager.instance.disableAutoCompaction();
    String tableName = "RowCacheSpace";
    String cfName = "CachedCF";
    Table table = Table.open(tableName);
    ColumnFamilyStore cfs = table.getColumnFamilyStore(cfName);
    DecoratedKey key = Util.dk("key3");
    RowMutation rm;
    // inserts
    rm = new RowMutation(tableName, key.key);
    for (int i = 0; i < 10; i++) {
        rm.add(new QueryPath(cfName, null, ByteBufferUtil.bytes(String.valueOf(i))), ByteBufferUtil.EMPTY_BYTE_BUFFER, 0);
    }
    rm.apply();
    // move the key up in row cache
    cfs.getColumnFamily(QueryFilter.getIdentityFilter(key, new QueryPath(cfName)));
    // deletes row
    rm = new RowMutation(tableName, key.key);
    rm.delete(new QueryPath(cfName, null, null), 1);
    rm.apply();
    // flush and major compact
    cfs.forceBlockingFlush();
    Util.compactAll(cfs).get();
    // re-inserts with timestamp lower than delete
    rm = new RowMutation(tableName, key.key);
    for (int i = 0; i < 10; i++) {
        rm.add(new QueryPath(cfName, null, ByteBufferUtil.bytes(String.valueOf(i))), ByteBufferUtil.EMPTY_BYTE_BUFFER, 0);
    }
    rm.apply();
    // Check that the second insert did went in
    ColumnFamily cf = cfs.getColumnFamily(QueryFilter.getIdentityFilter(key, new QueryPath(cfName)));
    assertEquals(10, cf.getColumnCount());
    for (IColumn c : cf) assert !c.isMarkedForDelete();
}
Also used : QueryPath(org.apache.cassandra.db.filter.QueryPath) Table(org.apache.cassandra.db.Table) IColumn(org.apache.cassandra.db.IColumn) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RowMutation(org.apache.cassandra.db.RowMutation) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Aggregations

RowMutation (org.apache.cassandra.db.RowMutation)13 QueryPath (org.apache.cassandra.db.filter.QueryPath)11 Table (org.apache.cassandra.db.Table)8 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)7 DecoratedKey (org.apache.cassandra.db.DecoratedKey)7 ColumnFamily (org.apache.cassandra.db.ColumnFamily)6 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)5 CFMetaData (org.apache.cassandra.config.CFMetaData)2 CounterMutation (org.apache.cassandra.db.CounterMutation)2 IColumn (org.apache.cassandra.db.IColumn)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 IMutation (org.apache.cassandra.db.IMutation)1 SuperColumn (org.apache.cassandra.db.SuperColumn)1 SSTable (org.apache.cassandra.io.sstable.SSTable)1 SSTableReader (org.apache.cassandra.io.sstable.SSTableReader)1 InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)1 Mutation (org.apache.cassandra.thrift.Mutation)1