Search in sources :

Example 1 with PartitionIterator

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

the class QueryProcessor method resultify.

public static UntypedResultSet resultify(String query, PartitionIterator partitions) {
    try (PartitionIterator iter = partitions) {
        SelectStatement ss = (SelectStatement) getStatement(query, null).statement;
        ResultSet cqlRows = ss.process(iter, FBUtilities.nowInSeconds());
        return UntypedResultSet.create(cqlRows);
    }
}
Also used : PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator)

Example 2 with PartitionIterator

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

the class SelectStatement method executeInternal.

public ResultMessage.Rows executeInternal(QueryState state, QueryOptions options, int nowInSec, long queryStartNanoTime) throws RequestExecutionException, RequestValidationException {
    int userLimit = getLimit(options);
    int userPerPartitionLimit = getPerPartitionLimit(options);
    int pageSize = options.getPageSize();
    ReadQuery query = getQuery(options, nowInSec, userLimit, userPerPartitionLimit, pageSize);
    try (ReadExecutionController executionController = query.executionController()) {
        if (aggregationSpec == null && (pageSize <= 0 || (query.limits().count() <= pageSize))) {
            try (PartitionIterator data = query.executeInternal(executionController)) {
                return processResults(data, options, nowInSec, userLimit);
            }
        } else {
            QueryPager pager = getPager(query, options);
            return execute(Pager.forInternalQuery(pager, executionController), options, pageSize, nowInSec, userLimit, queryStartNanoTime);
        }
    }
}
Also used : AggregationQueryPager(org.apache.cassandra.service.pager.AggregationQueryPager) QueryPager(org.apache.cassandra.service.pager.QueryPager) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator)

Example 3 with PartitionIterator

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

the class KeyspaceTest method testReversedWithFlushing.

@Test
public void testReversedWithFlushing() throws Throwable {
    String tableName = createTable("CREATE TABLE %s (a text, b int, c int, PRIMARY KEY (a, b)) WITH CLUSTERING ORDER BY (b DESC)");
    final ColumnFamilyStore cfs = Keyspace.open(KEYSPACE).getColumnFamilyStore(tableName);
    for (int i = 0; i < 10; i++) execute("INSERT INTO %s (a, b, c) VALUES (?, ?, ?)", "0", i, i);
    cfs.forceBlockingFlush();
    for (int i = 10; i < 20; i++) {
        execute("INSERT INTO %s (a, b, c) VALUES (?, ?, ?)", "0", i, i);
        RegularAndStaticColumns columns = RegularAndStaticColumns.of(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
        ClusteringIndexSliceFilter filter = new ClusteringIndexSliceFilter(Slices.ALL, false);
        SinglePartitionReadCommand command = singlePartitionSlice(cfs, "0", filter, null);
        try (ReadExecutionController executionController = command.executionController();
            PartitionIterator iterator = command.executeInternal(executionController)) {
            try (RowIterator rowIterator = iterator.next()) {
                Row row = rowIterator.next();
                Cell cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
                assertEquals(ByteBufferUtil.bytes(i), cell.value());
            }
        }
    }
}
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) Cell(org.apache.cassandra.db.rows.Cell) Test(org.junit.Test)

Example 4 with PartitionIterator

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

the class ReadCommandTest method testSinglePartitionGroupMerge.

@Test
public void testSinglePartitionGroupMerge() throws Exception {
    ColumnFamilyStore cfs = Keyspace.open(KEYSPACE).getColumnFamilyStore(CF3);
    String[][][] groups = new String[][][] { new String[][] { // "1" indicates to create the data, "-1" to delete the row
    new String[] { "1", "key1", "aa", "a" }, new String[] { "1", "key2", "bb", "b" }, new String[] { "1", "key3", "cc", "c" } }, new String[][] { new String[] { "1", "key3", "dd", "d" }, new String[] { "1", "key2", "ee", "e" }, new String[] { "1", "key1", "ff", "f" } }, new String[][] { new String[] { "1", "key6", "aa", "a" }, new String[] { "1", "key5", "bb", "b" }, new String[] { "1", "key4", "cc", "c" } }, new String[][] { new String[] { "-1", "key6", "aa", "a" }, new String[] { "-1", "key2", "bb", "b" } } };
    // Given the data above, when the keys are sorted and the deletions removed, we should
    // get these clustering rows in this order
    String[] expectedRows = new String[] { "aa", "ff", "ee", "cc", "dd", "cc", "bb" };
    List<ByteBuffer> buffers = new ArrayList<>(groups.length);
    int nowInSeconds = FBUtilities.nowInSeconds();
    ColumnFilter columnFilter = ColumnFilter.allRegularColumnsBuilder(cfs.metadata()).build();
    RowFilter rowFilter = RowFilter.create();
    Slice slice = Slice.make(ClusteringBound.BOTTOM, ClusteringBound.TOP);
    ClusteringIndexSliceFilter sliceFilter = new ClusteringIndexSliceFilter(Slices.with(cfs.metadata().comparator, slice), false);
    for (String[][] group : groups) {
        cfs.truncateBlocking();
        List<SinglePartitionReadCommand> commands = new ArrayList<>(group.length);
        for (String[] data : group) {
            if (data[0].equals("1")) {
                new RowUpdateBuilder(cfs.metadata(), 0, ByteBufferUtil.bytes(data[1])).clustering(data[2]).add(data[3], ByteBufferUtil.bytes("blah")).build().apply();
            } else {
                RowUpdateBuilder.deleteRow(cfs.metadata(), FBUtilities.timestampMicros(), ByteBufferUtil.bytes(data[1]), data[2]).apply();
            }
            commands.add(SinglePartitionReadCommand.create(cfs.metadata(), nowInSeconds, columnFilter, rowFilter, DataLimits.NONE, Util.dk(data[1]), sliceFilter));
        }
        cfs.forceBlockingFlush();
        ReadQuery query = new SinglePartitionReadCommand.Group(commands, DataLimits.NONE);
        try (ReadExecutionController executionController = query.executionController();
            UnfilteredPartitionIterator iter = query.executeLocally(executionController);
            DataOutputBuffer buffer = new DataOutputBuffer()) {
            UnfilteredPartitionIterators.serializerForIntraNode().serialize(iter, columnFilter, buffer, MessagingService.current_version);
            buffers.add(buffer.buffer());
        }
    }
    // deserialize, merge and check the results are all there
    List<UnfilteredPartitionIterator> iterators = new ArrayList<>();
    for (ByteBuffer buffer : buffers) {
        try (DataInputBuffer in = new DataInputBuffer(buffer, true)) {
            iterators.add(UnfilteredPartitionIterators.serializerForIntraNode().deserialize(in, MessagingService.current_version, cfs.metadata(), columnFilter, SerializationHelper.Flag.LOCAL));
        }
    }
    try (PartitionIterator partitionIterator = UnfilteredPartitionIterators.mergeAndFilter(iterators, nowInSeconds, new UnfilteredPartitionIterators.MergeListener() {

        public UnfilteredRowIterators.MergeListener getRowMergeListener(DecoratedKey partitionKey, List<UnfilteredRowIterator> versions) {
            return null;
        }

        public void close() {
        }
    })) {
        int i = 0;
        int numPartitions = 0;
        while (partitionIterator.hasNext()) {
            numPartitions++;
            try (RowIterator rowIterator = partitionIterator.next()) {
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    assertEquals("col=" + expectedRows[i++], row.clustering().toString(cfs.metadata()));
                //System.out.print(row.toString(cfs.metadata, true));
                }
            }
        }
        assertEquals(5, numPartitions);
        assertEquals(expectedRows.length, i);
    }
}
Also used : UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) ArrayList(java.util.ArrayList) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) ClusteringIndexSliceFilter(org.apache.cassandra.db.filter.ClusteringIndexSliceFilter) RowFilter(org.apache.cassandra.db.filter.RowFilter) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) ByteBuffer(java.nio.ByteBuffer) UnfilteredPartitionIterators(org.apache.cassandra.db.partitions.UnfilteredPartitionIterators) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) Row(org.apache.cassandra.db.rows.Row) Test(org.junit.Test)

Example 5 with PartitionIterator

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

the class QueryPagerTest method query.

private static List<FilteredPartition> query(QueryPager pager, int toQuery, int expectedSize) {
    StringBuilder sb = new StringBuilder();
    List<FilteredPartition> partitionList = new ArrayList<>();
    int rows = 0;
    try (ReadExecutionController executionController = pager.executionController();
        PartitionIterator iterator = pager.fetchPageInternal(toQuery, executionController)) {
        while (iterator.hasNext()) {
            try (RowIterator rowIter = iterator.next()) {
                FilteredPartition partition = FilteredPartition.create(rowIter);
                sb.append(partition);
                partitionList.add(partition);
                rows += partition.rowCount();
            }
        }
    }
    assertEquals(sb.toString(), expectedSize, rows);
    return partitionList;
}
Also used : PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) FilteredPartition(org.apache.cassandra.db.partitions.FilteredPartition)

Aggregations

PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)10 RowIterator (org.apache.cassandra.db.rows.RowIterator)6 Row (org.apache.cassandra.db.rows.Row)5 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)4 Cell (org.apache.cassandra.db.rows.Cell)3 QueryPager (org.apache.cassandra.service.pager.QueryPager)2 Test (org.junit.Test)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 ClusteringIndexSliceFilter (org.apache.cassandra.db.filter.ClusteringIndexSliceFilter)1 ColumnFilter (org.apache.cassandra.db.filter.ColumnFilter)1 RowFilter (org.apache.cassandra.db.filter.RowFilter)1 FilteredPartition (org.apache.cassandra.db.partitions.FilteredPartition)1 UnfilteredPartitionIterator (org.apache.cassandra.db.partitions.UnfilteredPartitionIterator)1 UnfilteredPartitionIterators (org.apache.cassandra.db.partitions.UnfilteredPartitionIterators)1 UnfilteredRowIterator (org.apache.cassandra.db.rows.UnfilteredRowIterator)1 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)1 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)1 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)1 AggregationQueryPager (org.apache.cassandra.service.pager.AggregationQueryPager)1