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);
}
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;
}
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;
}
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;
}
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();
}
Aggregations