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