use of org.apache.cassandra.db.rows.RowIterator in project cassandra by apache.
the class KeyspaceTest method assertRowsInResult.
private static void assertRowsInResult(ColumnFamilyStore cfs, SinglePartitionReadCommand command, int... columnValues) {
try (ReadExecutionController executionController = command.executionController();
PartitionIterator iterator = command.executeInternal(executionController)) {
if (columnValues.length == 0) {
if (iterator.hasNext())
fail("Didn't expect any results, but got rows starting with: " + iterator.next().next().toString(cfs.metadata()));
return;
}
try (RowIterator rowIterator = iterator.next()) {
for (int expected : columnValues) {
Row row = rowIterator.next();
Cell<?> cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
assertEquals(String.format("Expected %s, but got %s", ByteBufferUtil.bytesToHex(ByteBufferUtil.bytes(expected)), ByteBufferUtil.bytesToHex(cell.buffer())), ByteBufferUtil.bytes(expected), cell.buffer());
}
assertFalse(rowIterator.hasNext());
}
}
}
use of org.apache.cassandra.db.rows.RowIterator in project cassandra by apache.
the class QueryProcessor method executeAsync.
public static Future<UntypedResultSet> executeAsync(InetAddressAndPort address, String query, Object... values) {
Prepared prepared = prepareInternal(query);
QueryOptions options = makeInternalOptions(prepared.statement, values);
if (prepared.statement instanceof SelectStatement) {
SelectStatement select = (SelectStatement) prepared.statement;
int nowInSec = FBUtilities.nowInSeconds();
ReadQuery readQuery = select.getQuery(options, nowInSec);
List<ReadCommand> commands;
if (readQuery instanceof ReadCommand) {
commands = Collections.singletonList((ReadCommand) readQuery);
} else if (readQuery instanceof SinglePartitionReadQuery.Group) {
List<? extends SinglePartitionReadQuery> queries = ((SinglePartitionReadQuery.Group<? extends SinglePartitionReadQuery>) readQuery).queries;
queries.forEach(a -> {
if (!(a instanceof ReadCommand))
throw new IllegalArgumentException("Queries found which are not ReadCommand: " + a.getClass());
});
commands = (List<ReadCommand>) (List<?>) queries;
} else {
throw new IllegalArgumentException("Unable to handle; only expected ReadCommands but given " + readQuery.getClass());
}
Future<List<Message<ReadResponse>>> future = FutureCombiner.allOf(commands.stream().map(rc -> Message.out(rc.verb(), rc)).map(m -> MessagingService.instance().<ReadResponse>sendWithResult(m, address)).collect(Collectors.toList()));
ResultSetBuilder result = new ResultSetBuilder(select.getResultMetadata(), select.getSelection().newSelectors(options), null);
return future.map(list -> {
int i = 0;
for (Message<ReadResponse> m : list) {
ReadResponse rsp = m.payload;
try (PartitionIterator it = UnfilteredPartitionIterators.filter(rsp.makeIterator(commands.get(i++)), nowInSec)) {
while (it.hasNext()) {
try (RowIterator partition = it.next()) {
select.processPartition(partition, options, result, nowInSec);
}
}
}
}
return result.build();
}).map(UntypedResultSet::create);
}
throw new IllegalArgumentException("Unable to execute query; only SELECT supported but given: " + query);
}
use of org.apache.cassandra.db.rows.RowIterator in project cassandra by apache.
the class CounterCacheKey method readCounterValue.
/**
* Reads the value of the counter represented by this key.
*
* @param cfs the store for the table this is a key of.
* @return the value for the counter represented by this key, or {@code null} if there
* is not such counter.
*/
public ByteBuffer readCounterValue(ColumnFamilyStore cfs) {
TableMetadata metadata = cfs.metadata();
assert metadata.id.equals(tableId) && Objects.equals(metadata.indexName().orElse(null), indexName);
DecoratedKey key = cfs.decorateKey(partitionKey());
int clusteringSize = metadata.comparator.size();
List<ByteBuffer> buffers = CompositeType.splitName(ByteBuffer.wrap(cellName), ByteBufferAccessor.instance);
// See makeCellName above
assert buffers.size() >= clusteringSize + 1;
Clustering<?> clustering = Clustering.make(buffers.subList(0, clusteringSize).toArray(new ByteBuffer[clusteringSize]));
ColumnMetadata column = metadata.getColumn(buffers.get(clusteringSize));
// try to load it. Not point if failing in any case, just skip the value.
if (column == null)
return null;
CellPath path = column.isComplex() ? CellPath.create(buffers.get(buffers.size() - 1)) : null;
int nowInSec = FBUtilities.nowInSeconds();
ColumnFilter.Builder builder = ColumnFilter.selectionBuilder();
if (path == null)
builder.add(column);
else
builder.select(column, path);
ClusteringIndexFilter filter = new ClusteringIndexNamesFilter(FBUtilities.singleton(clustering, metadata.comparator), false);
SinglePartitionReadCommand cmd = SinglePartitionReadCommand.create(metadata, nowInSec, key, builder.build(), filter);
try (ReadExecutionController controller = cmd.executionController();
RowIterator iter = UnfilteredRowIterators.filter(cmd.queryMemtableAndDisk(cfs, controller), nowInSec)) {
ByteBuffer value = null;
if (column.isStatic())
value = iter.staticRow().getCell(column).buffer();
else if (iter.hasNext())
value = iter.next().getCell(column).buffer();
return value;
}
}
use of org.apache.cassandra.db.rows.RowIterator in project cassandra by apache.
the class BatchStatement method executeWithConditions.
private ResultMessage executeWithConditions(BatchQueryOptions options, QueryState state, long queryStartNanoTime) {
Pair<CQL3CasRequest, Set<ColumnMetadata>> p = makeCasRequest(options, state);
CQL3CasRequest casRequest = p.left;
Set<ColumnMetadata> columnsWithConditions = p.right;
String ksName = casRequest.metadata.keyspace;
String tableName = casRequest.metadata.name;
try (RowIterator result = StorageProxy.cas(ksName, tableName, casRequest.key, casRequest, options.getSerialConsistency(), options.getConsistency(), state.getClientState(), options.getNowInSeconds(state), queryStartNanoTime)) {
return new ResultMessage.Rows(ModificationStatement.buildCasResultSet(ksName, tableName, result, columnsWithConditions, true, state, options.forStatement(0)));
}
}
use of org.apache.cassandra.db.rows.RowIterator in project cassandra by apache.
the class DataResolverTest method testResolveComplexDelete.
@Test
public void testResolveComplexDelete() {
EndpointsForRange replicas = makeReplicas(2);
ReadCommand cmd = Util.cmd(cfs2, dk).withNowInSeconds(nowInSec).build();
TestableReadRepair readRepair = new TestableReadRepair(cmd);
DataResolver resolver = new DataResolver(cmd, plan(replicas, ALL), readRepair, nanoTime());
long[] ts = { 100, 200 };
Row.Builder builder = BTreeRow.unsortedBuilder();
builder.newRow(Clustering.EMPTY);
builder.addComplexDeletion(m, new DeletionTime(ts[0] - 1, nowInSec));
builder.addCell(mapCell(0, 0, ts[0]));
InetAddressAndPort peer1 = replicas.get(0).endpoint();
resolver.preprocess(response(cmd, peer1, iter(PartitionUpdate.singleRowUpdate(cfm2, dk, builder.build()))));
builder.newRow(Clustering.EMPTY);
DeletionTime expectedCmplxDelete = new DeletionTime(ts[1] - 1, nowInSec);
builder.addComplexDeletion(m, expectedCmplxDelete);
Cell<?> expectedCell = mapCell(1, 1, ts[1]);
builder.addCell(expectedCell);
InetAddressAndPort peer2 = replicas.get(1).endpoint();
resolver.preprocess(response(cmd, peer2, iter(PartitionUpdate.singleRowUpdate(cfm2, dk, builder.build()))));
try (PartitionIterator data = resolver.resolve()) {
try (RowIterator rows = Iterators.getOnlyElement(data)) {
Row row = Iterators.getOnlyElement(rows);
assertColumns(row, "m");
Assert.assertNull(row.getCell(m, CellPath.create(bb(0))));
Assert.assertNotNull(row.getCell(m, CellPath.create(bb(1))));
}
}
Mutation mutation = readRepair.getForEndpoint(peer1);
Iterator<Row> rowIter = mutation.getPartitionUpdate(cfm2).iterator();
assertTrue(rowIter.hasNext());
Row row = rowIter.next();
assertFalse(rowIter.hasNext());
ComplexColumnData cd = row.getComplexColumnData(m);
assertEquals(Collections.singleton(expectedCell), Sets.newHashSet(cd));
assertEquals(expectedCmplxDelete, cd.complexDeletion());
Assert.assertNull(readRepair.sent.get(peer2));
}
Aggregations