Search in sources :

Example 11 with Mutation

use of org.apache.cassandra.thrift.Mutation in project scale7-pelops by s7.

the class Mutator method deleteSubColumns.

/**
     * Delete a list of sub-columns
     * @param colFamily                 The column family
     * @param rowKey                    The key of the row to modify
     * @param colName               	The name of the super column to modify
     * @param subColNames               The sub-column names to delete
     */
public Mutator deleteSubColumns(String colFamily, Bytes rowKey, Bytes colName, List<Bytes> subColNames) {
    safeGetRowKey(rowKey);
    validateColumnName(colName);
    validateColumnNames(subColNames);
    Deletion deletion = new Deletion();
    deletion.setTimestamp(timestamp);
    deletion.setSuper_column(nullSafeGet(colName));
    // CASSANDRA-1027 allows for a null predicate
    deletion.setPredicate(subColNames != null && !subColNames.isEmpty() ? new SlicePredicate().setColumn_names(Bytes.transformBytesToList(subColNames)) : null);
    Mutation mutation = new Mutation();
    mutation.setDeletion(deletion);
    getMutationList(colFamily, rowKey).add(mutation);
    return this;
}
Also used : Deletion(org.apache.cassandra.thrift.Deletion) SlicePredicate(org.apache.cassandra.thrift.SlicePredicate) Mutation(org.apache.cassandra.thrift.Mutation)

Example 12 with Mutation

use of org.apache.cassandra.thrift.Mutation in project scale7-pelops by s7.

the class MutatorIntegrationTest method testWriteColumnsWithDeleteIfNullFromConstructor.

@Test
public void testWriteColumnsWithDeleteIfNullFromConstructor() throws Exception {
    IThriftPool pool = new DebuggingPool(getCluster(), KEYSPACE, new OperandPolicy(3, true));
    Bytes rowKey = Bytes.fromLong(Long.MAX_VALUE);
    Bytes colName1 = Bytes.fromInt(1);
    Bytes colName2 = Bytes.fromInt(2);
    Mutator mutator = pool.createMutator();
    assertTrue("Mutator is not in a valid state for this test", mutator.deleteIfNull);
    List<Column> columns = mutator.newColumnList(mutator.newColumn(colName1, (Bytes) null), mutator.newColumn(colName2, Bytes.fromInt(1)));
    mutator.writeColumns(CF, rowKey, columns);
    // make sure there is at least one deletion
    boolean isOneDeletion = false;
    for (Mutation mutation : mutator.getMutationList(CF, rowKey)) {
        if (mutation.isSetDeletion()) {
            isOneDeletion = true;
            break;
        }
    }
    assertTrue("There should be one deletion", isOneDeletion);
    pool.shutdown();
}
Also used : Column(org.apache.cassandra.thrift.Column) IThriftPool(org.scale7.cassandra.pelops.pool.IThriftPool) Mutation(org.apache.cassandra.thrift.Mutation) DebuggingPool(org.scale7.cassandra.pelops.pool.DebuggingPool) Test(org.junit.Test) AbstractIntegrationTest(org.scale7.cassandra.pelops.support.AbstractIntegrationTest)

Example 13 with Mutation

use of org.apache.cassandra.thrift.Mutation in project eiger by wlloyd.

the class ColumnFamilyRecordWriter method write.

/**
     * If the key is to be associated with a valid value, a mutation is created
     * for it with the given column family and columns. In the event the value
     * in the column is missing (i.e., null), then it is marked for
     * {@link Deletion}. Similarly, if the entire value for a key is missing
     * (i.e., null), then the entire key is marked for {@link Deletion}.
     * </p>
     *
     * @param keybuff
     *            the key to write.
     * @param value
     *            the value to write.
     * @throws IOException
     */
@Override
public void write(ByteBuffer keybuff, List<Mutation> value) throws IOException {
    Range<Token> range = ringCache.getRange(keybuff);
    // get the client for the given range, or create a new one
    RangeClient client = clients.get(range);
    if (client == null) {
        // haven't seen keys for this range: create new client
        client = new RangeClient(ringCache.getEndpoint(range));
        client.start();
        clients.put(range, client);
    }
    for (Mutation amut : value) client.put(new Pair<ByteBuffer, Mutation>(keybuff, amut));
}
Also used : Token(org.apache.cassandra.dht.Token) Mutation(org.apache.cassandra.thrift.Mutation) Pair(org.apache.cassandra.utils.Pair)

Example 14 with Mutation

use of org.apache.cassandra.thrift.Mutation in project eiger by wlloyd.

the class FacebookPopulator method getColumnsMutationMap.

private Map<String, List<Mutation>> getColumnsMutationMap(List<Column> columns) {
    List<Mutation> mutations = new ArrayList<Mutation>();
    Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>();
    for (Column c : columns) {
        ColumnOrSuperColumn column = new ColumnOrSuperColumn().setColumn(c);
        mutations.add(new Mutation().setColumn_or_supercolumn(column));
    }
    mutationMap.put("Standard1", mutations);
    return mutationMap;
}
Also used : ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) Column(org.apache.cassandra.thrift.Column) Mutation(org.apache.cassandra.thrift.Mutation)

Example 15 with Mutation

use of org.apache.cassandra.thrift.Mutation 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)

Aggregations

Mutation (org.apache.cassandra.thrift.Mutation)16 ColumnOrSuperColumn (org.apache.cassandra.thrift.ColumnOrSuperColumn)10 ByteBuffer (java.nio.ByteBuffer)6 IOException (java.io.IOException)4 Column (org.apache.cassandra.thrift.Column)4 Deletion (org.apache.cassandra.thrift.Deletion)4 Column (org.apache.cassandra.db.Column)2 IColumn (org.apache.cassandra.db.IColumn)2 org.apache.cassandra.thrift (org.apache.cassandra.thrift)2 CounterSuperColumn (org.apache.cassandra.thrift.CounterSuperColumn)2 SlicePredicate (org.apache.cassandra.thrift.SlicePredicate)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CFMetaData (org.apache.cassandra.config.CFMetaData)1 CounterMutation (org.apache.cassandra.db.CounterMutation)1 IMutation (org.apache.cassandra.db.IMutation)1