Search in sources :

Example 21 with PartitionUpdate

use of org.apache.cassandra.db.partitions.PartitionUpdate in project cassandra by apache.

the class Mutation method merge.

/**
     * Creates a new mutation that merges all the provided mutations.
     *
     * @param mutations the mutations to merge together. All mutation must be
     * on the same keyspace and partition key. There should also be at least one
     * mutation.
     * @return a mutation that contains all the modifications contained in {@code mutations}.
     *
     * @throws IllegalArgumentException if not all the mutations are on the same
     * keyspace and key.
     */
public static Mutation merge(List<Mutation> mutations) {
    assert !mutations.isEmpty();
    if (mutations.size() == 1)
        return mutations.get(0);
    Set<TableId> updatedTables = new HashSet<>();
    String ks = null;
    DecoratedKey key = null;
    for (Mutation mutation : mutations) {
        updatedTables.addAll(mutation.modifications.keySet());
        if (ks != null && !ks.equals(mutation.keyspaceName))
            throw new IllegalArgumentException();
        if (key != null && !key.equals(mutation.key))
            throw new IllegalArgumentException();
        ks = mutation.keyspaceName;
        key = mutation.key;
    }
    List<PartitionUpdate> updates = new ArrayList<>(mutations.size());
    Map<TableId, PartitionUpdate> modifications = new HashMap<>(updatedTables.size());
    for (TableId table : updatedTables) {
        for (Mutation mutation : mutations) {
            PartitionUpdate upd = mutation.modifications.get(table);
            if (upd != null)
                updates.add(upd);
        }
        if (updates.isEmpty())
            continue;
        modifications.put(table, updates.size() == 1 ? updates.get(0) : PartitionUpdate.merge(updates));
        updates.clear();
    }
    return new Mutation(ks, key, modifications);
}
Also used : TableId(org.apache.cassandra.schema.TableId) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 22 with PartitionUpdate

use of org.apache.cassandra.db.partitions.PartitionUpdate in project cassandra by apache.

the class Mutation method add.

/**
     * Adds PartitionUpdate to the local set of modifications.
     * Assumes no updates for the Table this PartitionUpdate impacts.
     *
     * @param update PartitionUpdate to append to Modifications list
     * @return Mutation this mutation
     * @throws IllegalArgumentException If PartitionUpdate for duplicate table is passed as argument
     */
public Mutation add(PartitionUpdate update) {
    assert update != null;
    assert update.partitionKey().getPartitioner() == key.getPartitioner();
    cdcEnabled |= update.metadata().params.cdc;
    PartitionUpdate prev = modifications.put(update.metadata().id, update);
    if (prev != null)
        // developer error
        throw new IllegalArgumentException("Table " + update.metadata().name + " already has modifications in this mutation: " + prev);
    return this;
}
Also used : PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 23 with PartitionUpdate

use of org.apache.cassandra.db.partitions.PartitionUpdate in project cassandra by apache.

the class Mutation method without.

public Mutation without(Set<TableId> tableIds) {
    if (tableIds.isEmpty())
        return this;
    Mutation copy = copy();
    copy.modifications.keySet().removeAll(tableIds);
    copy.cdcEnabled = false;
    for (PartitionUpdate pu : modifications.values()) copy.cdcEnabled |= pu.metadata().params.cdc;
    return copy;
}
Also used : PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 24 with PartitionUpdate

use of org.apache.cassandra.db.partitions.PartitionUpdate in project cassandra by apache.

the class UpdatesCollector method getPartitionUpdate.

/**
     * Gets the <code>PartitionUpdate</code> for the specified column family and key. If the update does not
     * exist it will be created.
     *
     * @param metadata the column family meta data
     * @param dk the partition key
     * @param consistency the consistency level
     * @return the <code>PartitionUpdate</code> for the specified column family and key
     */
public PartitionUpdate getPartitionUpdate(TableMetadata metadata, DecoratedKey dk, ConsistencyLevel consistency) {
    Mutation mut = getMutation(metadata, dk, consistency);
    PartitionUpdate upd = mut.get(metadata);
    if (upd == null) {
        RegularAndStaticColumns columns = updatedColumns.get(metadata.id);
        assert columns != null;
        upd = new PartitionUpdate(metadata, dk, columns, updatedRows);
        mut.add(upd);
    }
    return upd;
}
Also used : PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 25 with PartitionUpdate

use of org.apache.cassandra.db.partitions.PartitionUpdate in project cassandra by apache.

the class SystemKeyspace method updateSizeEstimates.

/**
     * Writes the current partition count and size estimates into SIZE_ESTIMATES_CF
     */
public static void updateSizeEstimates(String keyspace, String table, Map<Range<Token>, Pair<Long, Long>> estimates) {
    long timestamp = FBUtilities.timestampMicros();
    PartitionUpdate update = new PartitionUpdate(SizeEstimates, UTF8Type.instance.decompose(keyspace), SizeEstimates.regularAndStaticColumns(), estimates.size());
    Mutation mutation = new Mutation(update);
    // delete all previous values with a single range tombstone.
    int nowInSec = FBUtilities.nowInSeconds();
    update.add(new RangeTombstone(Slice.make(SizeEstimates.comparator, table), new DeletionTime(timestamp - 1, nowInSec)));
    // add a CQL row for each primary token range.
    for (Map.Entry<Range<Token>, Pair<Long, Long>> entry : estimates.entrySet()) {
        Range<Token> range = entry.getKey();
        Pair<Long, Long> values = entry.getValue();
        update.add(Rows.simpleBuilder(SizeEstimates, table, range.left.toString(), range.right.toString()).timestamp(timestamp).add("partitions_count", values.left).add("mean_partition_size", values.right).build());
    }
    mutation.apply();
}
Also used : Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) ImmutableMap(com.google.common.collect.ImmutableMap) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Aggregations

PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)40 Test (org.junit.Test)14 TableMetadata (org.apache.cassandra.schema.TableMetadata)7 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)3 Mutation (org.apache.cassandra.db.Mutation)3 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)3 Commit (org.apache.cassandra.service.paxos.Commit)3 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)2 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)2 TableId (org.apache.cassandra.schema.TableId)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1