Search in sources :

Example 16 with RowIterator

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());
        }
    }
}
Also used : PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row)

Example 17 with RowIterator

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);
}
Also used : ScheduledExecutors(org.apache.cassandra.concurrent.ScheduledExecutors) org.apache.cassandra.cql3.statements(org.apache.cassandra.cql3.statements) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) AbstractType(org.apache.cassandra.db.marshal.AbstractType) org.apache.cassandra.utils(org.apache.cassandra.utils) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) ByteBuffer(java.nio.ByteBuffer) Gossiper(org.apache.cassandra.gms.Gossiper) Function(org.apache.cassandra.cql3.functions.Function) ClientRequestMetrics(org.apache.cassandra.metrics.ClientRequestMetrics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ENABLE_NODELOCAL_QUERIES(org.apache.cassandra.config.CassandraRelevantProperties.ENABLE_NODELOCAL_QUERIES) FunctionName(org.apache.cassandra.cql3.functions.FunctionName) RowIterator(org.apache.cassandra.db.rows.RowIterator) PartitionIterators(org.apache.cassandra.db.partitions.PartitionIterators) com.google.common.collect(com.google.common.collect) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) RequestValidations.checkTrue(org.apache.cassandra.cql3.statements.RequestValidations.checkTrue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) QueryPager(org.apache.cassandra.service.pager.QueryPager) Predicate(com.google.common.base.Predicate) org.apache.cassandra.exceptions(org.apache.cassandra.exceptions) Future(org.apache.cassandra.utils.concurrent.Future) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) org.antlr.runtime(org.antlr.runtime) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) org.apache.cassandra.service(org.apache.cassandra.service) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) java.util(java.util) CQLMetrics(org.apache.cassandra.metrics.CQLMetrics) Message(org.apache.cassandra.net.Message) Cache(com.github.benmanes.caffeine.cache.Cache) ConcurrentMap(java.util.concurrent.ConcurrentMap) Schema(org.apache.cassandra.schema.Schema) SchemaChangeListener(org.apache.cassandra.schema.SchemaChangeListener) ResultSetBuilder(org.apache.cassandra.cql3.selection.ResultSetBuilder) ClientRequestsMetricsHolder(org.apache.cassandra.metrics.ClientRequestsMetricsHolder) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) MessagingService(org.apache.cassandra.net.MessagingService) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) FutureCombiner(org.apache.cassandra.utils.concurrent.FutureCombiner) Logger(org.slf4j.Logger) Tracing(org.apache.cassandra.tracing.Tracing) Ints(com.google.common.primitives.Ints) TimeUnit(java.util.concurrent.TimeUnit) UnfilteredPartitionIterators(org.apache.cassandra.db.partitions.UnfilteredPartitionIterators) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ImmediateExecutor(org.apache.cassandra.concurrent.ImmediateExecutor) ResultSetBuilder(org.apache.cassandra.cql3.selection.ResultSetBuilder) Message(org.apache.cassandra.net.Message) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator)

Example 18 with RowIterator

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;
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) CellPath(org.apache.cassandra.db.rows.CellPath) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ClusteringIndexNamesFilter(org.apache.cassandra.db.filter.ClusteringIndexNamesFilter) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) ByteBuffer(java.nio.ByteBuffer) RowIterator(org.apache.cassandra.db.rows.RowIterator) ClusteringIndexFilter(org.apache.cassandra.db.filter.ClusteringIndexFilter)

Example 19 with RowIterator

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)));
    }
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) RowIterator(org.apache.cassandra.db.rows.RowIterator)

Example 20 with RowIterator

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));
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) TestableReadRepair(org.apache.cassandra.service.reads.repair.TestableReadRepair) DeletionTime(org.apache.cassandra.db.DeletionTime) ReadCommand(org.apache.cassandra.db.ReadCommand) ComplexColumnData(org.apache.cassandra.db.rows.ComplexColumnData) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) EndpointsForRange(org.apache.cassandra.locator.EndpointsForRange) BTreeRow(org.apache.cassandra.db.rows.BTreeRow) Row(org.apache.cassandra.db.rows.Row) Mutation(org.apache.cassandra.db.Mutation) Test(org.junit.Test)

Aggregations

RowIterator (org.apache.cassandra.db.rows.RowIterator)25 PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)16 Row (org.apache.cassandra.db.rows.Row)15 Test (org.junit.Test)12 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)10 UnfilteredPartitionIterator (org.apache.cassandra.db.partitions.UnfilteredPartitionIterator)9 BTreeRow (org.apache.cassandra.db.rows.BTreeRow)9 EndpointsForRange (org.apache.cassandra.locator.EndpointsForRange)9 Mutation (org.apache.cassandra.db.Mutation)8 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)5 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)5 DeletionTime (org.apache.cassandra.db.DeletionTime)4 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)4 TableMetadata (org.apache.cassandra.schema.TableMetadata)4 ByteBuffer (java.nio.ByteBuffer)3 ReadCommand (org.apache.cassandra.db.ReadCommand)3 FilteredPartition (org.apache.cassandra.db.partitions.FilteredPartition)3 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)3 ComplexColumnData (org.apache.cassandra.db.rows.ComplexColumnData)3 TestableReadRepair (org.apache.cassandra.service.reads.repair.TestableReadRepair)3